2 * \file InsetListings.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "InsetListings.h"
15 #include "InsetCaption.h"
18 #include "BufferParams.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
25 #include "InsetList.h"
27 #include "MetricsInfo.h"
29 #include "support/lstrings.h"
36 using support::contains;
39 using std::istringstream;
41 using std::ostringstream;
44 char const lstinline_delimiters[] =
45 "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
47 void InsetListings::init()
50 Font font(Font::ALL_SANE);
53 font.setColor(Color::none);
55 // FIXME: what to do with those?
56 //text_.current_font.setLanguage(latex_language);
57 //text_.real_current_font.setLanguage(latex_language);
61 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
62 : InsetERT(bp, par.status())
68 InsetListings::InsetListings(InsetListings const & in)
69 : InsetERT(in), params_(in.params_)
75 Inset * InsetListings::clone() const
77 return new InsetListings(*this);
81 InsetListings::~InsetListings()
83 InsetListingsMailer(*this).hideDialog();
87 Inset::DisplayType InsetListings::display() const
89 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
93 void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
95 Counters & cnts = buf.params().getTextClass().counters();
96 string const saveflt = cnts.current_float();
98 // Tell to captions what the current float is
99 cnts.current_float("listing");
101 InsetCollapsable::updateLabels(buf, it);
104 cnts.current_float(saveflt);
108 void InsetListings::write(Buffer const & buf, ostream & os) const
110 os << "listings" << "\n";
111 InsetListingsParams const & par = params();
112 // parameter string is encoded to be a valid lyx token.
113 string opt = par.encodedString();
115 os << "lstparams \"" << opt << "\"\n";
117 os << "inline true\n";
119 os << "inline false\n";
120 InsetCollapsable::write(buf, os);
124 void InsetListings::read(Buffer const & buf, Lexer & lex)
128 string const token = lex.getString();
129 if (token == "lstparams") {
131 string const value = lex.getString();
132 params().fromEncodedString(value);
133 } else if (token == "inline") {
135 params().setInline(lex.getBool());
137 // no special option, push back 'status' etc
138 lex.pushToken(token);
142 InsetERT::read(buf, lex);
146 docstring const InsetListings::editMessage() const
148 return _("Opened Listing Inset");
152 int InsetListings::latex(Buffer const & buf, odocstream & os,
153 OutputParams const & runparams) const
155 string param_string = params().params();
156 // NOTE: I use {} to quote text, which is an experimental feature
157 // of the listings package (see page 25 of the manual)
159 bool isInline = params().isInline();
160 // get the paragraphs. We can not output them directly to given odocstream
161 // because we can not yet determine the delimiter character of \lstinline
163 ParagraphList::const_iterator par = paragraphs().begin();
164 ParagraphList::const_iterator end = paragraphs().end();
167 pos_type siz = par->size();
168 bool captionline = false;
169 for (pos_type i = 0; i < siz; ++i) {
170 if (i == 0 && par->isInset(i) && i + 1 == siz)
172 // ignore all struck out text and (caption) insets
173 if (par->isDeleted(i) || par->isInset(i))
175 code += par->getChar(i);
178 // for the inline case, if there are multiple paragraphs
179 // they are simply joined. Otherwise, expect latex errors.
180 if (par != end && !isInline && !captionline) {
186 char const * delimiter = lstinline_delimiters;
187 for (; delimiter != '\0'; ++delimiter)
188 if (!contains(code, *delimiter))
190 // This code piece contains all possible special character? !!!
191 // Replace ! with a warning message and use ! as delimiter.
192 if (*delimiter == '\0') {
193 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
194 delimiter = lstinline_delimiters;
196 if (param_string.empty())
197 os << "\\lstinline" << *delimiter;
199 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
203 OutputParams rp = runparams;
204 // FIXME: the line below would fix bug 4182,
205 // but real_current_font moved to cursor.
206 //rp.local_font = &text_.real_current_font;
207 rp.moving_arg = true;
208 docstring const caption = getCaption(buf, rp);
209 runparams.encoding = rp.encoding;
210 if (param_string.empty() && caption.empty())
211 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
213 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
214 if (!caption.empty()) {
215 os << "caption={" << caption << '}';
216 if (!param_string.empty())
219 os << from_utf8(param_string) << "]\n";
223 << "\n\\end{lstlisting}\n\\endgroup\n";
231 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
233 switch (cmd.action) {
235 case LFUN_INSET_MODIFY: {
236 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
239 case LFUN_INSET_DIALOG_UPDATE:
240 InsetListingsMailer(*this).updateDialog(&cur.bv());
242 case LFUN_MOUSE_RELEASE: {
243 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
244 InsetListingsMailer(*this).showDialog(&cur.bv());
247 InsetERT::doDispatch(cur, cmd);
251 InsetERT::doDispatch(cur, cmd);
257 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
258 FuncStatus & status) const
260 switch (cmd.action) {
261 case LFUN_INSET_DIALOG_UPDATE:
262 status.enabled(true);
264 case LFUN_CAPTION_INSERT:
265 status.enabled(!params().isInline());
268 return InsetERT::getStatus(cur, cmd, status);
273 void InsetListings::setButtonLabel()
276 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
280 void InsetListings::metrics(MetricsInfo & mi, Dimension & dim) const
282 Font tmpfont = mi.base.font;
283 getDrawFont(mi.base.font);
284 mi.base.font.realize(tmpfont);
285 InsetCollapsable::metrics(mi, dim);
286 mi.base.font = tmpfont;
290 void InsetListings::draw(PainterInfo & pi, int x, int y) const
292 Font tmpfont = pi.base.font;
293 getDrawFont(pi.base.font);
294 pi.base.font.realize(tmpfont);
295 InsetCollapsable::draw(pi, x, y);
296 pi.base.font = tmpfont;
300 void InsetListings::validate(LaTeXFeatures & features) const
302 features.require("listings");
303 InsetERT::validate(features);
307 bool InsetListings::showInsetDialog(BufferView * bv) const
309 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
314 void InsetListings::getDrawFont(Font & font) const
316 font = Font(Font::ALL_INHERIT, latex_language);
317 font.setFamily(Font::TYPEWRITER_FAMILY);
318 // FIXME: define Color::listing?
319 font.setColor(Color::foreground);
323 docstring InsetListings::getCaption(Buffer const & buf,
324 OutputParams const & runparams) const
326 if (paragraphs().empty())
329 ParagraphList::const_iterator pit = paragraphs().begin();
330 for (; pit != paragraphs().end(); ++pit) {
331 InsetList::const_iterator it = pit->insetList().begin();
332 for (; it != pit->insetList().end(); ++it) {
333 Inset & inset = *it->inset;
334 if (inset.lyxCode() == CAPTION_CODE) {
335 odocstringstream ods;
337 static_cast<InsetCaption *>(it->inset);
338 ins->getOptArg(buf, ods, runparams);
339 ins->getArgument(buf, ods, runparams);
348 string const InsetListingsMailer::name_("listings");
350 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
355 string const InsetListingsMailer::inset2string(Buffer const &) const
357 return params2string(inset_.params());
361 void InsetListingsMailer::string2params(string const & in,
362 InsetListingsParams & params)
364 params = InsetListingsParams();
367 istringstream data(in);
370 // discard "listings", which is only used to determine inset
377 InsetListingsMailer::params2string(InsetListingsParams const & params)
380 data << name_ << " ";