Do not apply \origin at existing files

The current behaviour of the \origin parameter replaces relative file names
with the absolute original names if a document has been moved even if the
files have been moved as well. This behaviour is annoying e.g. for editing the
LyX docs in a git checkout.

Now file names are only replaced if the referenced file sdo not exist.
This commit is contained in:
Georg Baum 2016-01-10 19:46:01 +01:00
parent 7e72c1d0d3
commit d80fa17175
14 changed files with 62 additions and 32 deletions

View File

@ -2,7 +2,7 @@
\lyxformat 503 \lyxformat 503
\begin_document \begin_document
\begin_header \begin_header
\origin unavailable \origin /systemlyxdir/doc/
\textclass scrbook \textclass scrbook
\begin_preamble \begin_preamble
% DO NOT ALTER THIS PREAMBLE!!! % DO NOT ALTER THIS PREAMBLE!!!

View File

@ -2,7 +2,7 @@
\lyxformat 503 \lyxformat 503
\begin_document \begin_document
\begin_header \begin_header
\origin unavailable \origin /systemlyxdir/doc/
\textclass scrbook \textclass scrbook
\begin_preamble \begin_preamble
% DO NOT ALTER THIS PREAMBLE!!! % DO NOT ALTER THIS PREAMBLE!!!

View File

@ -2,7 +2,7 @@
\lyxformat 503 \lyxformat 503
\begin_document \begin_document
\begin_header \begin_header
\origin unavailable \origin /systemlyxdir/doc/
\textclass scrbook \textclass scrbook
\begin_preamble \begin_preamble
% that links to image floats jumps % that links to image floats jumps

View File

@ -2,7 +2,7 @@
\lyxformat 503 \lyxformat 503
\begin_document \begin_document
\begin_header \begin_header
\origin unavailable \origin /systemlyxdir/doc/
\textclass article \textclass article
\begin_preamble \begin_preamble
% DO NOT ALTER THIS PREAMBLE!!! % DO NOT ALTER THIS PREAMBLE!!!

View File

@ -2,7 +2,7 @@
\lyxformat 503 \lyxformat 503
\begin_document \begin_document
\begin_header \begin_header
\origin unavailable \origin /systemlyxdir/doc/
\textclass article \textclass article
\use_default_options false \use_default_options false
\maintain_unincluded_children false \maintain_unincluded_children false

View File

@ -2,7 +2,7 @@
\lyxformat 503 \lyxformat 503
\begin_document \begin_document
\begin_header \begin_header
\origin unavailable \origin /systemlyxdir/doc/
\textclass scrartcl \textclass scrartcl
\begin_preamble \begin_preamble
% DO NOT ALTER THIS PREAMBLE!!! % DO NOT ALTER THIS PREAMBLE!!!

View File

@ -2,7 +2,7 @@
\lyxformat 503 \lyxformat 503
\begin_document \begin_document
\begin_header \begin_header
\origin unavailable \origin /systemlyxdir/doc/
\textclass book \textclass book
\begin_preamble \begin_preamble
% DO NOT ALTER THIS PREAMBLE!!! % DO NOT ALTER THIS PREAMBLE!!!

View File

@ -2,7 +2,7 @@
\lyxformat 503 \lyxformat 503
\begin_document \begin_document
\begin_header \begin_header
\origin unavailable \origin /systemlyxdir/doc/
\textclass scrbook \textclass scrbook
\begin_preamble \begin_preamble
% DO NOT ALTER THIS PREAMBLE!!! % DO NOT ALTER THIS PREAMBLE!!!

View File

@ -1033,7 +1033,10 @@ bool Buffer::readDocument(Lexer & lex)
params().indiceslist().addDefault(B_("Index")); params().indiceslist().addDefault(B_("Index"));
// read main text // read main text
d->old_position = originFilePath(); if (FileName::isAbsolute(params().origin))
d->old_position = params().origin;
else
d->old_position = filePath();
bool const res = text().read(lex, errorList, d->inset); bool const res = text().read(lex, errorList, d->inset);
d->old_position.clear(); d->old_position.clear();
@ -3029,12 +3032,21 @@ string Buffer::filePath() const
} }
string Buffer::originFilePath() const DocFileName Buffer::getReferencedFileName(string const & fn) const
{ {
if (FileName::isAbsolute(params().origin)) DocFileName result;
return params().origin; if (FileName::isAbsolute(fn) || !FileName::isAbsolute(params().origin))
result.set(fn, filePath());
else {
// filePath() ends with a path separator
FileName const test(filePath() + fn);
if (test.exists())
result.set(fn, filePath());
else
result.set(fn, params().origin);
}
return filePath(); return result;
} }
@ -5071,13 +5083,23 @@ void Buffer::checkMasterBuffer()
string Buffer::includedFilePath(string const & name, string const & ext) const string Buffer::includedFilePath(string const & name, string const & ext) const
{ {
bool isabsolute = FileName::isAbsolute(name); if (d->old_position.empty() ||
// old_position already contains a trailing path separator equivalent(FileName(d->old_position), FileName(filePath())))
string const absname = isabsolute ? name : d->old_position + name; return name;
if (d->old_position.empty() bool isabsolute = FileName::isAbsolute(name);
|| equivalent(FileName(d->old_position), FileName(filePath())) // both old_position and filePath() end with a path separator
|| !FileName(addExtension(absname, ext)).exists()) string absname = isabsolute ? name : d->old_position + name;
// if old_position is set to origin, we need to do the equivalent of
// getReferencedFileName() (see readDocument())
if (!isabsolute && d->old_position == params().origin) {
FileName const test(addExtension(filePath() + name, ext));
if (test.exists())
absname = filePath() + name;
}
if (!FileName(addExtension(absname, ext)).exists())
return name; return name;
if (isabsolute) if (isabsolute)

View File

@ -66,6 +66,7 @@ class WorkAreaManager;
} }
namespace support { namespace support {
class DocFileName;
class FileName; class FileName;
class FileNameList; class FileNameList;
} }
@ -405,12 +406,14 @@ public:
/// It is always an absolute path. /// It is always an absolute path.
std::string filePath() const; std::string filePath() const;
/** Returns the path where the document was last saved. /** Contructs a file name of a referenced file (child doc, included graphics etc).
* It may be different from filePath() if the document was later * Absolute names are returned as is. If the name is relative, it is
* manually moved to a different location. * interpreted relative to filePath() if the file exists, otherwise
* It is always an absolute path. * relative to the original path where the document was last saved.
* The original path may be different from filePath() if the document was
* later manually moved to a different location.
*/ */
std::string originFilePath() const; support::DocFileName getReferencedFileName(std::string const & fn) const;
/** Returns the path where a local layout file lives. /** Returns the path where a local layout file lives.
* An empty string is returned for standard system and user layouts. * An empty string is returned for standard system and user layouts.

View File

@ -273,7 +273,7 @@ bool InsetExternalParams::read(Buffer const & buffer, Lexer & lex)
case EX_FILENAME: { case EX_FILENAME: {
lex.eatLine(); lex.eatLine();
string const name = lex.getString(); string const name = lex.getString();
filename.set(name, buffer.originFilePath()); filename = buffer.getReferencedFileName(name);
break; break;
} }

View File

@ -139,7 +139,7 @@ string findTargetFormat(string const & format, OutputParams const & runparams)
} }
void readInsetGraphics(Lexer & lex, string const & bufpath, void readInsetGraphics(Lexer & lex, Buffer const & buf, bool allowOrigin,
InsetGraphicsParams & params) InsetGraphicsParams & params)
{ {
bool finished = false; bool finished = false;
@ -156,7 +156,7 @@ void readInsetGraphics(Lexer & lex, string const & bufpath,
if (token == "\\end_inset") { if (token == "\\end_inset") {
finished = true; finished = true;
} else { } else {
if (!params.Read(lex, token, bufpath)) if (!params.Read(lex, token, buf, allowOrigin))
lyxerr << "Unknown token, " lyxerr << "Unknown token, "
<< token << token
<< ", skipping." << ", skipping."
@ -299,7 +299,7 @@ void InsetGraphics::read(Lexer & lex)
{ {
lex.setContext("InsetGraphics::read"); lex.setContext("InsetGraphics::read");
//lex >> "Graphics"; //lex >> "Graphics";
readInsetGraphics(lex, buffer().originFilePath(), params_); readInsetGraphics(lex, buffer(), true, params_);
graphic_->update(params().as_grfxParams()); graphic_->update(params().as_grfxParams());
} }
@ -1054,7 +1054,7 @@ void InsetGraphics::string2params(string const & in, Buffer const & buffer,
lex.setContext("InsetGraphics::string2params"); lex.setContext("InsetGraphics::string2params");
lex >> "graphics"; lex >> "graphics";
params = InsetGraphicsParams(); params = InsetGraphicsParams();
readInsetGraphics(lex, buffer.filePath(), params); readInsetGraphics(lex, buffer, false, params);
} }

View File

@ -177,11 +177,15 @@ void InsetGraphicsParams::Write(ostream & os, Buffer const & buffer) const
} }
bool InsetGraphicsParams::Read(Lexer & lex, string const & token, string const & bufpath) bool InsetGraphicsParams::Read(Lexer & lex, string const & token,
Buffer const & buf, bool allowOrigin)
{ {
if (token == "filename") { if (token == "filename") {
lex.eatLine(); lex.eatLine();
filename.set(lex.getString(), bufpath); if (allowOrigin)
filename = buf.getReferencedFileName(lex.getString());
else
filename.set(lex.getString(), buf.filePath());
} else if (token == "lyxscale") { } else if (token == "lyxscale") {
lex.next(); lex.next();
lyxscale = lex.getInteger(); lyxscale = lex.getInteger();

View File

@ -73,7 +73,8 @@ public:
/// Buffer is needed to figure out if a figure is embedded. /// Buffer is needed to figure out if a figure is embedded.
void Write(std::ostream & os, Buffer const & buf) const; void Write(std::ostream & os, Buffer const & buf) const;
/// If the token belongs to our parameters, read it. /// If the token belongs to our parameters, read it.
bool Read(Lexer & lex, std::string const & token, std::string const & bufpath); bool Read(Lexer & lex, std::string const & token, Buffer const & buf,
bool allowOrigin);
/// convert /// convert
// Only a subset of InsetGraphicsParams is needed for display purposes. // Only a subset of InsetGraphicsParams is needed for display purposes.
// This function also interrogates lyxrc to ascertain whether // This function also interrogates lyxrc to ascertain whether