]> git.lyx.org Git - lyx.git/commitdiff
Choose a delimiter for lstinline (braces can not be used for some listings version)
authorBo Peng <bpeng@lyx.org>
Fri, 25 May 2007 16:15:02 +0000 (16:15 +0000)
committerBo Peng <bpeng@lyx.org>
Fri, 25 May 2007 16:15:02 +0000 (16:15 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18513 a592a061-630c-0410-9148-cb99ea01b6c8

src/insets/InsetListings.cpp

index 526df7954bcdd0220b35bb39de9164086f4ccbb3..6b76f41d58ad881467c0c9c7153bd71c1cb0c7d2 100644 (file)
@@ -28,6 +28,8 @@
 namespace lyx {
 
 using support::token;
+using support::contains;
+using support::subst;
 
 using std::auto_ptr;
 using std::istringstream;
@@ -35,6 +37,7 @@ using std::ostream;
 using std::ostringstream;
 using std::string;
 
+char const lstinline_delimiters[] = "!@#$^&*()-_=+|;:'\"~`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
 
 void InsetListings::init()
 {
@@ -133,26 +136,9 @@ int InsetListings::latex(Buffer const & buf, odocstream & os,
        // of the listings package (see page 25 of the manual)
        int lines = 0;
        bool lstinline = params().isInline();
-       if (lstinline) {
-               if (param_string.empty())
-                       os << "\\lstinline{";
-               else
-                       os << "\\lstinline[" << from_ascii(param_string) << "]{";
-       } else {
-               docstring const caption = getCaption(buf, runparams);
-               if (param_string.empty() && caption.empty())
-                       os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
-               else {
-                       os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
-                       if (!caption.empty()) {
-                               os << "caption={" << caption << '}';
-                               if (!param_string.empty())
-                                       os << ',';
-                       }
-                       os << from_utf8(param_string) << "]\n";
-               }
-               lines += 4;
-       }
+       // get the paragraphs. We can not output them directly to given odocstream
+       // because we can not yet determine the delimiter character of \lstinline
+       docstring code;
        ParagraphList::const_iterator par = paragraphs().begin();
        ParagraphList::const_iterator end = paragraphs().end();
 
@@ -165,18 +151,49 @@ int InsetListings::latex(Buffer const & buf, odocstream & os,
                        // ignore all struck out text and (caption) insets
                        if (par->isDeleted(i) || par->isInset(i))
                                continue;
-                       os.put(par->getChar(i));
+                       code += par->getChar(i);
                }
                ++par;
                // for the inline case, if there are multiple paragraphs
                // they are simply joined. Otherwise, expect latex errors.
                if (par != end && !lstinline && !captionline) {
-                       os << "\n";
+                       code += "\n";
                        ++lines;
                }
        }
+       char const * delimiter;
+       if (lstinline) {
+               for (delimiter = lstinline_delimiters; delimiter != '\0'; ++delimiter)
+                       if (!contains(code, *delimiter))
+                               break;
+               // this code piece contains all possible special character? !!!
+               // Replace ! with a warning message and use ! as delimiter.
+               if (*delimiter == '\0') {
+                       code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
+                       delimiter = lstinline_delimiters;
+               }
+               if (param_string.empty())
+                       os << "\\lstinline" << *delimiter;
+               else
+                       os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
+       } else {
+               docstring const caption = getCaption(buf, runparams);
+               if (param_string.empty() && caption.empty())
+                       os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
+               else {
+                       os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
+                       if (!caption.empty()) {
+                               os << "caption={" << caption << '}';
+                               if (!param_string.empty())
+                                       os << ',';
+                       }
+                       os << from_utf8(param_string) << "]\n";
+               }
+               lines += 4;
+       }
+       os << code;
        if (lstinline)
-               os << "}";              
+               os << *delimiter;               
        else {
                os << "\n\\end{lstlisting}\n\\endgroup\n";
                lines += 3;