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 if full-unicode aware backends
127 // such as XeTeX or LuaTeX are used, and with pLaTeX.
128 // Minted can deal with all encodings.
129 if (buffer().params().use_minted
130 || inner_enc->name() == "utf8-plain"
131 || (buffer().params().encoding().package() == Encoding::japanese
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 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 switchEncoding(os.os(), buffer().params(), runparams, *fixedlstenc, true);
224 runparams.encoding = fixedlstenc;
225 encoding_switched = true;
228 bool const captionfirst = !isfloat && par->isInset(0)
229 && par->getInset(0)->lyxCode() == CAPTION_CODE;
232 pos_type siz = par->size();
233 bool captionline = false;
234 for (pos_type i = 0; i < siz; ++i) {
235 if (i == 0 && par->isInset(i) && i + 1 == siz)
237 // ignore all struck out text and (caption) insets
238 if (par->isDeleted(i)
239 || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
241 if (par->isInset(i)) {
242 // Currently, this can only be a quote inset
243 // that is output as plain quote here, but
244 // we use more generic code anyway.
245 otexstringstream ots;
246 OutputParams rp = runparams;
248 par->getInset(i)->latex(ots, rp);
252 char_type c = par->getChar(i);
253 // we can only output characters covered by the current
256 if (runparams.encoding->encodable(c))
258 else if (runparams.dryrun) {
259 code += "<" + _("LyX Warning: ")
260 + _("uncodable character") + " '";
261 code += docstring(1, c);
265 } catch (EncodingException & /* e */) {
266 if (runparams.dryrun) {
267 code += "<" + _("LyX Warning: ")
268 + _("uncodable character") + " '";
269 code += docstring(1, c);
276 // for the inline case, if there are multiple paragraphs
277 // they are simply joined. Otherwise, expect latex errors.
278 if (par != end && !isInline && !captionline)
282 static const docstring delimiters =
283 from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
285 size_t pos = delimiters.find_first_not_of(code);
287 // This code piece contains all possible special character? !!!
288 // Replace ! with a warning message and use ! as delimiter.
289 if (pos == string::npos) {
290 docstring delim_error = "<" + _("LyX Warning: ")
291 + _("no more lstline delimiters available") + ">";
292 code = subst(code, from_ascii("!"), delim_error);
294 if (!runparams.dryrun && !runparams.silent) {
295 // FIXME: warning should be passed to the error dialog
296 frontend::Alert::warning(_("Running out of delimiters"),
297 _("For inline program listings, one character must be reserved\n"
298 "as a delimiter. One of the listings, however, uses all available\n"
299 "characters, so none is left for delimiting purposes.\n"
300 "For the time being, I have replaced '!' by a warning, but you\n"
301 "must investigate!"));
304 docstring const delim(1, delimiters[pos]);
306 os << "\\mintinline";
307 if (!param_string.empty())
308 os << "[" << from_utf8(param_string) << "]";
309 os << "{" << ascii_lowercase(minted_language) << "}";
312 if (!param_string.empty())
313 os << "[" << from_utf8(param_string) << "]";
314 else if (pos >= delimiters.find('Q'))
315 // We need to terminate the command before
319 os << delim << code << delim;
320 } else if (use_minted) {
321 OutputParams rp = runparams;
322 rp.moving_arg = true;
323 TexString caption = getCaption(rp);
325 os << breakln << "\\begin{listing}";
326 if (!float_placement.empty())
327 os << '[' << float_placement << "]";
328 } else if (captionfirst && !caption.str.empty()) {
329 os << breakln << "\\lyxmintcaption[t]{"
330 << move(caption) << "}\n";
332 os << breakln << "\\begin{minted}";
333 if (!param_string.empty())
334 os << "[" << param_string << "]";
335 os << "{" << ascii_lowercase(minted_language) << "}\n"
336 << code << breakln << "\\end{minted}\n";
338 if (!caption.str.empty())
339 os << "\\caption{" << move(caption) << "}\n";
340 os << "\\end{listing}\n";
341 } else if (!captionfirst && !caption.str.empty()) {
342 os << breakln << "\\lyxmintcaption[b]{"
343 << move(caption) << "}";
346 OutputParams rp = runparams;
347 rp.moving_arg = true;
348 TexString caption = getCaption(rp);
349 os << breakln << "\\begin{lstlisting}";
350 if (param_string.empty() && caption.str.empty())
356 if (!caption.str.empty()) {
357 os << "caption={" << move(caption) << '}';
358 if (!param_string.empty())
361 os << from_utf8(param_string) << "]\n";
363 os << code << breakln << "\\end{lstlisting}\n";
366 if (encoding_switched){
368 switchEncoding(os.os(), buffer().params(), runparams, *save_enc, true);
369 runparams.encoding = save_enc;
372 if (!uncodable.empty() && !runparams.silent) {
373 // issue a warning about omitted characters
374 // FIXME: should be passed to the error dialog
376 frontend::Alert::warning(_("Uncodable characters in listings inset"),
377 bformat(_("The following characters in one of the program listings are\n"
378 "not representable in the current encoding and have been omitted:\n%1$s.\n"
379 "This is due to a restriction of the listings package, which does\n"
380 "not support your encoding '%2$s'.\n"
381 "Toggling 'Use non-TeX fonts' in Document > Settings...\n"
383 uncodable, _(runparams.encoding->guiName())));
385 frontend::Alert::warning(_("Uncodable characters in listings inset"),
386 bformat(_("The following characters in one of the program listings are\n"
387 "not representable in the current encoding and have been omitted:\n%1$s."),
393 docstring InsetListings::xhtml(XHTMLStream & os, OutputParams const & rp) const
395 odocstringstream ods;
396 XHTMLStream out(ods);
398 bool const isInline = params().isInline();
400 out << html::CompTag("br");
402 out << html::StartTag("div", "class='float-listings'");
403 docstring caption = getCaptionHTML(rp);
404 if (!caption.empty())
405 out << html::StartTag("div", "class='listings-caption'")
406 << XHTMLStream::ESCAPE_NONE
407 << caption << html::EndTag("div");
410 InsetLayout const & il = getLayout();
411 string const & tag = il.htmltag();
412 string attr = "class ='listings";
413 string const lang = params().getParamValue("language");
417 out << html::StartTag(tag, attr);
418 OutputParams newrp = rp;
419 newrp.html_disable_captions = true;
420 // We don't want to convert dashes here. That's the only conversion we
421 // do for XHTML, so this is safe.
422 newrp.pass_thru = true;
423 docstring def = InsetText::insetAsXHTML(out, newrp, InsetText::JustText);
424 out << html::EndTag(tag);
427 out << html::CompTag("br");
428 // escaping will already have been done
429 os << XHTMLStream::ESCAPE_NONE << ods.str();
431 out << html::EndTag("div");
432 // In this case, this needs to be deferred, but we'll put it
433 // before anything the text itself deferred.
434 def = ods.str() + '\n' + def;
440 string InsetListings::contextMenuName() const
442 return "context-listings";
446 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
448 switch (cmd.action()) {
450 case LFUN_INSET_MODIFY: {
451 cur.recordUndoInset(this);
452 InsetListings::string2params(to_utf8(cmd.argument()), params());
456 case LFUN_INSET_DIALOG_UPDATE:
457 cur.bv().updateDialog("listings", params2string(params()));
461 InsetCaptionable::doDispatch(cur, cmd);
467 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
468 FuncStatus & status) const
470 switch (cmd.action()) {
471 case LFUN_INSET_MODIFY:
472 case LFUN_INSET_DIALOG_UPDATE:
473 status.setEnabled(true);
475 case LFUN_CAPTION_INSERT: {
476 // the inset outputs at most one caption
477 if (params().isInline() || getCaptionInset()) {
478 status.setEnabled(false);
484 return InsetCaptionable::getStatus(cur, cmd, status);
489 docstring const InsetListings::buttonLabel(BufferView const & bv) const
492 if (decoration() == InsetLayout::CLASSIC)
493 return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
495 return getNewLabel(_("Listing"));
499 void InsetListings::validate(LaTeXFeatures & features) const
501 features.useInsetLayout(getLayout());
502 string param_string = params().params();
503 if (buffer().params().use_minted) {
504 features.require("minted");
505 OutputParams rp = features.runparams();
506 if (!params().isFloat() && !getCaption(rp).str.empty())
507 features.require("lyxmintcaption");
508 if (features.usePolyglossia() && features.hasRTLLanguage())
509 // minted loads color, but color must be loaded before bidi
510 // (i.e., polyglossia)
511 features.require("color");
513 features.require("listings");
514 if (contains(param_string, "\\color"))
515 features.require("color");
517 InsetCaptionable::validate(features);
521 bool InsetListings::showInsetDialog(BufferView * bv) const
523 bv->showDialog("listings", params2string(params()),
524 const_cast<InsetListings *>(this));
529 TexString InsetListings::getCaption(OutputParams const & runparams) const
531 InsetCaption const * ins = getCaptionInset();
536 ins->getArgs(os, runparams);
537 ins->getArgument(os, runparams);
539 // TODO: The code below should be moved to support, and then the test
540 // in ../tests should be moved there as well.
542 // the caption may contain \label{} but the listings
543 // package prefer caption={}, label={}
544 TexString cap = os.release();
545 if (buffer().params().use_minted
546 || !contains(cap.str, from_ascii("\\label{")))
549 // blah1\label{blah2} blah3
551 // blah1 blah3},label={blah2
553 // caption={blah1 blah3},label={blah2}
555 // NOTE that } is not allowed in blah2.
556 regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
557 string const new_cap("$1$3},label={$2");
558 // TexString validity: the substitution preserves the number of newlines.
559 // Moreover we assume that $2 does not contain newlines, so that the texrow
560 // information remains accurate.
561 // Replace '\n' with an improbable character from Private Use Area-A
562 // and then return to '\n' after the regex replacement.
563 docstring const capstr = subst(cap.str, char_type('\n'), 0xffffd);
564 cap.str = subst(from_utf8(regex_replace(to_utf8(capstr), reg, new_cap)),
565 0xffffd, char_type('\n'));
570 void InsetListings::string2params(string const & in,
571 InsetListingsParams & params)
573 params = InsetListingsParams();
576 istringstream data(in);
579 // discard "listings", which is only used to determine inset
585 string InsetListings::params2string(InsetListingsParams const & params)
588 data << "listings" << ' ';