This patch makes tex2lyx recognize the "reLyXre" list of environments. It
adds furthermore two new lists in syntax.default: "environments" and
"mathenvironments". The first one is like "reLyXre" but more powerful (you
cannot specify arguments in "reLyXre"), and the second one contains the
environments that start a math formula. This gives us limited support for
math environments that LyX does not know of, such as empheq.
This is tested and works, and I am going to apply it if nobody objects.
Greetings to Paris,
Georg
Index: lib/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/ChangeLog,v
retrieving revision 1.715
diff -u -p -r1.715 ChangeLog
--- lib/ChangeLog 12 Jul 2005 10:33:43 -0000 1.715
+++ lib/ChangeLog 14 Jul 2005 09:39:21 -0000
@@ -1,3 +1,8 @@
+2005-07-13 Georg Baum <[EMAIL PROTECTED]>
+
+ * reLyX/syntax.default: new "environments" environments and
+ mathenvironments for tex2lyx
+
2005-07-12 Jean-Marc Lasgouttes <[EMAIL PROTECTED]>
* layouts/scrclass.inc: fix bad comment.
Index: lib/reLyX/syntax.default
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/reLyX/syntax.default,v
retrieving revision 1.11
diff -u -p -r1.11 syntax.default
--- lib/reLyX/syntax.default 30 Jun 2005 07:12:25 -0000 1.11
+++ lib/reLyX/syntax.default 14 Jul 2005 09:39:22 -0000
@@ -676,3 +676,41 @@ titlepage
\sp {^}
\ensuremath {} % If it's in math mode, \ensuremath is unnec.
\end{reLyXmt}
+
+
+% LaTeX environments.
+% They have always one extra "argument":
+% It contains "translate" if the contents of the environment contains normal
+% LaTeX code that can be translated to LyX.
+\begin{environments}
+bibunit[]{translate}
+psmatrix[]{}
+\end{environments}
+
+% Environments that start math mode.
+% $...$, $$...$$, \(...\) and \[...\] are hardcoded in tex2lyx.
+% The arguments are currently ignored.
+\begin{mathenvironments}
+equation
+equation*
+eqnarray
+eqnarray*
+align
+align*
+gather
+gather*
+multline
+multline*
+math
+displaymath
+flalign
+flalign
+% These require extra args
+alignat
+alignat*
+xalignat
+xalignat*
+xxalignat
+% These are not known by LyX but work nevertheless:
+empheq
+\end{mathenvironments}
Index: src/tex2lyx/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/ChangeLog,v
retrieving revision 1.97
diff -u -p -r1.97 ChangeLog
--- src/tex2lyx/ChangeLog 13 Jul 2005 11:38:55 -0000 1.97
+++ src/tex2lyx/ChangeLog 14 Jul 2005 09:39:25 -0000
@@ -1,3 +1,12 @@
+2005-07-13 Georg Baum <[EMAIL PROTECTED]>
+
+ * math.C (is_math_env): Don't hardcode known math environments anymore
+ * tex2lyx.[Ch] (known_environments, known_math_environments): new
+ * tex2lyx.C (read_command): new, split off from read_syntaxfile
+ * tex2lyx.C (read_environment): new
+ * text.C (parse_arguments): new, split off from parse_command
+ * text.C (parse_environment): handle known environments
+
2005-07-12 Georg Baum <[EMAIL PROTECTED]>
* text.C (parse_text): output font changes only if needed
Index: src/tex2lyx/math.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/math.C,v
retrieving revision 1.15
diff -u -p -r1.15 math.C
--- src/tex2lyx/math.C 23 Jul 2004 18:47:57 -0000 1.15
+++ src/tex2lyx/math.C 14 Jul 2005 09:39:25 -0000
@@ -25,18 +25,7 @@ using std::string;
bool is_math_env(string const & name)
{
- static char const * const known_math_envs[] = { "equation",
- "equation*", "eqnarray", "eqnarray*", "align", "align*", "gather",
- "gather*", "multline", "multline*", "math", "displaymath", "flalign",
- "flalign*",
- // These require extra args
- "alignat", "alignat*", "xalignat", "xalignat*", "xxalignat",
- 0};
-
- for (char const * const * what = known_math_envs; *what; ++what)
- if (*what == name)
- return true;
- return false;
+ return known_math_environments.find(name) != known_math_environments.end();
}
Index: src/tex2lyx/tex2lyx.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/tex2lyx.C,v
retrieving revision 1.70
diff -u -p -r1.70 tex2lyx.C
--- src/tex2lyx/tex2lyx.C 1 Jun 2005 14:01:18 -0000 1.70
+++ src/tex2lyx/tex2lyx.C 14 Jul 2005 09:39:25 -0000
@@ -127,7 +127,9 @@ string active_environment()
}
-map<string, vector<ArgumentType> > known_commands;
+CommandMap known_commands;
+CommandMap known_environments;
+CommandMap known_math_environments;
void add_known_command(string const & command, string const & o1,
@@ -162,6 +164,57 @@ namespace {
/*!
+ * Read one command definition from the syntax file
+ */
+void read_command(Parser & p, string command, CommandMap & commands) {
+ if (p.next_token().asInput() == "*") {
+ p.get_token();
+ command += '*';
+ }
+ vector<ArgumentType> arguments;
+ while (p.next_token().cat() == catBegin ||
+ p.next_token().asInput() == "[") {
+ if (p.next_token().cat() == catBegin) {
+ string const arg = p.getArg('{', '}');
+ if (arg == "translate")
+ arguments.push_back(required);
+ else
+ arguments.push_back(verbatim);
+ } else {
+ p.getArg('[', ']');
+ arguments.push_back(optional);
+ }
+ }
+ commands[command] = arguments;
+}
+
+
+/*!
+ * Read a class of environments from the syntax file
+ */
+void read_environment(Parser & p, string const & begin,
+ CommandMap & environments)
+{
+ string environment;
+ while (p.good()) {
+ Token const & t = p.get_token();
+ if (t.cat() == catLetter)
+ environment += t.asInput();
+ else if (!environment.empty()) {
+ p.putback();
+ read_command(p, environment, environments);
+ environment.erase();
+ }
+ if (t.cat() == catEscape && t.asInput() == "\\end") {
+ string const end = p.getArg('{', '}');
+ if (end == begin)
+ return;
+ }
+ }
+}
+
+
+/*!
* Read a list of TeX commands from a reLyX compatible syntax file.
* Since this list is used after all commands that have a LyX counterpart
* are handled, it does not matter that the "syntax.default" file from reLyX
@@ -184,27 +237,20 @@ void read_syntaxfile(string const & file
while (p.good()) {
Token const & t = p.get_token();
if (t.cat() == catEscape) {
- string command = t.asInput();
- if (p.next_token().asInput() == "*") {
- p.get_token();
- command += '*';
- }
- p.skip_spaces();
- vector<ArgumentType> arguments;
- while (p.next_token().cat() == catBegin ||
- p.next_token().asInput() == "[") {
- if (p.next_token().cat() == catBegin) {
- string const arg = p.getArg('{', '}');
- if (arg == "translate")
- arguments.push_back(required);
- else
- arguments.push_back(verbatim);
- } else {
- p.getArg('[', ']');
- arguments.push_back(optional);
- }
+ string const command = t.asInput();
+ if (command == "\\begin") {
+ string const name = p.getArg('{', '}');
+ if (name == "environments" || name == "reLyXre")
+ // We understand "reLyXre", but it is
+ // not as powerful as "environments".
+ read_environment(p, name,
+ known_environments);
+ else if (name == "mathenvironments")
+ read_environment(p, name,
+ known_math_environments);
+ } else {
+ read_command(p, command, known_commands);
}
- known_commands[command] = arguments;
}
}
}
Index: src/tex2lyx/tex2lyx.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/tex2lyx.h,v
retrieving revision 1.20
diff -u -p -r1.20 tex2lyx.h
--- src/tex2lyx/tex2lyx.h 25 May 2005 16:01:19 -0000 1.20
+++ src/tex2lyx/tex2lyx.h 14 Jul 2005 09:39:25 -0000
@@ -81,9 +81,14 @@ enum ArgumentType {
optional
};
-/// Known TeX commands with arguments that get parsed into ERT.
-extern std::map<std::string, std::vector<ArgumentType> > known_commands;
+typedef std::map<std::string, std::vector<ArgumentType> > CommandMap;
+/// Known TeX commands with arguments that get parsed into ERT.
+extern CommandMap known_commands;
+/// Known TeX environments with arguments that get parsed into ERT.
+extern CommandMap known_environments;
+/// Known TeX math environments with arguments that get parsed into LyX mathed.
+extern CommandMap known_math_environments;
/// path of the master .tex file
extern std::string getMasterFilePath();
Index: src/tex2lyx/text.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/tex2lyx/text.C,v
retrieving revision 1.60
diff -u -p -r1.60 text.C
--- src/tex2lyx/text.C 13 Jul 2005 11:38:55 -0000 1.60
+++ src/tex2lyx/text.C 14 Jul 2005 09:39:26 -0000
@@ -507,6 +507,36 @@ void check_space(Parser const & p, ostre
/*!
+ * Parse all arguments of \p command
+ */
+void parse_arguments(string const & command,
+ vector<ArgumentType> const & template_arguments,
+ Parser & p, ostream & os, bool outer, Context & context)
+{
+ string ert = command;
+ size_t no_arguments = template_arguments.size();
+ for (size_t i = 0; i < no_arguments; ++i) {
+ switch (template_arguments[i]) {
+ case required:
+ // This argument contains regular LaTeX
+ handle_ert(os, ert + '{', context);
+ parse_text(p, os, FLAG_ITEM, outer, context);
+ ert = "}";
+ break;
+ case verbatim:
+ // This argument may contain special characters
+ ert += '{' + p.verbatim_item() + '}';
+ break;
+ case optional:
+ ert += p.getOpt();
+ break;
+ }
+ }
+ handle_ert(os, ert, context);
+}
+
+
+/*!
* Check whether \p command is a known command. If yes,
* handle the command with all arguments.
* \return true if the command was parsed, false otherwise.
@@ -515,27 +545,8 @@ bool parse_command(string const & comman
bool outer, Context & context)
{
if (known_commands.find(command) != known_commands.end()) {
- vector<ArgumentType> const & template_arguments = known_commands[command];
- string ert = command;
- size_t no_arguments = template_arguments.size();
- for (size_t i = 0; i < no_arguments; ++i) {
- switch (template_arguments[i]) {
- case required:
- // This argument contains regular LaTeX
- handle_ert(os, ert + '{', context);
- parse_text(p, os, FLAG_ITEM, outer, context);
- ert = "}";
- break;
- case verbatim:
- // This argument may contain special characters
- ert += '{' + p.verbatim_item() + '}';
- break;
- case optional:
- ert += p.getOpt();
- break;
- }
- }
- handle_ert(os, ert, context);
+ parse_arguments(command, known_commands[command], p, os,
+ outer, context);
return true;
}
return false;
@@ -779,6 +790,27 @@ void parse_environment(Parser & p, ostre
handle_ert(os, "\\begin{" + name + "}", parent_context);
parse_text_snippet(p, os, FLAG_END | FLAG_TABBING, outer, parent_context);
handle_ert(os, "\\end{" + name + "}", parent_context);
+ }
+
+ else if (known_environments.find(name) != known_environments.end()) {
+ vector<ArgumentType> arguments = known_environments[name];
+ // The last "argument" denotes wether we may translate the
+ // environment contents to LyX
+ // The default required if no argument is given makes us
+ // compatible with the reLyXre environment.
+ ArgumentType contents = arguments.empty() ?
+ required :
+ arguments.back();
+ if (!arguments.empty())
+ arguments.pop_back();
+ parse_arguments("\\begin{" + name + "}", arguments, p, os,
+ outer, parent_context);
+ if (contents == verbatim)
+ handle_ert(os, p.verbatimEnvironment(name),
+ parent_context);
+ else
+ parse_text_snippet(p, os, FLAG_END, outer,
+ parent_context);
}
else {