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