]> git.lyx.org Git - features.git/blob - src/insets/InsetListings.cpp
7be13bcfbdc211868be1af509b01f6686a410ec0
[features.git] / src / insets / InsetListings.cpp
1 /**
2  * \file InsetListings.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetListings.h"
15
16 #include "Buffer.h"
17 #include "BufferView.h"
18 #include "BufferParams.h"
19 #include "Counters.h"
20 #include "Cursor.h"
21 #include "DispatchResult.h"
22 #include "Encoding.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetCaption.h"
26 #include "Language.h"
27 #include "LaTeXFeatures.h"
28 #include "Lexer.h"
29 #include "output_latex.h"
30 #include "output_xhtml.h"
31 #include "OutputParams.h"
32 #include "TextClass.h"
33 #include "TexRow.h"
34 #include "texstream.h"
35
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"
41
42 #include "frontends/alert.h"
43 #include "frontends/Application.h"
44
45 #include "support/regex.h"
46
47 #include <sstream>
48
49 using namespace std;
50 using namespace lyx::support;
51
52 namespace lyx {
53
54
55 InsetListings::InsetListings(Buffer * buf, InsetListingsParams const & par)
56         : InsetCaptionable(buf,"listing")
57 {
58         params_.setMinted(buffer().params().use_minted);
59         status_ = par.status();
60 }
61
62
63 InsetListings::~InsetListings()
64 {
65         hideDialogs("listings", this);
66 }
67
68
69 Inset::DisplayType InsetListings::display() const
70 {
71         return params().isInline() || params().isFloat() ? Inline : AlignLeft;
72 }
73
74
75 docstring InsetListings::layoutName() const
76 {
77         if (buffer().params().use_minted)
78                 return from_ascii("MintedListings");
79         else
80                 return from_ascii("Listings");
81 }
82
83
84 void InsetListings::write(ostream & os) const
85 {
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();
90         if (!opt.empty())
91                 os << "lstparams \"" << opt << "\"\n";
92         if (par.isInline())
93                 os << "inline true\n";
94         else
95                 os << "inline false\n";
96         InsetCaptionable::write(os);
97 }
98
99
100 void InsetListings::read(Lexer & lex)
101 {
102         while (lex.isOK()) {
103                 lex.next();
104                 string token = lex.getString();
105                 if (token == "lstparams") {
106                         lex.next();
107                         string const value = lex.getString();
108                         params().fromEncodedString(value);
109                 } else if (token == "inline") {
110                         lex.next();
111                         params().setInline(lex.getBool());
112                 } else {
113                         // no special option, push back 'status' etc
114                         lex.pushToken(token);
115                         break;
116                 }
117         }
118         InsetCaptionable::read(lex);
119 }
120
121
122 void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
123 {
124         string param_string = params().params();
125         // NOTE: I use {} to quote text, which is an experimental feature
126         // of the listings package (see page 25 of the manual)
127         bool const isInline = params().isInline();
128         bool const use_minted = buffer().params().use_minted;
129         string minted_language;
130         string float_placement;
131         bool const isfloat = params().isFloat();
132         if (use_minted && (isfloat || contains(param_string, "language="))) {
133                 // Get float placement and/or language of the code,
134                 // then remove the relative options.
135                 vector<string> opts =
136                         getVectorFromString(param_string, ",", false);
137                 for (size_t i = 0; i < opts.size(); ++i) {
138                         if (prefixIs(opts[i], "float")) {
139                                 if (prefixIs(opts[i], "float="))
140                                         float_placement = opts[i].substr(6);
141                                 opts.erase(opts.begin() + i--);
142                         }
143                         else if (prefixIs(opts[i], "language=")) {
144                                 minted_language = opts[i].substr(9);
145                                 opts.erase(opts.begin() + i--);
146                         }
147                 }
148                 param_string = getStringFromVector(opts, ",");
149         }
150         // Minted needs a language specification
151         if (minted_language.empty())
152                 minted_language = "TeX";
153
154         // get the paragraphs. We can not output them directly to given odocstream
155         // because we can not yet determine the delimiter character of \lstinline
156         docstring code;
157         docstring uncodable;
158         ParagraphList::const_iterator par = paragraphs().begin();
159         ParagraphList::const_iterator end = paragraphs().end();
160
161         bool encoding_switched = false;
162         Encoding const * const save_enc = runparams.encoding;
163         // The listings package cannot deal with multi-byte-encoded
164         // glyphs, except if full-unicode aware backends
165         // such as XeTeX or LuaTeX are used, and with pLaTeX.
166         bool const multibyte_possible = use_minted || runparams.isFullUnicode()
167             || (buffer().params().encoding().package() == Encoding::japanese
168                 && runparams.encoding->package() == Encoding::japanese);
169
170         if (!multibyte_possible && !runparams.encoding->hasFixedWidth()) {
171                 // We need to switch to a singlebyte encoding, due to
172                 // the restrictions of the listings package (see above).
173                 // This needs to be consistent with
174                 // LaTeXFeatures::getTClassI18nPreamble().
175                 Language const * const outer_language =
176                         (runparams.local_font != 0) ?
177                                 runparams.local_font->language()
178                                 : buffer().params().language;
179                 // We try if there's a singlebyte encoding for the current
180                 // language; if not, fall back to latin1.
181                 Encoding const * const lstenc =
182                         (outer_language->encoding()->hasFixedWidth()) ?
183                                 outer_language->encoding()
184                                 : encodings.fromLyXName("iso8859-1");
185                 switchEncoding(os.os(), buffer().params(), runparams, *lstenc, true);
186                 runparams.encoding = lstenc;
187                 encoding_switched = true;
188         }
189
190         while (par != end) {
191                 pos_type siz = par->size();
192                 bool captionline = false;
193                 for (pos_type i = 0; i < siz; ++i) {
194                         if (i == 0 && par->isInset(i) && i + 1 == siz)
195                                 captionline = true;
196                         // ignore all struck out text and (caption) insets
197                         if (par->isDeleted(i)
198                             || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
199                                 continue;
200                         if (par->isInset(i)) {
201                                 // Currently, this can only be a quote inset
202                                 // that is output as plain quote here, but
203                                 // we use more generic code anyway.
204                                 otexstringstream ots;
205                                 OutputParams rp = runparams;
206                                 rp.pass_thru = true;
207                                 par->getInset(i)->latex(ots, rp);
208                                 code += ots.str();
209                                 continue;
210                         }
211                         char_type c = par->getChar(i);
212                         // we can only output characters covered by the current
213                         // encoding!
214                         try {
215                                 if (runparams.encoding->encodable(c))
216                                         code += c;
217                                 else if (runparams.dryrun) {
218                                         code += "<" + _("LyX Warning: ")
219                                            + _("uncodable character") + " '";
220                                         code += docstring(1, c);
221                                         code += "'>";
222                                 } else
223                                         uncodable += c;
224                         } catch (EncodingException & /* e */) {
225                                 if (runparams.dryrun) {
226                                         code += "<" + _("LyX Warning: ")
227                                            + _("uncodable character") + " '";
228                                         code += docstring(1, c);
229                                         code += "'>";
230                                 } else
231                                         uncodable += c;
232                         }
233                 }
234                 ++par;
235                 // for the inline case, if there are multiple paragraphs
236                 // they are simply joined. Otherwise, expect latex errors.
237                 if (par != end && !isInline && !captionline)
238                         code += "\n";
239         }
240         if (isInline) {
241                 static const docstring delimiters =
242                                 from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
243
244                 size_t pos = delimiters.find_first_not_of(code);
245
246                 // This code piece contains all possible special character? !!!
247                 // Replace ! with a warning message and use ! as delimiter.
248                 if (pos == string::npos) {
249                         docstring delim_error = "<" + _("LyX Warning: ")
250                                 + _("no more lstline delimiters available") + ">";
251                         code = subst(code, from_ascii("!"), delim_error);
252                         pos = 0;
253                         if (!runparams.dryrun && !runparams.silent) {
254                                 // FIXME: warning should be passed to the error dialog
255                                 frontend::Alert::warning(_("Running out of delimiters"),
256                                 _("For inline program listings, one character must be reserved\n"
257                                   "as a delimiter. One of the listings, however, uses all available\n"
258                                   "characters, so none is left for delimiting purposes.\n"
259                                   "For the time being, I have replaced '!' by a warning, but you\n"
260                                   "must investigate!"));
261                         }
262                 }
263                 docstring const delim(1, delimiters[pos]);
264                 if (use_minted) {
265                         os << "\\mintinline";
266                         if (!param_string.empty())
267                                 os << "[" << from_utf8(param_string) << "]";
268                         os << "{" << minted_language << "}";
269                 } else {
270                         os << "\\lstinline";
271                         if (!param_string.empty())
272                                 os << "[" << from_utf8(param_string) << "]";
273                         else if (pos >= delimiters.find('Q'))
274                                 // We need to terminate the command before
275                                 // the delimiter
276                                 os << " ";
277                 }
278                 os << delim << code << delim;
279         } else if (use_minted) {
280                 if (isfloat) {
281                         os << breakln << "\\begin{listing}";
282                         if (!float_placement.empty())
283                                 os << '[' << float_placement << "]";
284                 }
285                 os << breakln << "\\begin{minted}";
286                 if (!param_string.empty())
287                         os << "[" << param_string << "]";
288                 os << "{" << minted_language << "}\n"
289                    << code << breakln << "\\end{minted}\n";
290                 if (isfloat) {
291                         OutputParams rp = runparams;
292                         rp.moving_arg = true;
293                         TexString caption = getCaption(rp);
294                         if (!caption.str.empty())
295                                 os << "\\caption{" << move(caption) << "}\n";
296                         os << "\\end{listing}\n";
297                 }
298         } else {
299                 OutputParams rp = runparams;
300                 rp.moving_arg = true;
301                 TexString caption = getCaption(rp);
302                 os << breakln << "\\begin{lstlisting}";
303                 if (param_string.empty() && caption.str.empty())
304                         os << "\n";
305                 else {
306                         if (!runparams.nice)
307                                 os << safebreakln;
308                         os << "[";
309                         if (!caption.str.empty()) {
310                                 os << "caption={" << move(caption) << '}';
311                                 if (!param_string.empty())
312                                         os << ',';
313                         }
314                         os << from_utf8(param_string) << "]\n";
315                 }
316                 os << code << breakln << "\\end{lstlisting}\n";
317         }
318
319         if (encoding_switched){
320                 // Switch back
321                 switchEncoding(os.os(), buffer().params(), runparams, *save_enc, true);
322                 runparams.encoding = save_enc;
323         }
324
325         if (!uncodable.empty() && !runparams.silent) {
326                 // issue a warning about omitted characters
327                 // FIXME: should be passed to the error dialog
328                 if (!multibyte_possible && !runparams.encoding->hasFixedWidth())
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.\n"
332                                           "This is due to a restriction of the listings package, which does\n"
333                                           "not support your encoding '%2$s'.\n"
334                                           "Toggling 'Use non-TeX fonts' in Document > Settings...\n"
335                                           "might help."),
336                                 uncodable, _(runparams.encoding->guiName())));
337                 else
338                         frontend::Alert::warning(_("Uncodable characters in listings inset"),
339                                 bformat(_("The following characters in one of the program listings are\n"
340                                           "not representable in the current encoding and have been omitted:\n%1$s."),
341                                 uncodable));
342         }
343 }
344
345
346 docstring InsetListings::xhtml(XHTMLStream & os, OutputParams const & rp) const
347 {
348         odocstringstream ods;
349         XHTMLStream out(ods);
350
351         bool const isInline = params().isInline();
352         if (isInline)
353                 out << html::CompTag("br");
354         else {
355                 out << html::StartTag("div", "class='float-listings'");
356                 docstring caption = getCaptionHTML(rp);
357                 if (!caption.empty())
358                         out << html::StartTag("div", "class='listings-caption'")
359                             << XHTMLStream::ESCAPE_NONE
360                             << caption << html::EndTag("div");
361         }
362
363         InsetLayout const & il = getLayout();
364         string const & tag = il.htmltag();
365         string attr = "class ='listings";
366         string const lang = params().getParamValue("language");
367         if (!lang.empty())
368                 attr += " " + lang;
369         attr += "'";
370         out << html::StartTag(tag, attr);
371         OutputParams newrp = rp;
372         newrp.html_disable_captions = true;
373         // We don't want to convert dashes here. That's the only conversion we
374         // do for XHTML, so this is safe.
375         newrp.pass_thru = true;
376         docstring def = InsetText::insetAsXHTML(out, newrp, InsetText::JustText);
377         out << html::EndTag(tag);
378
379         if (isInline) {
380                 out << html::CompTag("br");
381                 // escaping will already have been done
382                 os << XHTMLStream::ESCAPE_NONE << ods.str();
383         } else {
384                 out << html::EndTag("div");
385                 // In this case, this needs to be deferred, but we'll put it
386                 // before anything the text itself deferred.
387                 def = ods.str() + '\n' + def;
388         }
389         return def;
390 }
391
392
393 string InsetListings::contextMenuName() const
394 {
395         return "context-listings";
396 }
397
398
399 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
400 {
401         switch (cmd.action()) {
402
403         case LFUN_INSET_MODIFY: {
404                 cur.recordUndoInset(this);
405                 InsetListings::string2params(to_utf8(cmd.argument()), params());
406                 break;
407         }
408
409         case LFUN_INSET_DIALOG_UPDATE:
410                 cur.bv().updateDialog("listings", params2string(params()));
411                 break;
412
413         default:
414                 InsetCaptionable::doDispatch(cur, cmd);
415                 break;
416         }
417 }
418
419
420 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
421         FuncStatus & status) const
422 {
423         switch (cmd.action()) {
424                 case LFUN_INSET_MODIFY:
425                 case LFUN_INSET_DIALOG_UPDATE:
426                         status.setEnabled(true);
427                         return true;
428                 case LFUN_CAPTION_INSERT: {
429                         // the inset outputs at most one caption
430                         if (params().isInline() || !params().isFloat()
431                                                 || getCaptionInset()) {
432                                 status.setEnabled(false);
433                                 return true;
434                         }
435                 }
436                 default:
437                         return InsetCaptionable::getStatus(cur, cmd, status);
438         }
439 }
440
441
442 docstring const InsetListings::buttonLabel(BufferView const & bv) const
443 {
444         // FIXME UNICODE
445         if (decoration() == InsetLayout::CLASSIC)
446                 return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
447         else
448                 return getNewLabel(_("Listing"));
449 }
450
451
452 void InsetListings::validate(LaTeXFeatures & features) const
453 {
454         if (buffer().params().use_minted)
455                 features.require("minted");
456         else
457                 features.require("listings");
458         features.useInsetLayout(getLayout());
459         string param_string = params().params();
460         if (param_string.find("\\color") != string::npos)
461                 features.require("color");
462         InsetCaptionable::validate(features);
463 }
464
465
466 bool InsetListings::showInsetDialog(BufferView * bv) const
467 {
468         bv->showDialog("listings", params2string(params()),
469                 const_cast<InsetListings *>(this));
470         return true;
471 }
472
473
474 TexString InsetListings::getCaption(OutputParams const & runparams) const
475 {
476         InsetCaption const * ins = getCaptionInset();
477         if (ins == 0)
478                 return TexString();
479
480         otexstringstream os;
481         ins->getArgs(os, runparams);
482         ins->getArgument(os, runparams);
483
484         // TODO: The code below should be moved to support, and then the test
485         //       in ../tests should be moved there as well.
486
487         // the caption may contain \label{} but the listings
488         // package prefer caption={}, label={}
489         TexString cap = os.release();
490         if (buffer().params().use_minted
491             || !contains(cap.str, from_ascii("\\label{")))
492                 return cap;
493         // convert from
494         //     blah1\label{blah2} blah3
495         // to
496         //     blah1 blah3},label={blah2
497         // to form options
498         //     caption={blah1 blah3},label={blah2}
499         //
500         // NOTE that } is not allowed in blah2.
501         regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
502         string const new_cap("$1$3},label={$2");
503         // TexString validity: the substitution preserves the number of newlines.
504         // Moreover we assume that $2 does not contain newlines, so that the texrow
505         // information remains accurate.
506         cap.str = from_utf8(regex_replace(to_utf8(cap.str), reg, new_cap));
507         return cap;
508 }
509
510
511 void InsetListings::string2params(string const & in,
512                                    InsetListingsParams & params)
513 {
514         params = InsetListingsParams();
515         if (in.empty())
516                 return;
517         istringstream data(in);
518         Lexer lex;
519         lex.setStream(data);
520         // discard "listings", which is only used to determine inset
521         lex.next();
522         params.read(lex);
523 }
524
525
526 string InsetListings::params2string(InsetListingsParams const & params)
527 {
528         ostringstream data;
529         data << "listings" << ' ';
530         params.write(data);
531         return data.str();
532 }
533
534
535 } // namespace lyx