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