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 // FIXME: what to do with those?
54 //text_.current_font.setLanguage(latex_language);
55 //text_.real_current_font.setLanguage(latex_language);
59 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
60 : InsetERT(bp, par.status())
66 InsetListings::InsetListings(InsetListings const & in)
67 : InsetERT(in), params_(in.params_)
73 Inset * InsetListings::clone() const
75 return new InsetListings(*this);
79 InsetListings::~InsetListings()
81 InsetListingsMailer(*this).hideDialog();
85 Inset::DisplayType InsetListings::display() const
87 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
91 void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
93 Counters & cnts = buf.params().getTextClass().counters();
94 string const saveflt = cnts.current_float();
96 // Tell to captions what the current float is
97 cnts.current_float("listing");
99 InsetCollapsable::updateLabels(buf, it);
102 cnts.current_float(saveflt);
106 void InsetListings::write(Buffer const & buf, ostream & os) const
108 os << "listings" << "\n";
109 InsetListingsParams const & par = params();
110 // parameter string is encoded to be a valid lyx token.
111 string opt = par.encodedString();
113 os << "lstparams \"" << opt << "\"\n";
115 os << "inline true\n";
117 os << "inline false\n";
118 InsetCollapsable::write(buf, os);
122 void InsetListings::read(Buffer const & buf, Lexer & lex)
126 string const token = lex.getString();
127 if (token == "lstparams") {
129 string const value = lex.getString();
130 params().fromEncodedString(value);
131 } else if (token == "inline") {
133 params().setInline(lex.getBool());
135 // no special option, push back 'status' etc
136 lex.pushToken(token);
140 InsetERT::read(buf, lex);
144 docstring const InsetListings::editMessage() const
146 return _("Opened Listing Inset");
150 int InsetListings::latex(Buffer const & buf, odocstream & os,
151 OutputParams const & runparams) const
153 string param_string = params().params();
154 // NOTE: I use {} to quote text, which is an experimental feature
155 // of the listings package (see page 25 of the manual)
157 bool isInline = params().isInline();
158 // get the paragraphs. We can not output them directly to given odocstream
159 // because we can not yet determine the delimiter character of \lstinline
161 ParagraphList::const_iterator par = paragraphs().begin();
162 ParagraphList::const_iterator end = paragraphs().end();
165 pos_type siz = par->size();
166 bool captionline = false;
167 for (pos_type i = 0; i < siz; ++i) {
168 if (i == 0 && par->isInset(i) && i + 1 == siz)
170 // ignore all struck out text and (caption) insets
171 if (par->isDeleted(i) || par->isInset(i))
173 code += par->getChar(i);
176 // for the inline case, if there are multiple paragraphs
177 // they are simply joined. Otherwise, expect latex errors.
178 if (par != end && !isInline && !captionline) {
184 char const * delimiter = lstinline_delimiters;
185 for (; delimiter != '\0'; ++delimiter)
186 if (!contains(code, *delimiter))
188 // This code piece contains all possible special character? !!!
189 // Replace ! with a warning message and use ! as delimiter.
190 if (*delimiter == '\0') {
191 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
192 delimiter = lstinline_delimiters;
194 if (param_string.empty())
195 os << "\\lstinline" << *delimiter;
197 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
201 docstring const caption = getCaption(buf, runparams);
202 if (param_string.empty() && caption.empty())
203 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
205 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
206 if (!caption.empty()) {
207 os << "caption={" << caption << '}';
208 if (!param_string.empty())
211 os << from_utf8(param_string) << "]\n";
215 << "\n\\end{lstlisting}\n\\endgroup\n";
223 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
225 switch (cmd.action) {
227 case LFUN_INSET_MODIFY: {
228 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
231 case LFUN_INSET_DIALOG_UPDATE:
232 InsetListingsMailer(*this).updateDialog(&cur.bv());
234 case LFUN_MOUSE_RELEASE: {
235 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
236 InsetListingsMailer(*this).showDialog(&cur.bv());
239 InsetERT::doDispatch(cur, cmd);
243 InsetERT::doDispatch(cur, cmd);
249 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
250 FuncStatus & status) const
252 switch (cmd.action) {
253 case LFUN_INSET_DIALOG_UPDATE:
254 status.enabled(true);
256 case LFUN_CAPTION_INSERT:
257 status.enabled(!params().isInline());
260 return InsetERT::getStatus(cur, cmd, status);
265 void InsetListings::setButtonLabel()
268 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
272 bool InsetListings::metrics(MetricsInfo & mi, Dimension & dim) const
274 Font tmpfont = mi.base.font;
275 getDrawFont(mi.base.font);
276 mi.base.font.realize(tmpfont);
277 InsetCollapsable::metrics(mi, dim);
278 mi.base.font = tmpfont;
279 bool const changed = dim_ != dim;
285 void InsetListings::draw(PainterInfo & pi, int x, int y) const
287 Font tmpfont = pi.base.font;
288 getDrawFont(pi.base.font);
289 pi.base.font.realize(tmpfont);
290 InsetCollapsable::draw(pi, x, y);
291 pi.base.font = tmpfont;
295 void InsetListings::validate(LaTeXFeatures & features) const
297 features.require("listings");
298 InsetERT::validate(features);
302 bool InsetListings::showInsetDialog(BufferView * bv) const
304 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
309 void InsetListings::getDrawFont(Font & font) const
311 font = Font(Font::ALL_INHERIT, latex_language);
312 font.setFamily(Font::TYPEWRITER_FAMILY);
313 // FIXME: define Color::listing?
314 font.setColor(Color::foreground);
318 docstring InsetListings::getCaption(Buffer const & buf,
319 OutputParams const & runparams) const
321 if (paragraphs().empty())
324 ParagraphList::const_iterator pit = paragraphs().begin();
325 for (; pit != paragraphs().end(); ++pit) {
326 InsetList::const_iterator it = pit->insetlist.begin();
327 for (; it != pit->insetlist.end(); ++it) {
328 Inset & inset = *it->inset;
329 if (inset.lyxCode() == Inset::CAPTION_CODE) {
330 odocstringstream ods;
332 static_cast<InsetCaption *>(it->inset);
333 ins->getOptArg(buf, ods, runparams);
334 ins->getArgument(buf, ods, runparams);
343 string const InsetListingsMailer::name_("listings");
345 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
350 string const InsetListingsMailer::inset2string(Buffer const &) const
352 return params2string(inset_.params());
356 void InsetListingsMailer::string2params(string const & in,
357 InsetListingsParams & params)
359 params = InsetListingsParams();
362 istringstream data(in);
365 // discard "listings", which is only used to determine inset
372 InsetListingsMailer::params2string(InsetListingsParams const & params)
375 data << name_ << " ";