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"
36 using namespace lyx::support;
41 char const lstinline_delimiters[] =
42 "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
44 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
45 : InsetCollapsable(bp, par.status())
49 InsetListings::InsetListings(InsetListings const & in)
50 : InsetCollapsable(in), params_(in.params_)
54 Inset * InsetListings::clone() const
56 return new InsetListings(*this);
60 InsetListings::~InsetListings()
62 InsetListingsMailer(*this).hideDialog();
66 Inset::DisplayType InsetListings::display() const
68 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
72 void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
74 Counters & cnts = buf.params().getTextClass().counters();
75 string const saveflt = cnts.current_float();
77 // Tell to captions what the current float is
78 cnts.current_float("listing");
80 InsetCollapsable::updateLabels(buf, it);
83 cnts.current_float(saveflt);
87 void InsetListings::write(Buffer const & buf, ostream & os) const
89 os << "listings" << "\n";
90 InsetListingsParams const & par = params();
91 // parameter string is encoded to be a valid lyx token.
92 string opt = par.encodedString();
94 os << "lstparams \"" << opt << "\"\n";
96 os << "inline true\n";
98 os << "inline false\n";
99 InsetCollapsable::write(buf, os);
103 void InsetListings::read(Buffer const & buf, Lexer & lex)
107 string const token = lex.getString();
108 if (token == "lstparams") {
110 string const value = lex.getString();
111 params().fromEncodedString(value);
112 } else if (token == "inline") {
114 params().setInline(lex.getBool());
116 // no special option, push back 'status' etc
117 lex.pushToken(token);
121 InsetCollapsable::read(buf, lex);
125 docstring const InsetListings::editMessage() const
127 return _("Opened Listing Inset");
131 int InsetListings::latex(Buffer const & buf, odocstream & os,
132 OutputParams const & runparams) const
134 string param_string = params().params();
135 // NOTE: I use {} to quote text, which is an experimental feature
136 // of the listings package (see page 25 of the manual)
138 bool isInline = params().isInline();
139 // get the paragraphs. We can not output them directly to given odocstream
140 // because we can not yet determine the delimiter character of \lstinline
142 ParagraphList::const_iterator par = paragraphs().begin();
143 ParagraphList::const_iterator end = paragraphs().end();
146 pos_type siz = par->size();
147 bool captionline = false;
148 for (pos_type i = 0; i < siz; ++i) {
149 if (i == 0 && par->isInset(i) && i + 1 == siz)
151 // ignore all struck out text and (caption) insets
152 if (par->isDeleted(i) || par->isInset(i))
154 code += par->getChar(i);
157 // for the inline case, if there are multiple paragraphs
158 // they are simply joined. Otherwise, expect latex errors.
159 if (par != end && !isInline && !captionline) {
165 char const * delimiter = lstinline_delimiters;
166 for (; delimiter != '\0'; ++delimiter)
167 if (!contains(code, *delimiter))
169 // This code piece contains all possible special character? !!!
170 // Replace ! with a warning message and use ! as delimiter.
171 if (*delimiter == '\0') {
172 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
173 delimiter = lstinline_delimiters;
175 if (param_string.empty())
176 os << "\\lstinline" << *delimiter;
178 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
182 OutputParams rp = runparams;
183 // FIXME: the line below would fix bug 4182,
184 // but real_current_font moved to cursor.
185 //rp.local_font = &text_.real_current_font;
186 rp.moving_arg = true;
187 docstring const caption = getCaption(buf, rp);
188 runparams.encoding = rp.encoding;
189 if (param_string.empty() && caption.empty())
190 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
192 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
193 if (!caption.empty()) {
194 os << "caption={" << caption << '}';
195 if (!param_string.empty())
198 os << from_utf8(param_string) << "]\n";
201 os << code << "\n\\end{lstlisting}\n\\endgroup\n";
209 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
211 switch (cmd.action) {
213 case LFUN_INSET_MODIFY: {
214 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
217 case LFUN_INSET_DIALOG_UPDATE:
218 InsetListingsMailer(*this).updateDialog(&cur.bv());
220 case LFUN_MOUSE_RELEASE: {
221 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
222 InsetListingsMailer(*this).showDialog(&cur.bv());
225 InsetCollapsable::doDispatch(cur, cmd);
229 InsetCollapsable::doDispatch(cur, cmd);
235 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
236 FuncStatus & status) const
238 switch (cmd.action) {
239 case LFUN_INSET_DIALOG_UPDATE:
240 status.enabled(true);
242 case LFUN_CAPTION_INSERT:
243 status.enabled(!params().isInline());
246 return InsetCollapsable::getStatus(cur, cmd, status);
251 void InsetListings::setButtonLabel()
254 if (decoration() == Classic)
255 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
257 setLabel(getNewLabel(_("Listing")));
261 void InsetListings::validate(LaTeXFeatures & features) const
263 features.require("listings");
264 InsetCollapsable::validate(features);
268 bool InsetListings::showInsetDialog(BufferView * bv) const
270 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
275 docstring InsetListings::getCaption(Buffer const & buf,
276 OutputParams const & runparams) const
278 if (paragraphs().empty())
281 ParagraphList::const_iterator pit = paragraphs().begin();
282 for (; pit != paragraphs().end(); ++pit) {
283 InsetList::const_iterator it = pit->insetList().begin();
284 for (; it != pit->insetList().end(); ++it) {
285 Inset & inset = *it->inset;
286 if (inset.lyxCode() == CAPTION_CODE) {
287 odocstringstream ods;
289 static_cast<InsetCaption *>(it->inset);
290 ins->getOptArg(buf, ods, runparams);
291 ins->getArgument(buf, ods, runparams);
300 string const InsetListingsMailer::name_("listings");
302 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
307 string const InsetListingsMailer::inset2string(Buffer const &) const
309 return params2string(inset_.params());
313 void InsetListingsMailer::string2params(string const & in,
314 InsetListingsParams & params)
316 params = InsetListingsParams();
319 istringstream data(in);
322 // discard "listings", which is only used to determine inset
329 InsetListingsMailer::params2string(InsetListingsParams const & params)
332 data << name_ << " ";