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 layout_.labelfont.setColor(Color_none);
53 // FIXME: what to do with those?
54 //text_.current_font.setLanguage(latex_language);
55 //text_.real_current_font.setLanguage(latex_language);
59 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
60 : InsetCollapsable(bp, par.status())
67 InsetListings::InsetListings(InsetListings const & in)
68 : InsetCollapsable(in), params_(in.params_)
74 Inset * InsetListings::clone() const
76 return new InsetListings(*this);
80 InsetListings::~InsetListings()
82 InsetListingsMailer(*this).hideDialog();
86 Inset::DisplayType InsetListings::display() const
88 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
92 void InsetListings::updateLabels(Buffer const & buf, ParIterator const & it)
94 Counters & cnts = buf.params().getTextClass().counters();
95 string const saveflt = cnts.current_float();
97 // Tell to captions what the current float is
98 cnts.current_float("listing");
100 InsetCollapsable::updateLabels(buf, it);
103 cnts.current_float(saveflt);
107 void InsetListings::write(Buffer const & buf, ostream & os) const
109 os << "listings" << "\n";
110 InsetListingsParams const & par = params();
111 // parameter string is encoded to be a valid lyx token.
112 string opt = par.encodedString();
114 os << "lstparams \"" << opt << "\"\n";
116 os << "inline true\n";
118 os << "inline false\n";
119 InsetCollapsable::write(buf, os);
123 void InsetListings::read(Buffer const & buf, Lexer & lex)
127 string const token = lex.getString();
128 if (token == "lstparams") {
130 string const value = lex.getString();
131 params().fromEncodedString(value);
132 } else if (token == "inline") {
134 params().setInline(lex.getBool());
136 // no special option, push back 'status' etc
137 lex.pushToken(token);
141 InsetCollapsable::read(buf, lex);
145 docstring const InsetListings::editMessage() const
147 return _("Opened Listing Inset");
151 int InsetListings::latex(Buffer const & buf, odocstream & os,
152 OutputParams const & runparams) const
154 string param_string = params().params();
155 // NOTE: I use {} to quote text, which is an experimental feature
156 // of the listings package (see page 25 of the manual)
158 bool isInline = params().isInline();
159 // get the paragraphs. We can not output them directly to given odocstream
160 // because we can not yet determine the delimiter character of \lstinline
162 ParagraphList::const_iterator par = paragraphs().begin();
163 ParagraphList::const_iterator end = paragraphs().end();
166 pos_type siz = par->size();
167 bool captionline = false;
168 for (pos_type i = 0; i < siz; ++i) {
169 if (i == 0 && par->isInset(i) && i + 1 == siz)
171 // ignore all struck out text and (caption) insets
172 if (par->isDeleted(i) || par->isInset(i))
174 code += par->getChar(i);
177 // for the inline case, if there are multiple paragraphs
178 // they are simply joined. Otherwise, expect latex errors.
179 if (par != end && !isInline && !captionline) {
185 char const * delimiter = lstinline_delimiters;
186 for (; delimiter != '\0'; ++delimiter)
187 if (!contains(code, *delimiter))
189 // This code piece contains all possible special character? !!!
190 // Replace ! with a warning message and use ! as delimiter.
191 if (*delimiter == '\0') {
192 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
193 delimiter = lstinline_delimiters;
195 if (param_string.empty())
196 os << "\\lstinline" << *delimiter;
198 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
202 OutputParams rp = runparams;
203 // FIXME: the line below would fix bug 4182,
204 // but real_current_font moved to cursor.
205 //rp.local_font = &text_.real_current_font;
206 rp.moving_arg = true;
207 docstring const caption = getCaption(buf, rp);
208 runparams.encoding = rp.encoding;
209 if (param_string.empty() && caption.empty())
210 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
212 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
213 if (!caption.empty()) {
214 os << "caption={" << caption << '}';
215 if (!param_string.empty())
218 os << from_utf8(param_string) << "]\n";
221 os << code << "\n\\end{lstlisting}\n\\endgroup\n";
229 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
231 switch (cmd.action) {
233 case LFUN_INSET_MODIFY: {
234 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
237 case LFUN_INSET_DIALOG_UPDATE:
238 InsetListingsMailer(*this).updateDialog(&cur.bv());
240 case LFUN_MOUSE_RELEASE: {
241 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
242 InsetListingsMailer(*this).showDialog(&cur.bv());
245 InsetCollapsable::doDispatch(cur, cmd);
249 InsetCollapsable::doDispatch(cur, cmd);
255 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
256 FuncStatus & status) const
258 switch (cmd.action) {
259 case LFUN_INSET_DIALOG_UPDATE:
260 status.enabled(true);
262 case LFUN_CAPTION_INSERT:
263 status.enabled(!params().isInline());
266 return InsetCollapsable::getStatus(cur, cmd, status);
271 void InsetListings::setButtonLabel()
274 if (decoration() == Classic)
275 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
277 setLabel(getNewLabel(_("Listing")));
281 void InsetListings::validate(LaTeXFeatures & features) const
283 features.require("listings");
284 InsetCollapsable::validate(features);
288 bool InsetListings::showInsetDialog(BufferView * bv) const
290 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
295 docstring InsetListings::getCaption(Buffer const & buf,
296 OutputParams const & runparams) const
298 if (paragraphs().empty())
301 ParagraphList::const_iterator pit = paragraphs().begin();
302 for (; pit != paragraphs().end(); ++pit) {
303 InsetList::const_iterator it = pit->insetList().begin();
304 for (; it != pit->insetList().end(); ++it) {
305 Inset & inset = *it->inset;
306 if (inset.lyxCode() == CAPTION_CODE) {
307 odocstringstream ods;
309 static_cast<InsetCaption *>(it->inset);
310 ins->getOptArg(buf, ods, runparams);
311 ins->getArgument(buf, ods, runparams);
320 string const InsetListingsMailer::name_("listings");
322 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
327 string const InsetListingsMailer::inset2string(Buffer const &) const
329 return params2string(inset_.params());
333 void InsetListingsMailer::string2params(string const & in,
334 InsetListingsParams & params)
336 params = InsetListingsParams();
339 istringstream data(in);
342 // discard "listings", which is only used to determine inset
349 InsetListingsMailer::params2string(InsetListingsParams const & params)
352 data << name_ << " ";