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