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"
24 #include "support/gettext.h"
25 #include "InsetList.h"
27 #include "MetricsInfo.h"
28 #include "TextClass.h"
30 #include "support/docstream.h"
31 #include "support/lstrings.h"
33 #include <boost/regex.hpp>
38 using namespace lyx::support;
44 char const lstinline_delimiters[] =
45 "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
47 InsetListings::InsetListings(Buffer const & buf, InsetListingsParams const & par)
48 : InsetCollapsable(buf, par.status())
52 InsetListings::~InsetListings()
54 InsetListingsMailer(*this).hideDialog();
58 Inset::DisplayType InsetListings::display() const
60 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
64 void InsetListings::updateLabels(ParIterator const & it)
66 Counters & cnts = buffer().params().documentClass().counters();
67 string const saveflt = cnts.current_float();
69 // Tell to captions what the current float is
70 cnts.current_float("listing");
72 InsetCollapsable::updateLabels(it);
75 cnts.current_float(saveflt);
79 void InsetListings::write(ostream & os) const
81 os << "listings" << "\n";
82 InsetListingsParams const & par = params();
83 // parameter string is encoded to be a valid lyx token.
84 string opt = par.encodedString();
86 os << "lstparams \"" << opt << "\"\n";
88 os << "inline true\n";
90 os << "inline false\n";
91 InsetCollapsable::write(os);
95 void InsetListings::read(Lexer & lex)
99 string const token = lex.getString();
100 if (token == "lstparams") {
102 string const value = lex.getString();
103 params().fromEncodedString(value);
104 } else if (token == "inline") {
106 params().setInline(lex.getBool());
108 // no special option, push back 'status' etc
109 lex.pushToken(token);
113 InsetCollapsable::read(lex);
117 docstring InsetListings::editMessage() const
119 return _("Opened Listing Inset");
123 int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
125 string param_string = params().params();
126 // NOTE: I use {} to quote text, which is an experimental feature
127 // of the listings package (see page 25 of the manual)
129 bool isInline = params().isInline();
130 // get the paragraphs. We can not output them directly to given odocstream
131 // because we can not yet determine the delimiter character of \lstinline
133 ParagraphList::const_iterator par = paragraphs().begin();
134 ParagraphList::const_iterator end = paragraphs().end();
137 pos_type siz = par->size();
138 bool captionline = false;
139 for (pos_type i = 0; i < siz; ++i) {
140 if (i == 0 && par->isInset(i) && i + 1 == siz)
142 // ignore all struck out text and (caption) insets
143 if (par->isDeleted(i) || par->isInset(i))
145 code += par->getChar(i);
148 // for the inline case, if there are multiple paragraphs
149 // they are simply joined. Otherwise, expect latex errors.
150 if (par != end && !isInline && !captionline) {
156 char const * delimiter = lstinline_delimiters;
157 for (; delimiter != '\0'; ++delimiter)
158 if (!contains(code, *delimiter))
160 // This code piece contains all possible special character? !!!
161 // Replace ! with a warning message and use ! as delimiter.
162 if (*delimiter == '\0') {
163 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
164 delimiter = lstinline_delimiters;
166 if (param_string.empty())
167 os << "\\lstinline" << *delimiter;
169 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
173 OutputParams rp = runparams;
174 // FIXME: the line below would fix bug 4182,
175 // but real_current_font moved to cursor.
176 //rp.local_font = &text_.real_current_font;
177 rp.moving_arg = true;
178 docstring const caption = getCaption(rp);
179 runparams.encoding = rp.encoding;
180 if (param_string.empty() && caption.empty())
181 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
183 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
184 if (!caption.empty()) {
185 os << "caption={" << caption << '}';
186 if (!param_string.empty())
189 os << from_utf8(param_string) << "]\n";
192 os << code << "\n\\end{lstlisting}\n\\endgroup\n";
200 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
202 switch (cmd.action) {
204 case LFUN_INSET_MODIFY: {
205 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
208 case LFUN_INSET_DIALOG_UPDATE:
209 InsetListingsMailer(*this).updateDialog(&cur.bv());
211 case LFUN_MOUSE_RELEASE: {
212 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
213 InsetListingsMailer(*this).showDialog(&cur.bv());
216 InsetCollapsable::doDispatch(cur, cmd);
220 InsetCollapsable::doDispatch(cur, cmd);
226 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
227 FuncStatus & status) const
229 switch (cmd.action) {
230 case LFUN_INSET_DIALOG_UPDATE:
231 status.enabled(true);
233 case LFUN_CAPTION_INSERT:
234 status.enabled(!params().isInline());
237 return InsetCollapsable::getStatus(cur, cmd, status);
242 void InsetListings::setButtonLabel()
245 if (decoration() == InsetLayout::Classic)
246 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
248 setLabel(getNewLabel(_("Listing")));
252 void InsetListings::validate(LaTeXFeatures & features) const
254 features.require("listings");
255 InsetCollapsable::validate(features);
259 bool InsetListings::showInsetDialog(BufferView * bv) const
261 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
266 docstring InsetListings::getCaption(OutputParams const & runparams) const
268 if (paragraphs().empty())
271 ParagraphList::const_iterator pit = paragraphs().begin();
272 for (; pit != paragraphs().end(); ++pit) {
273 InsetList::const_iterator it = pit->insetList().begin();
274 for (; it != pit->insetList().end(); ++it) {
275 Inset & inset = *it->inset;
276 if (inset.lyxCode() == CAPTION_CODE) {
277 odocstringstream ods;
279 static_cast<InsetCaption *>(it->inset);
280 ins->getOptArg(ods, runparams);
281 ins->getArgument(ods, runparams);
282 // the caption may contain \label{} but the listings
283 // package prefer caption={}, label={}
284 docstring cap = ods.str();
285 if (!contains(to_utf8(cap), "\\label{"))
288 // blah1\label{blah2} blah3
290 // blah1 blah3},label={blah2
292 // caption={blah1 blah3},label={blah2}
294 // NOTE that } is not allowed in blah2.
295 regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
296 string const new_cap("\\1\\3},label={\\2");
297 return from_utf8(regex_replace(to_utf8(cap), reg, new_cap));
305 string const InsetListingsMailer::name_("listings");
307 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
312 string const InsetListingsMailer::inset2string(Buffer const &) const
314 return params2string(inset_.params());
318 void InsetListingsMailer::string2params(string const & in,
319 InsetListingsParams & params)
321 params = InsetListingsParams();
324 istringstream data(in);
327 // discard "listings", which is only used to determine inset
334 InsetListingsMailer::params2string(InsetListingsParams const & params)
337 data << name_ << " ";