Properly fix #6369

Avoid that \newcommand[x] definitions of math macros are pushed multiple
times to the preview loader.

Redefinitions (via \renewcommand[x]) are properly handled.
This commit is contained in:
Juergen Spitzmueller 2015-04-30 16:48:55 +02:00
parent 8efc734f1a
commit bc47054be8
6 changed files with 75 additions and 13 deletions

View File

@ -160,25 +160,18 @@ def extract_metrics_info(dvipng_stdout):
def fix_latex_file(latex_file): def fix_latex_file(latex_file):
documentclass_re = re.compile("(\\\\documentclass\[)(1[012]pt,?)(.+)") documentclass_re = re.compile("(\\\\documentclass\[)(1[012]pt,?)(.+)")
newcommandx_re = re.compile("^(\\\\newcommandx)(.+)")
tmp = mkstemp() tmp = mkstemp()
changed = 0 changed = 0
for line in open(latex_file, 'r').readlines(): for line in open(latex_file, 'r').readlines():
match = documentclass_re.match(line) match = documentclass_re.match(line)
if match != None:
changed = 1
tmp.write("%s%s\n" % (match.group(1), match.group(3)))
continue
match = newcommandx_re.match(line)
if match == None: if match == None:
tmp.write(line) tmp.write(line)
continue continue
changed = 1 changed = 1
tmp.write("\\providecommandx%s\n" % match.group(2)) tmp.write("%s%s\n" % (match.group(1), match.group(3)))
if changed: if changed:
copyfileobj(tmp, open(latex_file,"wb"), 1) copyfileobj(tmp, open(latex_file,"wb"), 1)

View File

@ -205,6 +205,10 @@ public:
void add(string const & latex_snippet); void add(string const & latex_snippet);
/// ///
void remove(string const & latex_snippet); void remove(string const & latex_snippet);
/// Record math macro definitions added to the loader
void addMacroDef(docstring const & latex_snippet);
/// Has a math macro definition already been added to the loader?
bool hasMacroDef(docstring const & latex_snippet) const;
/// \p wait whether to wait for the process to complete or, instead, /// \p wait whether to wait for the process to complete or, instead,
/// to do it in the background. /// to do it in the background.
void startLoading(bool wait = false); void startLoading(bool wait = false);
@ -242,6 +246,9 @@ private:
*/ */
InProgressProcesses in_progress_; InProgressProcesses in_progress_;
///
set<docstring> macrodefs_;
/// ///
PreviewLoader & parent_; PreviewLoader & parent_;
/// ///
@ -296,6 +303,18 @@ void PreviewLoader::remove(string const & latex_snippet) const
} }
void PreviewLoader::addMacroDef(docstring const & latex_snippet) const
{
pimpl_->addMacroDef(latex_snippet);
}
bool PreviewLoader::hasMacroDef(docstring const & latex_snippet) const
{
return pimpl_->hasMacroDef(latex_snippet);
}
void PreviewLoader::startLoading(bool wait) const void PreviewLoader::startLoading(bool wait) const
{ {
pimpl_->startLoading(wait); pimpl_->startLoading(wait);
@ -532,6 +551,18 @@ void PreviewLoader::Impl::remove(string const & latex_snippet)
} }
void PreviewLoader::Impl::addMacroDef(docstring const & latex_snippet)
{
macrodefs_.insert(latex_snippet);
}
bool PreviewLoader::Impl::hasMacroDef(docstring const & latex_snippet) const
{
return macrodefs_.find(latex_snippet) != macrodefs_.end();
}
void PreviewLoader::Impl::startLoading(bool wait) void PreviewLoader::Impl::startLoading(bool wait)
{ {
if (pending_.empty() || !pconverter_) if (pending_.empty() || !pconverter_)

View File

@ -19,6 +19,7 @@
#define PREVIEWLOADER_H #define PREVIEWLOADER_H
#include <boost/signal.hpp> #include <boost/signal.hpp>
#include "support/docstring.h"
#include "ColorCode.h" #include "ColorCode.h"
@ -65,6 +66,11 @@ public:
/// Remove this snippet of LaTeX from the PreviewLoader. /// Remove this snippet of LaTeX from the PreviewLoader.
void remove(std::string const & latex_snippet) const; void remove(std::string const & latex_snippet) const;
/// Record math macro definitions added to the loader
void addMacroDef(docstring const & latex_snippet) const;
/// Has a math macro definition already been added to the loader?
bool hasMacroDef(docstring const & latex_snippet) const;
/** We have accumulated several latex snippets with status "InQueue". /** We have accumulated several latex snippets with status "InQueue".
* Initiate their transformation into bitmap images. * Initiate their transformation into bitmap images.
*/ */

View File

@ -257,6 +257,24 @@ void RenderPreview::removePreview(Buffer const & buffer)
} }
void RenderPreview::addMacroDef(docstring const & latex_snippet,
Buffer const & buffer)
{
graphics::PreviewLoader * loader = buffer.loader();
LASSERT(loader, return);
loader->addMacroDef(latex_snippet);
}
bool RenderPreview::hasMacroDef(docstring const & latex_snippet,
Buffer const & buffer)
{
graphics::PreviewLoader * loader = buffer.loader();
LASSERT(loader, return false);
return loader->hasMacroDef(latex_snippet);
}
void RenderPreview::imageReady(graphics::PreviewImage const & pimage) void RenderPreview::imageReady(graphics::PreviewImage const & pimage)
{ {
// Check the current snippet is the same as that previewed. // Check the current snippet is the same as that previewed.

View File

@ -74,6 +74,11 @@ public:
graphics::PreviewLoader & ploader, graphics::PreviewLoader & ploader,
bool ignore_lyxrc = false); bool ignore_lyxrc = false);
/// Record math macro definitions added to the preview loader
void addMacroDef(docstring const & latex_snippet, Buffer const & buffer);
/// Has a math macro definition already been added to the preview loader?
bool hasMacroDef(docstring const & latex_snippet, Buffer const & buffer);
/// Begin the loading process. /// Begin the loading process.
/// \param forexport : whether this is intended for export. if so, /// \param forexport : whether this is intended for export. if so,
/// then we ignore LyXRC and wait for the image to be generated. /// then we ignore LyXRC and wait for the image to be generated.

View File

@ -637,12 +637,22 @@ void InsetMathHull::preparePreview(DocIterator const & pos,
buffer->listMacroNames(macros); buffer->listMacroNames(macros);
MacroNameSet::iterator it = macros.begin(); MacroNameSet::iterator it = macros.begin();
MacroNameSet::iterator end = macros.end(); MacroNameSet::iterator end = macros.end();
odocstringstream macro_preamble; docstring macro_preamble;
for (; it != end; ++it) { for (; it != end; ++it) {
MacroData const * data = buffer->getMacro(*it, pos, true); MacroData const * data = buffer->getMacro(*it, pos, true);
if (data) { if (data) {
data->write(macro_preamble, true); odocstringstream mac_preamble;
macro_preamble << endl; data->write(mac_preamble, false);
docstring const mps = mac_preamble.str();
bool const is_new_def = prefixIs(mps, from_ascii("\\newcomm"));
// assure that \newcommand definitions of macros are only added once
if (!is_new_def || !preview_->hasMacroDef(mps, *buffer)) {
if (is_new_def)
preview_->addMacroDef(mps, *buffer);
if (!macro_preamble.empty())
macro_preamble += '\n';
macro_preamble += mps;
}
} }
} }
@ -667,8 +677,7 @@ void InsetMathHull::preparePreview(DocIterator const & pos,
'{' + convert<docstring>(num) + '}'; '{' + convert<docstring>(num) + '}';
} }
} }
docstring const snippet = macro_preamble.str() + docstring const snippet = macro_preamble + setcnt + latexString(*this);
setcnt + latexString(*this);
LYXERR(Debug::MACROS, "Preview snippet: " << snippet); LYXERR(Debug::MACROS, "Preview snippet: " << snippet);
preview_->addPreview(snippet, *buffer, forexport); preview_->addPreview(snippet, *buffer, forexport);
} }