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/docstream.h"
30 #include "support/lstrings.h"
34 using std::istringstream;
36 using std::ostringstream;
42 using support::contains;
45 char const lstinline_delimiters[] =
46 "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
48 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
49 : InsetCollapsable(bp, par.status())
53 InsetListings::InsetListings(InsetListings const & in)
54 : InsetCollapsable(in), params_(in.params_)
58 Inset * InsetListings::clone() const
60 return new InsetListings(*this);
64 InsetListings::~InsetListings()
66 InsetListingsMailer(*this).hideDialog();
70 Inset::DisplayType InsetListings::display() const
72 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
76 void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
78 Counters & cnts = buf.params().getTextClass().counters();
79 string const saveflt = cnts.current_float();
81 // Tell to captions what the current float is
82 cnts.current_float("listing");
84 InsetCollapsable::updateLabels(buf, it);
87 cnts.current_float(saveflt);
91 void InsetListings::write(Buffer const & buf, ostream & os) const
93 os << "listings" << "\n";
94 InsetListingsParams const & par = params();
95 // parameter string is encoded to be a valid lyx token.
96 string opt = par.encodedString();
98 os << "lstparams \"" << opt << "\"\n";
100 os << "inline true\n";
102 os << "inline false\n";
103 InsetCollapsable::write(buf, os);
107 void InsetListings::read(Buffer const & buf, Lexer & lex)
111 string const token = lex.getString();
112 if (token == "lstparams") {
114 string const value = lex.getString();
115 params().fromEncodedString(value);
116 } else if (token == "inline") {
118 params().setInline(lex.getBool());
120 // no special option, push back 'status' etc
121 lex.pushToken(token);
125 InsetCollapsable::read(buf, lex);
129 docstring const InsetListings::editMessage() const
131 return _("Opened Listing Inset");
135 int InsetListings::latex(Buffer const & buf, odocstream & os,
136 OutputParams const & runparams) const
138 string param_string = params().params();
139 // NOTE: I use {} to quote text, which is an experimental feature
140 // of the listings package (see page 25 of the manual)
142 bool isInline = params().isInline();
143 // get the paragraphs. We can not output them directly to given odocstream
144 // because we can not yet determine the delimiter character of \lstinline
146 ParagraphList::const_iterator par = paragraphs().begin();
147 ParagraphList::const_iterator end = paragraphs().end();
150 pos_type siz = par->size();
151 bool captionline = false;
152 for (pos_type i = 0; i < siz; ++i) {
153 if (i == 0 && par->isInset(i) && i + 1 == siz)
155 // ignore all struck out text and (caption) insets
156 if (par->isDeleted(i) || par->isInset(i))
158 code += par->getChar(i);
161 // for the inline case, if there are multiple paragraphs
162 // they are simply joined. Otherwise, expect latex errors.
163 if (par != end && !isInline && !captionline) {
169 char const * delimiter = lstinline_delimiters;
170 for (; delimiter != '\0'; ++delimiter)
171 if (!contains(code, *delimiter))
173 // This code piece contains all possible special character? !!!
174 // Replace ! with a warning message and use ! as delimiter.
175 if (*delimiter == '\0') {
176 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
177 delimiter = lstinline_delimiters;
179 if (param_string.empty())
180 os << "\\lstinline" << *delimiter;
182 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
186 OutputParams rp = runparams;
187 // FIXME: the line below would fix bug 4182,
188 // but real_current_font moved to cursor.
189 //rp.local_font = &text_.real_current_font;
190 rp.moving_arg = true;
191 docstring const caption = getCaption(buf, rp);
192 runparams.encoding = rp.encoding;
193 if (param_string.empty() && caption.empty())
194 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
196 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
197 if (!caption.empty()) {
198 os << "caption={" << caption << '}';
199 if (!param_string.empty())
202 os << from_utf8(param_string) << "]\n";
205 os << code << "\n\\end{lstlisting}\n\\endgroup\n";
213 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
215 switch (cmd.action) {
217 case LFUN_INSET_MODIFY: {
218 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
221 case LFUN_INSET_DIALOG_UPDATE:
222 InsetListingsMailer(*this).updateDialog(&cur.bv());
224 case LFUN_MOUSE_RELEASE: {
225 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
226 InsetListingsMailer(*this).showDialog(&cur.bv());
229 InsetCollapsable::doDispatch(cur, cmd);
233 InsetCollapsable::doDispatch(cur, cmd);
239 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
240 FuncStatus & status) const
242 switch (cmd.action) {
243 case LFUN_INSET_DIALOG_UPDATE:
244 status.enabled(true);
246 case LFUN_CAPTION_INSERT:
247 status.enabled(!params().isInline());
250 return InsetCollapsable::getStatus(cur, cmd, status);
255 void InsetListings::setButtonLabel()
258 if (decoration() == Classic)
259 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
261 setLabel(getNewLabel(_("Listing")));
265 void InsetListings::validate(LaTeXFeatures & features) const
267 features.require("listings");
268 InsetCollapsable::validate(features);
272 bool InsetListings::showInsetDialog(BufferView * bv) const
274 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
279 docstring InsetListings::getCaption(Buffer const & buf,
280 OutputParams const & runparams) const
282 if (paragraphs().empty())
285 ParagraphList::const_iterator pit = paragraphs().begin();
286 for (; pit != paragraphs().end(); ++pit) {
287 InsetList::const_iterator it = pit->insetList().begin();
288 for (; it != pit->insetList().end(); ++it) {
289 Inset & inset = *it->inset;
290 if (inset.lyxCode() == CAPTION_CODE) {
291 odocstringstream ods;
293 static_cast<InsetCaption *>(it->inset);
294 ins->getOptArg(buf, ods, runparams);
295 ins->getArgument(buf, ods, runparams);
304 string const InsetListingsMailer::name_("listings");
306 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
311 string const InsetListingsMailer::inset2string(Buffer const &) const
313 return params2string(inset_.params());
317 void InsetListingsMailer::string2params(string const & in,
318 InsetListingsParams & params)
320 params = InsetListingsParams();
323 istringstream data(in);
326 // discard "listings", which is only used to determine inset
333 InsetListingsMailer::params2string(InsetListingsParams const & params)
336 data << name_ << " ";