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