]> git.lyx.org Git - lyx.git/blob - src/insets/InsetListings.cpp
Implement ForceLtR; cleanup of collapsable insets
[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 #include "InsetCaption.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "Counters.h"
20 #include "Cursor.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "gettext.h"
25 #include "InsetList.h"
26 #include "Language.h"
27 #include "MetricsInfo.h"
28
29 #include "support/lstrings.h"
30
31 #include <sstream>
32
33 namespace lyx {
34
35 using support::token;
36 using support::contains;
37 using support::subst;
38
39 using std::istringstream;
40 using std::ostream;
41 using std::ostringstream;
42 using std::string;
43
44 char const lstinline_delimiters[] =
45         "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
46
47 void InsetListings::init()
48 {
49         setButtonLabel();
50         FontInfo font = sane_font;
51         font.decSize();
52         font.decSize();
53         font.setColor(Color_none);
54         setLabelFont(font);
55         // FIXME: what to do with those?
56         //text_.current_font.setLanguage(latex_language);
57         //text_.real_current_font.setLanguage(latex_language);
58 }
59
60
61 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
62         : InsetCollapsable(bp, par.status())
63 {
64         setLayout(bp);
65         init();
66 }
67
68
69 InsetListings::InsetListings(InsetListings const & in)
70         : InsetCollapsable(in), params_(in.params_)
71 {
72         init();
73 }
74
75
76 Inset * InsetListings::clone() const
77 {
78         return new InsetListings(*this);
79 }
80
81
82 InsetListings::~InsetListings()
83 {
84         InsetListingsMailer(*this).hideDialog();
85 }
86
87
88 Inset::DisplayType InsetListings::display() const
89 {
90         return params().isInline() || params().isFloat() ? Inline : AlignLeft;
91 }
92
93
94 void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
95 {
96         Counters & cnts = buf.params().getTextClass().counters();
97         string const saveflt = cnts.current_float();
98
99         // Tell to captions what the current float is
100         cnts.current_float("listing");
101
102         InsetCollapsable::updateLabels(buf, it);
103
104         //reset afterwards
105         cnts.current_float(saveflt);
106 }
107
108
109 void InsetListings::write(Buffer const & buf, ostream & os) const
110 {
111         os << "listings" << "\n";
112         InsetListingsParams const & par = params();
113         // parameter string is encoded to be a valid lyx token.
114         string opt = par.encodedString();
115         if (!opt.empty())
116                 os << "lstparams \"" << opt << "\"\n";
117         if (par.isInline())
118                 os << "inline true\n";
119         else
120                 os << "inline false\n";
121         InsetCollapsable::write(buf, os);
122 }
123
124
125 void InsetListings::read(Buffer const & buf, Lexer & lex)
126 {
127         while (lex.isOK()) {
128                 lex.next();
129                 string const token = lex.getString();
130                 if (token == "lstparams") {
131                         lex.next();
132                         string const value = lex.getString();
133                         params().fromEncodedString(value);
134                 } else if (token == "inline") {
135                         lex.next();
136                         params().setInline(lex.getBool());
137                 } else {
138                         // no special option, push back 'status' etc
139                         lex.pushToken(token);
140                         break;
141                 }
142         }
143         InsetCollapsable::read(buf, lex);
144 }
145
146
147 docstring const InsetListings::editMessage() const
148 {
149         return _("Opened Listing Inset");
150 }
151
152
153 int InsetListings::latex(Buffer const & buf, odocstream & os,
154                     OutputParams const & runparams) const
155 {
156         string param_string = params().params();
157         // NOTE: I use {} to quote text, which is an experimental feature
158         // of the listings package (see page 25 of the manual)
159         int lines = 0;
160         bool isInline = params().isInline();
161         // get the paragraphs. We can not output them directly to given odocstream
162         // because we can not yet determine the delimiter character of \lstinline
163         docstring code;
164         ParagraphList::const_iterator par = paragraphs().begin();
165         ParagraphList::const_iterator end = paragraphs().end();
166
167         while (par != end) {
168                 pos_type siz = par->size();
169                 bool captionline = false;
170                 for (pos_type i = 0; i < siz; ++i) {
171                         if (i == 0 && par->isInset(i) && i + 1 == siz)
172                                 captionline = true;
173                         // ignore all struck out text and (caption) insets
174                         if (par->isDeleted(i) || par->isInset(i))
175                                 continue;
176                         code += par->getChar(i);
177                 }
178                 ++par;
179                 // for the inline case, if there are multiple paragraphs
180                 // they are simply joined. Otherwise, expect latex errors.
181                 if (par != end && !isInline && !captionline) {
182                         code += "\n";
183                         ++lines;
184                 }
185         }
186         if (isInline) {
187                 char const * delimiter = lstinline_delimiters;
188                 for (; delimiter != '\0'; ++delimiter)
189                         if (!contains(code, *delimiter))
190                                 break;
191                 // This code piece contains all possible special character? !!!
192                 // Replace ! with a warning message and use ! as delimiter.
193                 if (*delimiter == '\0') {
194                         code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
195                         delimiter = lstinline_delimiters;
196                 }
197                 if (param_string.empty())
198                         os << "\\lstinline" << *delimiter;
199                 else
200                         os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
201                 os << code
202                    << *delimiter;
203         } else {
204                 OutputParams rp = runparams;
205                 // FIXME: the line below would fix bug 4182,
206                 // but real_current_font moved to cursor.
207                 //rp.local_font = &text_.real_current_font;
208                 rp.moving_arg = true;
209                 docstring const caption = getCaption(buf, rp);
210                 runparams.encoding = rp.encoding;
211                 if (param_string.empty() && caption.empty())
212                         os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
213                 else {
214                         os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
215                         if (!caption.empty()) {
216                                 os << "caption={" << caption << '}';
217                                 if (!param_string.empty())
218                                         os << ',';
219                         }
220                         os << from_utf8(param_string) << "]\n";
221                 }
222                 lines += 4;
223                 os << code
224                    << "\n\\end{lstlisting}\n\\endgroup\n";
225                 lines += 3;
226         }
227
228         return lines;
229 }
230
231
232 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
233 {
234         switch (cmd.action) {
235
236         case LFUN_INSET_MODIFY: {
237                 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
238                 break;
239         }
240         case LFUN_INSET_DIALOG_UPDATE:
241                 InsetListingsMailer(*this).updateDialog(&cur.bv());
242                 break;
243         case LFUN_MOUSE_RELEASE: {
244                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
245                         InsetListingsMailer(*this).showDialog(&cur.bv());
246                         break;
247                 }
248                 InsetCollapsable::doDispatch(cur, cmd);
249                 break;
250         }
251         default:
252                 InsetCollapsable::doDispatch(cur, cmd);
253                 break;
254         }
255 }
256
257
258 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
259         FuncStatus & status) const
260 {
261         switch (cmd.action) {
262                 case LFUN_INSET_DIALOG_UPDATE:
263                         status.enabled(true);
264                         return true;
265                 case LFUN_CAPTION_INSERT:
266                         status.enabled(!params().isInline());
267                         return true;
268                 default:
269                         return InsetCollapsable::getStatus(cur, cmd, status);
270         }
271 }
272
273
274 void InsetListings::setButtonLabel()
275 {
276         // FIXME UNICODE
277         if (decoration() == Classic)
278                 setLabel(isOpen() ?  _("Listing") : getNewLabel(_("Listing")));
279         else
280                 setLabel(getNewLabel(_("Listing")));
281 }
282
283
284 void InsetListings::validate(LaTeXFeatures & features) const
285 {
286         features.require("listings");
287         InsetCollapsable::validate(features);
288 }
289
290
291 bool InsetListings::showInsetDialog(BufferView * bv) const
292 {
293         InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
294         return true;
295 }
296
297
298 docstring InsetListings::getCaption(Buffer const & buf,
299                     OutputParams const & runparams) const
300 {
301         if (paragraphs().empty())
302                 return docstring();
303
304         ParagraphList::const_iterator pit = paragraphs().begin();
305         for (; pit != paragraphs().end(); ++pit) {
306                 InsetList::const_iterator it = pit->insetList().begin();
307                 for (; it != pit->insetList().end(); ++it) {
308                         Inset & inset = *it->inset;
309                         if (inset.lyxCode() == CAPTION_CODE) {
310                                 odocstringstream ods;
311                                 InsetCaption * ins =
312                                         static_cast<InsetCaption *>(it->inset);
313                                 ins->getOptArg(buf, ods, runparams);
314                                 ins->getArgument(buf, ods, runparams);
315                                 return ods.str();
316                         }
317                 }
318         }
319         return docstring();
320 }
321
322
323 string const InsetListingsMailer::name_("listings");
324
325 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
326         : inset_(inset)
327 {}
328
329
330 string const InsetListingsMailer::inset2string(Buffer const &) const
331 {
332         return params2string(inset_.params());
333 }
334
335
336 void InsetListingsMailer::string2params(string const & in,
337                                    InsetListingsParams & params)
338 {
339         params = InsetListingsParams();
340         if (in.empty())
341                 return;
342         istringstream data(in);
343         Lexer lex(0, 0);
344         lex.setStream(data);
345         // discard "listings", which is only used to determine inset
346         lex.next();
347         params.read(lex);
348 }
349
350
351 string const
352 InsetListingsMailer::params2string(InsetListingsParams const & params)
353 {
354         ostringstream data;
355         data << name_ << " ";
356         params.write(data);
357         return data.str();
358 }
359
360
361 } // namespace lyx