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"
26 #include "InsetLabel.h"
28 #include "LaTeXFeatures.h"
30 #include "output_latex.h"
31 #include "output_docbook.h"
32 #include "output_xhtml.h"
33 #include "OutputParams.h"
34 #include "TextClass.h"
36 #include "texstream.h"
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
42 #include "support/lassert.h"
44 #include "frontends/alert.h"
45 #include "frontends/Application.h"
47 #include "support/regex.h"
52 using namespace lyx::support;
57 InsetListings::InsetListings(Buffer * buf, InsetListingsParams const & par)
58 : InsetCaptionable(buf,"listing")
60 params_.setMinted(buffer().params().use_minted);
61 status_ = par.status();
65 InsetListings::~InsetListings()
67 hideDialogs("listings", this);
71 Inset::RowFlags InsetListings::rowFlags() const
73 return params().isInline() || params().isFloat() ? Inline : Display | AlignLeft;
77 docstring InsetListings::layoutName() const
79 if (buffer().params().use_minted)
80 return from_ascii("MintedListings");
82 return from_ascii("Listings");
86 void InsetListings::write(ostream & os) const
88 os << "listings" << "\n";
89 InsetListingsParams const & par = params();
90 // parameter string is encoded to be a valid lyx token.
91 string opt = par.encodedString();
93 os << "lstparams \"" << opt << "\"\n";
95 os << "inline true\n";
97 os << "inline false\n";
98 InsetCaptionable::write(os);
102 void InsetListings::read(Lexer & lex)
106 string token = lex.getString();
107 if (token == "lstparams") {
109 string const value = lex.getString();
110 params().fromEncodedString(value);
111 } else if (token == "inline") {
113 params().setInline(lex.getBool());
115 // no special option, push back 'status' etc
116 lex.pushToken(token);
120 InsetCaptionable::read(lex);
124 Encoding const * InsetListings::forcedEncoding(Encoding const * inner_enc,
125 Encoding const * outer_enc) const
127 // The listings package cannot deal with multi-byte-encoded
128 // glyphs, except for Xe/LuaTeX (with non-TeX fonts) or pLaTeX.
129 // Minted can deal with all encodings.
130 if (buffer().params().use_minted
131 || inner_enc->name() == "utf8-plain"
132 || inner_enc->package() == Encoding::japanese
133 || inner_enc->hasFixedWidth())
136 // We try if there's a singlebyte encoding for the outer
137 // language; if not, fall back to latin1.
138 // Power-users can set inputenc to utf8-plain to bypass this workaround
139 // and provide alternatives in the user-preamble.
140 return (outer_enc->hasFixedWidth()) ?
141 outer_enc : encodings.fromLyXName("iso8859-1");
145 void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
147 string param_string = params().params();
148 // NOTE: I use {} to quote text, which is an experimental feature
149 // of the listings package (see page 25 of the manual)
150 bool const isInline = params().isInline();
151 bool const use_minted = buffer().params().use_minted;
152 static regex const reg1("(.*)(basicstyle=\\{)([^\\}]*)(\\\\ttfamily)([^\\}]*)(\\})(.*)");
153 static regex const reg2("(.*)(basicstyle=\\{)([^\\}]*)(\\\\rmfamily)([^\\}]*)(\\})(.*)");
154 static regex const reg3("(.*)(basicstyle=\\{)([^\\}]*)(\\\\sffamily)([^\\}]*)(\\})(.*)");
155 static regex const reg4("(.*)(basicstyle=\\{)([^\\}]*)(\\\\(tiny|scriptsize|footnotesize|small|normalsize|large|Large))([^\\}]*)(\\})(.*)");
156 static regex const reg5("(.*)(fontfamily=)(tt|sf|rm)(.*)");
157 static regex const reg6("(.*)(fontsize=\\{)(\\\\(tiny|scriptsize|footnotesize|small|normalsize|large|Large))(\\})(.*)");
159 // If params have been entered with "listings", and then the user switched to "minted",
160 // we have params that need to be translated.
161 // FIXME: We should use a backend-abstract syntax in listings params instead!
162 // Substitute fontstyle option
164 if (regex_match(param_string, sub, reg1))
165 param_string = sub.str(1) + "fontfamily=tt," + sub.str(2) + sub.str(3)
166 + sub.str(5) + sub.str(6) + sub.str(7);
167 if (regex_match(param_string, sub, reg2))
168 param_string = sub.str(1) + "fontfamily=rm," + sub.str(2) + sub.str(3)
169 + sub.str(5) + sub.str(6) + sub.str(7);
170 if (regex_match(param_string, sub, reg3))
171 param_string = sub.str(1) + "fontfamily=sf," + sub.str(2) + sub.str(3)
172 + sub.str(5) + sub.str(6) + sub.str(7);
173 // as well as fontsize option
174 if (regex_match(param_string, sub, reg4))
175 param_string = sub.str(1) + "fontsize={" + sub.str(4) + sub.str(3) + sub.str(7) + sub.str(8);
177 // And the same vice versa
178 // Substitute fontstyle option
181 if (regex_match(param_string, sub, reg5)) {
182 basicstyle = "\\" + sub.str(3) + "family";
183 param_string = sub.str(1) + sub.str(4);
185 // as well as fontsize option
186 if (regex_match(param_string, sub, reg6)) {
187 basicstyle += sub.str(3);
188 param_string = sub.str(1) + sub.str(6);
190 if (!basicstyle.empty())
191 param_string = rtrim(param_string, ",") + ",basicstyle={" + basicstyle + "}";
193 if (runparams.use_polyglossia && runparams.local_font->isRightToLeft()) {
194 // We need to use the *latin switches (#11554)
196 if (regex_match(param_string, sub, reg1))
197 param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
198 + "latin" + sub.str(5) + sub.str(6) + sub.str(7);
199 if (regex_match(param_string, sub, reg2))
200 param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
201 + "latin" + sub.str(5) + sub.str(6) + sub.str(7);
202 if (regex_match(param_string, sub, reg3))
203 param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
204 + "latin" + sub.str(5) + sub.str(6) + sub.str(7);
206 string minted_language;
207 string float_placement;
208 bool const isfloat = params().isFloat();
209 if (use_minted && (isfloat || contains(param_string, "language="))) {
210 // Get float placement and/or language of the code,
211 // then remove the relative options.
212 vector<string> opts =
213 getVectorFromString(param_string, ",", false);
214 for (size_t i = 0; i < opts.size(); ++i) {
215 if (prefixIs(opts[i], "float")) {
216 if (prefixIs(opts[i], "float="))
217 float_placement = opts[i].substr(6);
218 opts.erase(opts.begin() + int(i--));
220 else if (prefixIs(opts[i], "language=")) {
221 minted_language = opts[i].substr(9);
222 opts.erase(opts.begin() + int(i--));
225 param_string = getStringFromVector(opts, ",");
227 // Minted needs a language specification
228 if (minted_language.empty()) {
229 // If a language has been set globally, use that,
230 // otherwise use TeX by default
231 string const & blp = buffer().params().listings_params;
232 size_t start = blp.find("language=");
233 if (start != string::npos) {
234 start += strlen("language=");
235 size_t len = blp.find(",", start);
236 if (len != string::npos)
238 minted_language = blp.substr(start, len);
240 minted_language = "TeX";
243 // get the paragraphs. We can not output them directly to given odocstream
244 // because we can not yet determine the delimiter character of \lstinline
247 ParagraphList::const_iterator par = paragraphs().begin();
248 ParagraphList::const_iterator end = paragraphs().end();
250 bool encoding_switched = false;
251 Encoding const * const save_enc = runparams.encoding;
253 Encoding const * const outer_encoding =
254 (runparams.local_font != 0) ?
255 runparams.local_font->language()->encoding()
256 : buffer().params().language->encoding();
257 Encoding const * fixedlstenc = forcedEncoding(runparams.encoding, outer_encoding);
259 // We need to switch to a singlebyte encoding, due to
260 // the restrictions of the listings package (see above).
261 // This needs to be consistent with
262 // LaTeXFeatures::getTClassI18nPreamble().
263 // We need to put this into a group in order to prevent encoding leaks
264 // (happens with cprotect).
266 switchEncoding(os.os(), buffer().params(), runparams, *fixedlstenc, true);
267 runparams.encoding = fixedlstenc;
268 encoding_switched = true;
271 bool const captionfirst = !isfloat && par->isInset(0)
272 && par->getInset(0)->lyxCode() == CAPTION_CODE;
275 pos_type const siz = par->size();
276 bool captionline = false;
277 for (pos_type i = 0; i < siz; ++i) {
278 if (i == 0 && par->isInset(i) && i + 1 == siz)
280 // ignore all struck out text and (caption) insets
281 if (par->isDeleted(i)
282 || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
284 if (par->isInset(i)) {
285 // Currently, this can only be a quote inset
286 // that is output as plain quote here, but
287 // we use more generic code anyway.
288 otexstringstream ots;
289 OutputParams rp = runparams;
291 par->getInset(i)->latex(ots, rp);
295 char_type c = par->getChar(i);
296 // we can only output characters covered by the current
299 if (runparams.encoding->encodable(c))
301 else if (runparams.dryrun) {
302 code += "<" + _("LyX Warning: ")
303 + _("uncodable character") + " '";
304 code += docstring(1, c);
308 } catch (EncodingException & /* e */) {
309 if (runparams.dryrun) {
310 code += "<" + _("LyX Warning: ")
311 + _("uncodable character") + " '";
312 code += docstring(1, c);
319 // for the inline case, if there are multiple paragraphs
320 // they are simply joined. Otherwise, expect latex errors.
321 if (par != end && !isInline && !captionline)
325 static const docstring delimiters =
326 from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
328 size_t pos = delimiters.find_first_not_of(code);
330 // This code piece contains all possible special character? !!!
331 // Replace ! with a warning message and use ! as delimiter.
332 if (pos == string::npos) {
333 docstring delim_error = "<" + _("LyX Warning: ")
334 + _("no more lstline delimiters available") + ">";
335 code = subst(code, from_ascii("!"), delim_error);
337 if (!runparams.dryrun && !runparams.silent) {
338 // FIXME: warning should be passed to the error dialog
339 frontend::Alert::warning(_("Running out of delimiters"),
340 _("For inline program listings, one character must be reserved\n"
341 "as a delimiter. One of the listings, however, uses all available\n"
342 "characters, so none is left for delimiting purposes.\n"
343 "For the time being, I have replaced '!' by a warning, but you\n"
344 "must investigate!"));
347 docstring const delim(1, delimiters[pos]);
349 os << "\\mintinline";
350 if (!param_string.empty())
351 os << "[" << from_utf8(param_string) << "]";
352 os << "{" << ascii_lowercase(minted_language) << "}";
355 if (!param_string.empty())
356 os << "[" << from_utf8(param_string) << "]";
357 else if (pos >= delimiters.find('Q'))
358 // We need to terminate the command before
362 os << delim << code << delim;
363 } else if (use_minted) {
364 OutputParams rp = runparams;
365 rp.moving_arg = true;
366 TexString caption = getCaption(rp);
368 os << breakln << "\\begin{listing}";
369 if (!float_placement.empty())
370 os << '[' << float_placement << "]";
371 } else if (captionfirst && !caption.str.empty()) {
372 os << breakln << "\\lyxmintcaption[t]{"
373 << move(caption) << "}\n";
375 os << breakln << "\\begin{minted}";
376 if (!param_string.empty())
377 os << "[" << param_string << "]";
378 os << "{" << ascii_lowercase(minted_language) << "}\n"
379 << code << breakln << "\\end{minted}\n";
381 if (!caption.str.empty())
382 os << "\\caption{" << move(caption) << "}\n";
383 os << "\\end{listing}\n";
384 } else if (!captionfirst && !caption.str.empty()) {
385 os << breakln << "\\lyxmintcaption[b]{"
386 << move(caption) << "}";
389 OutputParams rp = runparams;
390 rp.moving_arg = true;
391 TexString caption = getCaption(rp);
392 os << breakln << "\\begin{lstlisting}";
393 if (param_string.empty() && caption.str.empty())
399 if (!caption.str.empty()) {
400 os << "caption={" << move(caption) << '}';
401 if (!param_string.empty())
404 os << from_utf8(param_string) << "]\n";
406 os << code << breakln << "\\end{lstlisting}\n";
409 if (encoding_switched){
411 switchEncoding(os.os(), buffer().params(),
412 runparams, *save_enc, true, true);
413 os << "\\egroup" << breakln;
414 runparams.encoding = save_enc;
417 if (!uncodable.empty() && !runparams.silent) {
418 // issue a warning about omitted characters
419 // FIXME: should be passed to the error dialog
421 frontend::Alert::warning(_("Uncodable characters in listings inset"),
422 bformat(_("The following characters in one of the program listings are\n"
423 "not representable in the current encoding and have been omitted:\n%1$s.\n"
424 "This is due to a restriction of the listings package, which does\n"
425 "not support your encoding '%2$s'.\n"
426 "Toggling 'Use non-TeX fonts' in Document > Settings...\n"
428 uncodable, _(runparams.encoding->guiName())));
430 frontend::Alert::warning(_("Uncodable characters in listings inset"),
431 bformat(_("The following characters in one of the program listings are\n"
432 "not representable in the current encoding and have been omitted:\n%1$s."),
438 docstring InsetListings::xhtml(XMLStream & os, OutputParams const & rp) const
440 odocstringstream ods;
443 bool const isInline = params().isInline();
445 out << xml::CompTag("br");
447 out << xml::StartTag("div", "class='float-listings'");
448 docstring caption = getCaptionHTML(rp);
449 if (!caption.empty())
450 out << xml::StartTag("div", "class='listings-caption'")
451 << XMLStream::ESCAPE_NONE
452 << caption << xml::EndTag("div");
455 InsetLayout const & il = getLayout();
456 string const & tag = il.htmltag();
457 string attr = "class ='listings";
458 string const lang = params().getParamValue("language");
462 out << xml::StartTag(tag, attr);
463 OutputParams newrp = rp;
464 newrp.html_disable_captions = true;
465 // We don't want to convert dashes here. That's the only conversion we
466 // do for XHTML, so this is safe.
467 newrp.pass_thru = true;
468 docstring def = InsetText::insetAsXHTML(out, newrp, InsetText::JustText);
469 out << xml::EndTag(tag);
472 out << xml::CompTag("br");
473 // escaping will already have been done
474 os << XMLStream::ESCAPE_NONE << ods.str();
476 out << xml::EndTag("div");
477 // In this case, this needs to be deferred, but we'll put it
478 // before anything the text itself deferred.
479 def = ods.str() + '\n' + def;
487 const InsetLabel* findLabelInParagraph(const Paragraph &par)
489 for (pos_type pos = 0; pos < par.size(); ++pos) {
490 const Inset *inset = par.getInset(pos);
492 // Maybe an inset is directly a label, in which case no more work is needed.
493 if (inset && dynamic_cast<const InsetLabel *>(inset))
494 return dynamic_cast<const InsetLabel *>(inset);
496 // More likely, the label is hidden in an inset of a paragraph (only if a subtype of InsetText).
497 if (!dynamic_cast<const InsetText *>(inset))
500 auto insetAsText = dynamic_cast<const InsetText *>(inset);
501 auto itIn = insetAsText->paragraphs().begin();
502 auto endIn = insetAsText->paragraphs().end();
503 for (; itIn != endIn; ++itIn) {
504 for (pos_type posIn = 0; posIn < itIn->size(); ++posIn) {
505 const Inset *insetIn = itIn->getInset(posIn);
506 if (insetIn && dynamic_cast<const InsetLabel *>(insetIn)) {
507 return dynamic_cast<const InsetLabel *>(insetIn);
512 // Obviously, this solution does not scale with more levels of paragraphs-insets, but this should be enough.
518 } // anonymous namespace
521 void InsetListings::docbook(XMLStream & xs, OutputParams const & rp) const
523 InsetLayout const & il = getLayout();
524 bool isInline = params().isInline();
526 if (!isInline && !xs.isLastTagCR())
529 // In case of caption, the code must be wrapped, for instance in a figure. Also detect if there is a label.
530 // http://www.sagehill.net/docbookxsl/ProgramListings.html
531 // TODO: parts of this code could be merged with InsetFloat and findLabelInParagraph.
532 InsetCaption const * caption = getCaptionInset();
534 // Find the label in the caption, if any.
535 InsetLabel const * label;
536 auto const end = caption->paragraphs().end();
537 for (auto it = caption->paragraphs().begin(); it != end; ++it) {
538 label = findLabelInParagraph(*it);
543 // Ensure that the label will not be output a second time as an anchor.
544 OutputParams rpNoLabel = rp;
546 rpNoLabel.docbook_anchors_to_ignore.emplace(label->screenLabel());
548 // Prepare the right set of attributes, including the label.
549 docstring attr = from_ascii("type='listing'");
551 attr += " xml:id=\"" + xml::cleanID(label->screenLabel()) + "\"";
553 // Finally, generate the wrapper (including the label).
554 xs << xml::StartTag("figure", attr);
556 xs << xml::StartTag("title");
557 xs << XMLStream::ESCAPE_NONE << getCaptionDocBook(rpNoLabel);
558 xs << xml::EndTag("title");
562 // Forge the attributes.
564 if (!il.docbookattr().empty())
565 attrs += " role=\"" + il.docbookattr() + "\"";
566 std::string const lang = params().getParamValue("language");
568 attrs += " language=\"" + lang + "\"";
570 // Determine the tag to use. Use the layout-defined value if outside a paragraph.
571 std::string tag = il.docbooktag();
575 // Start the listing.
576 xs << xml::StartTag(tag, attrs);
577 xs.startDivision(false);
579 // Deal with the content of the listing.
580 OutputParams newrp = rp;
581 newrp.pass_thru = true;
582 newrp.docbook_make_pars = false;
584 newrp.par_end = text().paragraphs().size();
585 newrp.docbook_in_listing = true;
587 docbookParagraphs(text(), buffer(), xs, newrp);
589 // Done with the listing.
591 xs << xml::EndTag(tag);
596 xs << xml::EndTag("figure");
602 string InsetListings::contextMenuName() const
604 return "context-listings";
608 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
610 switch (cmd.action()) {
612 case LFUN_INSET_MODIFY: {
613 cur.recordUndoInset(this);
614 InsetListings::string2params(to_utf8(cmd.argument()), params());
618 case LFUN_INSET_DIALOG_UPDATE:
619 cur.bv().updateDialog("listings", params2string(params()));
623 InsetCaptionable::doDispatch(cur, cmd);
629 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
630 FuncStatus & status) const
632 switch (cmd.action()) {
633 case LFUN_INSET_MODIFY:
634 case LFUN_INSET_DIALOG_UPDATE:
635 status.setEnabled(true);
637 case LFUN_CAPTION_INSERT: {
638 // the inset outputs at most one caption
639 if (params().isInline() || getCaptionInset()) {
640 status.setEnabled(false);
646 return InsetCaptionable::getStatus(cur, cmd, status);
651 docstring const InsetListings::buttonLabel(BufferView const & bv) const
654 if (decoration() == InsetLayout::CLASSIC)
655 return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
657 return getNewLabel(_("Listing"));
661 void InsetListings::validate(LaTeXFeatures & features) const
663 features.useInsetLayout(getLayout());
664 string param_string = params().params();
665 if (buffer().params().use_minted) {
666 features.require("minted");
667 OutputParams rp = features.runparams();
668 if (!params().isFloat() && !getCaption(rp).str.empty())
669 features.require("lyxmintcaption");
670 if (features.usePolyglossia() && features.hasRTLLanguage())
671 // minted loads color, but color must be loaded before bidi
672 // (i.e., polyglossia)
673 features.require("color");
675 features.require("listings");
676 if (contains(param_string, "\\color"))
677 features.require("color");
679 InsetCaptionable::validate(features);
683 bool InsetListings::showInsetDialog(BufferView * bv) const
685 bv->showDialog("listings", params2string(params()),
686 const_cast<InsetListings *>(this));
691 TexString InsetListings::getCaption(OutputParams const & runparams) const
693 InsetCaption const * ins = getCaptionInset();
698 ins->getArgs(os, runparams);
699 ins->getArgument(os, runparams);
701 // TODO: The code below should be moved to support, and then the test
702 // in ../tests should be moved there as well.
704 // the caption may contain \label{} but the listings
705 // package prefer caption={}, label={}
706 TexString cap = os.release();
707 if (buffer().params().use_minted
708 || !contains(cap.str, from_ascii("\\label{")))
711 // blah1\label{blah2} blah3
713 // blah1 blah3},label={blah2
715 // caption={blah1 blah3},label={blah2}
717 // NOTE that } is not allowed in blah2.
718 regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
719 string const new_cap("$1$3},label={$2");
720 // Remove potential \protect'ion of \label.
721 docstring capstr = subst(cap.str, from_ascii("\\protect\\label"),
722 from_ascii("\\label"));
723 // TexString validity: the substitution preserves the number of newlines.
724 // Moreover we assume that $2 does not contain newlines, so that the texrow
725 // information remains accurate.
726 // Replace '\n' with an improbable character from Private Use Area-A
727 // and then return to '\n' after the regex replacement.
728 capstr = subst(capstr, char_type('\n'), 0xffffd);
729 cap.str = subst(from_utf8(regex_replace(to_utf8(capstr), reg, new_cap)),
730 0xffffd, char_type('\n'));
735 void InsetListings::string2params(string const & in,
736 InsetListingsParams & params)
738 params = InsetListingsParams();
741 istringstream data(in);
744 // discard "listings", which is only used to determine inset
750 string InsetListings::params2string(InsetListingsParams const & params)
753 data << "listings" << ' ';