Am Freitag, 22. Oktober 2004 22:59 schrieb Angus Leeming:
> I won't apply this until I've finished the stuff above, but it now works
> as-is. Perhaps someone else would like to test it out?

I did, and I could not break it, but I found another reason to use this 
mover stuff: Bug 605 is still not completely fixed. Consider the 
following situation:

master.lyx includes sub/child.lyx
sub/child.lyx includes sub/pic.fig (external material, relative file name)
sub/pic.fig

You will get the following files for a latex export:

master.tex
sub/child.tex
sub/pic.pstex_t
sub/pic.eps

sub/child.tex contains "\input{sub/pic.pstex_t}"
sub/pic.pstex_t contains "\includegraphics{pic}" -> BANG! It should be 
"\includegraphics{sub/pic}".

This can be fixed if the external inset runs the conversion in the temp 
dir, too, and a copier is used to copy the resulting .pstex_t file. The 
attached patch builds on top of the mover patch and implements that. I 
did create copy and rename functions with a third argument. This argument 
is the "latex name" of the file, that means it is either absolute or 
relative to the master document.
BTW, getExtFromContents() is inconsistent: It returns format names, not 
extensions for formats that it knows, and it returns file extensions for 
formats that it does not know. I have a fix for this, but I wait with 
this until the mover stuff is finished.

> Incidentaly, is there a more elegant (shell script) way to ascertain
> whether two directories are the same than:

I would prefer python and use
os.path.normpath(FROM_DIR) == os.path.normpath(TO_DIR)
, but I guess this is not the answer you want to hear ;-)


Georg
diff -p -r -U 3 -X excl.tmp lyx-1.4-mover/lib/configure.m4 lyx-1.4-cvs/lib/configure.m4
--- lyx-1.4-mover/lib/configure.m4	2004-10-23 18:02:38.000000000 +0200
+++ lyx-1.4-cvs/lib/configure.m4	2004-10-24 21:12:05.000000000 +0200
@@ -589,6 +589,8 @@ cat >$outfile <<EOF
 \\converter word       latex      "$word_to_latex_command"	""
 
 \\copier    fig        "sh \$\$s/fig_copy.sh \$\$i \$\$o"
+\\copier    pstex      "python \$\$s/tex_copy.py \$\$i \$\$o \$\$l"
+\\copier    pdftex     "python \$\$s/tex_copy.py \$\$i \$\$o \$\$l"
 EOF
 
 ### the graphic converter part with the predefined ones
diff -p -r -U 3 -X excl.tmp lyx-1.4-mover/lib/external_templates lyx-1.4-cvs/lib/external_templates
--- lyx-1.4-mover/lib/external_templates	2004-06-01 15:32:22.000000000 +0200
+++ lyx-1.4-cvs/lib/external_templates	2004-10-24 20:38:58.000000000 +0200
@@ -121,9 +121,9 @@ Template XFig
 		Requirement "graphicx"
 		# Preamble WarnNotFound
 		# Preamble InputOrWarn
-		ReferencedFile latex "$$AbsPath$$Basename.pstex_t"
-		ReferencedFile latex "$$AbsPath$$Basename.pstex"
-		ReferencedFile dvi   "$$AbsPath$$Basename.pstex"
+		ReferencedFile latex "$$AbsOrRelPathMaster$$Basename.pstex_t"
+		ReferencedFile latex "$$AbsPath$$Basename.eps"
+		ReferencedFile dvi   "$$AbsPath$$Basename.eps"
 	FormatEnd
 	Format PDFLaTeX
 		TransformCommand Rotate RotationLatexCommand
@@ -134,8 +134,8 @@ Template XFig
 		Requirement "graphicx"
 		# Preamble WarnNotFound
 		# Preamble InputOrWarn
-		ReferencedFile latex "$$AbsPath$$Basename.pdftex_t"
-		ReferencedFile latex "$$AbsPath$$Basename.pdftex"
+		ReferencedFile latex "$$AbsOrRelPathMaster$$Basename.pdftex_t"
+		ReferencedFile latex "$$AbsPath$$Basename.pdf"
 	FormatEnd
 	Format Ascii
 		Product "$$Contents(\"$$AbsPath$$Basename.asc\")"
diff -p -r -U 3 -X excl.tmp lyx-1.4-mover/lib/scripts/tex_copy.py lyx-1.4-cvs/lib/scripts/tex_copy.py
--- lyx-1.4-mover/lib/scripts/tex_copy.py	2004-10-23 22:08:12.000000000 +0200
+++ lyx-1.4-cvs/lib/scripts/tex_copy.py	2004-10-24 09:10:35.000000000 +0200
@@ -0,0 +1,69 @@
+#! /usr/bin/env python
+# -*- coding: iso-8859-1 -*-
+
+# file tex_copy.py
+# This file is part of LyX, the document processor.
+# Licence details can be found in the file COPYING.
+
+# author Angus Leeming
+# author Georg Baum
+
+# Full author contact details are available in file CREDITS
+
+# Usage:
+# tex_copy.py from file> <to file> <latex name>
+
+# This script will copy a file <from file> to <to file>.
+# <to file> is no exact copy of <from file>, but any occurence of <basename>
+# where <basename> is <from file> without directory and extension parts is
+# replaced by <latex name> without extension.
+
+
+import os, string, sys
+
+from lyxpreview_tools import error
+
+
+def usage(prog_name):
+    return "Usage: %s <from file> <to file> <latex name>" % prog_name
+
+
+def main(argv):
+    # Parse and manipulate the command line arguments.
+    if len(argv) != 4:
+        error(usage(argv[0]))
+
+    # input file
+    abs_from_file = argv[1]
+    if not os.path.isabs(abs_from_file):
+        error("%s is no absolute file name.\n%s"\
+              % abs_from_file, usage(argv[0]))
+    from_dir, rel_from_file = os.path.split(abs_from_file)
+    from_base, from_ext = os.path.splitext(rel_from_file)
+
+    # output file
+    abs_to_file = argv[2]
+    if not os.path.isabs(abs_to_file):
+        error("%s is no absolute file name.\n%s"\
+              % abs_to_file, usage(argv[0]))
+    to_dir, rel_to_file = os.path.split(abs_to_file)
+    to_base, to_ext = os.path.splitext(rel_to_file)
+
+    # latex file name
+    latex_file = argv[3]
+    latex_base, latex_ext = os.path.splitext(latex_file)
+
+    # Read the input file and write the output file
+    from_file = open(abs_from_file, 'rb')
+    to_file = open(abs_to_file, 'wb')
+    lines = from_file.readlines()
+    for line in lines:
+        to_file.write(line.replace(from_base, latex_base))
+    from_file.close()
+    to_file.close()
+
+    return 0
+
+
+if __name__ == "__main__":
+    main(sys.argv)
diff -p -r -U 3 -X excl.tmp lyx-1.4-mover/src/exporter.C lyx-1.4-cvs/src/exporter.C
--- lyx-1.4-mover/src/exporter.C	2004-10-11 14:06:24.000000000 +0200
+++ lyx-1.4-cvs/src/exporter.C	2004-10-24 17:59:25.000000000 +0200
@@ -25,6 +25,7 @@
 #include "format.h"
 #include "gettext.h"
 #include "lyxrc.h"
+#include "mover.h"
 #include "output_plaintext.h"
 #include "outputparams.h"
 #include "frontends/Alert.h"
@@ -92,8 +94,9 @@ enum CopyStatus {
  *            overwriting files anymore.
  *  - CANCEL  if the export should be cancelled
  */
-CopyStatus copyFile(string const & sourceFile, string const & destFile,
-		    bool force)
+CopyStatus copyFile(string const & format,
+                    string const & sourceFile, string const & destFile,
+                    string const & latexFile, bool force)
 {
 	CopyStatus ret = force ? FORCE : SUCCESS;
 
@@ -117,7 +120,8 @@ CopyStatus copyFile(string const & sourc
 		}
 	}
 
-	if (!lyx::support::copy(sourceFile, destFile))
+	Mover const & mover = movers(format);
+	if (!mover.copy(sourceFile, destFile, latexFile))
 		Alert::error(_("Couldn't copy file"),
 		             bformat(_("Copying %1$s to %2$s failed."),
 		                     MakeDisplayPath(sourceFile),
@@ -203,15 +210,18 @@ bool Exporter::Export(Buffer * buffer, s
 		string const dest = OnlyPath(result_file);
 		CopyStatus status = SUCCESS;
 		for (vector<ExportedFile>::const_iterator it = files.begin();
-				it != files.end() && status != CANCEL; ++it)
-			status = copyFile(it->sourceName,
+				it != files.end() && status != CANCEL; ++it) {
+			string const fmt = getExtFromContents(it->sourceName);
+			status = copyFile(fmt, it->sourceName,
 			                  MakeAbsPath(it->exportName, dest),
-			                  status == FORCE);
+			                  it->exportName, status == FORCE);
+		}
 		if (status == CANCEL) {
 			buffer->message(_("Document export cancelled."));
 		} else {
 			// Finally copy the main file
-			status = copyFile(tmp_result_file, result_file,
+			status = copyFile(format, tmp_result_file,
+			                  result_file, result_file,
 			                  status == FORCE);
 			buffer->message(bformat(_("Document exported as %1$s"
 			                          "to file `%2$s'"),
diff -p -r -U 3 -X excl.tmp lyx-1.4-mover/src/insets/ExternalSupport.C lyx-1.4-cvs/src/insets/ExternalSupport.C
--- lyx-1.4-mover/src/insets/ExternalSupport.C	2004-10-23 18:02:38.000000000 +0200
+++ lyx-1.4-cvs/src/insets/ExternalSupport.C	2004-10-24 20:39:20.000000000 +0200
@@ -196,11 +198,12 @@ void updateExternal(InsetExternalParams 
 	// of include files
 	Buffer const * m_buffer = buffer.getMasterBuffer();
 
-	if (external_in_tmpdir && !abs_from_file.empty()) {
-		// We are running stuff through LaTeX
-		string const temp_file =
-			support::MakeAbsPath(params.filename.mangledFilename(),
-					     m_buffer->temppath());
+	// We copy the source file to the temp dir and do the conversion
+	// there if necessary
+	string const temp_file =
+		support::MakeAbsPath(params.filename.mangledFilename(),
+				     m_buffer->temppath());
+	if (!abs_from_file.empty()) {
 		unsigned long const from_checksum = support::sum(abs_from_file);
 		unsigned long const temp_checksum = support::sum(temp_file);
 
@@ -214,20 +217,17 @@ void updateExternal(InsetExternalParams 
 				return; // FAILURE
 			}
 		}
-
-		abs_from_file = temp_file;
 	}
 
+	// the generated file (always in the temp dir)
 	string const to_file = doSubstitution(params, buffer,
 					      outputFormat.updateResult,
-	                                      external_in_tmpdir);
-
+	                                      true);
 	string const abs_to_file =
-		support::MakeAbsPath(to_file, external_in_tmpdir
-			? m_buffer->temppath()
-			: buffer.filePath());
+		support::MakeAbsPath(to_file, m_buffer->temppath());
 
-	// record the referenced files for the exporter
+	// Record the referenced files for the exporter.
+	// The exporter will copy them to the export dir.
 	typedef Template::Format::FileMap FileMap;
 	FileMap::const_iterator rit  = outputFormat.referencedFiles.begin();
 	FileMap::const_iterator rend = outputFormat.referencedFiles.end();
@@ -235,22 +235,27 @@ void updateExternal(InsetExternalParams 
 		vector<string>::const_iterator fit  = rit->second.begin();
 		vector<string>::const_iterator fend = rit->second.end();
 		for (; fit != fend; ++fit) {
+			string const source = support::MakeAbsPath(
+					doSubstitution(params, buffer, *fit,
+					               true),
+					m_buffer->temppath());
 			string const file = doSubstitution(params, buffer,
 			                                   *fit,
 			                                   external_in_tmpdir);
-			exportdata.addExternalFile(rit->first, file);
+			// if file is a relative name, it is interpreted
+			// relative to the master document.
+			exportdata.addExternalFile(rit->first, source, file);
 		}
 	}
 
 	// Do we need to perform the conversion?
 	// Yes if to_file does not exist or if from_file is newer than to_file
-	if (support::compare_timestamps(abs_from_file, abs_to_file) < 0)
+	if (support::compare_timestamps(temp_file, abs_to_file) < 0)
 		return; // SUCCESS
-
 	string const to_file_base =
 		support::ChangeExtension(to_file, string());
 	/* bool const success = */
-		converters.convert(&buffer, abs_from_file, to_file_base,
+		converters.convert(&buffer, temp_file, to_file_base,
 				   from_format, to_format);
 	// return success
 }
diff -p -r -U 3 -X excl.tmp lyx-1.4-mover/src/mover.C lyx-1.4-cvs/src/mover.C
--- lyx-1.4-mover/src/mover.C	2004-10-23 18:02:38.000000000 +0200
+++ lyx-1.4-cvs/src/mover.C	2004-10-24 09:36:04.000000000 +0200
@@ -26,13 +26,15 @@ Movers movers;
 Movers system_movers;
 
 
-bool Mover::do_copy(string const & from, string const & to) const
+bool Mover::do_copy(string const & from, string const & to,
+                    string const &) const
 {
 	return support::copy(from, to);
 }
 
 
-bool Mover::do_rename(string const & from, string const & to) const
+bool Mover::do_rename(string const & from, string const & to,
+                      string const &) const
 {
 	return support::rename(from, to);
 }
@@ -43,19 +45,22 @@ SpecialisedMover::SpecialisedMover(strin
 {}
 
 
-bool SpecialisedMover::do_copy(string const & from, string const & to) const
+bool SpecialisedMover::do_copy(string const & from, string const & to,
+                               string const & latex) const
 {
 	string command = support::subst(command_, "$$i", from);
 	command = support::subst(command, "$$o", to);
+	command = support::subst(command, "$$l", latex);
 
 	support::Systemcall one;
 	return one.startscript(support::Systemcall::Wait, command) == 0;
 }
 
 
-bool SpecialisedMover::do_rename(string const & from, string const & to) const
+bool SpecialisedMover::do_rename(string const & from, string const & to,
+                                 string const & latex) const
 {
-	if (!do_copy(from, to))
+	if (!do_copy(from, to, latex))
 		return false;
 	return support::unlink(from) == 0;
 }
diff -p -r -U 3 -X excl.tmp lyx-1.4-mover/src/mover.h lyx-1.4-cvs/src/mover.h
--- lyx-1.4-mover/src/mover.h	2004-10-23 18:02:38.000000000 +0200
+++ lyx-1.4-cvs/src/mover.h	2004-10-24 09:36:09.000000000 +0200
@@ -31,7 +31,17 @@ public:
 	bool
 	copy(std::string const & from, std::string const & to) const
 	{
-		return do_copy(from, to);
+		return do_copy(from, to, to);
+	}
+
+	/** Copy file @c from to @c to.
+	 *  \returns true if successful.
+	 */
+	bool
+	copy(std::string const & from, std::string const & to,
+	     std::string const & latex) const
+	{
+		return do_copy(from, to, latex);
 	}
 
 	/** Rename file @c from as @c to.
@@ -40,15 +50,27 @@ public:
 	bool
 	rename(std::string const & from, std::string const & to) const
 	{
-		return do_rename(from, to);
+		return do_rename(from, to, to);
+	}
+
+	/** Rename file @c from as @c to.
+	 *  \returns true if successful.
+	 */
+	bool
+	rename(std::string const & from, std::string const & to,
+	       std::string const & latex) const
+	{
+		return do_rename(from, to, latex);
 	}
 
 private:
 	virtual bool
-	do_copy(std::string const & from, std::string const & to) const;
+	do_copy(std::string const & from, std::string const & to,
+	        std::string const & latex) const;
 
 	virtual bool
-	do_rename(std::string const & from, std::string const & to) const;
+	do_rename(std::string const & from, std::string const & to,
+	          std::string const & latex) const;
 };
 
 
@@ -69,15 +91,19 @@ struct SpecialisedMover : public Mover {
 	 *  where $$s is a placeholder for the lyx script directory,
 	 *        $$i is a placeholder for the name of the file to be moved,
 	 *        $$o is a placeholder for the name of the file after moving.
+	 *        $$l is a placeholder for the name of the file after moving,
+	 *            suitable as argument to a latex include command.
 	 */
 	SpecialisedMover(std::string const & command);
 
 private:
 	virtual bool
-	do_copy(std::string const & from, std::string const & to) const;
+	do_copy(std::string const & from, std::string const & to,
+	        std::string const & latex) const;
 
 	virtual bool
-	do_rename(std::string const & from, std::string const & to) const;
+	do_rename(std::string const & from, std::string const & to,
+	          std::string const & latex) const;
 
 	std::string command_;
 };

Reply via email to