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"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "MetricsInfo.h"
24 #include "support/lstrings.h"
31 using support::contains;
35 using std::istringstream;
37 using std::ostringstream;
40 char const lstinline_delimiters[] = "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
42 void InsetListings::init()
45 Font font(Font::ALL_SANE);
48 font.setColor(Color::none);
50 text_.current_font.setLanguage(latex_language);
51 text_.real_current_font.setLanguage(latex_language);
55 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
56 : InsetERT(bp, par.status())
62 InsetListings::InsetListings(InsetListings const & in)
63 : InsetERT(in), params_(in.params_)
69 auto_ptr<Inset> InsetListings::doClone() const
71 return auto_ptr<Inset>(new InsetListings(*this));
75 InsetListings::~InsetListings()
77 InsetListingsMailer(*this).hideDialog();
81 Inset::DisplayType InsetListings::display() const
83 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
87 void InsetListings::write(Buffer const & buf, ostream & os) const
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();
94 os << "lstparams \"" << opt << "\"\n";
96 os << "inline true\n";
98 os << "inline false\n";
99 InsetCollapsable::write(buf, os);
103 void InsetListings::read(Buffer const & buf, Lexer & lex)
107 string const token = lex.getString();
108 if (token == "lstparams") {
110 string const value = lex.getString();
111 params().fromEncodedString(value);
112 } else if (token == "inline") {
114 params().setInline(lex.getBool());
116 // no special option, push back 'status' etc
117 lex.pushToken(token);
121 InsetERT::read(buf, lex);
125 docstring const InsetListings::editMessage() const
127 return _("Opened Listings Inset");
131 int InsetListings::latex(Buffer const & buf, odocstream & os,
132 OutputParams const & runparams) const
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)
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
142 ParagraphList::const_iterator par = paragraphs().begin();
143 ParagraphList::const_iterator end = paragraphs().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)
151 // ignore all struck out text and (caption) insets
152 if (par->isDeleted(i) || par->isInset(i))
154 code += par->getChar(i);
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) {
164 char const * delimiter;
166 for (delimiter = lstinline_delimiters; delimiter != '\0'; ++delimiter)
167 if (!contains(code, *delimiter))
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;
175 if (param_string.empty())
176 os << "\\lstinline" << *delimiter;
178 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
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";
184 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
185 if (!caption.empty()) {
186 os << "caption={" << caption << '}';
187 if (!param_string.empty())
190 os << from_utf8(param_string) << "]\n";
198 os << "\n\\end{lstlisting}\n\\endgroup\n";
206 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
208 switch (cmd.action) {
210 case LFUN_INSET_MODIFY: {
211 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
214 case LFUN_INSET_DIALOG_UPDATE:
215 InsetListingsMailer(*this).updateDialog(&cur.bv());
217 case LFUN_MOUSE_RELEASE: {
218 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
219 InsetListingsMailer(*this).showDialog(&cur.bv());
222 InsetERT::doDispatch(cur, cmd);
226 InsetERT::doDispatch(cur, cmd);
232 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
233 FuncStatus & status) const
235 switch (cmd.action) {
236 case LFUN_INSET_DIALOG_UPDATE:
237 status.enabled(true);
239 case LFUN_CAPTION_INSERT:
240 status.enabled(!params().isInline());
243 return InsetERT::getStatus(cur, cmd, status);
248 void InsetListings::setButtonLabel()
251 setLabel(isOpen() ? _("Listings") : getNewLabel(_("Listings")));
255 bool InsetListings::metrics(MetricsInfo & mi, Dimension & dim) const
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;
268 void InsetListings::draw(PainterInfo & pi, int x, int y) const
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;
278 void InsetListings::validate(LaTeXFeatures & features) const
280 features.require("listings");
281 InsetERT::validate(features);
285 bool InsetListings::showInsetDialog(BufferView * bv) const
287 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
292 void InsetListings::getDrawFont(Font & font) const
294 font = Font(Font::ALL_INHERIT, latex_language);
295 font.setFamily(Font::TYPEWRITER_FAMILY);
296 // FIXME: define Color::listing?
297 font.setColor(Color::foreground);
301 docstring InsetListings::getCaption(Buffer const & buf,
302 OutputParams const & runparams) const
304 if (paragraphs().empty())
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;
315 static_cast<InsetCaption *>(it->inset);
316 ins->getOptArg(buf, ods, runparams);
317 ins->getArgument(buf, ods, runparams);
326 string const InsetListingsMailer::name_("listings");
328 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
333 string const InsetListingsMailer::inset2string(Buffer const &) const
335 return params2string(inset_.params());
339 void InsetListingsMailer::string2params(string const & in,
340 InsetListingsParams & params)
342 params = InsetListingsParams();
345 istringstream data(in);
348 // discard "listings", which is only used to determine inset
355 InsetListingsMailer::params2string(InsetListingsParams const & params)
358 data << name_ << " ";