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