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"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetCaption.h"
27 #include "LaTeXFeatures.h"
29 #include "output_latex.h"
30 #include "output_xhtml.h"
31 #include "OutputParams.h"
32 #include "TextClass.h"
34 #include "texstream.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40 #include "support/lassert.h"
42 #include "frontends/alert.h"
43 #include "frontends/Application.h"
45 #include "support/regex.h"
50 using namespace lyx::support;
55 InsetListings::InsetListings(Buffer * buf, InsetListingsParams const & par)
56 : InsetCaptionable(buf,"listing")
58 params_.setMinted(buffer().params().use_minted);
59 status_ = par.status();
63 InsetListings::~InsetListings()
65 hideDialogs("listings", this);
69 Inset::DisplayType InsetListings::display() const
71 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
75 docstring InsetListings::layoutName() const
77 if (buffer().params().use_minted)
78 return from_ascii("MintedListings");
80 return from_ascii("Listings");
84 void InsetListings::write(ostream & os) const
86 os << "listings" << "\n";
87 InsetListingsParams const & par = params();
88 // parameter string is encoded to be a valid lyx token.
89 string opt = par.encodedString();
91 os << "lstparams \"" << opt << "\"\n";
93 os << "inline true\n";
95 os << "inline false\n";
96 InsetCaptionable::write(os);
100 void InsetListings::read(Lexer & lex)
104 string token = lex.getString();
105 if (token == "lstparams") {
107 string const value = lex.getString();
108 params().fromEncodedString(value);
109 } else if (token == "inline") {
111 params().setInline(lex.getBool());
113 // no special option, push back 'status' etc
114 lex.pushToken(token);
118 InsetCaptionable::read(lex);
122 void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
124 string param_string = params().params();
125 // NOTE: I use {} to quote text, which is an experimental feature
126 // of the listings package (see page 25 of the manual)
127 bool const isInline = params().isInline();
128 bool const use_minted = buffer().params().use_minted;
129 string minted_language;
130 string float_placement;
131 bool const isfloat = params().isFloat();
132 if (use_minted && (isfloat || contains(param_string, "language="))) {
133 // Get float placement and/or language of the code,
134 // then remove the relative options.
135 vector<string> opts =
136 getVectorFromString(param_string, ",", false);
137 for (size_t i = 0; i < opts.size(); ++i) {
138 if (prefixIs(opts[i], "float")) {
139 if (prefixIs(opts[i], "float="))
140 float_placement = opts[i].substr(6);
141 opts.erase(opts.begin() + i--);
143 else if (prefixIs(opts[i], "language=")) {
144 minted_language = opts[i].substr(9);
145 opts.erase(opts.begin() + i--);
148 param_string = getStringFromVector(opts, ",");
150 // Minted needs a language specification
151 if (minted_language.empty())
152 minted_language = "TeX";
154 // get the paragraphs. We can not output them directly to given odocstream
155 // because we can not yet determine the delimiter character of \lstinline
158 ParagraphList::const_iterator par = paragraphs().begin();
159 ParagraphList::const_iterator end = paragraphs().end();
161 bool encoding_switched = false;
162 Encoding const * const save_enc = runparams.encoding;
163 // The listings package cannot deal with multi-byte-encoded
164 // glyphs, except if full-unicode aware backends
165 // such as XeTeX or LuaTeX are used, and with pLaTeX.
166 bool const multibyte_possible = use_minted || runparams.isFullUnicode()
167 || (buffer().params().encoding().package() == Encoding::japanese
168 && runparams.encoding->package() == Encoding::japanese);
170 if (!multibyte_possible && !runparams.encoding->hasFixedWidth()) {
171 // We need to switch to a singlebyte encoding, due to
172 // the restrictions of the listings package (see above).
173 // This needs to be consistent with
174 // LaTeXFeatures::getTClassI18nPreamble().
175 Language const * const outer_language =
176 (runparams.local_font != 0) ?
177 runparams.local_font->language()
178 : buffer().params().language;
179 // We try if there's a singlebyte encoding for the current
180 // language; if not, fall back to latin1.
181 Encoding const * const lstenc =
182 (outer_language->encoding()->hasFixedWidth()) ?
183 outer_language->encoding()
184 : encodings.fromLyXName("iso8859-1");
185 switchEncoding(os.os(), buffer().params(), runparams, *lstenc, true);
186 runparams.encoding = lstenc;
187 encoding_switched = true;
191 pos_type siz = par->size();
192 bool captionline = false;
193 for (pos_type i = 0; i < siz; ++i) {
194 if (i == 0 && par->isInset(i) && i + 1 == siz)
196 // ignore all struck out text and (caption) insets
197 if (par->isDeleted(i)
198 || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
200 if (par->isInset(i)) {
201 // Currently, this can only be a quote inset
202 // that is output as plain quote here, but
203 // we use more generic code anyway.
204 otexstringstream ots;
205 OutputParams rp = runparams;
207 par->getInset(i)->latex(ots, rp);
211 char_type c = par->getChar(i);
212 // we can only output characters covered by the current
215 if (runparams.encoding->encodable(c))
217 else if (runparams.dryrun) {
218 code += "<" + _("LyX Warning: ")
219 + _("uncodable character") + " '";
220 code += docstring(1, c);
224 } catch (EncodingException & /* e */) {
225 if (runparams.dryrun) {
226 code += "<" + _("LyX Warning: ")
227 + _("uncodable character") + " '";
228 code += docstring(1, c);
235 // for the inline case, if there are multiple paragraphs
236 // they are simply joined. Otherwise, expect latex errors.
237 if (par != end && !isInline && !captionline)
241 static const docstring delimiters =
242 from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
244 size_t pos = delimiters.find_first_not_of(code);
246 // This code piece contains all possible special character? !!!
247 // Replace ! with a warning message and use ! as delimiter.
248 if (pos == string::npos) {
249 docstring delim_error = "<" + _("LyX Warning: ")
250 + _("no more lstline delimiters available") + ">";
251 code = subst(code, from_ascii("!"), delim_error);
253 if (!runparams.dryrun && !runparams.silent) {
254 // FIXME: warning should be passed to the error dialog
255 frontend::Alert::warning(_("Running out of delimiters"),
256 _("For inline program listings, one character must be reserved\n"
257 "as a delimiter. One of the listings, however, uses all available\n"
258 "characters, so none is left for delimiting purposes.\n"
259 "For the time being, I have replaced '!' by a warning, but you\n"
260 "must investigate!"));
263 docstring const delim(1, delimiters[pos]);
265 os << "\\mintinline";
266 if (!param_string.empty())
267 os << "[" << from_utf8(param_string) << "]";
268 os << "{" << minted_language << "}";
271 if (!param_string.empty())
272 os << "[" << from_utf8(param_string) << "]";
273 else if (pos >= delimiters.find('Q'))
274 // We need to terminate the command before
278 os << delim << code << delim;
279 } else if (use_minted) {
281 os << breakln << "\\begin{listing}";
282 if (!float_placement.empty())
283 os << '[' << float_placement << "]";
285 os << breakln << "\\begin{minted}";
286 if (!param_string.empty())
287 os << "[" << param_string << "]";
288 os << "{" << minted_language << "}\n"
289 << code << breakln << "\\end{minted}\n";
291 OutputParams rp = runparams;
292 rp.moving_arg = true;
293 TexString caption = getCaption(rp);
294 if (!caption.str.empty())
295 os << "\\caption{" << move(caption) << "}\n";
296 os << "\\end{listing}\n";
299 OutputParams rp = runparams;
300 rp.moving_arg = true;
301 TexString caption = getCaption(rp);
302 os << breakln << "\\begin{lstlisting}";
303 if (param_string.empty() && caption.str.empty())
309 if (!caption.str.empty()) {
310 os << "caption={" << move(caption) << '}';
311 if (!param_string.empty())
314 os << from_utf8(param_string) << "]\n";
316 os << code << breakln << "\\end{lstlisting}\n";
319 if (encoding_switched){
321 switchEncoding(os.os(), buffer().params(), runparams, *save_enc, true);
322 runparams.encoding = save_enc;
325 if (!uncodable.empty() && !runparams.silent) {
326 // issue a warning about omitted characters
327 // FIXME: should be passed to the error dialog
328 if (!multibyte_possible && !runparams.encoding->hasFixedWidth())
329 frontend::Alert::warning(_("Uncodable characters in listings inset"),
330 bformat(_("The following characters in one of the program listings are\n"
331 "not representable in the current encoding and have been omitted:\n%1$s.\n"
332 "This is due to a restriction of the listings package, which does\n"
333 "not support your encoding '%2$s'.\n"
334 "Toggling 'Use non-TeX fonts' in Document > Settings...\n"
336 uncodable, _(runparams.encoding->guiName())));
338 frontend::Alert::warning(_("Uncodable characters in listings inset"),
339 bformat(_("The following characters in one of the program listings are\n"
340 "not representable in the current encoding and have been omitted:\n%1$s."),
346 docstring InsetListings::xhtml(XHTMLStream & os, OutputParams const & rp) const
348 odocstringstream ods;
349 XHTMLStream out(ods);
351 bool const isInline = params().isInline();
353 out << html::CompTag("br");
355 out << html::StartTag("div", "class='float-listings'");
356 docstring caption = getCaptionHTML(rp);
357 if (!caption.empty())
358 out << html::StartTag("div", "class='listings-caption'")
359 << XHTMLStream::ESCAPE_NONE
360 << caption << html::EndTag("div");
363 InsetLayout const & il = getLayout();
364 string const & tag = il.htmltag();
365 string attr = "class ='listings";
366 string const lang = params().getParamValue("language");
370 out << html::StartTag(tag, attr);
371 OutputParams newrp = rp;
372 newrp.html_disable_captions = true;
373 // We don't want to convert dashes here. That's the only conversion we
374 // do for XHTML, so this is safe.
375 newrp.pass_thru = true;
376 docstring def = InsetText::insetAsXHTML(out, newrp, InsetText::JustText);
377 out << html::EndTag(tag);
380 out << html::CompTag("br");
381 // escaping will already have been done
382 os << XHTMLStream::ESCAPE_NONE << ods.str();
384 out << html::EndTag("div");
385 // In this case, this needs to be deferred, but we'll put it
386 // before anything the text itself deferred.
387 def = ods.str() + '\n' + def;
393 string InsetListings::contextMenuName() const
395 return "context-listings";
399 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
401 switch (cmd.action()) {
403 case LFUN_INSET_MODIFY: {
404 cur.recordUndoInset(this);
405 InsetListings::string2params(to_utf8(cmd.argument()), params());
409 case LFUN_INSET_DIALOG_UPDATE:
410 cur.bv().updateDialog("listings", params2string(params()));
414 InsetCaptionable::doDispatch(cur, cmd);
420 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
421 FuncStatus & status) const
423 switch (cmd.action()) {
424 case LFUN_INSET_MODIFY:
425 case LFUN_INSET_DIALOG_UPDATE:
426 status.setEnabled(true);
428 case LFUN_CAPTION_INSERT: {
429 // the inset outputs at most one caption
430 if (params().isInline() || !params().isFloat()
431 || getCaptionInset()) {
432 status.setEnabled(false);
437 return InsetCaptionable::getStatus(cur, cmd, status);
442 docstring const InsetListings::buttonLabel(BufferView const & bv) const
445 if (decoration() == InsetLayout::CLASSIC)
446 return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
448 return getNewLabel(_("Listing"));
452 void InsetListings::validate(LaTeXFeatures & features) const
454 if (buffer().params().use_minted)
455 features.require("minted");
457 features.require("listings");
458 features.useInsetLayout(getLayout());
459 string param_string = params().params();
460 if (param_string.find("\\color") != string::npos)
461 features.require("color");
462 InsetCaptionable::validate(features);
466 bool InsetListings::showInsetDialog(BufferView * bv) const
468 bv->showDialog("listings", params2string(params()),
469 const_cast<InsetListings *>(this));
474 TexString InsetListings::getCaption(OutputParams const & runparams) const
476 InsetCaption const * ins = getCaptionInset();
481 ins->getArgs(os, runparams);
482 ins->getArgument(os, runparams);
484 // TODO: The code below should be moved to support, and then the test
485 // in ../tests should be moved there as well.
487 // the caption may contain \label{} but the listings
488 // package prefer caption={}, label={}
489 TexString cap = os.release();
490 if (buffer().params().use_minted
491 || !contains(cap.str, from_ascii("\\label{")))
494 // blah1\label{blah2} blah3
496 // blah1 blah3},label={blah2
498 // caption={blah1 blah3},label={blah2}
500 // NOTE that } is not allowed in blah2.
501 regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
502 string const new_cap("$1$3},label={$2");
503 // TexString validity: the substitution preserves the number of newlines.
504 // Moreover we assume that $2 does not contain newlines, so that the texrow
505 // information remains accurate.
506 cap.str = from_utf8(regex_replace(to_utf8(cap.str), reg, new_cap));
511 void InsetListings::string2params(string const & in,
512 InsetListingsParams & params)
514 params = InsetListingsParams();
517 istringstream data(in);
520 // discard "listings", which is only used to determine inset
526 string InsetListings::params2string(InsetListingsParams const & params)
529 data << "listings" << ' ';