On Fri, Jun 05, 2015 at 09:40:39AM +0200, Jean-Marc Lasgouttes wrote:

> Le 05/06/2015 09:14, Enrico Forestieri a écrit :
> >A possible way out would be outputting all the macros once at the
> >beginning of the file, such that they are seen by all snippets, and then
> >the specific one appearing in a math inset (as done in the patch I was
> >proposing). I'll have a look whether this can be done in a simple way.
> 
> Isn't it possible to output them as they are output by the normal latex
> export? Of course, this would mean that creating previews would be a very
> different process, more like a special version of our latex output code,
> instead of some registration done by the insets themselves.

I don't think that is possible. The point is that the previews should be
updated whenever they change. Now, they can change even if the math inset
was not modified, because one could have modified a macro. Thus, the
snippets have also to contain the macros they use.

I tried to solve this problem by including in a snippet only the macros that
are actually used. I almost succeeded with the attached patch for stable
(master would require more surgery). I say almost because it seems that
only the macros used by the current macro are spotted, but not the ones
that, in turn, are used by them. I mean, after

\newcommand{\first}{pluto}
\newcommand{\second}{goofy\,and\,\first}
\newcommand{\third}{mickey,\,\second}
$\third$

the macros \second and \third are spotted but not \first. See also the
attached examples. After activating instant previews, when loading
lyx-preview-macros-2.lyx you will correctly see

Found: first
Found: second

but loading lyx-preview-macros-3.lyx produces

Found: second
Found: third
Warning: Failed to produce 1 preview snippet(s)

Essentially, the patch goes recursively through the definition of the
macros found in an inset, but it seems that the definition of "second
level" macros are empty? I am not so familiar with the code dealing
with math macros, but I thought that their definition in terms of a
LyX inset should be held in MacroData::definition_. However, it does
not seem to (always) be so?

Any help or suggestion will be appreciated.

-- 
Enrico
diff --git a/src/mathed/InsetMathHull.cpp b/src/mathed/InsetMathHull.cpp
index d4bdf87..a7db85f 100644
--- a/src/mathed/InsetMathHull.cpp
+++ b/src/mathed/InsetMathHull.cpp
@@ -31,6 +31,7 @@
 #include "Language.h"
 #include "LaTeXFeatures.h"
 #include "LyXRC.h"
+#include "MathMacro.h"
 #include "MacroTable.h"
 #include "output_xhtml.h"
 #include "Paragraph.h"
@@ -622,6 +623,18 @@ void InsetMathHull::addPreview(DocIterator const & 
inset_pos,
 }
 
 
+void InsetMathHull::usedMacros(MathData const & md, MacroNameSet & macros) 
const
+{
+       for (size_t i = 0; i < md.size(); ++i) {
+               MathMacro * mi = const_cast<MathMacro 
*>(md[i].nucleus()->asMacro());
+               if (mi) {
+                       macros.insert(mi->name());
+                       usedMacros(mi->definition(), macros);
+               }
+       }
+}
+
+
 void InsetMathHull::preparePreview(DocIterator const & pos,
                                    bool forexport) const
 {
@@ -632,13 +645,17 @@ void InsetMathHull::preparePreview(DocIterator const & 
pos,
 
        Buffer const * buffer = pos.buffer();
 
-       // collect macros at this position
+       // collect macros used in this inset
        MacroNameSet macros;
-       buffer->listMacroNames(macros);
+       for (row_type row = 0; row < nrows(); ++row)
+               for (col_type col = 0; col < ncols(); ++col)
+                       usedMacros(cell(index(row, col)), macros);
+
        MacroNameSet::iterator it = macros.begin();
        MacroNameSet::iterator end = macros.end();
        odocstringstream macro_preamble;
        for (; it != end; ++it) {
+               lyxerr << "Found: " << *it << endl;
                MacroData const * data = buffer->getMacro(*it, pos, true);
                if (data) {
                        data->write(macro_preamble, true);
diff --git a/src/mathed/InsetMathHull.h b/src/mathed/InsetMathHull.h
index 0cffffd..65cb0ea 100644
--- a/src/mathed/InsetMathHull.h
+++ b/src/mathed/InsetMathHull.h
@@ -25,6 +25,7 @@ namespace lyx {
 class InsetLabel;
 class ParConstIterator;
 class RenderPreview;
+class MacroNameSet;
 
 
 /// This provides an interface between "LyX insets" and "LyX math insets"
@@ -190,6 +191,8 @@ private:
        /// used by image export
        void loadPreview(DocIterator const & pos) const;
        ///
+       void usedMacros(MathData const & md, MacroNameSet & macros) const;
+       ///
        void setType(HullType type);
        ///
        void validate1(LaTeXFeatures & features);
diff --git a/src/mathed/MathMacro.h b/src/mathed/MathMacro.h
index 239495e..18048d3 100644
--- a/src/mathed/MathMacro.h
+++ b/src/mathed/MathMacro.h
@@ -135,6 +135,7 @@ protected:
        friend class MathData;
        friend class ArgumentProxy;
        friend class Cursor;
+       friend class InsetMathHull;
 
        /// update the display mode (should only be called after detaching 
arguments)
        void setDisplayMode(DisplayMode mode, int appetite = -1);
@@ -153,6 +154,8 @@ protected:
        ///
        MacroData const * macro() { return macro_; }
        ///
+       MathData const & definition() const { return definition_; }
+       ///
        bool editMetrics(BufferView const * bv) const;
 
 private:
#LyX 2.1 created this file. For more info see http://www.lyx.org/
\lyxformat 474
\begin_document
\begin_header
\textclass article
\use_default_options true
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_math auto
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\use_hyperref false
\papersize default
\use_geometry false
\use_package amsmath 1
\use_package amssymb 1
\use_package cancel 1
\use_package esint 1
\use_package mathdots 1
\use_package mathtools 1
\use_package mhchem 1
\use_package stackrel 1
\use_package stmaryrd 1
\use_package undertilde 1
\cite_engine basic
\cite_engine_type default
\biblio_style plain
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\justification true
\use_refstyle 1
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header

\begin_body

\begin_layout Standard
\begin_inset FormulaMacro
\newcommand{\first}{pluto}
\end_inset


\begin_inset FormulaMacro
\newcommand{\second}{goofy\,and\,\first}
\end_inset


\end_layout

\begin_layout Standard
\begin_inset Formula $\second$
\end_inset


\end_layout

\end_body
\end_document
#LyX 2.1 created this file. For more info see http://www.lyx.org/
\lyxformat 474
\begin_document
\begin_header
\textclass article
\use_default_options true
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_math auto
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\use_hyperref false
\papersize default
\use_geometry false
\use_package amsmath 1
\use_package amssymb 1
\use_package cancel 1
\use_package esint 1
\use_package mathdots 1
\use_package mathtools 1
\use_package mhchem 1
\use_package stackrel 1
\use_package stmaryrd 1
\use_package undertilde 1
\cite_engine basic
\cite_engine_type default
\biblio_style plain
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\justification true
\use_refstyle 1
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header

\begin_body

\begin_layout Standard
\begin_inset FormulaMacro
\newcommand{\first}{pluto}
\end_inset


\begin_inset FormulaMacro
\newcommand{\second}{goofy\,and\,\first}
\end_inset


\begin_inset FormulaMacro
\newcommand{\third}{mickey,\,\second}
\end_inset


\end_layout

\begin_layout Standard
\begin_inset Formula $\third$
\end_inset


\end_layout

\end_body
\end_document

Reply via email to