]> git.lyx.org Git - lyx.git/blob - src/insets/InsetListings.cpp
Don't allow captions in non-floating listings
[lyx.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 void InsetListings::write(ostream & os) const
76 {
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();
81         if (!opt.empty())
82                 os << "lstparams \"" << opt << "\"\n";
83         if (par.isInline())
84                 os << "inline true\n";
85         else
86                 os << "inline false\n";
87         InsetCaptionable::write(os);
88 }
89
90
91 void InsetListings::read(Lexer & lex)
92 {
93         while (lex.isOK()) {
94                 lex.next();
95                 string token = lex.getString();
96                 if (token == "lstparams") {
97                         lex.next();
98                         string const value = lex.getString();
99                         params().fromEncodedString(value);
100                 } else if (token == "inline") {
101                         lex.next();
102                         params().setInline(lex.getBool());
103                 } else {
104                         // no special option, push back 'status' etc
105                         lex.pushToken(token);
106                         break;
107                 }
108         }
109         InsetCaptionable::read(lex);
110 }
111
112
113 void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
114 {
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--);
133                         }
134                         else if (prefixIs(opts[i], "language=")) {
135                                 minted_language = opts[i].substr(9);
136                                 opts.erase(opts.begin() + i--);
137                         }
138                 }
139                 param_string = getStringFromVector(opts, ",");
140         }
141         // Minted needs a language specification
142         if (minted_language.empty())
143                 minted_language = "TeX";
144
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
147         docstring code;
148         docstring uncodable;
149         ParagraphList::const_iterator par = paragraphs().begin();
150         ParagraphList::const_iterator end = paragraphs().end();
151
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);
160
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;
179         }
180
181         while (par != end) {
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)
186                                 captionline = true;
187                         // ignore all struck out text and (caption) insets
188                         if (par->isDeleted(i)
189                             || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
190                                 continue;
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;
197                                 rp.pass_thru = true;
198                                 par->getInset(i)->latex(ots, rp);
199                                 code += ots.str();
200                                 continue;
201                         }
202                         char_type c = par->getChar(i);
203                         // we can only output characters covered by the current
204                         // encoding!
205                         try {
206                                 if (runparams.encoding->encodable(c))
207                                         code += c;
208                                 else if (runparams.dryrun) {
209                                         code += "<" + _("LyX Warning: ")
210                                            + _("uncodable character") + " '";
211                                         code += docstring(1, c);
212                                         code += "'>";
213                                 } else
214                                         uncodable += c;
215                         } catch (EncodingException & /* e */) {
216                                 if (runparams.dryrun) {
217                                         code += "<" + _("LyX Warning: ")
218                                            + _("uncodable character") + " '";
219                                         code += docstring(1, c);
220                                         code += "'>";
221                                 } else
222                                         uncodable += c;
223                         }
224                 }
225                 ++par;
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)
229                         code += "\n";
230         }
231         if (isInline) {
232                 static const docstring delimiters =
233                                 from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
234
235                 size_t pos = delimiters.find_first_not_of(code);
236
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);
243                         pos = 0;
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!"));
252                         }
253                 }
254                 docstring const delim(1, delimiters[pos]);
255                 if (use_minted) {
256                         os << "\\mintinline";
257                         if (!param_string.empty())
258                                 os << "[" << from_utf8(param_string) << "]";
259                         os << "{" << minted_language << "}";
260                 } else {
261                         os << "\\lstinline";
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
266                                 // the delimiter
267                                 os << " ";
268                 }
269                 os << delim << code << delim;
270         } else if (use_minted) {
271                 if (isfloat) {
272                         os << breakln << "\\begin{listing}";
273                         if (!float_placement.empty())
274                                 os << '[' << float_placement << "]";
275                 }
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";
281                 if (isfloat) {
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";
288                 }
289         } else {
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())
295                         os << "\n";
296                 else {
297                         if (!runparams.nice)
298                                 os << safebreakln;
299                         os << "[";
300                         if (!caption.str.empty()) {
301                                 os << "caption={" << move(caption) << '}';
302                                 if (!param_string.empty())
303                                         os << ',';
304                         }
305                         os << from_utf8(param_string) << "]\n";
306                 }
307                 os << code << breakln << "\\end{lstlisting}\n";
308         }
309
310         if (encoding_switched){
311                 // Switch back
312                 switchEncoding(os.os(), buffer().params(), runparams, *save_enc, true);
313                 runparams.encoding = save_enc;
314         }
315
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"
326                                           "might help."),
327                                 uncodable, _(runparams.encoding->guiName())));
328                 else
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."),
332                                 uncodable));
333         }
334 }
335
336
337 docstring InsetListings::xhtml(XHTMLStream & os, OutputParams const & rp) const
338 {
339         odocstringstream ods;
340         XHTMLStream out(ods);
341
342         bool const isInline = params().isInline();
343         if (isInline)
344                 out << html::CompTag("br");
345         else {
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");
352         }
353
354         InsetLayout const & il = getLayout();
355         string const & tag = il.htmltag();
356         string attr = "class ='listings";
357         string const lang = params().getParamValue("language");
358         if (!lang.empty())
359                 attr += " " + lang;
360         attr += "'";
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);
369
370         if (isInline) {
371                 out << html::CompTag("br");
372                 // escaping will already have been done
373                 os << XHTMLStream::ESCAPE_NONE << ods.str();
374         } else {
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;
379         }
380         return def;
381 }
382
383
384 string InsetListings::contextMenuName() const
385 {
386         return "context-listings";
387 }
388
389
390 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
391 {
392         switch (cmd.action()) {
393
394         case LFUN_INSET_MODIFY: {
395                 cur.recordUndoInset(this);
396                 InsetListings::string2params(to_utf8(cmd.argument()), params());
397                 break;
398         }
399
400         case LFUN_INSET_DIALOG_UPDATE:
401                 cur.bv().updateDialog("listings", params2string(params()));
402                 break;
403
404         default:
405                 InsetCaptionable::doDispatch(cur, cmd);
406                 break;
407         }
408 }
409
410
411 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
412         FuncStatus & status) const
413 {
414         switch (cmd.action()) {
415                 case LFUN_INSET_MODIFY:
416                 case LFUN_INSET_DIALOG_UPDATE:
417                         status.setEnabled(true);
418                         return true;
419                 case LFUN_CAPTION_INSERT: {
420                         // the inset outputs at most one caption
421                         if (params().isInline() || !params().isFloat()
422                                                 || getCaptionInset()) {
423                                 status.setEnabled(false);
424                                 return true;
425                         }
426                 }
427                 default:
428                         return InsetCaptionable::getStatus(cur, cmd, status);
429         }
430 }
431
432
433 docstring const InsetListings::buttonLabel(BufferView const & bv) const
434 {
435         // FIXME UNICODE
436         if (decoration() == InsetLayout::CLASSIC)
437                 return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
438         else
439                 return getNewLabel(_("Listing"));
440 }
441
442
443 void InsetListings::validate(LaTeXFeatures & features) const
444 {
445         if (buffer().params().use_minted)
446                 features.require("minted");
447         else
448                 features.require("listings");
449         features.useInsetLayout(getLayout());
450         string param_string = params().params();
451         if (param_string.find("\\color") != string::npos)
452                 features.require("color");
453         InsetCaptionable::validate(features);
454 }
455
456
457 bool InsetListings::showInsetDialog(BufferView * bv) const
458 {
459         bv->showDialog("listings", params2string(params()),
460                 const_cast<InsetListings *>(this));
461         return true;
462 }
463
464
465 TexString InsetListings::getCaption(OutputParams const & runparams) const
466 {
467         InsetCaption const * ins = getCaptionInset();
468         if (ins == 0)
469                 return TexString();
470
471         otexstringstream os;
472         ins->getArgs(os, runparams);
473         ins->getArgument(os, runparams);
474
475         // TODO: The code below should be moved to support, and then the test
476         //       in ../tests should be moved there as well.
477
478         // the caption may contain \label{} but the listings
479         // package prefer caption={}, label={}
480         TexString cap = os.release();
481         if (buffer().params().use_minted
482             || !contains(cap.str, from_ascii("\\label{")))
483                 return cap;
484         // convert from
485         //     blah1\label{blah2} blah3
486         // to
487         //     blah1 blah3},label={blah2
488         // to form options
489         //     caption={blah1 blah3},label={blah2}
490         //
491         // NOTE that } is not allowed in blah2.
492         regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
493         string const new_cap("$1$3},label={$2");
494         // TexString validity: the substitution preserves the number of newlines.
495         // Moreover we assume that $2 does not contain newlines, so that the texrow
496         // information remains accurate.
497         cap.str = from_utf8(regex_replace(to_utf8(cap.str), reg, new_cap));
498         return cap;
499 }
500
501
502 void InsetListings::string2params(string const & in,
503                                    InsetListingsParams & params)
504 {
505         params = InsetListingsParams();
506         if (in.empty())
507                 return;
508         istringstream data(in);
509         Lexer lex;
510         lex.setStream(data);
511         // discard "listings", which is only used to determine inset
512         lex.next();
513         params.read(lex);
514 }
515
516
517 string InsetListings::params2string(InsetListingsParams const & params)
518 {
519         ostringstream data;
520         data << "listings" << ' ';
521         params.write(data);
522         return data.str();
523 }
524
525
526 } // namespace lyx