]> git.lyx.org Git - lyx.git/blob - src/insets/InsetListings.cpp
XHTML output for InsetListings.
[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 "MetricsInfo.h"
28 #include "output_latex.h"
29 #include "TextClass.h"
30
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35 #include "support/lassert.h"
36
37 #include "frontends/alert.h"
38 #include "frontends/Application.h"
39
40 #include <boost/regex.hpp>
41
42 #include <sstream>
43
44 using namespace std;
45 using namespace lyx::support;
46
47 namespace lyx {
48
49 using boost::regex;
50
51 char const lstinline_delimiters[] =
52         "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
53
54 InsetListings::InsetListings(Buffer const & buf, InsetListingsParams const & par)
55         : InsetCollapsable(buf)
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::updateLabels(ParIterator const & it)
74 {
75         Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
76         string const saveflt = cnts.current_float();
77
78         // Tell to captions what the current float is
79         cnts.current_float("listing");
80
81         InsetCollapsable::updateLabels(it);
82
83         //reset afterwards
84         cnts.current_float(saveflt);
85 }
86
87
88 void InsetListings::write(ostream & os) const
89 {
90         os << "listings" << "\n";
91         InsetListingsParams const & par = params();
92         // parameter string is encoded to be a valid lyx token.
93         string opt = par.encodedString();
94         if (!opt.empty())
95                 os << "lstparams \"" << opt << "\"\n";
96         if (par.isInline())
97                 os << "inline true\n";
98         else
99                 os << "inline false\n";
100         InsetCollapsable::write(os);
101 }
102
103
104 void InsetListings::read(Lexer & lex)
105 {
106         while (lex.isOK()) {
107                 lex.next();
108                 string token = lex.getString();
109                 if (token == "lstparams") {
110                         lex.next();
111                         string const value = lex.getString();
112                         params().fromEncodedString(value);
113                 } else if (token == "inline") {
114                         lex.next();
115                         params().setInline(lex.getBool());
116                 } else {
117                         // no special option, push back 'status' etc
118                         lex.pushToken(token);
119                         break;
120                 }
121         }
122         InsetCollapsable::read(lex);
123 }
124
125
126 docstring InsetListings::editMessage() const
127 {
128         return _("Opened Listing Inset");
129 }
130
131
132 int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
133 {
134         string param_string = params().params();
135         // NOTE: I use {} to quote text, which is an experimental feature
136         // of the listings package (see page 25 of the manual)
137         int lines = 0;
138         bool const isInline = params().isInline();
139         // get the paragraphs. We can not output them directly to given odocstream
140         // because we can not yet determine the delimiter character of \lstinline
141         docstring code;
142         docstring uncodable;
143         ParagraphList::const_iterator par = paragraphs().begin();
144         ParagraphList::const_iterator end = paragraphs().end();
145
146         bool encoding_switched = false;
147         Encoding const * const save_enc = runparams.encoding;
148
149         if (!runparams.encoding->hasFixedWidth()) {
150                 // We need to switch to a singlebyte encoding, since the listings
151                 // package cannot deal with multiple-byte-encoded glyphs
152                 Language const * const outer_language =
153                         (runparams.local_font != 0) ?
154                                 runparams.local_font->language()
155                                 : buffer().params().language;
156                 // We try if there's a singlebyte encoding for the current
157                 // language; if not, fall back to latin1.
158                 Encoding const * const lstenc =
159                         (outer_language->encoding()->hasFixedWidth()) ?
160                                 outer_language->encoding() 
161                                 : encodings.fromLyXName("iso8859-1");
162                 pair<bool, int> const c = switchEncoding(os, buffer().params(),
163                                 runparams, *lstenc, true);
164                 runparams.encoding = lstenc;
165                 encoding_switched = true;
166         }
167
168         while (par != end) {
169                 pos_type siz = par->size();
170                 bool captionline = false;
171                 for (pos_type i = 0; i < siz; ++i) {
172                         if (i == 0 && par->isInset(i) && i + 1 == siz)
173                                 captionline = true;
174                         // ignore all struck out text and (caption) insets
175                         if (par->isDeleted(i) || par->isInset(i))
176                                 continue;
177                         char_type c = par->getChar(i);
178                         // we can only output characters covered by the current
179                         // encoding!
180                         try {
181                                 if (runparams.encoding->latexChar(c) == docstring(1, c))
182                                         code += c;
183                                 else if (runparams.dryrun) {
184                                         code += "<" + _("LyX Warning: ")
185                                            + _("uncodable character") + " '";
186                                         code += docstring(1, c);
187                                         code += "'>";
188                                 } else
189                                         uncodable += c;
190                         } catch (EncodingException & /* e */) {
191                                 if (runparams.dryrun) {
192                                         code += "<" + _("LyX Warning: ")
193                                            + _("uncodable character") + " '";
194                                         code += docstring(1, c);
195                                         code += "'>";
196                                 } else
197                                         uncodable += c;
198                         }
199                 }
200                 ++par;
201                 // for the inline case, if there are multiple paragraphs
202                 // they are simply joined. Otherwise, expect latex errors.
203                 if (par != end && !isInline && !captionline) {
204                         code += "\n";
205                         ++lines;
206                 }
207         }
208         if (isInline) {
209                 char const * delimiter = lstinline_delimiters;
210                 for (; delimiter != '\0'; ++delimiter)
211                         if (!contains(code, *delimiter))
212                                 break;
213                 // This code piece contains all possible special character? !!!
214                 // Replace ! with a warning message and use ! as delimiter.
215                 if (*delimiter == '\0') {
216                         docstring delim_error = "<" + _("LyX Warning: ")
217                                 + _("no more lstline delimiters available") + ">";
218                         code = subst(code, from_ascii("!"), delim_error);
219                         delimiter = lstinline_delimiters;
220                         if (!runparams.dryrun) {
221                                 // FIXME: warning should be passed to the error dialog
222                                 frontend::Alert::warning(_("Running out of delimiters"),
223                                 _("For inline program listings, one character must be reserved\n"
224                                   "as a delimiter. One of the listings, however, uses all available\n"
225                                   "characters, so none is left for delimiting purposes.\n"
226                                   "For the time being, I have replaced '!' by a warning, but you\n"
227                                   "must investigate!"));
228                         }
229                 }
230                 if (param_string.empty())
231                         os << "\\lstinline" << *delimiter;
232                 else
233                         os << "\\lstinline[" << from_utf8(param_string) << "]" << *delimiter;
234                 os << code
235                    << *delimiter;
236         } else {
237                 OutputParams rp = runparams;
238                 rp.moving_arg = true;
239                 docstring const caption = getCaption(rp);
240                 if (param_string.empty() && caption.empty())
241                         os << "\n\\begin{lstlisting}\n";
242                 else {
243                         os << "\n\\begin{lstlisting}[";
244                         if (!caption.empty()) {
245                                 os << "caption={" << caption << '}';
246                                 if (!param_string.empty())
247                                         os << ',';
248                         }
249                         os << from_utf8(param_string) << "]\n";
250                 }
251                 lines += 2;
252                 os << code << "\n\\end{lstlisting}\n";
253                 lines += 2;
254         }
255
256         if (encoding_switched){
257                 // Switch back
258                 pair<bool, int> const c = switchEncoding(os, buffer().params(),
259                                 runparams, *save_enc, true);
260                 runparams.encoding = save_enc;
261         }
262
263         if (!uncodable.empty()) {
264                 // issue a warning about omitted characters
265                 // FIXME: should be passed to the error dialog
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."),
269                         uncodable));
270         }
271
272         return lines;
273 }
274
275
276 docstring InsetListings::xhtml(odocstream & os, OutputParams const & rp) const
277 {
278         odocstringstream out;
279
280         bool const isInline = params().isInline();
281         if (isInline) 
282                 out << "<br />\n";
283         else {
284                 out << "<div class='float float-listings'>\n";
285                 docstring caption = getCaptionHTML(rp);
286                 if (!caption.empty())
287                         out << "<div class='float-caption'>" << caption << "</div>\n";
288         }
289
290         out << "<pre>\n";
291         docstring def = InsetText::xhtml(out, rp);
292         out << "\n</pre>\n";
293
294         if (isInline) {
295                 out << "<br />\n";
296                 os << out.str();
297         } else {
298                 out <<  "</div>";
299                 // In this case, this needs to be deferred, but we'll put it
300                 // before anything the text itself deferred.
301                 def = out.str() + '\n' + def;
302         }
303         return def;
304 }
305
306
307 docstring InsetListings::contextMenu(BufferView const &, int, int) const
308 {
309         return from_ascii("context-listings");
310 }
311
312
313 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
314 {
315         switch (cmd.action) {
316
317         case LFUN_INSET_MODIFY: {
318                 InsetListings::string2params(to_utf8(cmd.argument()), params());
319                 break;
320         }
321
322         case LFUN_INSET_DIALOG_UPDATE:
323                 cur.bv().updateDialog("listings", params2string(params()));
324                 break;
325
326         case LFUN_TAB_INSERT: {
327                 bool const multi_par_selection = cur.selection() &&
328                         cur.selBegin().pit() != cur.selEnd().pit();
329                 if (multi_par_selection) {
330                         // If there is a multi-paragraph selection, a tab is inserted
331                         // at the beginning of each paragraph.
332                         cur.recordUndoSelection();
333                         pit_type const pit_end = cur.selEnd().pit();
334                         for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
335                                 paragraphs()[pit].insertChar(0, '\t', 
336                                         buffer().params().trackChanges);
337                                 // Update the selection pos to make sure the selection does not
338                                 // change as the inserted tab will increase the logical pos.
339                                 if (cur.anchor_.pit() == pit)
340                                         cur.anchor_.forwardPos();
341                                 if (cur.pit() == pit)
342                                         cur.forwardPos();
343                         }
344                         cur.finishUndo();
345                 } else {
346                         // Maybe we shouldn't allow tabs within a line, because they
347                         // are not (yet) aligned as one might do expect.
348                         FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
349                         dispatch(cur, cmd);     
350                 }
351                 break;
352         }
353
354         case LFUN_TAB_DELETE:
355                 if (cur.selection()) {
356                         // If there is a selection, a tab (if present) is removed from
357                         // the beginning of each paragraph.
358                         cur.recordUndoSelection();
359                         pit_type const pit_end = cur.selEnd().pit();
360                         for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
361                                 Paragraph & par = paragraphs()[pit];
362                                 if (par.getChar(0) == '\t') {
363                                         if (cur.pit() == pit)
364                                                 cur.posBackward();
365                                         if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
366                                                 cur.anchor_.backwardPos();
367
368                                         par.eraseChar(0, buffer().params().trackChanges);
369                                 } else 
370                                         // If no tab was present, try to remove up to four spaces.
371                                         for (int n_spaces = 0;
372                                                 par.getChar(0) == ' ' && n_spaces < 4; ++n_spaces) {
373                                                         if (cur.pit() == pit)
374                                                                 cur.posBackward();
375                                                         if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
376                                                                 cur.anchor_.backwardPos();
377
378                                                         par.eraseChar(0, buffer().params().trackChanges);
379                                         }
380                         }
381                         cur.finishUndo();
382                 } else {
383                         // If there is no selection, try to remove a tab or some spaces 
384                         // before the position of the cursor.
385                         Paragraph & par = paragraphs()[cur.pit()];
386                         pos_type const pos = cur.pos();
387
388                         if (pos == 0)
389                                 break;
390
391                         char_type const c = par.getChar(pos - 1);
392                         cur.recordUndo();
393                         if (c == '\t') {
394                                 cur.posBackward();
395                                 par.eraseChar(cur.pos(), buffer().params().trackChanges);
396                         } else
397                                 for (int n_spaces = 0; cur.pos() > 0
398                                         && par.getChar(cur.pos() - 1) == ' ' && n_spaces < 4;
399                                         ++n_spaces) {
400                                                 cur.posBackward();
401                                                 par.eraseChar(cur.pos(), buffer().params().trackChanges);
402                                 }
403                                 cur.finishUndo();
404                 }
405                 break;
406         default:
407                 InsetCollapsable::doDispatch(cur, cmd);
408                 break;
409         }
410 }
411
412
413 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
414         FuncStatus & status) const
415 {
416         switch (cmd.action) {
417                 case LFUN_INSET_MODIFY:
418                 case LFUN_INSET_DIALOG_UPDATE:
419                         status.setEnabled(true);
420                         return true;
421                 case LFUN_CAPTION_INSERT:
422                         status.setEnabled(!params().isInline());
423                         return true;
424                         case LFUN_TAB_INSERT:
425                         case LFUN_TAB_DELETE:
426                                 status.setEnabled(true);
427                                 return true;
428                 default:
429                         return InsetCollapsable::getStatus(cur, cmd, status);
430         }
431 }
432
433
434 docstring const InsetListings::buttonLabel(BufferView const & bv) const
435 {
436         // FIXME UNICODE
437         if (decoration() == InsetLayout::CLASSIC)
438                 return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
439         else
440                 return getNewLabel(_("Listing"));
441 }
442
443
444 void InsetListings::validate(LaTeXFeatures & features) const
445 {
446         features.require("listings");
447         string param_string = params().params();
448         if (param_string.find("\\color") != string::npos)
449                 features.require("color");
450         InsetCollapsable::validate(features);
451 }
452
453
454 bool InsetListings::showInsetDialog(BufferView * bv) const
455 {
456         bv->showDialog("listings", params2string(params()),
457                 const_cast<InsetListings *>(this));
458         return true;
459 }
460
461
462 docstring InsetListings::getCaption(OutputParams const & runparams) const
463 {
464         if (paragraphs().empty())
465                 return docstring();
466
467         InsetCaption const * ins = getCaptionInset();
468         if (ins == 0)
469                 return docstring();
470
471         odocstringstream ods;
472         ins->getOptArg(ods, runparams);
473         ins->getArgument(ods, runparams);
474         // the caption may contain \label{} but the listings
475         // package prefer caption={}, label={}
476         docstring cap = ods.str();
477         if (!contains(to_utf8(cap), "\\label{"))
478                 return cap;
479         // convert from
480         //     blah1\label{blah2} blah3
481         // to
482         //     blah1 blah3},label={blah2
483         // to form options
484         //     caption={blah1 blah3},label={blah2}
485         //
486         // NOTE that } is not allowed in blah2.
487         regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
488         string const new_cap("\\1\\3},label={\\2");
489         return from_utf8(regex_replace(to_utf8(cap), reg, new_cap));
490 }
491
492
493 void InsetListings::string2params(string const & in,
494                                    InsetListingsParams & params)
495 {
496         params = InsetListingsParams();
497         if (in.empty())
498                 return;
499         istringstream data(in);
500         Lexer lex;
501         lex.setStream(data);
502         // discard "listings", which is only used to determine inset
503         lex.next();
504         params.read(lex);
505 }
506
507
508 string InsetListings::params2string(InsetListingsParams const & params)
509 {
510         ostringstream data;
511         data << "listings" << ' ';
512         params.write(data);
513         return data.str();
514 }
515
516
517 } // namespace lyx