]> git.lyx.org Git - features.git/blob - src/insets/InsetListings.cpp
Use proper listings font styles with polyglossia and RTL
[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 Encoding const * InsetListings::forcedEncoding(Encoding const * inner_enc,
123                                                Encoding const * outer_enc) const
124 {
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())
132                 return 0;
133
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");
140 }
141
142
143 void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
144 {
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)
155                 smatch sub;
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);
165         }
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--));
179                         }
180                         else if (prefixIs(opts[i], "language=")) {
181                                 minted_language = opts[i].substr(9);
182                                 opts.erase(opts.begin() + int(i--));
183                         }
184                 }
185                 param_string = getStringFromVector(opts, ",");
186         }
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)
197                                 len -= start;
198                         minted_language = blp.substr(start, len);
199                 } else
200                         minted_language = "TeX";
201         }
202
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
205         docstring code;
206         docstring uncodable;
207         ParagraphList::const_iterator par = paragraphs().begin();
208         ParagraphList::const_iterator end = paragraphs().end();
209
210         bool encoding_switched = false;
211         Encoding const * const save_enc = runparams.encoding;
212
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);
218         if (fixedlstenc) {
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).
225                 os << "\\bgroup";
226                 switchEncoding(os.os(), buffer().params(), runparams, *fixedlstenc, true);
227                 runparams.encoding = fixedlstenc;
228                 encoding_switched = true;
229         }
230
231         bool const captionfirst = !isfloat && par->isInset(0)
232                                 && par->getInset(0)->lyxCode() == CAPTION_CODE;
233
234         while (par != end) {
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)
239                                 captionline = true;
240                         // ignore all struck out text and (caption) insets
241                         if (par->isDeleted(i)
242                             || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
243                                 continue;
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;
250                                 rp.pass_thru = true;
251                                 par->getInset(i)->latex(ots, rp);
252                                 code += ots.str();
253                                 continue;
254                         }
255                         char_type c = par->getChar(i);
256                         // we can only output characters covered by the current
257                         // encoding!
258                         try {
259                                 if (runparams.encoding->encodable(c))
260                                         code += c;
261                                 else if (runparams.dryrun) {
262                                         code += "<" + _("LyX Warning: ")
263                                            + _("uncodable character") + " '";
264                                         code += docstring(1, c);
265                                         code += "'>";
266                                 } else
267                                         uncodable += c;
268                         } catch (EncodingException & /* e */) {
269                                 if (runparams.dryrun) {
270                                         code += "<" + _("LyX Warning: ")
271                                            + _("uncodable character") + " '";
272                                         code += docstring(1, c);
273                                         code += "'>";
274                                 } else
275                                         uncodable += c;
276                         }
277                 }
278                 ++par;
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)
282                         code += "\n";
283         }
284         if (isInline) {
285                 static const docstring delimiters =
286                                 from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
287
288                 size_t pos = delimiters.find_first_not_of(code);
289
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);
296                         pos = 0;
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!"));
305                         }
306                 }
307                 docstring const delim(1, delimiters[pos]);
308                 if (use_minted) {
309                         os << "\\mintinline";
310                         if (!param_string.empty())
311                                 os << "[" << from_utf8(param_string) << "]";
312                         os << "{" << ascii_lowercase(minted_language) << "}";
313                 } else {
314                         os << "\\lstinline";
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
319                                 // the delimiter
320                                 os << " ";
321                 }
322                 os << delim << code << delim;
323         } else if (use_minted) {
324                 OutputParams rp = runparams;
325                 rp.moving_arg = true;
326                 TexString caption = getCaption(rp);
327                 if (isfloat) {
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";
334                 }
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";
340                 if (isfloat) {
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) << "}";
347                 }
348         } else {
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())
354                         os << "\n";
355                 else {
356                         if (!runparams.nice)
357                                 os << safebreakln;
358                         os << "[";
359                         if (!caption.str.empty()) {
360                                 os << "caption={" << move(caption) << '}';
361                                 if (!param_string.empty())
362                                         os << ',';
363                         }
364                         os << from_utf8(param_string) << "]\n";
365                 }
366                 os << code << breakln << "\\end{lstlisting}\n";
367         }
368
369         if (encoding_switched){
370                 // Switch back
371                 switchEncoding(os.os(), buffer().params(),
372                                runparams, *save_enc, true, true);
373                 os << "\\egroup" << breakln;
374                 runparams.encoding = save_enc;
375         }
376
377         if (!uncodable.empty() && !runparams.silent) {
378                 // issue a warning about omitted characters
379                 // FIXME: should be passed to the error dialog
380                 if (fixedlstenc)
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"
387                                           "might help."),
388                                 uncodable, _(runparams.encoding->guiName())));
389                 else
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."),
393                                 uncodable));
394         }
395 }
396
397
398 docstring InsetListings::xhtml(XHTMLStream & os, OutputParams const & rp) const
399 {
400         odocstringstream ods;
401         XHTMLStream out(ods);
402
403         bool const isInline = params().isInline();
404         if (isInline)
405                 out << html::CompTag("br");
406         else {
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");
413         }
414
415         InsetLayout const & il = getLayout();
416         string const & tag = il.htmltag();
417         string attr = "class ='listings";
418         string const lang = params().getParamValue("language");
419         if (!lang.empty())
420                 attr += " " + lang;
421         attr += "'";
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);
430
431         if (isInline) {
432                 out << html::CompTag("br");
433                 // escaping will already have been done
434                 os << XHTMLStream::ESCAPE_NONE << ods.str();
435         } else {
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;
440         }
441         return def;
442 }
443
444
445 string InsetListings::contextMenuName() const
446 {
447         return "context-listings";
448 }
449
450
451 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
452 {
453         switch (cmd.action()) {
454
455         case LFUN_INSET_MODIFY: {
456                 cur.recordUndoInset(this);
457                 InsetListings::string2params(to_utf8(cmd.argument()), params());
458                 break;
459         }
460
461         case LFUN_INSET_DIALOG_UPDATE:
462                 cur.bv().updateDialog("listings", params2string(params()));
463                 break;
464
465         default:
466                 InsetCaptionable::doDispatch(cur, cmd);
467                 break;
468         }
469 }
470
471
472 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
473         FuncStatus & status) const
474 {
475         switch (cmd.action()) {
476                 case LFUN_INSET_MODIFY:
477                 case LFUN_INSET_DIALOG_UPDATE:
478                         status.setEnabled(true);
479                         return true;
480                 case LFUN_CAPTION_INSERT: {
481                         // the inset outputs at most one caption
482                         if (params().isInline() || getCaptionInset()) {
483                                 status.setEnabled(false);
484                                 return true;
485                         }
486                 }
487                 // fall through
488                 default:
489                         return InsetCaptionable::getStatus(cur, cmd, status);
490         }
491 }
492
493
494 docstring const InsetListings::buttonLabel(BufferView const & bv) const
495 {
496         // FIXME UNICODE
497         if (decoration() == InsetLayout::CLASSIC)
498                 return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
499         else
500                 return getNewLabel(_("Listing"));
501 }
502
503
504 void InsetListings::validate(LaTeXFeatures & features) const
505 {
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");
517         } else {
518                 features.require("listings");
519                 if (contains(param_string, "\\color"))
520                         features.require("color");
521         }
522         InsetCaptionable::validate(features);
523 }
524
525
526 bool InsetListings::showInsetDialog(BufferView * bv) const
527 {
528         bv->showDialog("listings", params2string(params()),
529                 const_cast<InsetListings *>(this));
530         return true;
531 }
532
533
534 TexString InsetListings::getCaption(OutputParams const & runparams) const
535 {
536         InsetCaption const * ins = getCaptionInset();
537         if (ins == 0)
538                 return TexString();
539
540         otexstringstream os;
541         ins->getArgs(os, runparams);
542         ins->getArgument(os, runparams);
543
544         // TODO: The code below should be moved to support, and then the test
545         //       in ../tests should be moved there as well.
546
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{")))
552                 return cap;
553         // convert from
554         //     blah1\label{blah2} blah3
555         // to
556         //     blah1 blah3},label={blah2
557         // to form options
558         //     caption={blah1 blah3},label={blah2}
559         //
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'));
574         return cap;
575 }
576
577
578 void InsetListings::string2params(string const & in,
579                                    InsetListingsParams & params)
580 {
581         params = InsetListingsParams();
582         if (in.empty())
583                 return;
584         istringstream data(in);
585         Lexer lex;
586         lex.setStream(data);
587         // discard "listings", which is only used to determine inset
588         lex.next();
589         params.read(lex);
590 }
591
592
593 string InsetListings::params2string(InsetListingsParams const & params)
594 {
595         ostringstream data;
596         data << "listings" << ' ';
597         params.write(data);
598         return data.str();
599 }
600
601
602 } // namespace lyx