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