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"
40 using support::contains;
43 char const lstinline_delimiters[] =
44 "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
46 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
47 : InsetCollapsable(bp, par.status())
51 InsetListings::InsetListings(InsetListings const & in)
52 : InsetCollapsable(in), params_(in.params_)
56 Inset * InsetListings::clone() const
58 return new InsetListings(*this);
62 InsetListings::~InsetListings()
64 InsetListingsMailer(*this).hideDialog();
68 Inset::DisplayType InsetListings::display() const
70 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
74 void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
76 Counters & cnts = buf.params().getTextClass().counters();
77 string const saveflt = cnts.current_float();
79 // Tell to captions what the current float is
80 cnts.current_float("listing");
82 InsetCollapsable::updateLabels(buf, it);
85 cnts.current_float(saveflt);
89 void InsetListings::write(Buffer const & buf, ostream & os) const
91 os << "listings" << "\n";
92 InsetListingsParams const & par = params();
93 // parameter string is encoded to be a valid lyx token.
94 string opt = par.encodedString();
96 os << "lstparams \"" << opt << "\"\n";
98 os << "inline true\n";
100 os << "inline false\n";
101 InsetCollapsable::write(buf, os);
105 void InsetListings::read(Buffer const & buf, Lexer & lex)
109 string const token = lex.getString();
110 if (token == "lstparams") {
112 string const value = lex.getString();
113 params().fromEncodedString(value);
114 } else if (token == "inline") {
116 params().setInline(lex.getBool());
118 // no special option, push back 'status' etc
119 lex.pushToken(token);
123 InsetCollapsable::read(buf, lex);
127 docstring const InsetListings::editMessage() const
129 return _("Opened Listing Inset");
133 int InsetListings::latex(Buffer const & buf, odocstream & os,
134 OutputParams const & runparams) const
136 string param_string = params().params();
137 // NOTE: I use {} to quote text, which is an experimental feature
138 // of the listings package (see page 25 of the manual)
140 bool isInline = params().isInline();
141 // get the paragraphs. We can not output them directly to given odocstream
142 // because we can not yet determine the delimiter character of \lstinline
144 ParagraphList::const_iterator par = paragraphs().begin();
145 ParagraphList::const_iterator end = paragraphs().end();
148 pos_type siz = par->size();
149 bool captionline = false;
150 for (pos_type i = 0; i < siz; ++i) {
151 if (i == 0 && par->isInset(i) && i + 1 == siz)
153 // ignore all struck out text and (caption) insets
154 if (par->isDeleted(i) || par->isInset(i))
156 code += par->getChar(i);
159 // for the inline case, if there are multiple paragraphs
160 // they are simply joined. Otherwise, expect latex errors.
161 if (par != end && !isInline && !captionline) {
167 char const * delimiter = lstinline_delimiters;
168 for (; delimiter != '\0'; ++delimiter)
169 if (!contains(code, *delimiter))
171 // This code piece contains all possible special character? !!!
172 // Replace ! with a warning message and use ! as delimiter.
173 if (*delimiter == '\0') {
174 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
175 delimiter = lstinline_delimiters;
177 if (param_string.empty())
178 os << "\\lstinline" << *delimiter;
180 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
184 OutputParams rp = runparams;
185 // FIXME: the line below would fix bug 4182,
186 // but real_current_font moved to cursor.
187 //rp.local_font = &text_.real_current_font;
188 rp.moving_arg = true;
189 docstring const caption = getCaption(buf, rp);
190 runparams.encoding = rp.encoding;
191 if (param_string.empty() && caption.empty())
192 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
194 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
195 if (!caption.empty()) {
196 os << "caption={" << caption << '}';
197 if (!param_string.empty())
200 os << from_utf8(param_string) << "]\n";
203 os << code << "\n\\end{lstlisting}\n\\endgroup\n";
211 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
213 switch (cmd.action) {
215 case LFUN_INSET_MODIFY: {
216 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
219 case LFUN_INSET_DIALOG_UPDATE:
220 InsetListingsMailer(*this).updateDialog(&cur.bv());
222 case LFUN_MOUSE_RELEASE: {
223 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
224 InsetListingsMailer(*this).showDialog(&cur.bv());
227 InsetCollapsable::doDispatch(cur, cmd);
231 InsetCollapsable::doDispatch(cur, cmd);
237 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
238 FuncStatus & status) const
240 switch (cmd.action) {
241 case LFUN_INSET_DIALOG_UPDATE:
242 status.enabled(true);
244 case LFUN_CAPTION_INSERT:
245 status.enabled(!params().isInline());
248 return InsetCollapsable::getStatus(cur, cmd, status);
253 void InsetListings::setButtonLabel()
256 if (decoration() == Classic)
257 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
259 setLabel(getNewLabel(_("Listing")));
263 void InsetListings::validate(LaTeXFeatures & features) const
265 features.require("listings");
266 InsetCollapsable::validate(features);
270 bool InsetListings::showInsetDialog(BufferView * bv) const
272 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
277 docstring InsetListings::getCaption(Buffer const & buf,
278 OutputParams const & runparams) const
280 if (paragraphs().empty())
283 ParagraphList::const_iterator pit = paragraphs().begin();
284 for (; pit != paragraphs().end(); ++pit) {
285 InsetList::const_iterator it = pit->insetList().begin();
286 for (; it != pit->insetList().end(); ++it) {
287 Inset & inset = *it->inset;
288 if (inset.lyxCode() == CAPTION_CODE) {
289 odocstringstream ods;
291 static_cast<InsetCaption *>(it->inset);
292 ins->getOptArg(buf, ods, runparams);
293 ins->getArgument(buf, ods, runparams);
302 string const InsetListingsMailer::name_("listings");
304 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
309 string const InsetListingsMailer::inset2string(Buffer const &) const
311 return params2string(inset_.params());
315 void InsetListingsMailer::string2params(string const & in,
316 InsetListingsParams & params)
318 params = InsetListingsParams();
321 istringstream data(in);
324 // discard "listings", which is only used to determine inset
331 InsetListingsMailer::params2string(InsetListingsParams const & params)
334 data << name_ << " ";