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"
37 using support::contains;
40 using std::istringstream;
42 using std::ostringstream;
45 char const lstinline_delimiters[] =
46 "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
48 void InsetListings::init()
51 FontInfo font = sane_font;
54 font.setColor(Color_none);
56 // FIXME: what to do with those?
57 //text_.current_font.setLanguage(latex_language);
58 //text_.real_current_font.setLanguage(latex_language);
62 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
63 : InsetCollapsable(bp, par.status())
70 InsetListings::InsetListings(InsetListings const & in)
71 : InsetCollapsable(in), params_(in.params_)
77 Inset * InsetListings::clone() const
79 return new InsetListings(*this);
83 InsetListings::~InsetListings()
85 InsetListingsMailer(*this).hideDialog();
89 Inset::DisplayType InsetListings::display() const
91 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
95 void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
97 Counters & cnts = buf.params().getTextClass().counters();
98 string const saveflt = cnts.current_float();
100 // Tell to captions what the current float is
101 cnts.current_float("listing");
103 InsetCollapsable::updateLabels(buf, it);
106 cnts.current_float(saveflt);
110 void InsetListings::write(Buffer const & buf, ostream & os) const
112 os << "listings" << "\n";
113 InsetListingsParams const & par = params();
114 // parameter string is encoded to be a valid lyx token.
115 string opt = par.encodedString();
117 os << "lstparams \"" << opt << "\"\n";
119 os << "inline true\n";
121 os << "inline false\n";
122 InsetCollapsable::write(buf, os);
126 void InsetListings::read(Buffer const & buf, Lexer & lex)
130 string const token = lex.getString();
131 if (token == "lstparams") {
133 string const value = lex.getString();
134 params().fromEncodedString(value);
135 } else if (token == "inline") {
137 params().setInline(lex.getBool());
139 // no special option, push back 'status' etc
140 lex.pushToken(token);
144 InsetCollapsable::read(buf, lex);
148 docstring const InsetListings::editMessage() const
150 return _("Opened Listing Inset");
154 int InsetListings::latex(Buffer const & buf, odocstream & os,
155 OutputParams const & runparams) const
157 string param_string = params().params();
158 // NOTE: I use {} to quote text, which is an experimental feature
159 // of the listings package (see page 25 of the manual)
161 bool isInline = params().isInline();
162 // get the paragraphs. We can not output them directly to given odocstream
163 // because we can not yet determine the delimiter character of \lstinline
165 ParagraphList::const_iterator par = paragraphs().begin();
166 ParagraphList::const_iterator end = paragraphs().end();
169 pos_type siz = par->size();
170 bool captionline = false;
171 for (pos_type i = 0; i < siz; ++i) {
172 if (i == 0 && par->isInset(i) && i + 1 == siz)
174 // ignore all struck out text and (caption) insets
175 if (par->isDeleted(i) || par->isInset(i))
177 code += par->getChar(i);
180 // for the inline case, if there are multiple paragraphs
181 // they are simply joined. Otherwise, expect latex errors.
182 if (par != end && !isInline && !captionline) {
188 char const * delimiter = lstinline_delimiters;
189 for (; delimiter != '\0'; ++delimiter)
190 if (!contains(code, *delimiter))
192 // This code piece contains all possible special character? !!!
193 // Replace ! with a warning message and use ! as delimiter.
194 if (*delimiter == '\0') {
195 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
196 delimiter = lstinline_delimiters;
198 if (param_string.empty())
199 os << "\\lstinline" << *delimiter;
201 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
205 OutputParams rp = runparams;
206 // FIXME: the line below would fix bug 4182,
207 // but real_current_font moved to cursor.
208 //rp.local_font = &text_.real_current_font;
209 rp.moving_arg = true;
210 docstring const caption = getCaption(buf, rp);
211 runparams.encoding = rp.encoding;
212 if (param_string.empty() && caption.empty())
213 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
215 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
216 if (!caption.empty()) {
217 os << "caption={" << caption << '}';
218 if (!param_string.empty())
221 os << from_utf8(param_string) << "]\n";
224 os << code << "\n\\end{lstlisting}\n\\endgroup\n";
232 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
234 switch (cmd.action) {
236 case LFUN_INSET_MODIFY: {
237 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
240 case LFUN_INSET_DIALOG_UPDATE:
241 InsetListingsMailer(*this).updateDialog(&cur.bv());
243 case LFUN_MOUSE_RELEASE: {
244 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
245 InsetListingsMailer(*this).showDialog(&cur.bv());
248 InsetCollapsable::doDispatch(cur, cmd);
252 InsetCollapsable::doDispatch(cur, cmd);
258 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
259 FuncStatus & status) const
261 switch (cmd.action) {
262 case LFUN_INSET_DIALOG_UPDATE:
263 status.enabled(true);
265 case LFUN_CAPTION_INSERT:
266 status.enabled(!params().isInline());
269 return InsetCollapsable::getStatus(cur, cmd, status);
274 void InsetListings::setButtonLabel()
277 if (decoration() == Classic)
278 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
280 setLabel(getNewLabel(_("Listing")));
284 void InsetListings::validate(LaTeXFeatures & features) const
286 features.require("listings");
287 InsetCollapsable::validate(features);
291 bool InsetListings::showInsetDialog(BufferView * bv) const
293 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
298 docstring InsetListings::getCaption(Buffer const & buf,
299 OutputParams const & runparams) const
301 if (paragraphs().empty())
304 ParagraphList::const_iterator pit = paragraphs().begin();
305 for (; pit != paragraphs().end(); ++pit) {
306 InsetList::const_iterator it = pit->insetList().begin();
307 for (; it != pit->insetList().end(); ++it) {
308 Inset & inset = *it->inset;
309 if (inset.lyxCode() == CAPTION_CODE) {
310 odocstringstream ods;
312 static_cast<InsetCaption *>(it->inset);
313 ins->getOptArg(buf, ods, runparams);
314 ins->getArgument(buf, ods, runparams);
323 string const InsetListingsMailer::name_("listings");
325 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
330 string const InsetListingsMailer::inset2string(Buffer const &) const
332 return params2string(inset_.params());
336 void InsetListingsMailer::string2params(string const & in,
337 InsetListingsParams & params)
339 params = InsetListingsParams();
342 istringstream data(in);
345 // discard "listings", which is only used to determine inset
352 InsetListingsMailer::params2string(InsetListingsParams const & params)
355 data << name_ << " ";