2 * \file InsetListings.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "InsetListings.h"
17 #include "BufferView.h"
18 #include "BufferParams.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "InsetCaption.h"
25 #include "InsetList.h"
27 #include "MetricsInfo.h"
28 #include "TextClass.h"
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
35 #include "frontends/Application.h"
37 #include <boost/regex.hpp>
42 using namespace lyx::support;
48 char const lstinline_delimiters[] =
49 "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
51 InsetListings::InsetListings(Buffer const & buf, InsetListingsParams const & par)
52 : InsetCollapsable(buf, par.status())
56 InsetListings::~InsetListings()
58 hideDialogs("listings", this);
62 Inset::DisplayType InsetListings::display() const
64 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
68 void InsetListings::updateLabels(ParIterator const & it)
70 Counters & cnts = buffer().params().documentClass().counters();
71 string const saveflt = cnts.current_float();
73 // Tell to captions what the current float is
74 cnts.current_float("listing");
76 InsetCollapsable::updateLabels(it);
79 cnts.current_float(saveflt);
83 void InsetListings::write(ostream & os) const
85 os << "listings" << "\n";
86 InsetListingsParams const & par = params();
87 // parameter string is encoded to be a valid lyx token.
88 string opt = par.encodedString();
90 os << "lstparams \"" << opt << "\"\n";
92 os << "inline true\n";
94 os << "inline false\n";
95 InsetCollapsable::write(os);
99 void InsetListings::read(Lexer & lex)
103 string token = lex.getString();
104 if (token == "lstparams") {
106 string const value = lex.getString();
107 params().fromEncodedString(value);
108 } else if (token == "inline") {
110 params().setInline(lex.getBool());
112 // no special option, push back 'status' etc
113 lex.pushToken(token);
117 InsetCollapsable::read(lex);
121 docstring InsetListings::editMessage() const
123 return _("Opened Listing Inset");
127 int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
129 string param_string = params().params();
130 // NOTE: I use {} to quote text, which is an experimental feature
131 // of the listings package (see page 25 of the manual)
133 bool isInline = params().isInline();
134 // get the paragraphs. We can not output them directly to given odocstream
135 // because we can not yet determine the delimiter character of \lstinline
137 ParagraphList::const_iterator par = paragraphs().begin();
138 ParagraphList::const_iterator end = paragraphs().end();
141 pos_type siz = par->size();
142 bool captionline = false;
143 for (pos_type i = 0; i < siz; ++i) {
144 if (i == 0 && par->isInset(i) && i + 1 == siz)
146 // ignore all struck out text and (caption) insets
147 if (par->isDeleted(i) || par->isInset(i))
149 code += par->getChar(i);
152 // for the inline case, if there are multiple paragraphs
153 // they are simply joined. Otherwise, expect latex errors.
154 if (par != end && !isInline && !captionline) {
160 char const * delimiter = lstinline_delimiters;
161 for (; delimiter != '\0'; ++delimiter)
162 if (!contains(code, *delimiter))
164 // This code piece contains all possible special character? !!!
165 // Replace ! with a warning message and use ! as delimiter.
166 if (*delimiter == '\0') {
167 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
168 delimiter = lstinline_delimiters;
170 if (param_string.empty())
171 os << "\\lstinline" << *delimiter;
173 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
177 OutputParams rp = runparams;
178 // FIXME: the line below would fix bug 4182,
179 // but real_current_font moved to cursor.
180 //rp.local_font = &text_.real_current_font;
181 rp.moving_arg = true;
182 docstring const caption = getCaption(rp);
183 runparams.encoding = rp.encoding;
184 if (param_string.empty() && caption.empty())
185 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
187 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
188 if (!caption.empty()) {
189 os << "caption={" << caption << '}';
190 if (!param_string.empty())
193 os << from_utf8(param_string) << "]\n";
196 os << code << "\n\\end{lstlisting}\n\\endgroup\n";
204 docstring InsetListings::contextMenu(BufferView const &, int, int) const
206 return from_ascii("context-listings");
210 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
212 switch (cmd.action) {
214 case LFUN_INSET_MODIFY: {
215 InsetListings::string2params(to_utf8(cmd.argument()), params());
218 case LFUN_INSET_DIALOG_UPDATE:
219 cur.bv().updateDialog("listings", params2string(params()));
222 InsetCollapsable::doDispatch(cur, cmd);
228 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
229 FuncStatus & status) const
231 LYXERR0("CURSOR SIZE: " << cur.depth());
232 switch (cmd.action) {
233 case LFUN_INSET_DIALOG_UPDATE:
234 status.enabled(true);
236 case LFUN_CAPTION_INSERT:
237 status.enabled(!params().isInline());
240 return InsetCollapsable::getStatus(cur, cmd, status);
245 void InsetListings::setButtonLabel()
248 if (decoration() == InsetLayout::Classic)
249 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
251 setLabel(getNewLabel(_("Listing")));
255 void InsetListings::validate(LaTeXFeatures & features) const
257 features.require("listings");
258 InsetCollapsable::validate(features);
262 bool InsetListings::showInsetDialog(BufferView * bv) const
264 bv->showDialog("listings", params2string(params()),
265 const_cast<InsetListings *>(this));
270 docstring InsetListings::getCaption(OutputParams const & runparams) const
272 if (paragraphs().empty())
275 ParagraphList::const_iterator pit = paragraphs().begin();
276 for (; pit != paragraphs().end(); ++pit) {
277 InsetList::const_iterator it = pit->insetList().begin();
278 for (; it != pit->insetList().end(); ++it) {
279 Inset & inset = *it->inset;
280 if (inset.lyxCode() == CAPTION_CODE) {
281 odocstringstream ods;
283 static_cast<InsetCaption *>(it->inset);
284 ins->getOptArg(ods, runparams);
285 ins->getArgument(ods, runparams);
286 // the caption may contain \label{} but the listings
287 // package prefer caption={}, label={}
288 docstring cap = ods.str();
289 if (!contains(to_utf8(cap), "\\label{"))
292 // blah1\label{blah2} blah3
294 // blah1 blah3},label={blah2
296 // caption={blah1 blah3},label={blah2}
298 // NOTE that } is not allowed in blah2.
299 regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
300 string const new_cap("\\1\\3},label={\\2");
301 return from_utf8(regex_replace(to_utf8(cap), reg, new_cap));
309 void InsetListings::string2params(string const & in,
310 InsetListingsParams & params)
312 params = InsetListingsParams();
315 istringstream data(in);
318 // discard "listings", which is only used to determine inset
324 string InsetListings::params2string(InsetListingsParams const & params)
327 data << "listings" << ' ';