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"
22 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "MetricsInfo.h"
27 #include "support/lstrings.h"
34 using support::contains;
37 using std::istringstream;
39 using std::ostringstream;
42 char const lstinline_delimiters[] =
43 "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
45 void InsetListings::init()
48 Font font(Font::ALL_SANE);
51 font.setColor(Color::none);
53 text_.current_font.setLanguage(latex_language);
54 text_.real_current_font.setLanguage(latex_language);
58 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
59 : InsetERT(bp, par.status())
65 InsetListings::InsetListings(InsetListings const & in)
66 : InsetERT(in), params_(in.params_)
72 Inset * InsetListings::clone() const
74 return new InsetListings(*this);
78 InsetListings::~InsetListings()
80 InsetListingsMailer(*this).hideDialog();
84 Inset::DisplayType InsetListings::display() const
86 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
90 void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
92 Counters & cnts = buf.params().getTextClass().counters();
93 string const saveflt = cnts.current_float();
95 // Tell to captions what the current float is
96 cnts.current_float("listing");
98 InsetCollapsable::updateLabels(buf, it);
101 cnts.current_float(saveflt);
105 void InsetListings::write(Buffer const & buf, ostream & os) const
107 os << "listings" << "\n";
108 InsetListingsParams const & par = params();
109 // parameter string is encoded to be a valid lyx token.
110 string opt = par.encodedString();
112 os << "lstparams \"" << opt << "\"\n";
114 os << "inline true\n";
116 os << "inline false\n";
117 InsetCollapsable::write(buf, os);
121 void InsetListings::read(Buffer const & buf, Lexer & lex)
125 string const token = lex.getString();
126 if (token == "lstparams") {
128 string const value = lex.getString();
129 params().fromEncodedString(value);
130 } else if (token == "inline") {
132 params().setInline(lex.getBool());
134 // no special option, push back 'status' etc
135 lex.pushToken(token);
139 InsetERT::read(buf, lex);
143 docstring const InsetListings::editMessage() const
145 return _("Opened Listing Inset");
149 int InsetListings::latex(Buffer const & buf, odocstream & os,
150 OutputParams const & runparams) const
152 string param_string = params().params();
153 // NOTE: I use {} to quote text, which is an experimental feature
154 // of the listings package (see page 25 of the manual)
156 bool isInline = params().isInline();
157 // get the paragraphs. We can not output them directly to given odocstream
158 // because we can not yet determine the delimiter character of \lstinline
160 ParagraphList::const_iterator par = paragraphs().begin();
161 ParagraphList::const_iterator end = paragraphs().end();
164 pos_type siz = par->size();
165 bool captionline = false;
166 for (pos_type i = 0; i < siz; ++i) {
167 if (i == 0 && par->isInset(i) && i + 1 == siz)
169 // ignore all struck out text and (caption) insets
170 if (par->isDeleted(i) || par->isInset(i))
172 code += par->getChar(i);
175 // for the inline case, if there are multiple paragraphs
176 // they are simply joined. Otherwise, expect latex errors.
177 if (par != end && !isInline && !captionline) {
183 char const * delimiter = lstinline_delimiters;
184 for (; delimiter != '\0'; ++delimiter)
185 if (!contains(code, *delimiter))
187 // This code piece contains all possible special character? !!!
188 // Replace ! with a warning message and use ! as delimiter.
189 if (*delimiter == '\0') {
190 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
191 delimiter = lstinline_delimiters;
193 if (param_string.empty())
194 os << "\\lstinline" << *delimiter;
196 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
200 docstring const caption = getCaption(buf, runparams);
201 if (param_string.empty() && caption.empty())
202 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
204 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
205 if (!caption.empty()) {
206 os << "caption={" << caption << '}';
207 if (!param_string.empty())
210 os << from_utf8(param_string) << "]\n";
214 << "\n\\end{lstlisting}\n\\endgroup\n";
222 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
224 switch (cmd.action) {
226 case LFUN_INSET_MODIFY: {
227 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
230 case LFUN_INSET_DIALOG_UPDATE:
231 InsetListingsMailer(*this).updateDialog(&cur.bv());
233 case LFUN_MOUSE_RELEASE: {
234 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
235 InsetListingsMailer(*this).showDialog(&cur.bv());
238 InsetERT::doDispatch(cur, cmd);
242 InsetERT::doDispatch(cur, cmd);
248 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
249 FuncStatus & status) const
251 switch (cmd.action) {
252 case LFUN_INSET_DIALOG_UPDATE:
253 status.enabled(true);
255 case LFUN_CAPTION_INSERT:
256 status.enabled(!params().isInline());
259 return InsetERT::getStatus(cur, cmd, status);
264 void InsetListings::setButtonLabel()
267 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
271 bool InsetListings::metrics(MetricsInfo & mi, Dimension & dim) const
273 Font tmpfont = mi.base.font;
274 getDrawFont(mi.base.font);
275 mi.base.font.realize(tmpfont);
276 InsetCollapsable::metrics(mi, dim);
277 mi.base.font = tmpfont;
278 bool const changed = dim_ != dim;
284 void InsetListings::draw(PainterInfo & pi, int x, int y) const
286 Font tmpfont = pi.base.font;
287 getDrawFont(pi.base.font);
288 pi.base.font.realize(tmpfont);
289 InsetCollapsable::draw(pi, x, y);
290 pi.base.font = tmpfont;
294 void InsetListings::validate(LaTeXFeatures & features) const
296 features.require("listings");
297 InsetERT::validate(features);
301 bool InsetListings::showInsetDialog(BufferView * bv) const
303 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
308 void InsetListings::getDrawFont(Font & font) const
310 font = Font(Font::ALL_INHERIT, latex_language);
311 font.setFamily(Font::TYPEWRITER_FAMILY);
312 // FIXME: define Color::listing?
313 font.setColor(Color::foreground);
317 docstring InsetListings::getCaption(Buffer const & buf,
318 OutputParams const & runparams) const
320 if (paragraphs().empty())
323 ParagraphList::const_iterator pit = paragraphs().begin();
324 for (; pit != paragraphs().end(); ++pit) {
325 InsetList::const_iterator it = pit->insetlist.begin();
326 for (; it != pit->insetlist.end(); ++it) {
327 Inset & inset = *it->inset;
328 if (inset.lyxCode() == Inset::CAPTION_CODE) {
329 odocstringstream ods;
331 static_cast<InsetCaption *>(it->inset);
332 ins->getOptArg(buf, ods, runparams);
333 ins->getArgument(buf, ods, runparams);
342 string const InsetListingsMailer::name_("listings");
344 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
349 string const InsetListingsMailer::inset2string(Buffer const &) const
351 return params2string(inset_.params());
355 void InsetListingsMailer::string2params(string const & in,
356 InsetListingsParams & params)
358 params = InsetListingsParams();
361 istringstream data(in);
364 // discard "listings", which is only used to determine inset
371 InsetListingsMailer::params2string(InsetListingsParams const & params)
374 data << name_ << " ";