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 || (buffer().params().encoding().package() == Encoding::japanese
131 && inner_enc->package() == Encoding::japanese)
132 || inner_enc->hasFixedWidth())
135 // We try if there's a singlebyte encoding for the outer
136 // language; if not, fall back to latin1.
137 // Power-users can set inputenc to utf8-plain to bypass this workaround
138 // and provide alternatives in the user-preamble.
139 return (outer_enc->hasFixedWidth()) ?
140 outer_enc : encodings.fromLyXName("iso8859-1");
144 void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
146 string param_string = params().params();
147 // NOTE: I use {} to quote text, which is an experimental feature
148 // of the listings package (see page 25 of the manual)
149 bool const isInline = params().isInline();
150 bool const use_minted = buffer().params().use_minted;
151 string minted_language;
152 string float_placement;
153 bool const isfloat = params().isFloat();
154 if (use_minted && (isfloat || contains(param_string, "language="))) {
155 // Get float placement and/or language of the code,
156 // then remove the relative options.
157 vector<string> opts =
158 getVectorFromString(param_string, ",", false);
159 for (size_t i = 0; i < opts.size(); ++i) {
160 if (prefixIs(opts[i], "float")) {
161 if (prefixIs(opts[i], "float="))
162 float_placement = opts[i].substr(6);
163 opts.erase(opts.begin() + i--);
165 else if (prefixIs(opts[i], "language=")) {
166 minted_language = opts[i].substr(9);
167 opts.erase(opts.begin() + i--);
170 param_string = getStringFromVector(opts, ",");
172 // Minted needs a language specification
173 if (minted_language.empty()) {
174 // If a language has been set globally, use that,
175 // otherwise use TeX by default
176 string const & blp = buffer().params().listings_params;
177 size_t start = blp.find("language=");
178 if (start != string::npos) {
179 start += strlen("language=");
180 size_t len = blp.find(",", start);
181 if (len != string::npos)
183 minted_language = blp.substr(start, len);
185 minted_language = "TeX";
188 // get the paragraphs. We can not output them directly to given odocstream
189 // because we can not yet determine the delimiter character of \lstinline
192 ParagraphList::const_iterator par = paragraphs().begin();
193 ParagraphList::const_iterator end = paragraphs().end();
195 bool encoding_switched = false;
196 Encoding const * const save_enc = runparams.encoding;
198 Encoding const * const outer_encoding =
199 (runparams.local_font != 0) ?
200 runparams.local_font->language()->encoding()
201 : buffer().params().language->encoding();
202 Encoding const * fixedlstenc = forcedEncoding(runparams.encoding, outer_encoding);
204 // We need to switch to a singlebyte encoding, due to
205 // the restrictions of the listings package (see above).
206 // This needs to be consistent with
207 // LaTeXFeatures::getTClassI18nPreamble().
208 // We need to put this into a group in order to prevent encoding leaks
209 // (happens with cprotect).
211 switchEncoding(os.os(), buffer().params(), runparams, *fixedlstenc, true);
212 runparams.encoding = fixedlstenc;
213 encoding_switched = true;
216 bool const captionfirst = !isfloat && par->isInset(0)
217 && par->getInset(0)->lyxCode() == CAPTION_CODE;
220 pos_type const siz = par->size();
221 bool captionline = false;
222 for (pos_type i = 0; i < siz; ++i) {
223 if (i == 0 && par->isInset(i) && i + 1 == siz)
225 // ignore all struck out text and (caption) insets
226 if (par->isDeleted(i)
227 || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
229 if (par->isInset(i)) {
230 // Currently, this can only be a quote inset
231 // that is output as plain quote here, but
232 // we use more generic code anyway.
233 otexstringstream ots;
234 OutputParams rp = runparams;
236 par->getInset(i)->latex(ots, rp);
240 char_type c = par->getChar(i);
241 // we can only output characters covered by the current
244 if (runparams.encoding->encodable(c))
246 else if (runparams.dryrun) {
247 code += "<" + _("LyX Warning: ")
248 + _("uncodable character") + " '";
249 code += docstring(1, c);
253 } catch (EncodingException & /* e */) {
254 if (runparams.dryrun) {
255 code += "<" + _("LyX Warning: ")
256 + _("uncodable character") + " '";
257 code += docstring(1, c);
264 // for the inline case, if there are multiple paragraphs
265 // they are simply joined. Otherwise, expect latex errors.
266 if (par != end && !isInline && !captionline)
270 static const docstring delimiters =
271 from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
273 size_t pos = delimiters.find_first_not_of(code);
275 // This code piece contains all possible special character? !!!
276 // Replace ! with a warning message and use ! as delimiter.
277 if (pos == string::npos) {
278 docstring delim_error = "<" + _("LyX Warning: ")
279 + _("no more lstline delimiters available") + ">";
280 code = subst(code, from_ascii("!"), delim_error);
282 if (!runparams.dryrun && !runparams.silent) {
283 // FIXME: warning should be passed to the error dialog
284 frontend::Alert::warning(_("Running out of delimiters"),
285 _("For inline program listings, one character must be reserved\n"
286 "as a delimiter. One of the listings, however, uses all available\n"
287 "characters, so none is left for delimiting purposes.\n"
288 "For the time being, I have replaced '!' by a warning, but you\n"
289 "must investigate!"));
292 docstring const delim(1, delimiters[pos]);
294 os << "\\mintinline";
295 if (!param_string.empty())
296 os << "[" << from_utf8(param_string) << "]";
297 os << "{" << ascii_lowercase(minted_language) << "}";
300 if (!param_string.empty())
301 os << "[" << from_utf8(param_string) << "]";
302 else if (pos >= delimiters.find('Q'))
303 // We need to terminate the command before
307 os << delim << code << delim;
308 } else if (use_minted) {
309 OutputParams rp = runparams;
310 rp.moving_arg = true;
311 TexString caption = getCaption(rp);
313 os << breakln << "\\begin{listing}";
314 if (!float_placement.empty())
315 os << '[' << float_placement << "]";
316 } else if (captionfirst && !caption.str.empty()) {
317 os << breakln << "\\lyxmintcaption[t]{"
318 << move(caption) << "}\n";
320 os << breakln << "\\begin{minted}";
321 if (!param_string.empty())
322 os << "[" << param_string << "]";
323 os << "{" << ascii_lowercase(minted_language) << "}\n"
324 << code << breakln << "\\end{minted}\n";
326 if (!caption.str.empty())
327 os << "\\caption{" << move(caption) << "}\n";
328 os << "\\end{listing}\n";
329 } else if (!captionfirst && !caption.str.empty()) {
330 os << breakln << "\\lyxmintcaption[b]{"
331 << move(caption) << "}";
334 OutputParams rp = runparams;
335 rp.moving_arg = true;
336 TexString caption = getCaption(rp);
337 os << breakln << "\\begin{lstlisting}";
338 if (param_string.empty() && caption.str.empty())
344 if (!caption.str.empty()) {
345 os << "caption={" << move(caption) << '}';
346 if (!param_string.empty())
349 os << from_utf8(param_string) << "]\n";
351 os << code << breakln << "\\end{lstlisting}\n";
354 if (encoding_switched){
356 switchEncoding(os.os(), buffer().params(),
357 runparams, *save_enc, true, true);
358 os << "\\egroup" << breakln;
359 runparams.encoding = save_enc;
362 if (!uncodable.empty() && !runparams.silent) {
363 // issue a warning about omitted characters
364 // FIXME: should be passed to the error dialog
366 frontend::Alert::warning(_("Uncodable characters in listings inset"),
367 bformat(_("The following characters in one of the program listings are\n"
368 "not representable in the current encoding and have been omitted:\n%1$s.\n"
369 "This is due to a restriction of the listings package, which does\n"
370 "not support your encoding '%2$s'.\n"
371 "Toggling 'Use non-TeX fonts' in Document > Settings...\n"
373 uncodable, _(runparams.encoding->guiName())));
375 frontend::Alert::warning(_("Uncodable characters in listings inset"),
376 bformat(_("The following characters in one of the program listings are\n"
377 "not representable in the current encoding and have been omitted:\n%1$s."),
383 docstring InsetListings::xhtml(XHTMLStream & os, OutputParams const & rp) const
385 odocstringstream ods;
386 XHTMLStream out(ods);
388 bool const isInline = params().isInline();
390 out << html::CompTag("br");
392 out << html::StartTag("div", "class='float-listings'");
393 docstring caption = getCaptionHTML(rp);
394 if (!caption.empty())
395 out << html::StartTag("div", "class='listings-caption'")
396 << XHTMLStream::ESCAPE_NONE
397 << caption << html::EndTag("div");
400 InsetLayout const & il = getLayout();
401 string const & tag = il.htmltag();
402 string attr = "class ='listings";
403 string const lang = params().getParamValue("language");
407 out << html::StartTag(tag, attr);
408 OutputParams newrp = rp;
409 newrp.html_disable_captions = true;
410 // We don't want to convert dashes here. That's the only conversion we
411 // do for XHTML, so this is safe.
412 newrp.pass_thru = true;
413 docstring def = InsetText::insetAsXHTML(out, newrp, InsetText::JustText);
414 out << html::EndTag(tag);
417 out << html::CompTag("br");
418 // escaping will already have been done
419 os << XHTMLStream::ESCAPE_NONE << ods.str();
421 out << html::EndTag("div");
422 // In this case, this needs to be deferred, but we'll put it
423 // before anything the text itself deferred.
424 def = ods.str() + '\n' + def;
430 string InsetListings::contextMenuName() const
432 return "context-listings";
436 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
438 switch (cmd.action()) {
440 case LFUN_INSET_MODIFY: {
441 cur.recordUndoInset(this);
442 InsetListings::string2params(to_utf8(cmd.argument()), params());
446 case LFUN_INSET_DIALOG_UPDATE:
447 cur.bv().updateDialog("listings", params2string(params()));
451 InsetCaptionable::doDispatch(cur, cmd);
457 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
458 FuncStatus & status) const
460 switch (cmd.action()) {
461 case LFUN_INSET_MODIFY:
462 case LFUN_INSET_DIALOG_UPDATE:
463 status.setEnabled(true);
465 case LFUN_CAPTION_INSERT: {
466 // the inset outputs at most one caption
467 if (params().isInline() || getCaptionInset()) {
468 status.setEnabled(false);
474 return InsetCaptionable::getStatus(cur, cmd, status);
479 docstring const InsetListings::buttonLabel(BufferView const & bv) const
482 if (decoration() == InsetLayout::CLASSIC)
483 return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
485 return getNewLabel(_("Listing"));
489 void InsetListings::validate(LaTeXFeatures & features) const
491 features.useInsetLayout(getLayout());
492 string param_string = params().params();
493 if (buffer().params().use_minted) {
494 features.require("minted");
495 OutputParams rp = features.runparams();
496 if (!params().isFloat() && !getCaption(rp).str.empty())
497 features.require("lyxmintcaption");
499 features.require("listings");
500 if (contains(param_string, "\\color"))
501 features.require("color");
503 InsetCaptionable::validate(features);
507 bool InsetListings::showInsetDialog(BufferView * bv) const
509 bv->showDialog("listings", params2string(params()),
510 const_cast<InsetListings *>(this));
515 TexString InsetListings::getCaption(OutputParams const & runparams) const
517 InsetCaption const * ins = getCaptionInset();
522 ins->getArgs(os, runparams);
523 ins->getArgument(os, runparams);
525 // TODO: The code below should be moved to support, and then the test
526 // in ../tests should be moved there as well.
528 // the caption may contain \label{} but the listings
529 // package prefer caption={}, label={}
530 TexString cap = os.release();
531 if (buffer().params().use_minted
532 || !contains(cap.str, from_ascii("\\label{")))
535 // blah1\label{blah2} blah3
537 // blah1 blah3},label={blah2
539 // caption={blah1 blah3},label={blah2}
541 // NOTE that } is not allowed in blah2.
542 regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
543 string const new_cap("$1$3},label={$2");
544 // Remove potential \protect'ion of \label.
545 docstring capstr = subst(cap.str, from_ascii("\\protect\\label"),
546 from_ascii("\\label"));
547 // TexString validity: the substitution preserves the number of newlines.
548 // Moreover we assume that $2 does not contain newlines, so that the texrow
549 // information remains accurate.
550 // Replace '\n' with an improbable character from Private Use Area-A
551 // and then return to '\n' after the regex replacement.
552 capstr = subst(capstr, char_type('\n'), 0xffffd);
553 cap.str = subst(from_utf8(regex_replace(to_utf8(capstr), reg, new_cap)),
554 0xffffd, char_type('\n'));
559 void InsetListings::string2params(string const & in,
560 InsetListingsParams & params)
562 params = InsetListingsParams();
565 istringstream data(in);
568 // discard "listings", which is only used to determine inset
574 string InsetListings::params2string(InsetListingsParams const & params)
577 data << "listings" << ' ';