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 "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "InsetCaption.h"
24 #include "InsetLabel.h"
25 #include "InsetLayout.h"
27 #include "LaTeXFeatures.h"
28 #include "output_latex.h"
29 #include "output_docbook.h"
30 #include "output_xhtml.h"
32 #include "texstream.h"
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/Lexer.h"
38 #include "support/lstrings.h"
39 #include "support/lassert.h"
41 #include "frontends/alert.h"
42 #include "frontends/Application.h"
49 using namespace lyx::support;
54 InsetListings::InsetListings(Buffer * buf, InsetListingsParams const & par)
55 : InsetCaptionable(buf,"listing")
57 params_.setMinted(buffer().params().use_minted);
58 status_ = par.status();
62 InsetListings::~InsetListings()
64 hideDialogs("listings", this);
68 int InsetListings::rowFlags() const
70 return params().isInline() || params().isFloat() ? Inline : Display | AlignLeft;
74 docstring InsetListings::layoutName() const
76 if (buffer().params().use_minted)
77 return from_ascii("MintedListings");
79 return from_ascii("Listings");
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 InsetCaptionable::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 InsetCaptionable::read(lex);
121 Encoding const * InsetListings::forcedEncoding(Encoding const * inner_enc,
122 Encoding const * outer_enc) const
124 // The listings package cannot deal with multi-byte-encoded
125 // glyphs, except for Xe/LuaTeX (with non-TeX fonts) or pLaTeX.
126 // Minted can deal with all encodings.
127 if (buffer().params().use_minted
128 || inner_enc->name() == "utf8-plain"
129 || inner_enc->package() == Encoding::japanese
130 || inner_enc->hasFixedWidth())
133 // We try if there's a singlebyte encoding for the outer
134 // language; if not, fall back to latin1.
135 // Power-users can set inputenc to utf8-plain to bypass this workaround
136 // and provide alternatives in the user-preamble.
137 return (outer_enc->hasFixedWidth()) ?
138 outer_enc : encodings.fromLyXName("iso8859-1");
142 void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
144 string param_string = params().params();
145 // NOTE: I use {} to quote text, which is an experimental feature
146 // of the listings package (see page 25 of the manual)
147 bool const isInline = params().isInline();
148 bool const use_minted = buffer().params().use_minted;
149 static regex const reg1("(.*)(basicstyle=\\{)([^\\}]*)(\\\\ttfamily)([^\\}]*)(\\})(.*)");
150 static regex const reg2("(.*)(basicstyle=\\{)([^\\}]*)(\\\\rmfamily)([^\\}]*)(\\})(.*)");
151 static regex const reg3("(.*)(basicstyle=\\{)([^\\}]*)(\\\\sffamily)([^\\}]*)(\\})(.*)");
152 static regex const reg4("(.*)(basicstyle=\\{)([^\\}]*)(\\\\(tiny|scriptsize|footnotesize|small|normalsize|large|Large))([^\\}]*)(\\})(.*)");
153 static regex const reg5("(.*)(fontfamily=)(tt|sf|rm)(.*)");
154 static regex const reg6("(.*)(fontsize=\\{)(\\\\(tiny|scriptsize|footnotesize|small|normalsize|large|Large))(\\})(.*)");
156 // If params have been entered with "listings", and then the user switched to "minted",
157 // we have params that need to be translated.
158 // FIXME: We should use a backend-abstract syntax in listings params instead!
159 // Substitute fontstyle option
161 if (regex_match(param_string, sub, reg1))
162 param_string = sub.str(1) + "fontfamily=tt," + sub.str(2) + sub.str(3)
163 + sub.str(5) + sub.str(6) + sub.str(7);
164 if (regex_match(param_string, sub, reg2))
165 param_string = sub.str(1) + "fontfamily=rm," + sub.str(2) + sub.str(3)
166 + sub.str(5) + sub.str(6) + sub.str(7);
167 if (regex_match(param_string, sub, reg3))
168 param_string = sub.str(1) + "fontfamily=sf," + sub.str(2) + sub.str(3)
169 + sub.str(5) + sub.str(6) + sub.str(7);
170 // as well as fontsize option
171 if (regex_match(param_string, sub, reg4))
172 param_string = sub.str(1) + "fontsize={" + sub.str(4) + sub.str(3) + sub.str(7) + sub.str(8);
174 // And the same vice versa
175 // Substitute fontstyle option
178 if (regex_match(param_string, sub, reg5)) {
179 basicstyle = "\\" + sub.str(3) + "family";
180 param_string = sub.str(1) + sub.str(4);
182 // as well as fontsize option
183 if (regex_match(param_string, sub, reg6)) {
184 basicstyle += sub.str(3);
185 param_string = sub.str(1) + sub.str(6);
187 if (!basicstyle.empty())
188 param_string = rtrim(param_string, ",") + ",basicstyle={" + basicstyle + "}";
190 if (runparams.use_polyglossia && runparams.local_font
191 && runparams.local_font->isRightToLeft()) {
192 // We need to use the *latin switches (#11554)
194 if (regex_match(param_string, sub, reg1))
195 param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
196 + "latin" + sub.str(5) + sub.str(6) + sub.str(7);
197 if (regex_match(param_string, sub, reg2))
198 param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
199 + "latin" + sub.str(5) + sub.str(6) + sub.str(7);
200 if (regex_match(param_string, sub, reg3))
201 param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
202 + "latin" + sub.str(5) + sub.str(6) + sub.str(7);
204 string minted_language;
205 string float_placement;
206 bool const isfloat = params().isFloat();
207 if (use_minted && (isfloat || contains(param_string, "language="))) {
208 // Get float placement and/or language of the code,
209 // then remove the relative options.
210 vector<string> opts =
211 getVectorFromString(param_string, ",", false);
212 for (size_t i = 0; i < opts.size(); ++i) {
213 if (prefixIs(opts[i], "float")) {
214 if (prefixIs(opts[i], "float="))
215 float_placement = opts[i].substr(6);
216 opts.erase(opts.begin() + int(i--));
218 else if (prefixIs(opts[i], "language=")) {
219 minted_language = opts[i].substr(9);
220 opts.erase(opts.begin() + int(i--));
223 param_string = getStringFromVector(opts, ",");
225 // Minted needs a language specification
226 if (minted_language.empty()) {
227 // If a language has been set globally, use that,
228 // otherwise use TeX by default
229 string const & blp = buffer().params().listings_params;
230 size_t start = blp.find("language=");
231 if (start != string::npos) {
232 start += strlen("language=");
233 size_t len = blp.find(",", start);
234 if (len != string::npos)
236 minted_language = blp.substr(start, len);
238 minted_language = "TeX";
241 // get the paragraphs. We can not output them directly to given odocstream
242 // because we can not yet determine the delimiter character of \lstinline
245 ParagraphList::const_iterator par = paragraphs().begin();
246 ParagraphList::const_iterator end = paragraphs().end();
248 bool encoding_switched = false;
249 Encoding const * const save_enc = runparams.encoding;
251 Encoding const * const outer_encoding =
252 getLocalOrDefaultLang(runparams)->encoding();
253 Encoding const * fixedlstenc = forcedEncoding(runparams.encoding, outer_encoding);
255 // We need to switch to a singlebyte encoding, due to
256 // the restrictions of the listings package (see above).
257 // This needs to be consistent with
258 // LaTeXFeatures::getTClassI18nPreamble().
259 // We need to put this into a group in order to prevent encoding leaks
260 // (happens with cprotect).
262 switchEncoding(os.os(), buffer().params(), runparams, *fixedlstenc, true);
263 runparams.encoding = fixedlstenc;
264 encoding_switched = true;
267 bool const captionfirst = !isfloat && par->isInset(0)
268 && par->getInset(0)->lyxCode() == CAPTION_CODE;
271 pos_type const siz = par->size();
272 bool captionline = false;
273 for (pos_type i = 0; i < siz; ++i) {
274 if (i == 0 && par->isInset(i) && i + 1 == siz)
276 // ignore all struck out text and (caption) insets
277 if (par->isDeleted(i)
278 || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
280 if (par->isInset(i)) {
281 // Currently, this can only be a quote inset
282 // that is output as plain quote here, but
283 // we use more generic code anyway.
284 otexstringstream ots;
285 OutputParams rp = runparams;
287 par->getInset(i)->latex(ots, rp);
291 char_type c = par->getChar(i);
292 // we can only output characters covered by the current
295 if (runparams.encoding->encodable(c))
297 else if (runparams.dryrun) {
298 code += "<" + _("LyX Warning: ")
299 + _("uncodable character") + " '";
300 code += docstring(1, c);
304 } catch (EncodingException & /* e */) {
305 if (runparams.dryrun) {
306 code += "<" + _("LyX Warning: ")
307 + _("uncodable character") + " '";
308 code += docstring(1, c);
315 // Add new line between paragraphs in displayed listings.
316 // Exception: merged paragraphs in change tracking mode.
317 // Also, for the inline case, if there are multiple paragraphs
318 // they are simply joined. Otherwise, expect latex errors.
319 if (par != end && !isInline && !captionline && !par->parEndChange().deleted())
323 static const docstring delimiters =
324 from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
326 size_t pos = delimiters.find_first_not_of(code);
328 // This code piece contains all possible special character? !!!
329 // Replace ! with a warning message and use ! as delimiter.
330 if (pos == string::npos) {
331 docstring delim_error = "<" + _("LyX Warning: ")
332 + _("no more lstline delimiters available") + ">";
333 code = subst(code, from_ascii("!"), delim_error);
335 if (!runparams.dryrun && !runparams.silent) {
336 // FIXME: warning should be passed to the error dialog
337 frontend::Alert::warning(_("Running out of delimiters"),
338 _("For inline program listings, one character must be reserved\n"
339 "as a delimiter. One of the listings, however, uses all available\n"
340 "characters, so none is left for delimiting purposes.\n"
341 "For the time being, I have replaced '!' by a warning, but you\n"
342 "must investigate!"));
345 docstring const delim(1, delimiters[pos]);
347 os << "\\mintinline";
348 if (!param_string.empty())
349 os << "[" << from_utf8(param_string) << "]";
350 os << "{" << ascii_lowercase(minted_language) << "}";
353 if (!param_string.empty())
354 os << "[" << from_utf8(param_string) << "]";
355 else if (pos >= delimiters.find('Q'))
356 // We need to terminate the command before
360 os << delim << code << delim;
361 } else if (use_minted) {
362 OutputParams rp = runparams;
363 rp.moving_arg = true;
364 TexString caption = getCaption(rp);
366 os << breakln << "\\begin{listing}";
367 if (!float_placement.empty())
368 os << '[' << float_placement << "]";
369 } else if (captionfirst && !caption.str.empty()) {
370 os << breakln << "\\lyxmintcaption[t]{"
371 << std::move(caption) << "}\n";
373 os << breakln << "\\begin{minted}";
374 if (!param_string.empty())
375 os << "[" << param_string << "]";
376 os << "{" << ascii_lowercase(minted_language) << "}\n"
377 << code << breakln << "\\end{minted}\n";
379 if (!caption.str.empty())
380 os << "\\caption{" << std::move(caption) << "}\n";
381 os << "\\end{listing}\n";
382 } else if (!captionfirst && !caption.str.empty()) {
383 os << breakln << "\\lyxmintcaption[b]{"
384 << std::move(caption) << "}";
387 OutputParams rp = runparams;
388 rp.moving_arg = true;
389 TexString caption = getCaption(rp);
390 os << breakln << "\\begin{lstlisting}";
391 if (param_string.empty() && caption.str.empty())
397 if (!caption.str.empty()) {
398 os << "caption={" << std::move(caption) << '}';
399 if (!param_string.empty())
402 os << from_utf8(param_string) << "]\n";
404 os << code << breakln << "\\end{lstlisting}\n";
407 if (encoding_switched) {
409 switchEncoding(os.os(), buffer().params(),
410 runparams, *save_enc, true, true);
412 // Go out of vertical mode. Otherwise \egroup
413 // causes a paragraph break (#12821)
414 os << "\\leavevmode";
415 os << "\\egroup" << breakln;
416 runparams.encoding = save_enc;
419 if (!uncodable.empty() && !runparams.silent) {
420 // issue a warning about omitted characters
421 // FIXME: should be passed to the error dialog
423 frontend::Alert::warning(_("Uncodable characters in listings inset"),
424 bformat(_("The following characters in one of the program listings are\n"
425 "not representable in the current encoding and have been omitted:\n%1$s.\n"
426 "This is due to a restriction of the listings package, which does\n"
427 "not support your encoding '%2$s'.\n"
428 "Toggling 'Use non-TeX fonts' in Document > Settings...\n"
430 uncodable, _(runparams.encoding->guiName())));
432 frontend::Alert::warning(_("Uncodable characters in listings inset"),
433 bformat(_("The following characters in one of the program listings are\n"
434 "not representable in the current encoding and have been omitted:\n%1$s."),
440 docstring InsetListings::xhtml(XMLStream & os, OutputParams const & rp) const
442 bool const isInline = params().isInline();
444 os << xml::CompTag("br");
446 os << xml::StartTag("div", "class='float-listings'");
447 docstring caption = getCaptionHTML(rp);
448 if (!caption.empty())
449 os << xml::StartTag("div", "class='listings-caption'")
450 << XMLStream::ESCAPE_NONE
451 << caption << xml::EndTag("div");
454 string const & tag = getLayout().htmltag();
455 string attr = "class='listings";
456 string const lang = params().getParamValue("language");
460 os << xml::StartTag(tag, attr);
461 OutputParams newrp = rp;
462 newrp.html_disable_captions = true;
463 // We don't want to convert dashes here. That's the only conversion we
464 // do for XHTML, so this is safe.
465 newrp.pass_thru = true;
466 docstring def = InsetText::insetAsXHTML(os, newrp, InsetText::JustText);
467 os << xml::EndTag(tag);
470 os << xml::CompTag("br");
475 os << xml::EndTag("div");
481 void InsetListings::docbook(XMLStream & xs, OutputParams const & rp) const
483 InsetLayout const & il = getLayout();
484 bool isInline = params().isInline();
486 if (!isInline && !xs.isLastTagCR())
489 // In case of caption, the code must be wrapped, for instance in a figure. Also detect if there is a label.
490 // http://www.sagehill.net/docbookxsl/ProgramListings.html
491 // TODO: parts of this code could be merged with InsetFloat and findLabelInParagraph.
492 InsetCaption const * caption = getCaptionInset();
494 InsetLabel const * label = getLabelInset();
496 // Ensure that the label will not be output a second time as an anchor.
497 OutputParams rpNoLabel = rp;
499 rpNoLabel.docbook_anchors_to_ignore.emplace(label->screenLabel());
501 // Prepare the right set of attributes, including the label.
502 docstring attr = from_ascii("type='listing'");
504 attr += " xml:id=\"" + xml::cleanID(label->screenLabel()) + "\"";
506 // Finally, generate the wrapper (including the label).
507 xs << xml::StartTag("figure", attr);
509 xs << xml::StartTag("title");
510 xs << XMLStream::ESCAPE_NONE << getCaptionDocBook(rpNoLabel);
511 xs << xml::EndTag("title");
515 // Forge the attributes.
517 if (!il.docbookattr().empty())
518 attrs += " role=\"" + il.docbookattr() + "\"";
519 std::string const lang = params().getParamValue("language");
521 attrs += " language=\"" + lang + "\"";
523 // Determine the tag to use. Use the layout-defined value if outside a paragraph.
524 std::string tag = il.docbooktag();
528 // Start the listing.
529 xs << xml::StartTag(tag, attrs);
530 xs.startDivision(false);
532 // Deal with the content of the listing.
533 OutputParams newrp = rp;
534 newrp.pass_thru = true;
535 newrp.docbook_make_pars = false;
537 newrp.par_end = text().paragraphs().size();
538 newrp.docbook_in_listing = true;
540 docbookParagraphs(text(), buffer(), xs, newrp);
542 // Done with the listing.
544 xs << xml::EndTag(tag);
549 xs << xml::EndTag("figure");
555 string InsetListings::contextMenuName() const
557 return "context-listings";
561 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
563 switch (cmd.action()) {
565 case LFUN_INSET_MODIFY: {
566 cur.recordUndoInset(this);
567 InsetListings::string2params(to_utf8(cmd.argument()), params());
571 case LFUN_INSET_DIALOG_UPDATE:
572 cur.bv().updateDialog("listings", params2string(params()));
576 InsetCaptionable::doDispatch(cur, cmd);
582 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
583 FuncStatus & status) const
585 switch (cmd.action()) {
586 case LFUN_INSET_MODIFY:
587 case LFUN_INSET_DIALOG_UPDATE:
588 status.setEnabled(true);
590 case LFUN_CAPTION_INSERT: {
591 // the inset outputs at most one caption
592 if (params().isInline() || getCaptionInset()) {
593 status.setEnabled(false);
599 return InsetCaptionable::getStatus(cur, cmd, status);
604 docstring const InsetListings::buttonLabel(BufferView const & bv) const
607 docstring const locked = tempfile_ ? docstring(1, 0x1F512) : docstring();
608 if (decoration() == InsetDecoration::CLASSIC)
609 return locked + (isOpen(bv) ? _("Listing") : getNewLabel(_("Listing")));
610 return locked + getNewLabel(_("Listing"));
614 void InsetListings::validate(LaTeXFeatures & features) const
616 features.useInsetLayout(getLayout());
617 string param_string = params().params();
618 if (buffer().params().use_minted) {
619 features.require("minted");
620 OutputParams rp = features.runparams();
621 if (!params().isFloat() && !getCaption(rp).str.empty())
622 features.require("lyxmintcaption");
623 if (features.usePolyglossia() && features.hasRTLLanguage())
624 // minted loads color, but color must be loaded before bidi
625 // (i.e., polyglossia)
626 features.require("color");
628 features.require("listings");
629 if (contains(param_string, "\\color"))
630 features.require("color");
632 InsetCaptionable::validate(features);
636 bool InsetListings::showInsetDialog(BufferView * bv) const
638 bv->showDialog("listings", params2string(params()),
639 const_cast<InsetListings *>(this));
644 TexString InsetListings::getCaption(OutputParams const & runparams) const
646 InsetCaption const * ins = getCaptionInset();
651 ins->getArgs(os, runparams);
652 ins->getArgument(os, runparams);
654 // TODO: The code below should be moved to support, and then the test
655 // in ../tests should be moved there as well.
657 // the caption may contain \label{} but the listings
658 // package prefer caption={}, label={}
659 TexString cap = os.release();
660 if (buffer().params().use_minted
661 || !contains(cap.str, from_ascii("\\label{")))
664 // blah1\label{blah2} blah3
666 // blah1 blah3},label={blah2
668 // caption={blah1 blah3},label={blah2}
670 // NOTE that } is not allowed in blah2.
671 regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
672 string const new_cap("$1$3},label={$2");
673 // Remove potential \protect'ion of \label.
674 docstring capstr = subst(cap.str, from_ascii("\\protect\\label"),
675 from_ascii("\\label"));
676 // TexString validity: the substitution preserves the number of newlines.
677 // Moreover we assume that $2 does not contain newlines, so that the texrow
678 // information remains accurate.
679 // Replace '\n' with an improbable character from Private Use Area-A
680 // and then return to '\n' after the regex replacement.
681 capstr = subst(capstr, char_type('\n'), 0xffffd);
682 cap.str = subst(from_utf8(regex_replace(to_utf8(capstr), reg, new_cap)),
683 0xffffd, char_type('\n'));
688 void InsetListings::string2params(string const & in,
689 InsetListingsParams & params)
691 params = InsetListingsParams();
694 istringstream data(in);
697 // discard "listings", which is only used to determine inset
703 string InsetListings::params2string(InsetListingsParams const & params)
706 data << "listings" << ' ';