On 03/05/2012 05:26 PM, Uwe Stöhr wrote:
Richard, can the attached patch go to branch?

Can someone else comment on this? I don't know tex2lyx at all.

Richard


====

Index: Parser.cpp
===================================================================
--- Parser.cpp  (revision 40849)
+++ Parser.cpp  (working copy)
@@ -481,6 +481,49 @@
 }


+string const Parser::plainEnvironment(string const&  name)
+{
+       if (!good())
+               return string();
+
+       ostringstream os;
+       for (Token t = get_token(); good(); t = get_token()) {
+               if (t.asInput() == "\\end") {
+                       string const end = getArg('{', '}');
+                       if (end == name)
+                               return os.str();
+                       else
+                               os<<  "\\end{"<<  end<<  '}';
+               } else
+                       os<<  t.asInput();
+       }
+       cerr<<  "unexpected end of input"<<  endl;
+       return os.str();
+}
+
+
+string const Parser::plainCommand(char left, char right, string const&  name)
+{
+       if (!good())
+               return string();
+       // ceck if first token is really the start character
+       Token tok = get_token();
+       if (tok.character() != left) {
+               cerr<<  "first character does not match start character of command 
\\"<<  name<<  endl;
+               return string();
+       }
+       ostringstream os;
+       for (Token t = get_token(); good(); t = get_token()) {
+               if (t.character() == right) {
+                       return os.str();
+               } else
+                       os<<  t.asInput();
+       }
+       cerr<<  "unexpected end of input"<<  endl;
+       return os.str();
+}
+
+
 void Parser::tokenize_one()
 {
        catInit();
Index: Parser.h
===================================================================
--- Parser.h    (revision 40849)
+++ Parser.h    (working copy)
@@ -196,6 +196,19 @@
         * is parsed but not returned.
         */
        std::string const verbatimEnvironment(std::string const&  name);
+       /*
+       * The same as verbatimEnvironment(std::string const&  name) but
+       * \begin and \end commands inside the name environment are not parsed.
+       * This function is designed to parse verbatim environments.
+       */
+       std::string const plainEnvironment(std::string const&  name);
+       /*
+       * Basically the same as plainEnvironment(std::string const&  name) but
+       * instead of \begin and \end commands the parsing is started/stopped
+       * at given characters.
+       * This function is designed to parse verbatim commands.
+       */
+       std::string const plainCommand(char left, char right, std::string 
const&  name);
        /*!
         * Returns the character of the current token and increments
         * the token position.
Index: test/test-insets.tex
===================================================================
--- test/test-insets.tex        (revision 40849)
+++ test/test-insets.tex        (working copy)
@@ -156,6 +156,42 @@
 \listoftables


+\section{Listings}
+
+Inline: \lstinline[language={C++}]!int a=5;!\\
+Float:
+
+\begin{lstlisting}[caption={Example Listing 
float},label={lst:Example-Listing},language=Python]
+# Example listing float
+def func(param):
+    'this is a python function'
+    pass
+\end{lstlisting}
+
+Here is an example listing with left line numbering, step ``3'',
+language ``Python'', options ``Extended character table'' and
+``Space~as~symbol'', range lines 3\,-\,8:
+
+\begin{lstlisting}[extendedchars=true,firstline=3,language=Python,lastline=8,numbers=left,showspaces=true,stepnumber=3]
+def func(param):
+    'this is a python function'
+    pass
+def func(param):
+'This is a German word: Tschüß'
+pass
+def func(param):
+'this is a python function'
+pass
+\end{lstlisting}
+
+Special cases:
+\begin{lstlisting}[abovecaptionskip=2em,basicstyle={\large\ttfamily},breaklines=true,extendedchars=true,firstline=2,float=h,language={[R/3
 
3.1]ABAP},lastline=5,numbers=left,numberstyle={\scriptsize},showspaces=true,showstringspaces=false,stepnumber=3,tabsize=4]
+hello
+\end{lstlisting}
+
+\lstinline[language=TeX]!\begin{centering} hello!
+
+
 \section{Graphics\index{Graphics}}

 There is also some basic support for graphics, in the form
Index: text.cpp
===================================================================
--- text.cpp    (revision 40851)
+++ text.cpp    (working copy)
@@ -1110,16 +1110,35 @@
 }


-void parse_listings(Parser&  p, ostream&  os, Context&  parent_context)
+void parse_listings(Parser&  p, ostream&  os, Context&  parent_context, bool 
in_line)
 {
        parent_context.check_layout(os);
        begin_inset(os, "listings\n");
-       os<<  "inline false\n"
-       <<  "status collapsed\n";
+       if (p.hasOpt()) {
+               // there can be a [] pair inside the argument for the language
+               string arg = p.getArg('[', ']');
+               if (arg.find("language={[") != string::npos) {
+                       char start = p.next_token().character();
+                       arg += ']';
+                       arg += start;
+                       arg += p.getArg(start, ']');
+               }
+               os<<  "lstparams "<<  '"'<<  arg<<  '"'<<  '\n';
+       }
+       if (in_line)
+               os<<  "inline true\n";
+       else
+               os<<  "inline false\n";
+       os<<  "status collapsed\n";
        Context context(true, parent_context.textclass);
        context.layout =&parent_context.textclass.plainLayout();
-       context.check_layout(os);
-       string const s = p.verbatimEnvironment("lstlisting");
+       string s;
+       if (in_line) {
+               s = p.plainCommand('!', '!', "lstinline");
+               context.new_paragraph(os);
+               context.check_layout(os);
+       } else
+               s = p.plainEnvironment("lstlisting");
        for (string::const_iterator it = s.begin(), et = s.end(); it != et; 
++it) {
                if (*it == '\\')
                        os<<  "\n\\backslash\n";
@@ -1352,14 +1371,9 @@

        else if (name == "lstlisting") {
                eat_whitespace(p, os, parent_context, false);
-               // FIXME handle listings with parameters
-               //       If this is added, don't forgot to handle the
-               //       automatic color package loading
-               if (p.hasOpt())
-                       parse_unknown_environment(p, name, os, FLAG_END,
-                                                 outer, parent_context);
-               else
-                       parse_listings(p, os, parent_context);
+               // FIXME handle the automatic color package loading
+               // uwestoehr asks: In what case color is loaded?
+               parse_listings(p, os, parent_context, false);
                p.skip_spaces();
        }

@@ -2731,6 +2745,11 @@
                        end_inset(os);
                }

+               else if (t.cs() == "lstinline") {
+                       p.skip_spaces();
+                       parse_listings(p, os, context, true);
+               }
+
                else if (t.cs() == "ensuremath") {
                        p.skip_spaces();
                        context.check_layout(os);



Reply via email to