From e9c209c29883b515e73dc11dab42a32783118250 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Thu, 4 Sep 2003 17:01:00 +0000 Subject: [PATCH] The remaining External Template clean-up patch ;-) git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7666 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/ChangeLog | 5 ++ lib/external_templates | 36 ++++---- src/ChangeLog | 7 ++ src/LaTeXFeatures.C | 14 ++-- src/box.C | 8 +- src/debug.C | 3 +- src/debug.h | 4 +- src/insets/ChangeLog | 10 +++ src/insets/ExternalTemplate.C | 152 +++++++++++++++++++++++----------- src/insets/ExternalTemplate.h | 14 +++- src/insets/insetexternal.C | 44 +++++----- 11 files changed, 192 insertions(+), 105 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index f046ccbaa4..bfb9361af1 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,8 @@ +2003-09-04 Angus Leeming + + * external_templates: add a new PreambleDef section and use it in the + XFig template. + 2003-09-04 Johnathan Burchill * external_templates: add some clever LaTeX to the XFIG template so that diff --git a/lib/external_templates b/lib/external_templates index 32fdc95e6c..639c5b3533 100644 --- a/lib/external_templates +++ b/lib/external_templates @@ -1,5 +1,17 @@ # Basic External Templates for LyX +PreambleDef XFigInput + %% This inputs the file if it exists, else prints a warning + %% encased in an fbox. + \def\stripprefix#1>{} + \newcommand*{\xfiginput}[1]{% + \edef\tempfilename{#1}% + \InputIfFileExists{#1}{}{% + \fbox{Could not find + \ttfamily\expandafter\stripprefix\meaning\tempfilename + !}}} +PreambleDefEnd + Template XFig GuiName "[XFig: $$Basename]" HelpText @@ -15,34 +27,14 @@ Template XFig UpdateFormat pstex UpdateResult "$$Basename.pstex_t" Requirement "graphicx" - Preamble - %% This inputs the file if it exists, else prints a warning - %% encased in an fbox. - \def\stripprefix#1>{} - \newcommand*{\xfiginput}[1]{% - \edef\tempfilename{#1}% - \InputIfFileExists{#1}{}{% - \fbox{Could not find - \ttfamily\expandafter\stripprefix\meaning\tempfilename - !}}} - PreambleEnd + Preamble XFigInput FormatEnd Format PDFLaTeX Product "\\xfiginput{$$Basename.pdftex_t}" UpdateFormat pdftex UpdateResult "$$Basename.pdftex_t" Requirement "graphicx" - Preamble - %% This inputs the file if it exists, else prints a warning - %% encased in an fbox. - \def\stripprefix#1>{} - \newcommand*{\xfiginput}[1]{% - \edef\tempfilename{#1}% - \InputIfFileExists{#1}{}{% - \fbox{Could not find - \ttfamily\expandafter\stripprefix\meaning\tempfilename - !}}} - PreambleEnd + Preamble XFigInput FormatEnd Format Ascii Product "$$Contents(\"$$Basename.asc\")" diff --git a/src/ChangeLog b/src/ChangeLog index d09477a021..9eaffcc2b2 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2003-09-04 Angus Leeming + + * LaTeXFeatures.C (getMacros): move the output of the preamble_snippets + here (from getPackages). + + * debug.[Ch]: add a new EXTERNAL tag. + 2003-09-04 Lars Gullik Bjønnes * text2.C (cursorEnd): simplify diff --git a/src/LaTeXFeatures.C b/src/LaTeXFeatures.C index 6984882c5e..9f6e599206 100644 --- a/src/LaTeXFeatures.C +++ b/src/LaTeXFeatures.C @@ -302,12 +302,6 @@ string const LaTeXFeatures::getPackages() const packages << "]{natbib}\n"; } - FeaturesList::const_iterator pit = preamble_snippets.begin(); - FeaturesList::const_iterator pend = preamble_snippets.end(); - for (; pit != pend; ++pit) { - packages << *pit << '\n'; - } - return STRCONV(packages.str()); } @@ -316,6 +310,14 @@ string const LaTeXFeatures::getMacros() const { ostringstream macros; + if (!preamble_snippets.empty()) + macros << '\n'; + FeaturesList::const_iterator pit = preamble_snippets.begin(); + FeaturesList::const_iterator pend = preamble_snippets.end(); + for (; pit != pend; ++pit) { + macros << *pit << '\n'; + } + if (isRequired("LyX")) macros << lyx_def << '\n'; diff --git a/src/box.C b/src/box.C index 4ee0b2afb0..af520569dc 100644 --- a/src/box.C +++ b/src/box.C @@ -19,13 +19,15 @@ using std::ostream; +Box::Box() + : x1(0), x2(0), y1(0), y2(0) +{} + + Box::Box(int x1_, int x2_, int y1_, int y2_) : x1(x1_), x2(x2_), y1(y1_), y2(y2_) {} -Box::Box() - : x1(0), x2(0), y1(0), y2(0) -{} bool Box::contains(int x, int y) { diff --git a/src/debug.C b/src/debug.C index 9fe1f2a903..478fe99a7b 100644 --- a/src/debug.C +++ b/src/debug.C @@ -57,6 +57,7 @@ error_item errorTags[] = { { Debug::INSETTEXT, "insettext", N_("Insettext/tabular messages")}, { Debug::GRAPHICS, "graphics", N_("Graphics conversion and loading")}, { Debug::CHANGES, "changes", N_("Change tracking")}, + { Debug::EXTERNAL, "external", N_("External template/inset messages")}, { Debug::ANY, "any", N_("All debugging messages")} }; @@ -72,7 +73,7 @@ Debug::type const Debug::ANY = Debug::type( Debug::MATHED | Debug::FONT | Debug::TCLASS | Debug::LYXVC | Debug::LYXSERVER | Debug::ROFF | Debug::ACTION | Debug::LYXLEX | Debug::DEPEND | Debug::INSETS | Debug::FILES | Debug::WORKAREA | - Debug::INSETTEXT | Debug::GRAPHICS | Debug::CHANGES); + Debug::INSETTEXT | Debug::GRAPHICS | Debug::CHANGES | Debug::EXTERNAL); Debug::type Debug::value(string const & val) diff --git a/src/debug.h b/src/debug.h index 6b8a7f5940..7d8ee4b8b2 100644 --- a/src/debug.h +++ b/src/debug.h @@ -71,7 +71,9 @@ struct Debug { /// GRAPHICS = (1 << 21), /// change tracking - CHANGES = (1 << 22) + CHANGES = (1 << 22), + /// + EXTERNAL = (1 << 23) }; /// static type const ANY; diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index df8d6fafe2..f22b964bc2 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -10,6 +10,16 @@ up the dialogs. Cursor has to be in front of the inset (i.e. start of row) for this to function. +2003-09-04 Angus Leeming + + * ExternalTemplate.[Ch]: store the preamble snippets in a separate + structure outside of the Format struct. Format::preambleName refers to + an item in the map of all preamble snippets. + Make use of the new Debug::EXTERNAL flag. + + * insetexternal.C: make use of the new Debug::EXTERNAL flag. + (validate): Small change due to the changed storage of preamble snippets. + 2003-09-04 Angus Leeming * insetgraphics.C: #include "support/os.h" diff --git a/src/insets/ExternalTemplate.C b/src/insets/ExternalTemplate.C index c413190929..959706e994 100644 --- a/src/insets/ExternalTemplate.C +++ b/src/insets/ExternalTemplate.C @@ -17,11 +17,12 @@ #include "support/path.h" #include "support/LAssert.h" #include "support/filetools.h" +#include "support/lstrings.h" #include "support/path_defines.h" #include -using namespace lyx::support; +namespace support = lyx::support; using std::endl; using std::ostream; @@ -41,28 +42,50 @@ ExternalTemplate::FormatTemplate::FormatTemplate() ExternalTemplateManager::ExternalTemplateManager() { // gimp gnuchess gnuplot ical netscape tetris xpaint - readTemplates(user_lyxdir()); - if (lyxerr.debugging()) - dumpTemplates(); + readTemplates(support::user_lyxdir()); + if (lyxerr.debugging(Debug::EXTERNAL)) { + dumpPreambleDefs(lyxerr); + lyxerr << '\n'; + dumpTemplates(lyxerr); + } } +class dumpPreambleDef { +public: + typedef ExternalTemplateManager::PreambleDefs::value_type value_type; + + dumpPreambleDef(ostream & o) : ost(o) {} + + void operator()(value_type const & vt) { + ost << "PreambleDef " << vt.first << '\n' + << vt.second + << "PreambleDefEnd" << endl; + } + +private: + ostream & ost; +}; + + class dumpTemplate { public: - dumpTemplate(ostream & o) - : ost(o) {} - void operator()(ExternalTemplateManager::Templates::value_type const & vt) { + typedef ExternalTemplateManager::Templates::value_type value_type; + + dumpTemplate(ostream & o) : ost(o) {} + + void operator()(value_type const & vt) { ExternalTemplate const & et = vt.second; - ost << "Template " << et.lyxName << "\n" - << "\tGuiName " << et.guiName << "\n" + ost << "Template " << et.lyxName << '\n' + << "\tGuiName " << et.guiName << '\n' << "\tHelpText\n" << et.helpText << "\tHelpTextEnd\n" - << "\tInputFormat " << et.inputFormat << "\n" - << "\tFileFilter " << et.fileRegExp << "\n" - << "\tEditCommand " << et.editCommand << "\n" - << "\tAutomaticProduction " << et.automaticProduction << "\n"; + << "\tInputFormat " << et.inputFormat << '\n' + << "\tFileFilter " << et.fileRegExp << '\n' + << "\tEditCommand " << et.editCommand << '\n' + << "\tAutomaticProduction " << et.automaticProduction << '\n'; et.dumpFormats(ost); ost << "TemplateEnd" << endl; @@ -74,18 +97,18 @@ private: class dumpFormat { public: - dumpFormat(ostream & o) - : ost(o) {} - void operator()(ExternalTemplate::Formats::value_type const & vt) const{ + typedef ExternalTemplate::Formats::value_type value_type; + + dumpFormat(ostream & o) : ost(o) {} + + void operator()(value_type const & vt) const{ ExternalTemplate::FormatTemplate const & ft = vt.second; - ost << "\tFormat " << vt.first << "\n" - << "\t\tProduct " << ft.product << "\n" - << "\t\tUpdateFormat " << ft.updateFormat << "\n" - << "\t\tUpdateResult " << ft.updateResult << "\n" - << "\t\tRequirement " << ft.requirement << "\n" - << "\t\tPreamble\n" - << ft.preamble - << "\t\tPreambleEnd\n" + ost << "\tFormat " << vt.first << '\n' + << "\t\tProduct " << ft.product << '\n' + << "\t\tUpdateFormat " << ft.updateFormat << '\n' + << "\t\tUpdateResult " << ft.updateResult << '\n' + << "\t\tRequirement " << ft.requirement << '\n' + << "\t\tPreamble " << ft.preambleName << '\n' << "\tFormatEnd\n"; } private: @@ -99,9 +122,15 @@ void ExternalTemplate::dumpFormats(ostream & os) const } -void ExternalTemplateManager::dumpTemplates() const +void ExternalTemplateManager::dumpPreambleDefs(ostream & os) const { - for_each(templates.begin(), templates.end(), dumpTemplate(lyxerr)); + for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os)); +} + + +void ExternalTemplateManager::dumpTemplates(ostream & os) const +{ + for_each(templates.begin(), templates.end(), dumpTemplate(os)); } @@ -132,48 +161,76 @@ ExternalTemplate const & ExternalTemplateManager::getTemplateByName(string const } +string const +ExternalTemplateManager::getPreambleDefByName(string const & name) const +{ + string const trimmed_name = support::trim(name); + if (trimmed_name.empty()) + return string(); + + PreambleDefs::const_iterator it = preambledefs.find(trimmed_name); + if (it == preambledefs.end()) + return string(); + + return it->second; +} + + void ExternalTemplateManager::readTemplates(string const & path) { - Path p(path); + support::Path p(path); enum TemplateTags { - TM_TEMPLATE = 1, - TM_END + TM_PREAMBLEDEF = 1, + TM_PREAMBLEDEF_END, + TM_TEMPLATE, + TM_TEMPLATE_END }; keyword_item templatetags[] = { + { "preambledef", TM_PREAMBLEDEF }, + { "preambledefend", TM_PREAMBLEDEF_END }, { "template", TM_TEMPLATE }, - { "templateend", TM_END } + { "templateend", TM_TEMPLATE_END } }; - string filename = LibFileSearch("", "external_templates"); - if (filename.empty()) { - lyxerr << "ExternalTemplateManager::readTemplates: " - "No template file" << endl; - return; - } + LyXLex lex(templatetags, TM_TEMPLATE_END); - LyXLex lex(templatetags, TM_END); - if (!lex.setFile(filename)) { - lyxerr << "ExternalTemplateManager::readTemplates: " - "No template file" << endl; + string filename = support::LibFileSearch("", "external_templates"); + if (filename.empty() || !lex.setFile(filename)) { + lex.printError("ExternalTemplateManager::readTemplates: " + "No template file"); return; } + char const * const preamble_end_tag = + templatetags[TM_PREAMBLEDEF_END-1].tag; + while (lex.isOK()) { switch (lex.lex()) { + case TM_PREAMBLEDEF: { + lex.next(); + string const name = lex.getString(); + preambledefs[name] = lex.getLongString(preamble_end_tag); + } + break; + case TM_TEMPLATE: { lex.next(); - string const temp = lex.getString(); - ExternalTemplate & tmp = templates[temp]; - tmp.lyxName = temp; + string const name = lex.getString(); + ExternalTemplate & tmp = templates[name]; + tmp.lyxName = name; tmp.readTemplate(lex); } break; - case TM_END: + case TM_TEMPLATE_END: lex.printError("Warning: End outside Template."); break; + + case TM_PREAMBLEDEF_END: + lex.printError("Warning: End outside PreambleDef."); + break; } } } @@ -247,7 +304,7 @@ void ExternalTemplate::readTemplate(LyXLex & lex) default: lex.printError("ExternalTemplate::readTemplate: " "Wrong tag: $$Token"); - Assert(false); + support::Assert(false); break; } } @@ -299,12 +356,11 @@ void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex) break; case FO_PREAMBLE: - preamble = lex.getLongString("preambleend"); + lex.next(true); + preambleName = lex.getString(); break; case FO_END: - if (lyxerr.debugging()) - lex.printError("FormatEnd"); return; } } diff --git a/src/insets/ExternalTemplate.h b/src/insets/ExternalTemplate.h index 6617dcba00..a10de636a8 100644 --- a/src/insets/ExternalTemplate.h +++ b/src/insets/ExternalTemplate.h @@ -48,8 +48,8 @@ struct ExternalTemplate { string updateFormat; /// What features does this external inset require? string requirement; - /// What should be inserted into the preamble - string preamble; + /// Identify the preamble snippet using \c preambleName. + string preambleName; /// void readFormat(LyXLex &); /// This constructor has to default a command for safety reasons! @@ -76,17 +76,25 @@ class ExternalTemplateManager : boost::noncopyable { public: /// Map from the LyX name of the template to the template structure typedef std::map Templates; + /** Map from the LyX name of the preamble definition to the preamble + * definition itself. + */ + typedef std::map PreambleDefs; static ExternalTemplateManager & get(); Templates & getTemplates(); Templates const & getTemplates() const; /// return the template by LyX name ExternalTemplate const & getTemplateByName(string const & name); + string const getPreambleDefByName(string const & name) const; private: ExternalTemplateManager(); void readTemplates(string const & path); - void dumpTemplates() const; + void dumpTemplates(std::ostream &) const; + void dumpPreambleDefs(std::ostream &) const; + Templates templates; + PreambleDefs preambledefs; }; #endif diff --git a/src/insets/insetexternal.C b/src/insets/insetexternal.C index 11c0eaec2c..38d891e77f 100644 --- a/src/insets/insetexternal.C +++ b/src/insets/insetexternal.C @@ -367,12 +367,13 @@ void InsetExternal::read(Buffer const & buffer, LyXLex & lex) // Replace the inset's store setParams(params, buffer); - lyxerr[Debug::INFO] << "InsetExternal::Read: " - << "template: '" << params_.templatename - << "' filename: '" << params_.filename.absFilename() - << "' display: '" << params_.display - << "' scale: '" << params_.lyxscale - << '\'' << endl; + lyxerr[Debug::EXTERNAL] + << "InsetExternal::Read: " + << "template: '" << params_.templatename + << "' filename: '" << params_.filename.absFilename() + << "' display: '" << params_.display + << "' scale: '" << params_.lyxscale + << '\'' << endl; } @@ -388,9 +389,10 @@ int InsetExternal::write(string const & format, ExternalTemplate::Formats::const_iterator cit = et.formats.find(format); if (cit == et.formats.end()) { - lyxerr << "External template format '" << format - << "' not specified in template " - << params_.templatename << endl; + lyxerr[Debug::EXTERNAL] + << "External template format '" << format + << "' not specified in template " + << params_.templatename << endl; return 0; } @@ -454,18 +456,17 @@ void InsetExternal::validate(LaTeXFeatures & features) const return; ExternalTemplate const & et = *et_ptr; - ExternalTemplate::Formats::const_iterator cit = - et.formats.find("LaTeX"); - + ExternalTemplate::Formats::const_iterator cit = et.formats.find("LaTeX"); if (cit == et.formats.end()) return; - if (!cit->second.requirement.empty()) { + if (!cit->second.requirement.empty()) features.require(cit->second.requirement); - } - if (!cit->second.preamble.empty()) { - features.addExternalPreamble(cit->second.preamble); - } + + ExternalTemplateManager & etm = ExternalTemplateManager::get(); + string const preamble = etm.getPreambleDefByName(cit->second.preambleName); + if (!preamble.empty()) + features.addExternalPreamble(preamble); } @@ -511,9 +512,10 @@ void InsetExternal::updateExternal(string const & format, return; if (!converters.isReachable(from_format, to_format)) { - lyxerr << "InsetExternal::updateExternal. " - "Unable to convert from " - << from_format << " to " << to_format << endl; + lyxerr[Debug::EXTERNAL] + << "InsetExternal::updateExternal. " + << "Unable to convert from " + << from_format << " to " << to_format << endl; return; } @@ -605,7 +607,7 @@ void editExternal(InsetExternal::Params const & params, Buffer const & buffer) support::Path p(buffer.filePath()); support::Forkedcall call; - if (lyxerr.debugging()) { + if (lyxerr.debugging(Debug::EXTERNAL)) { lyxerr << "Executing '" << command << "' in '" << buffer.filePath() << '\'' << endl; } -- 2.39.2