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 FontInfo font = sane_font;
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 : InsetCollapsable(bp, par.status())
69 InsetListings::InsetListings(InsetListings const & in)
70 : InsetCollapsable(in), params_(in.params_)
76 Inset * InsetListings::clone() const
78 return new InsetListings(*this);
82 InsetListings::~InsetListings()
84 InsetListingsMailer(*this).hideDialog();
88 Inset::DisplayType InsetListings::display() const
90 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
94 void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
96 Counters & cnts = buf.params().getTextClass().counters();
97 string const saveflt = cnts.current_float();
99 // Tell to captions what the current float is
100 cnts.current_float("listing");
102 InsetCollapsable::updateLabels(buf, it);
105 cnts.current_float(saveflt);
109 void InsetListings::write(Buffer const & buf, ostream & os) const
111 os << "listings" << "\n";
112 InsetListingsParams const & par = params();
113 // parameter string is encoded to be a valid lyx token.
114 string opt = par.encodedString();
116 os << "lstparams \"" << opt << "\"\n";
118 os << "inline true\n";
120 os << "inline false\n";
121 InsetCollapsable::write(buf, os);
125 void InsetListings::read(Buffer const & buf, Lexer & lex)
129 string const token = lex.getString();
130 if (token == "lstparams") {
132 string const value = lex.getString();
133 params().fromEncodedString(value);
134 } else if (token == "inline") {
136 params().setInline(lex.getBool());
138 // no special option, push back 'status' etc
139 lex.pushToken(token);
143 InsetCollapsable::read(buf, lex);
147 docstring const InsetListings::editMessage() const
149 return _("Opened Listing Inset");
153 int InsetListings::latex(Buffer const & buf, odocstream & os,
154 OutputParams const & runparams) const
156 string param_string = params().params();
157 // NOTE: I use {} to quote text, which is an experimental feature
158 // of the listings package (see page 25 of the manual)
160 bool isInline = params().isInline();
161 // get the paragraphs. We can not output them directly to given odocstream
162 // because we can not yet determine the delimiter character of \lstinline
164 ParagraphList::const_iterator par = paragraphs().begin();
165 ParagraphList::const_iterator end = paragraphs().end();
168 pos_type siz = par->size();
169 bool captionline = false;
170 for (pos_type i = 0; i < siz; ++i) {
171 if (i == 0 && par->isInset(i) && i + 1 == siz)
173 // ignore all struck out text and (caption) insets
174 if (par->isDeleted(i) || par->isInset(i))
176 code += par->getChar(i);
179 // for the inline case, if there are multiple paragraphs
180 // they are simply joined. Otherwise, expect latex errors.
181 if (par != end && !isInline && !captionline) {
187 char const * delimiter = lstinline_delimiters;
188 for (; delimiter != '\0'; ++delimiter)
189 if (!contains(code, *delimiter))
191 // This code piece contains all possible special character? !!!
192 // Replace ! with a warning message and use ! as delimiter.
193 if (*delimiter == '\0') {
194 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
195 delimiter = lstinline_delimiters;
197 if (param_string.empty())
198 os << "\\lstinline" << *delimiter;
200 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
204 OutputParams rp = runparams;
205 // FIXME: the line below would fix bug 4182,
206 // but real_current_font moved to cursor.
207 //rp.local_font = &text_.real_current_font;
208 rp.moving_arg = true;
209 docstring const caption = getCaption(buf, rp);
210 runparams.encoding = rp.encoding;
211 if (param_string.empty() && caption.empty())
212 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
214 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
215 if (!caption.empty()) {
216 os << "caption={" << caption << '}';
217 if (!param_string.empty())
220 os << from_utf8(param_string) << "]\n";
224 << "\n\\end{lstlisting}\n\\endgroup\n";
232 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
234 switch (cmd.action) {
236 case LFUN_INSET_MODIFY: {
237 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
240 case LFUN_INSET_DIALOG_UPDATE:
241 InsetListingsMailer(*this).updateDialog(&cur.bv());
243 case LFUN_MOUSE_RELEASE: {
244 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
245 InsetListingsMailer(*this).showDialog(&cur.bv());
248 InsetCollapsable::doDispatch(cur, cmd);
252 InsetCollapsable::doDispatch(cur, cmd);
258 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
259 FuncStatus & status) const
261 switch (cmd.action) {
262 case LFUN_INSET_DIALOG_UPDATE:
263 status.enabled(true);
265 case LFUN_CAPTION_INSERT:
266 status.enabled(!params().isInline());
269 return InsetCollapsable::getStatus(cur, cmd, status);
274 void InsetListings::setButtonLabel()
277 if (decoration() == Classic)
278 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
280 setLabel(getNewLabel(_("Listing")));
284 void InsetListings::validate(LaTeXFeatures & features) const
286 features.require("listings");
287 InsetCollapsable::validate(features);
291 bool InsetListings::showInsetDialog(BufferView * bv) const
293 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
298 docstring InsetListings::getCaption(Buffer const & buf,
299 OutputParams const & runparams) const
301 if (paragraphs().empty())
304 ParagraphList::const_iterator pit = paragraphs().begin();
305 for (; pit != paragraphs().end(); ++pit) {
306 InsetList::const_iterator it = pit->insetList().begin();
307 for (; it != pit->insetList().end(); ++it) {
308 Inset & inset = *it->inset;
309 if (inset.lyxCode() == CAPTION_CODE) {
310 odocstringstream ods;
312 static_cast<InsetCaption *>(it->inset);
313 ins->getOptArg(buf, ods, runparams);
314 ins->getArgument(buf, ods, runparams);
323 string const InsetListingsMailer::name_("listings");
325 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
330 string const InsetListingsMailer::inset2string(Buffer const &) const
332 return params2string(inset_.params());
336 void InsetListingsMailer::string2params(string const & in,
337 InsetListingsParams & params)
339 params = InsetListingsParams();
342 istringstream data(in);
345 // discard "listings", which is only used to determine inset
352 InsetListingsMailer::params2string(InsetListingsParams const & params)
355 data << name_ << " ";