On Sat, Jun 20, 2015 at 08:14:03AM +0100, Guillaume M-M wrote:
> Le 20/06/2015 01:11, Enrico Forestieri a écrit :
> >On Fri, Jun 19, 2015 at 04:50:43PM +0100, Guillaume M-M wrote:
> >
> >>Lastly I see that you prevent loading the microtype package by redefining
> >>\usepackage as you described earlier in the thread. While your redefinition
> >>of \usepackage seems to be very careful, I still think that passing the
> >>option draft to microtype is more reliable in case the user actually uses
> >>macros from microtype (in addition there's no need to test whether the user
> >>loads microtype).
> >
> >I am concerned with the fact that in dvi mode the microtype package
> >considerably slows down the processing and prefer to not allow it.
> 
> Yes, and the draft option disables the microtype features completely
> according to the manual. That's also why I mentioned it. But it's your call.

In this case I will reconsider. Please, let me perform some checks first.

> >Please, try again with master and report back. The required changes to
> >address these glitches are straightforward and maybe Richard will allow
> >backporting them to stable.
> >
> 
> I simply applied your patch 98a5072a to stable directly (quicker to
> recompile).

You were missing the amendment at b610c13f that solves the issue with
lyx-bug-undefined5.lyx.

> lyx-bug-undefined3.lyx: macros inside nested macro definitions (!) are not
> taken into account.

I knew you would have been able to spot another corner case :)
This should also be solved at cabc7c4b.

> lyx-bug-undefined4.lyx: a combination of two bugs I think. In particular,
> our friend \renewcommandx again. More detailed comments in the files.

Here it was going as follows. The script detected a postscript special
in the second preview snippet and thus decided to use the legacy route
through dvips for obtaining the preview instead of directly using dvipng.
In order to do that, the affected snippets are extracted from the original
latex file. Now, in the original file the second snippet was correctly
using \renewcommandx because the macro was already defined in the first
snippet, but now it was incorrect, of course. Also solved at cabc7c4b.

> lyx-bug-undefined5.lyx: the arguments are only scanned on the first
> occurrence of a macro.

This had already been taken into account at b610c13f.

For your convenience I am attaching the overall set of patches for
the stable branch. Please, let me know if you are able to find some
other remaining glitches :)

-- 
Enrico
diff --git a/lib/scripts/lyxpreview2bitmap.py b/lib/scripts/lyxpreview2bitmap.py
index a7d7623..c2b94d4 100755
--- a/lib/scripts/lyxpreview2bitmap.py
+++ b/lib/scripts/lyxpreview2bitmap.py
@@ -160,7 +160,7 @@ def extract_metrics_info(dvipng_stdout):
 
 def fix_latex_file(latex_file, pdf_output):
     documentclass_re = re.compile("(\\\\documentclass\[)(1[012]pt,?)(.+)")
-    def_re = re.compile(r"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z])(.+)")
+    def_re = 
re.compile(r"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z]+)(.+)")
     usepackage_re = re.compile("\\\\usepackage")
     userpreamble_re = re.compile("User specified LaTeX commands")
     enduserpreamble_re = re.compile("\\\\makeatother")
diff --git a/lib/scripts/lyxpreview_tools.py b/lib/scripts/lyxpreview_tools.py
index 40ac711..7db524d 100644
--- a/lib/scripts/lyxpreview_tools.py
+++ b/lib/scripts/lyxpreview_tools.py
@@ -272,11 +272,13 @@ def write_metrics_info(metrics_info, metrics_file):
 # Reads a .tex files and create an identical file but only with
 # pages whose index is in pages_to_keep
 def filter_pages(source_path, destination_path, pages_to_keep):
+    def_re = 
re.compile(r"(\\newcommandx|\\renewcommandx|\\global\\long\\def)(\\[a-zA-Z]+)(.+)")
     source_file = open(source_path, "r")
     destination_file = open(destination_path, "w")
 
     page_index = 0
     skip_page = False
+    macros = []
     for line in source_file:
         # We found a new page
         if line.startswith("\\begin{preview}"):
@@ -285,6 +287,14 @@ def filter_pages(source_path, destination_path, 
pages_to_keep):
             skip_page = page_index not in pages_to_keep
 
         if not skip_page:
+            match = def_re.match(line)
+            if match != None:
+                definecmd = match.group(1)
+                macroname = match.group(2)
+                if not macroname in macros:
+                    macros.append(macroname)
+                    if definecmd == "\\renewcommandx":
+                        line = line.replace(definecmd, "\\newcommandx")
             destination_file.write(line)
 
         # End of a page, we reset the skip_page bool
diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index 69643bb..0f62732 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -37,6 +37,7 @@
 #include "LyXRC.h"
 #include "MacroTable.h"
 #include "MathMacro.h"
+#include "MathMacroTemplate.h"
 #include "output_xhtml.h"
 #include "Paragraph.h"
 #include "ParIterator.h"
@@ -634,11 +635,15 @@ void InsetMathHull::usedMacros(MathData const & md, 
DocIterator const & pos,
 
        for (size_t i = 0; i < md.size(); ++i) {
                MathMacro const * mi = md[i].nucleus()->asMacro();
+               MathMacroTemplate const * mt = 
md[i].nucleus()->asMacroTemplate();
                InsetMathScript const * si = md[i].nucleus()->asScriptInset();
                InsetMathFracBase const * fi = 
md[i].nucleus()->asFracBaseInset();
                InsetMathGrid const * gi = md[i].nucleus()->asGridInset();
                InsetMathNest const * ni = md[i].nucleus()->asNestInset();
                if (mi) {
+                       // Look for macros in the arguments of this macro.
+                       for (idx_type idx = 0; idx < mi->nargs(); ++idx)
+                               usedMacros(mi->cell(idx), pos, macros, defs);
                        // Make sure this is a macro defined in the document
                        // (as we also spot the macros in the symbols file)
                        // or that we have not already accounted for it.
@@ -646,6 +651,7 @@ void InsetMathHull::usedMacros(MathData const & md, 
DocIterator const & pos,
                        if (macros.find(name) == end)
                                continue;
                        macros.erase(name);
+                       // Look for macros in the definition of this macro.
                        MathData ar(pos.buffer());
                        MacroData const * data =
                                pos.buffer()->getMacro(name, pos, true);
@@ -657,6 +663,10 @@ void InsetMathHull::usedMacros(MathData const & md, 
DocIterator const & pos,
                                asArray(data->definition(), ar);
                        }
                        usedMacros(ar, pos, macros, defs);
+               } else if (mt) {
+                       MathData ar(pos.buffer());
+                       asArray(mt->definition(), ar);
+                       usedMacros(ar, pos, macros, defs);
                } else if (si) {
                        if (!si->nuc().empty())
                                usedMacros(si->nuc(), pos, macros, defs);

Reply via email to