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