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 "InsetList.h"
26 #include "MetricsInfo.h"
27 #include "TextClass.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
34 #include <boost/regex.hpp>
39 using namespace lyx::support;
45 char const lstinline_delimiters[] =
46 "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
48 InsetListings::InsetListings(Buffer const & buf, InsetListingsParams const & par)
49 : InsetCollapsable(buf, par.status())
53 InsetListings::~InsetListings()
55 InsetListingsMailer(*this).hideDialog();
59 Inset::DisplayType InsetListings::display() const
61 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
65 void InsetListings::updateLabels(ParIterator const & it)
67 Counters & cnts = buffer().params().documentClass().counters();
68 string const saveflt = cnts.current_float();
70 // Tell to captions what the current float is
71 cnts.current_float("listing");
73 InsetCollapsable::updateLabels(it);
76 cnts.current_float(saveflt);
80 void InsetListings::write(ostream & os) const
82 os << "listings" << "\n";
83 InsetListingsParams const & par = params();
84 // parameter string is encoded to be a valid lyx token.
85 string opt = par.encodedString();
87 os << "lstparams \"" << opt << "\"\n";
89 os << "inline true\n";
91 os << "inline false\n";
92 InsetCollapsable::write(os);
96 void InsetListings::read(Lexer & lex)
100 string const token = lex.getString();
101 if (token == "lstparams") {
103 string const value = lex.getString();
104 params().fromEncodedString(value);
105 } else if (token == "inline") {
107 params().setInline(lex.getBool());
109 // no special option, push back 'status' etc
110 lex.pushToken(token);
114 InsetCollapsable::read(lex);
118 docstring InsetListings::editMessage() const
120 return _("Opened Listing Inset");
124 int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
126 string param_string = params().params();
127 // NOTE: I use {} to quote text, which is an experimental feature
128 // of the listings package (see page 25 of the manual)
130 bool isInline = params().isInline();
131 // get the paragraphs. We can not output them directly to given odocstream
132 // because we can not yet determine the delimiter character of \lstinline
134 ParagraphList::const_iterator par = paragraphs().begin();
135 ParagraphList::const_iterator end = paragraphs().end();
138 pos_type siz = par->size();
139 bool captionline = false;
140 for (pos_type i = 0; i < siz; ++i) {
141 if (i == 0 && par->isInset(i) && i + 1 == siz)
143 // ignore all struck out text and (caption) insets
144 if (par->isDeleted(i) || par->isInset(i))
146 code += par->getChar(i);
149 // for the inline case, if there are multiple paragraphs
150 // they are simply joined. Otherwise, expect latex errors.
151 if (par != end && !isInline && !captionline) {
157 char const * delimiter = lstinline_delimiters;
158 for (; delimiter != '\0'; ++delimiter)
159 if (!contains(code, *delimiter))
161 // This code piece contains all possible special character? !!!
162 // Replace ! with a warning message and use ! as delimiter.
163 if (*delimiter == '\0') {
164 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
165 delimiter = lstinline_delimiters;
167 if (param_string.empty())
168 os << "\\lstinline" << *delimiter;
170 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
174 OutputParams rp = runparams;
175 // FIXME: the line below would fix bug 4182,
176 // but real_current_font moved to cursor.
177 //rp.local_font = &text_.real_current_font;
178 rp.moving_arg = true;
179 docstring const caption = getCaption(rp);
180 runparams.encoding = rp.encoding;
181 if (param_string.empty() && caption.empty())
182 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
184 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
185 if (!caption.empty()) {
186 os << "caption={" << caption << '}';
187 if (!param_string.empty())
190 os << from_utf8(param_string) << "]\n";
193 os << code << "\n\\end{lstlisting}\n\\endgroup\n";
201 docstring InsetListings::contextMenu(BufferView const &, int, int) const
203 return from_ascii("context-listings");
207 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
209 switch (cmd.action) {
211 case LFUN_INSET_MODIFY: {
212 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
215 case LFUN_INSET_DIALOG_UPDATE:
216 InsetListingsMailer(*this).updateDialog(&cur.bv());
219 InsetCollapsable::doDispatch(cur, cmd);
225 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
226 FuncStatus & status) const
228 LYXERR0("CURSOR SIZE: " << cur.depth());
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_ << " ";