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 Listing 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 isInline = 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 && !isInline && !captionline) {
165 char const * delimiter = lstinline_delimiters;
166 for (; 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;
182 docstring const caption = getCaption(buf, runparams);
183 if (param_string.empty() && caption.empty())
184 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
186 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
187 if (!caption.empty()) {
188 os << "caption={" << caption << '}';
189 if (!param_string.empty())
192 os << from_utf8(param_string) << "]\n";
196 << "\n\\end{lstlisting}\n\\endgroup\n";
204 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
206 switch (cmd.action) {
208 case LFUN_INSET_MODIFY: {
209 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
212 case LFUN_INSET_DIALOG_UPDATE:
213 InsetListingsMailer(*this).updateDialog(&cur.bv());
215 case LFUN_MOUSE_RELEASE: {
216 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
217 InsetListingsMailer(*this).showDialog(&cur.bv());
220 InsetERT::doDispatch(cur, cmd);
224 InsetERT::doDispatch(cur, cmd);
230 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
231 FuncStatus & status) const
233 switch (cmd.action) {
234 case LFUN_INSET_DIALOG_UPDATE:
235 status.enabled(true);
237 case LFUN_CAPTION_INSERT:
238 status.enabled(!params().isInline());
241 return InsetERT::getStatus(cur, cmd, status);
246 void InsetListings::setButtonLabel()
249 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
253 bool InsetListings::metrics(MetricsInfo & mi, Dimension & dim) const
255 Font tmpfont = mi.base.font;
256 getDrawFont(mi.base.font);
257 mi.base.font.realize(tmpfont);
258 InsetCollapsable::metrics(mi, dim);
259 mi.base.font = tmpfont;
260 bool const changed = dim_ != dim;
266 void InsetListings::draw(PainterInfo & pi, int x, int y) const
268 Font tmpfont = pi.base.font;
269 getDrawFont(pi.base.font);
270 pi.base.font.realize(tmpfont);
271 InsetCollapsable::draw(pi, x, y);
272 pi.base.font = tmpfont;
276 void InsetListings::validate(LaTeXFeatures & features) const
278 features.require("listings");
279 InsetERT::validate(features);
283 bool InsetListings::showInsetDialog(BufferView * bv) const
285 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
290 void InsetListings::getDrawFont(Font & font) const
292 font = Font(Font::ALL_INHERIT, latex_language);
293 font.setFamily(Font::TYPEWRITER_FAMILY);
294 // FIXME: define Color::listing?
295 font.setColor(Color::foreground);
299 docstring InsetListings::getCaption(Buffer const & buf,
300 OutputParams const & runparams) const
302 if (paragraphs().empty())
305 ParagraphList::const_iterator pit = paragraphs().begin();
306 for (; pit != paragraphs().end(); ++pit) {
307 InsetList::const_iterator it = pit->insetlist.begin();
308 for (; it != pit->insetlist.end(); ++it) {
309 Inset & inset = *it->inset;
310 if (inset.lyxCode() == Inset::CAPTION_CODE) {
311 odocstringstream ods;
313 static_cast<InsetCaption *>(it->inset);
314 ins->getOptArg(buf, ods, runparams);
315 ins->getArgument(buf, ods, runparams);
324 string const InsetListingsMailer::name_("listings");
326 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
331 string const InsetListingsMailer::inset2string(Buffer const &) const
333 return params2string(inset_.params());
337 void InsetListingsMailer::string2params(string const & in,
338 InsetListingsParams & params)
340 params = InsetListingsParams();
343 istringstream data(in);
346 // discard "listings", which is only used to determine inset
353 InsetListingsMailer::params2string(InsetListingsParams const & params)
356 data << name_ << " ";