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):
documentclass_re = re.compile("(\\\\documentclass\[)(1[012]pt,?)(.+)")
newcommandx_re = re.compile("^(\\\\newcommandx)(.+)")
tmp = mkstemp()
changed = 0
for line in open(latex_file, 'r').readlines():
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:
tmp.write(line)
continue
changed = 1
tmp.write("\\providecommandx%s\n" % match.group(2))
tmp.write("%s%s\n" % (match.group(1), match.group(3)))
if changed:
copyfileobj(tmp, open(latex_file,"wb"), 1)

View File

@ -205,6 +205,10 @@ public:
void add(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,
/// to do it in the background.
void startLoading(bool wait = false);
@ -242,6 +246,9 @@ private:
*/
InProgressProcesses in_progress_;
///
set<docstring> macrodefs_;
///
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
{
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)
{
if (pending_.empty() || !pconverter_)

View File

@ -19,6 +19,7 @@
#define PREVIEWLOADER_H
#include <boost/signal.hpp>
#include "support/docstring.h"
#include "ColorCode.h"
@ -65,6 +66,11 @@ public:
/// Remove this snippet of LaTeX from the PreviewLoader.
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".
* 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)
{
// Check the current snippet is the same as that previewed.

View File

@ -74,6 +74,11 @@ public:
graphics::PreviewLoader & ploader,
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.
/// \param forexport : whether this is intended for export. if so,
/// 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);
MacroNameSet::iterator it = macros.begin();
MacroNameSet::iterator end = macros.end();
odocstringstream macro_preamble;
docstring macro_preamble;
for (; it != end; ++it) {
MacroData const * data = buffer->getMacro(*it, pos, true);
if (data) {
data->write(macro_preamble, true);
macro_preamble << endl;
odocstringstream mac_preamble;
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) + '}';
}
}
docstring const snippet = macro_preamble.str() +
setcnt + latexString(*this);
docstring const snippet = macro_preamble + setcnt + latexString(*this);
LYXERR(Debug::MACROS, "Preview snippet: " << snippet);
preview_->addPreview(snippet, *buffer, forexport);
}