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