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"
28 #include "TextClass.h"
30 #include "support/docstream.h"
31 #include "support/lstrings.h"
35 using std::istringstream;
37 using std::ostringstream;
43 using support::contains;
46 char const lstinline_delimiters[] =
47 "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
49 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
50 : InsetCollapsable(bp, par.status())
54 InsetListings::InsetListings(InsetListings const & in)
55 : InsetCollapsable(in), params_(in.params_)
59 Inset * InsetListings::clone() const
61 return new InsetListings(*this);
65 InsetListings::~InsetListings()
67 InsetListingsMailer(*this).hideDialog();
71 Inset::DisplayType InsetListings::display() const
73 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
77 void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
79 Counters & cnts = buf.params().getTextClass().counters();
80 string const saveflt = cnts.current_float();
82 // Tell to captions what the current float is
83 cnts.current_float("listing");
85 InsetCollapsable::updateLabels(buf, it);
88 cnts.current_float(saveflt);
92 void InsetListings::write(Buffer const & buf, ostream & os) const
94 os << "listings" << "\n";
95 InsetListingsParams const & par = params();
96 // parameter string is encoded to be a valid lyx token.
97 string opt = par.encodedString();
99 os << "lstparams \"" << opt << "\"\n";
101 os << "inline true\n";
103 os << "inline false\n";
104 InsetCollapsable::write(buf, os);
108 void InsetListings::read(Buffer const & buf, Lexer & lex)
112 string const token = lex.getString();
113 if (token == "lstparams") {
115 string const value = lex.getString();
116 params().fromEncodedString(value);
117 } else if (token == "inline") {
119 params().setInline(lex.getBool());
121 // no special option, push back 'status' etc
122 lex.pushToken(token);
126 InsetCollapsable::read(buf, lex);
130 docstring const InsetListings::editMessage() const
132 return _("Opened Listing Inset");
136 int InsetListings::latex(Buffer const & buf, odocstream & os,
137 OutputParams const & runparams) const
139 string param_string = params().params();
140 // NOTE: I use {} to quote text, which is an experimental feature
141 // of the listings package (see page 25 of the manual)
143 bool isInline = params().isInline();
144 // get the paragraphs. We can not output them directly to given odocstream
145 // because we can not yet determine the delimiter character of \lstinline
147 ParagraphList::const_iterator par = paragraphs().begin();
148 ParagraphList::const_iterator end = paragraphs().end();
151 pos_type siz = par->size();
152 bool captionline = false;
153 for (pos_type i = 0; i < siz; ++i) {
154 if (i == 0 && par->isInset(i) && i + 1 == siz)
156 // ignore all struck out text and (caption) insets
157 if (par->isDeleted(i) || par->isInset(i))
159 code += par->getChar(i);
162 // for the inline case, if there are multiple paragraphs
163 // they are simply joined. Otherwise, expect latex errors.
164 if (par != end && !isInline && !captionline) {
170 char const * delimiter = lstinline_delimiters;
171 for (; delimiter != '\0'; ++delimiter)
172 if (!contains(code, *delimiter))
174 // This code piece contains all possible special character? !!!
175 // Replace ! with a warning message and use ! as delimiter.
176 if (*delimiter == '\0') {
177 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
178 delimiter = lstinline_delimiters;
180 if (param_string.empty())
181 os << "\\lstinline" << *delimiter;
183 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
187 OutputParams rp = runparams;
188 // FIXME: the line below would fix bug 4182,
189 // but real_current_font moved to cursor.
190 //rp.local_font = &text_.real_current_font;
191 rp.moving_arg = true;
192 docstring const caption = getCaption(buf, rp);
193 runparams.encoding = rp.encoding;
194 if (param_string.empty() && caption.empty())
195 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
197 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
198 if (!caption.empty()) {
199 os << "caption={" << caption << '}';
200 if (!param_string.empty())
203 os << from_utf8(param_string) << "]\n";
206 os << code << "\n\\end{lstlisting}\n\\endgroup\n";
214 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
216 switch (cmd.action) {
218 case LFUN_INSET_MODIFY: {
219 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
222 case LFUN_INSET_DIALOG_UPDATE:
223 InsetListingsMailer(*this).updateDialog(&cur.bv());
225 case LFUN_MOUSE_RELEASE: {
226 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
227 InsetListingsMailer(*this).showDialog(&cur.bv());
230 InsetCollapsable::doDispatch(cur, cmd);
234 InsetCollapsable::doDispatch(cur, cmd);
240 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
241 FuncStatus & status) const
243 switch (cmd.action) {
244 case LFUN_INSET_DIALOG_UPDATE:
245 status.enabled(true);
247 case LFUN_CAPTION_INSERT:
248 status.enabled(!params().isInline());
251 return InsetCollapsable::getStatus(cur, cmd, status);
256 void InsetListings::setButtonLabel()
259 if (decoration() == Classic)
260 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
262 setLabel(getNewLabel(_("Listing")));
266 void InsetListings::validate(LaTeXFeatures & features) const
268 features.require("listings");
269 InsetCollapsable::validate(features);
273 bool InsetListings::showInsetDialog(BufferView * bv) const
275 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
280 docstring InsetListings::getCaption(Buffer const & buf,
281 OutputParams const & runparams) const
283 if (paragraphs().empty())
286 ParagraphList::const_iterator pit = paragraphs().begin();
287 for (; pit != paragraphs().end(); ++pit) {
288 InsetList::const_iterator it = pit->insetList().begin();
289 for (; it != pit->insetList().end(); ++it) {
290 Inset & inset = *it->inset;
291 if (inset.lyxCode() == CAPTION_CODE) {
292 odocstringstream ods;
294 static_cast<InsetCaption *>(it->inset);
295 ins->getOptArg(buf, ods, runparams);
296 ins->getArgument(buf, ods, runparams);
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_ << " ";