]> git.lyx.org Git - features.git/commitdiff
The remaining External Template clean-up patch ;-)
authorAngus Leeming <leeming@lyx.org>
Thu, 4 Sep 2003 17:01:00 +0000 (17:01 +0000)
committerAngus Leeming <leeming@lyx.org>
Thu, 4 Sep 2003 17:01:00 +0000 (17:01 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@7666 a592a061-630c-0410-9148-cb99ea01b6c8

lib/ChangeLog
lib/external_templates
src/ChangeLog
src/LaTeXFeatures.C
src/box.C
src/debug.C
src/debug.h
src/insets/ChangeLog
src/insets/ExternalTemplate.C
src/insets/ExternalTemplate.h
src/insets/insetexternal.C

index f046ccbaa42b2d77231da981e0765d7577ef15cd..bfb9361af15974ef964eb5590c3f55fedda420cd 100644 (file)
@@ -1,3 +1,8 @@
+2003-09-04  Angus Leeming  <leeming@lyx.org>
+
+       * external_templates: add a new PreambleDef section and use it in the
+       XFig template.
+
 2003-09-04  Johnathan Burchill  <jkerrb@shaw.ca>
 
        * external_templates: add some clever LaTeX to the XFIG template so that
index 32fdc95e6c8b8f474829ebd943c1b4739d3d76d0..639c5b35334e7ffe39a110aa441605f7c277230b 100644 (file)
@@ -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\")"
index d09477a0212f255903644f8cfc4c4e17a001a547..9eaffcc2b25dd11ceea10a17121266b3063b53f8 100644 (file)
@@ -1,3 +1,10 @@
+2003-09-04  Angus Leeming  <leeming@lyx.org>
+
+       * 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  <larsbj@lyx.org>
 
        * text2.C (cursorEnd): simplify
index 6984882c5eaa2cd7575f43f3ca3f2ecf8b98e5f6..9f6e599206e7670344635a1f209a175fb965a590 100644 (file)
@@ -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';
 
index 4ee0b2afb036229096309624d7a3cf6e3416b252..af520569dcc7d47d9988a99eb85e9ea55a78077a 100644 (file)
--- a/src/box.C
+++ b/src/box.C
 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)
 {
index 9fe1f2a9036c04008259ca215618d19a9fd3fe1c..478fe99a7ba08887abb983d8d47ad4b83d211405 100644 (file)
@@ -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)
index 6b8a7f594027107aeee89096524e250ccaa8de31..7d8ee4b8b2bf0a83bdba142293ba1e643e1198c7 100644 (file)
@@ -71,7 +71,9 @@ struct Debug {
                ///
                GRAPHICS   = (1 << 21),
                /// change tracking
-               CHANGES    = (1 << 22)
+               CHANGES    = (1 << 22),
+               ///
+               EXTERNAL   = (1 << 23)
        };
        ///
        static type const ANY;
index df8d6fafe21901257ea8f819a06161ff56d576c5..f22b964bc29b8c1f7641648fb971246ced557fca 100644 (file)
        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  <leeming@lyx.org>
+
+       * 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  <leeming@lyx.org>
 
        * insetgraphics.C: #include "support/os.h"
index c413190929722ce03f8a3cdbcd0789e0808009c3..959706e9944b41953fb8ec7e04e2b7a96857e6fa 100644 (file)
 #include "support/path.h"
 #include "support/LAssert.h"
 #include "support/filetools.h"
+#include "support/lstrings.h"
 #include "support/path_defines.h"
 
 #include <algorithm>
 
-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;
                }
        }
index 6617dcba003debf67f4ddce5372c85ab16fef54b..a10de636a83f98c02ab638fc21940fe045cdf8d9 100644 (file)
@@ -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<string, ExternalTemplate> Templates;
+       /** Map from the LyX name of the preamble definition to the preamble
+        *  definition itself.
+        */
+       typedef std::map<string, string> 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
index 11c0eaec2ca1ce2acb34cd8f6b0d79cd34b3fc27..38d891e77f74a11ea6b0fa500817b28c1737edb5 100644 (file)
@@ -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;
        }