From: Bo Peng Date: Fri, 25 May 2007 16:15:02 +0000 (+0000) Subject: Choose a delimiter for lstinline (braces can not be used for some listings version) X-Git-Tag: 1.6.10~9624 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=093b9452a42f09b1154d001f94626f33f33d8bab;p=lyx.git Choose a delimiter for lstinline (braces can not be used for some listings version) git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18513 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/insets/InsetListings.cpp b/src/insets/InsetListings.cpp index 526df7954b..6b76f41d58 100644 --- a/src/insets/InsetListings.cpp +++ b/src/insets/InsetListings.cpp @@ -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;