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 Encoding const * InsetListings::forcedEncoding(Encoding const * inner_enc,
123 Encoding const * outer_enc) const
125 // The listings package cannot deal with multi-byte-encoded
126 // glyphs, except for Xe/LuaTeX (with non-TeX fonts) or pLaTeX.
127 // Minted can deal with all encodings.
128 if (buffer().params().use_minted
129 || inner_enc->name() == "utf8-plain"
130 || inner_enc->package() == Encoding::japanese
131 || inner_enc->hasFixedWidth())
134 // We try if there's a singlebyte encoding for the outer
135 // language; if not, fall back to latin1.
136 // Power-users can set inputenc to utf8-plain to bypass this workaround
137 // and provide alternatives in the user-preamble.
138 return (outer_enc->hasFixedWidth()) ?
139 outer_enc : encodings.fromLyXName("iso8859-1");
143 void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
145 string param_string = params().params();
146 // NOTE: I use {} to quote text, which is an experimental feature
147 // of the listings package (see page 25 of the manual)
148 bool const isInline = params().isInline();
149 bool const use_minted = buffer().params().use_minted;
150 static regex const reg1("(.*)(basicstyle=\\{)([^\\}]*)(\\\\ttfamily)([^\\}]*)(\\})(.*)");
151 static regex const reg2("(.*)(basicstyle=\\{)([^\\}]*)(\\\\rmfamily)([^\\}]*)(\\})(.*)");
152 static regex const reg3("(.*)(basicstyle=\\{)([^\\}]*)(\\\\sffamily)([^\\}]*)(\\})(.*)");
153 if (runparams.use_polyglossia && runparams.local_font->isRightToLeft()) {
154 // We need to use the *latin switches (#11554)
156 if (regex_match(param_string, sub, reg1))
157 param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
158 + "latin" + sub.str(5) + sub.str(6) + sub.str(7);
159 if (regex_match(param_string, sub, reg2))
160 param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
161 + "latin" + sub.str(5) + sub.str(6) + sub.str(7);
162 if (regex_match(param_string, sub, reg3))
163 param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
164 + "latin" + sub.str(5) + sub.str(6) + sub.str(7);
166 string minted_language;
167 string float_placement;
168 bool const isfloat = params().isFloat();
169 if (use_minted && (isfloat || contains(param_string, "language="))) {
170 // Get float placement and/or language of the code,
171 // then remove the relative options.
172 vector<string> opts =
173 getVectorFromString(param_string, ",", false);
174 for (size_t i = 0; i < opts.size(); ++i) {
175 if (prefixIs(opts[i], "float")) {
176 if (prefixIs(opts[i], "float="))
177 float_placement = opts[i].substr(6);
178 opts.erase(opts.begin() + int(i--));
180 else if (prefixIs(opts[i], "language=")) {
181 minted_language = opts[i].substr(9);
182 opts.erase(opts.begin() + int(i--));
185 param_string = getStringFromVector(opts, ",");
187 // Minted needs a language specification
188 if (minted_language.empty()) {
189 // If a language has been set globally, use that,
190 // otherwise use TeX by default
191 string const & blp = buffer().params().listings_params;
192 size_t start = blp.find("language=");
193 if (start != string::npos) {
194 start += strlen("language=");
195 size_t len = blp.find(",", start);
196 if (len != string::npos)
198 minted_language = blp.substr(start, len);
200 minted_language = "TeX";
203 // get the paragraphs. We can not output them directly to given odocstream
204 // because we can not yet determine the delimiter character of \lstinline
207 ParagraphList::const_iterator par = paragraphs().begin();
208 ParagraphList::const_iterator end = paragraphs().end();
210 bool encoding_switched = false;
211 Encoding const * const save_enc = runparams.encoding;
213 Encoding const * const outer_encoding =
214 (runparams.local_font != 0) ?
215 runparams.local_font->language()->encoding()
216 : buffer().params().language->encoding();
217 Encoding const * fixedlstenc = forcedEncoding(runparams.encoding, outer_encoding);
219 // We need to switch to a singlebyte encoding, due to
220 // the restrictions of the listings package (see above).
221 // This needs to be consistent with
222 // LaTeXFeatures::getTClassI18nPreamble().
223 // We need to put this into a group in order to prevent encoding leaks
224 // (happens with cprotect).
226 switchEncoding(os.os(), buffer().params(), runparams, *fixedlstenc, true);
227 runparams.encoding = fixedlstenc;
228 encoding_switched = true;
231 bool const captionfirst = !isfloat && par->isInset(0)
232 && par->getInset(0)->lyxCode() == CAPTION_CODE;
235 pos_type const siz = par->size();
236 bool captionline = false;
237 for (pos_type i = 0; i < siz; ++i) {
238 if (i == 0 && par->isInset(i) && i + 1 == siz)
240 // ignore all struck out text and (caption) insets
241 if (par->isDeleted(i)
242 || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
244 if (par->isInset(i)) {
245 // Currently, this can only be a quote inset
246 // that is output as plain quote here, but
247 // we use more generic code anyway.
248 otexstringstream ots;
249 OutputParams rp = runparams;
251 par->getInset(i)->latex(ots, rp);
255 char_type c = par->getChar(i);
256 // we can only output characters covered by the current
259 if (runparams.encoding->encodable(c))
261 else if (runparams.dryrun) {
262 code += "<" + _("LyX Warning: ")
263 + _("uncodable character") + " '";
264 code += docstring(1, c);
268 } catch (EncodingException & /* e */) {
269 if (runparams.dryrun) {
270 code += "<" + _("LyX Warning: ")
271 + _("uncodable character") + " '";
272 code += docstring(1, c);
279 // for the inline case, if there are multiple paragraphs
280 // they are simply joined. Otherwise, expect latex errors.
281 if (par != end && !isInline && !captionline)
285 static const docstring delimiters =
286 from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
288 size_t pos = delimiters.find_first_not_of(code);
290 // This code piece contains all possible special character? !!!
291 // Replace ! with a warning message and use ! as delimiter.
292 if (pos == string::npos) {
293 docstring delim_error = "<" + _("LyX Warning: ")
294 + _("no more lstline delimiters available") + ">";
295 code = subst(code, from_ascii("!"), delim_error);
297 if (!runparams.dryrun && !runparams.silent) {
298 // FIXME: warning should be passed to the error dialog
299 frontend::Alert::warning(_("Running out of delimiters"),
300 _("For inline program listings, one character must be reserved\n"
301 "as a delimiter. One of the listings, however, uses all available\n"
302 "characters, so none is left for delimiting purposes.\n"
303 "For the time being, I have replaced '!' by a warning, but you\n"
304 "must investigate!"));
307 docstring const delim(1, delimiters[pos]);
309 os << "\\mintinline";
310 if (!param_string.empty())
311 os << "[" << from_utf8(param_string) << "]";
312 os << "{" << ascii_lowercase(minted_language) << "}";
315 if (!param_string.empty())
316 os << "[" << from_utf8(param_string) << "]";
317 else if (pos >= delimiters.find('Q'))
318 // We need to terminate the command before
322 os << delim << code << delim;
323 } else if (use_minted) {
324 OutputParams rp = runparams;
325 rp.moving_arg = true;
326 TexString caption = getCaption(rp);
328 os << breakln << "\\begin{listing}";
329 if (!float_placement.empty())
330 os << '[' << float_placement << "]";
331 } else if (captionfirst && !caption.str.empty()) {
332 os << breakln << "\\lyxmintcaption[t]{"
333 << move(caption) << "}\n";
335 os << breakln << "\\begin{minted}";
336 if (!param_string.empty())
337 os << "[" << param_string << "]";
338 os << "{" << ascii_lowercase(minted_language) << "}\n"
339 << code << breakln << "\\end{minted}\n";
341 if (!caption.str.empty())
342 os << "\\caption{" << move(caption) << "}\n";
343 os << "\\end{listing}\n";
344 } else if (!captionfirst && !caption.str.empty()) {
345 os << breakln << "\\lyxmintcaption[b]{"
346 << move(caption) << "}";
349 OutputParams rp = runparams;
350 rp.moving_arg = true;
351 TexString caption = getCaption(rp);
352 os << breakln << "\\begin{lstlisting}";
353 if (param_string.empty() && caption.str.empty())
359 if (!caption.str.empty()) {
360 os << "caption={" << move(caption) << '}';
361 if (!param_string.empty())
364 os << from_utf8(param_string) << "]\n";
366 os << code << breakln << "\\end{lstlisting}\n";
369 if (encoding_switched){
371 switchEncoding(os.os(), buffer().params(),
372 runparams, *save_enc, true, true);
373 os << "\\egroup" << breakln;
374 runparams.encoding = save_enc;
377 if (!uncodable.empty() && !runparams.silent) {
378 // issue a warning about omitted characters
379 // FIXME: should be passed to the error dialog
381 frontend::Alert::warning(_("Uncodable characters in listings inset"),
382 bformat(_("The following characters in one of the program listings are\n"
383 "not representable in the current encoding and have been omitted:\n%1$s.\n"
384 "This is due to a restriction of the listings package, which does\n"
385 "not support your encoding '%2$s'.\n"
386 "Toggling 'Use non-TeX fonts' in Document > Settings...\n"
388 uncodable, _(runparams.encoding->guiName())));
390 frontend::Alert::warning(_("Uncodable characters in listings inset"),
391 bformat(_("The following characters in one of the program listings are\n"
392 "not representable in the current encoding and have been omitted:\n%1$s."),
398 docstring InsetListings::xhtml(XHTMLStream & os, OutputParams const & rp) const
400 odocstringstream ods;
401 XHTMLStream out(ods);
403 bool const isInline = params().isInline();
405 out << html::CompTag("br");
407 out << html::StartTag("div", "class='float-listings'");
408 docstring caption = getCaptionHTML(rp);
409 if (!caption.empty())
410 out << html::StartTag("div", "class='listings-caption'")
411 << XHTMLStream::ESCAPE_NONE
412 << caption << html::EndTag("div");
415 InsetLayout const & il = getLayout();
416 string const & tag = il.htmltag();
417 string attr = "class ='listings";
418 string const lang = params().getParamValue("language");
422 out << html::StartTag(tag, attr);
423 OutputParams newrp = rp;
424 newrp.html_disable_captions = true;
425 // We don't want to convert dashes here. That's the only conversion we
426 // do for XHTML, so this is safe.
427 newrp.pass_thru = true;
428 docstring def = InsetText::insetAsXHTML(out, newrp, InsetText::JustText);
429 out << html::EndTag(tag);
432 out << html::CompTag("br");
433 // escaping will already have been done
434 os << XHTMLStream::ESCAPE_NONE << ods.str();
436 out << html::EndTag("div");
437 // In this case, this needs to be deferred, but we'll put it
438 // before anything the text itself deferred.
439 def = ods.str() + '\n' + def;
445 string InsetListings::contextMenuName() const
447 return "context-listings";
451 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
453 switch (cmd.action()) {
455 case LFUN_INSET_MODIFY: {
456 cur.recordUndoInset(this);
457 InsetListings::string2params(to_utf8(cmd.argument()), params());
461 case LFUN_INSET_DIALOG_UPDATE:
462 cur.bv().updateDialog("listings", params2string(params()));
466 InsetCaptionable::doDispatch(cur, cmd);
472 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
473 FuncStatus & status) const
475 switch (cmd.action()) {
476 case LFUN_INSET_MODIFY:
477 case LFUN_INSET_DIALOG_UPDATE:
478 status.setEnabled(true);
480 case LFUN_CAPTION_INSERT: {
481 // the inset outputs at most one caption
482 if (params().isInline() || getCaptionInset()) {
483 status.setEnabled(false);
489 return InsetCaptionable::getStatus(cur, cmd, status);
494 docstring const InsetListings::buttonLabel(BufferView const & bv) const
497 if (decoration() == InsetLayout::CLASSIC)
498 return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
500 return getNewLabel(_("Listing"));
504 void InsetListings::validate(LaTeXFeatures & features) const
506 features.useInsetLayout(getLayout());
507 string param_string = params().params();
508 if (buffer().params().use_minted) {
509 features.require("minted");
510 OutputParams rp = features.runparams();
511 if (!params().isFloat() && !getCaption(rp).str.empty())
512 features.require("lyxmintcaption");
513 if (features.usePolyglossia() && features.hasRTLLanguage())
514 // minted loads color, but color must be loaded before bidi
515 // (i.e., polyglossia)
516 features.require("color");
518 features.require("listings");
519 if (contains(param_string, "\\color"))
520 features.require("color");
522 InsetCaptionable::validate(features);
526 bool InsetListings::showInsetDialog(BufferView * bv) const
528 bv->showDialog("listings", params2string(params()),
529 const_cast<InsetListings *>(this));
534 TexString InsetListings::getCaption(OutputParams const & runparams) const
536 InsetCaption const * ins = getCaptionInset();
541 ins->getArgs(os, runparams);
542 ins->getArgument(os, runparams);
544 // TODO: The code below should be moved to support, and then the test
545 // in ../tests should be moved there as well.
547 // the caption may contain \label{} but the listings
548 // package prefer caption={}, label={}
549 TexString cap = os.release();
550 if (buffer().params().use_minted
551 || !contains(cap.str, from_ascii("\\label{")))
554 // blah1\label{blah2} blah3
556 // blah1 blah3},label={blah2
558 // caption={blah1 blah3},label={blah2}
560 // NOTE that } is not allowed in blah2.
561 regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
562 string const new_cap("$1$3},label={$2");
563 // Remove potential \protect'ion of \label.
564 docstring capstr = subst(cap.str, from_ascii("\\protect\\label"),
565 from_ascii("\\label"));
566 // TexString validity: the substitution preserves the number of newlines.
567 // Moreover we assume that $2 does not contain newlines, so that the texrow
568 // information remains accurate.
569 // Replace '\n' with an improbable character from Private Use Area-A
570 // and then return to '\n' after the regex replacement.
571 capstr = subst(capstr, char_type('\n'), 0xffffd);
572 cap.str = subst(from_utf8(regex_replace(to_utf8(capstr), reg, new_cap)),
573 0xffffd, char_type('\n'));
578 void InsetListings::string2params(string const & in,
579 InsetListingsParams & params)
581 params = InsetListingsParams();
584 istringstream data(in);
587 // discard "listings", which is only used to determine inset
593 string InsetListings::params2string(InsetListingsParams const & params)
596 data << "listings" << ' ';