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 void InsetListings::write(ostream & os) const
77 os << "listings" << "\n";
78 InsetListingsParams const & par = params();
79 // parameter string is encoded to be a valid lyx token.
80 string opt = par.encodedString();
82 os << "lstparams \"" << opt << "\"\n";
84 os << "inline true\n";
86 os << "inline false\n";
87 InsetCaptionable::write(os);
91 void InsetListings::read(Lexer & lex)
95 string token = lex.getString();
96 if (token == "lstparams") {
98 string const value = lex.getString();
99 params().fromEncodedString(value);
100 } else if (token == "inline") {
102 params().setInline(lex.getBool());
104 // no special option, push back 'status' etc
105 lex.pushToken(token);
109 InsetCaptionable::read(lex);
113 void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
115 string param_string = params().params();
116 // NOTE: I use {} to quote text, which is an experimental feature
117 // of the listings package (see page 25 of the manual)
118 bool const isInline = params().isInline();
119 bool const use_minted = buffer().params().use_minted;
120 string minted_language;
121 string float_placement;
122 bool const isfloat = params().isFloat();
123 if (use_minted && (isfloat || contains(param_string, "language="))) {
124 // Get float placement and/or language of the code,
125 // then remove the relative options.
126 vector<string> opts =
127 getVectorFromString(param_string, ",", false);
128 for (size_t i = 0; i < opts.size(); ++i) {
129 if (prefixIs(opts[i], "float")) {
130 if (prefixIs(opts[i], "float="))
131 float_placement = opts[i].substr(6);
132 opts.erase(opts.begin() + i--);
134 else if (prefixIs(opts[i], "language=")) {
135 minted_language = opts[i].substr(9);
136 opts.erase(opts.begin() + i--);
139 param_string = getStringFromVector(opts, ",");
141 // Minted needs a language specification
142 if (minted_language.empty())
143 minted_language = "TeX";
145 // get the paragraphs. We can not output them directly to given odocstream
146 // because we can not yet determine the delimiter character of \lstinline
149 ParagraphList::const_iterator par = paragraphs().begin();
150 ParagraphList::const_iterator end = paragraphs().end();
152 bool encoding_switched = false;
153 Encoding const * const save_enc = runparams.encoding;
154 // The listings package cannot deal with multi-byte-encoded
155 // glyphs, except if full-unicode aware backends
156 // such as XeTeX or LuaTeX are used, and with pLaTeX.
157 bool const multibyte_possible = use_minted || runparams.isFullUnicode()
158 || (buffer().params().encoding().package() == Encoding::japanese
159 && runparams.encoding->package() == Encoding::japanese);
161 if (!multibyte_possible && !runparams.encoding->hasFixedWidth()) {
162 // We need to switch to a singlebyte encoding, due to
163 // the restrictions of the listings package (see above).
164 // This needs to be consistent with
165 // LaTeXFeatures::getTClassI18nPreamble().
166 Language const * const outer_language =
167 (runparams.local_font != 0) ?
168 runparams.local_font->language()
169 : buffer().params().language;
170 // We try if there's a singlebyte encoding for the current
171 // language; if not, fall back to latin1.
172 Encoding const * const lstenc =
173 (outer_language->encoding()->hasFixedWidth()) ?
174 outer_language->encoding()
175 : encodings.fromLyXName("iso8859-1");
176 switchEncoding(os.os(), buffer().params(), runparams, *lstenc, true);
177 runparams.encoding = lstenc;
178 encoding_switched = true;
182 pos_type siz = par->size();
183 bool captionline = false;
184 for (pos_type i = 0; i < siz; ++i) {
185 if (i == 0 && par->isInset(i) && i + 1 == siz)
187 // ignore all struck out text and (caption) insets
188 if (par->isDeleted(i)
189 || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
191 if (par->isInset(i)) {
192 // Currently, this can only be a quote inset
193 // that is output as plain quote here, but
194 // we use more generic code anyway.
195 otexstringstream ots;
196 OutputParams rp = runparams;
198 par->getInset(i)->latex(ots, rp);
202 char_type c = par->getChar(i);
203 // we can only output characters covered by the current
206 if (runparams.encoding->encodable(c))
208 else if (runparams.dryrun) {
209 code += "<" + _("LyX Warning: ")
210 + _("uncodable character") + " '";
211 code += docstring(1, c);
215 } catch (EncodingException & /* e */) {
216 if (runparams.dryrun) {
217 code += "<" + _("LyX Warning: ")
218 + _("uncodable character") + " '";
219 code += docstring(1, c);
226 // for the inline case, if there are multiple paragraphs
227 // they are simply joined. Otherwise, expect latex errors.
228 if (par != end && !isInline && !captionline)
232 static const docstring delimiters =
233 from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
235 size_t pos = delimiters.find_first_not_of(code);
237 // This code piece contains all possible special character? !!!
238 // Replace ! with a warning message and use ! as delimiter.
239 if (pos == string::npos) {
240 docstring delim_error = "<" + _("LyX Warning: ")
241 + _("no more lstline delimiters available") + ">";
242 code = subst(code, from_ascii("!"), delim_error);
244 if (!runparams.dryrun && !runparams.silent) {
245 // FIXME: warning should be passed to the error dialog
246 frontend::Alert::warning(_("Running out of delimiters"),
247 _("For inline program listings, one character must be reserved\n"
248 "as a delimiter. One of the listings, however, uses all available\n"
249 "characters, so none is left for delimiting purposes.\n"
250 "For the time being, I have replaced '!' by a warning, but you\n"
251 "must investigate!"));
254 docstring const delim(1, delimiters[pos]);
256 os << "\\mintinline";
257 if (!param_string.empty())
258 os << "[" << from_utf8(param_string) << "]";
259 os << "{" << minted_language << "}";
262 if (!param_string.empty())
263 os << "[" << from_utf8(param_string) << "]";
264 else if (pos >= delimiters.find('Q'))
265 // We need to terminate the command before
269 os << delim << code << delim;
270 } else if (use_minted) {
272 os << breakln << "\\begin{listing}";
273 if (!float_placement.empty())
274 os << '[' << float_placement << "]";
276 os << breakln << "\\begin{minted}";
277 if (!param_string.empty())
278 os << "[" << param_string << "]";
279 os << "{" << minted_language << "}\n"
280 << code << breakln << "\\end{minted}\n";
282 OutputParams rp = runparams;
283 rp.moving_arg = true;
284 TexString caption = getCaption(rp);
285 if (!caption.str.empty())
286 os << "\\caption{" << move(caption) << "}\n";
287 os << "\\end{listing}\n";
290 OutputParams rp = runparams;
291 rp.moving_arg = true;
292 TexString caption = getCaption(rp);
293 os << breakln << "\\begin{lstlisting}";
294 if (param_string.empty() && caption.str.empty())
300 if (!caption.str.empty()) {
301 os << "caption={" << move(caption) << '}';
302 if (!param_string.empty())
305 os << from_utf8(param_string) << "]\n";
307 os << code << breakln << "\\end{lstlisting}\n";
310 if (encoding_switched){
312 switchEncoding(os.os(), buffer().params(), runparams, *save_enc, true);
313 runparams.encoding = save_enc;
316 if (!uncodable.empty() && !runparams.silent) {
317 // issue a warning about omitted characters
318 // FIXME: should be passed to the error dialog
319 if (!multibyte_possible && !runparams.encoding->hasFixedWidth())
320 frontend::Alert::warning(_("Uncodable characters in listings inset"),
321 bformat(_("The following characters in one of the program listings are\n"
322 "not representable in the current encoding and have been omitted:\n%1$s.\n"
323 "This is due to a restriction of the listings package, which does\n"
324 "not support your encoding '%2$s'.\n"
325 "Toggling 'Use non-TeX fonts' in Document > Settings...\n"
327 uncodable, _(runparams.encoding->guiName())));
329 frontend::Alert::warning(_("Uncodable characters in listings inset"),
330 bformat(_("The following characters in one of the program listings are\n"
331 "not representable in the current encoding and have been omitted:\n%1$s."),
337 docstring InsetListings::xhtml(XHTMLStream & os, OutputParams const & rp) const
339 odocstringstream ods;
340 XHTMLStream out(ods);
342 bool const isInline = params().isInline();
344 out << html::CompTag("br");
346 out << html::StartTag("div", "class='float-listings'");
347 docstring caption = getCaptionHTML(rp);
348 if (!caption.empty())
349 out << html::StartTag("div", "class='listings-caption'")
350 << XHTMLStream::ESCAPE_NONE
351 << caption << html::EndTag("div");
354 InsetLayout const & il = getLayout();
355 string const & tag = il.htmltag();
356 string attr = "class ='listings";
357 string const lang = params().getParamValue("language");
361 out << html::StartTag(tag, attr);
362 OutputParams newrp = rp;
363 newrp.html_disable_captions = true;
364 // We don't want to convert dashes here. That's the only conversion we
365 // do for XHTML, so this is safe.
366 newrp.pass_thru = true;
367 docstring def = InsetText::insetAsXHTML(out, newrp, InsetText::JustText);
368 out << html::EndTag(tag);
371 out << html::CompTag("br");
372 // escaping will already have been done
373 os << XHTMLStream::ESCAPE_NONE << ods.str();
375 out << html::EndTag("div");
376 // In this case, this needs to be deferred, but we'll put it
377 // before anything the text itself deferred.
378 def = ods.str() + '\n' + def;
384 string InsetListings::contextMenuName() const
386 return "context-listings";
390 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
392 switch (cmd.action()) {
394 case LFUN_INSET_MODIFY: {
395 cur.recordUndoInset(this);
396 InsetListings::string2params(to_utf8(cmd.argument()), params());
400 case LFUN_INSET_DIALOG_UPDATE:
401 cur.bv().updateDialog("listings", params2string(params()));
405 InsetCaptionable::doDispatch(cur, cmd);
411 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
412 FuncStatus & status) const
414 switch (cmd.action()) {
415 case LFUN_INSET_MODIFY:
416 case LFUN_INSET_DIALOG_UPDATE:
417 status.setEnabled(true);
419 case LFUN_CAPTION_INSERT: {
420 // the inset outputs at most one caption
421 if (params().isInline() || getCaptionInset()) {
422 status.setEnabled(false);
427 return InsetCaptionable::getStatus(cur, cmd, status);
432 docstring const InsetListings::buttonLabel(BufferView const & bv) const
435 if (decoration() == InsetLayout::CLASSIC)
436 return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
438 return getNewLabel(_("Listing"));
442 void InsetListings::validate(LaTeXFeatures & features) const
444 if (buffer().params().use_minted)
445 features.require("minted");
447 features.require("listings");
448 features.useInsetLayout(getLayout());
449 string param_string = params().params();
450 if (param_string.find("\\color") != string::npos)
451 features.require("color");
452 InsetCaptionable::validate(features);
456 bool InsetListings::showInsetDialog(BufferView * bv) const
458 bv->showDialog("listings", params2string(params()),
459 const_cast<InsetListings *>(this));
464 TexString InsetListings::getCaption(OutputParams const & runparams) const
466 InsetCaption const * ins = getCaptionInset();
471 ins->getArgs(os, runparams);
472 ins->getArgument(os, runparams);
474 // TODO: The code below should be moved to support, and then the test
475 // in ../tests should be moved there as well.
477 // the caption may contain \label{} but the listings
478 // package prefer caption={}, label={}
479 TexString cap = os.release();
480 if (buffer().params().use_minted
481 || !contains(cap.str, from_ascii("\\label{")))
484 // blah1\label{blah2} blah3
486 // blah1 blah3},label={blah2
488 // caption={blah1 blah3},label={blah2}
490 // NOTE that } is not allowed in blah2.
491 regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
492 string const new_cap("$1$3},label={$2");
493 // TexString validity: the substitution preserves the number of newlines.
494 // Moreover we assume that $2 does not contain newlines, so that the texrow
495 // information remains accurate.
496 cap.str = from_utf8(regex_replace(to_utf8(cap.str), reg, new_cap));
501 void InsetListings::string2params(string const & in,
502 InsetListingsParams & params)
504 params = InsetListingsParams();
507 istringstream data(in);
510 // discard "listings", which is only used to determine inset
516 string InsetListings::params2string(InsetListingsParams const & params)
519 data << "listings" << ' ';