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"
36 using namespace lyx::support;
41 char const lstinline_delimiters[] =
42 "!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
44 InsetListings::InsetListings(Buffer const & buf, InsetListingsParams const & par)
45 : InsetCollapsable(buf, par.status())
49 InsetListings::~InsetListings()
51 InsetListingsMailer(*this).hideDialog();
55 Inset::DisplayType InsetListings::display() const
57 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
61 void InsetListings::updateLabels(ParIterator const & it)
63 Counters & cnts = buffer().params().documentClass().counters();
64 string const saveflt = cnts.current_float();
66 // Tell to captions what the current float is
67 cnts.current_float("listing");
69 InsetCollapsable::updateLabels(it);
72 cnts.current_float(saveflt);
76 void InsetListings::write(ostream & os) const
78 os << "listings" << "\n";
79 InsetListingsParams const & par = params();
80 // parameter string is encoded to be a valid lyx token.
81 string opt = par.encodedString();
83 os << "lstparams \"" << opt << "\"\n";
85 os << "inline true\n";
87 os << "inline false\n";
88 InsetCollapsable::write(os);
92 void InsetListings::read(Lexer & lex)
96 string const token = lex.getString();
97 if (token == "lstparams") {
99 string const value = lex.getString();
100 params().fromEncodedString(value);
101 } else if (token == "inline") {
103 params().setInline(lex.getBool());
105 // no special option, push back 'status' etc
106 lex.pushToken(token);
110 InsetCollapsable::read(lex);
114 docstring InsetListings::editMessage() const
116 return _("Opened Listing Inset");
120 int InsetListings::latex(odocstream & os, OutputParams const & runparams) const
122 string param_string = params().params();
123 // NOTE: I use {} to quote text, which is an experimental feature
124 // of the listings package (see page 25 of the manual)
126 bool isInline = params().isInline();
127 // get the paragraphs. We can not output them directly to given odocstream
128 // because we can not yet determine the delimiter character of \lstinline
130 ParagraphList::const_iterator par = paragraphs().begin();
131 ParagraphList::const_iterator end = paragraphs().end();
134 pos_type siz = par->size();
135 bool captionline = false;
136 for (pos_type i = 0; i < siz; ++i) {
137 if (i == 0 && par->isInset(i) && i + 1 == siz)
139 // ignore all struck out text and (caption) insets
140 if (par->isDeleted(i) || par->isInset(i))
142 code += par->getChar(i);
145 // for the inline case, if there are multiple paragraphs
146 // they are simply joined. Otherwise, expect latex errors.
147 if (par != end && !isInline && !captionline) {
153 char const * delimiter = lstinline_delimiters;
154 for (; delimiter != '\0'; ++delimiter)
155 if (!contains(code, *delimiter))
157 // This code piece contains all possible special character? !!!
158 // Replace ! with a warning message and use ! as delimiter.
159 if (*delimiter == '\0') {
160 code = subst(code, from_ascii("!"), from_ascii(" WARNING: no lstline delimiter can be used "));
161 delimiter = lstinline_delimiters;
163 if (param_string.empty())
164 os << "\\lstinline" << *delimiter;
166 os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter;
170 OutputParams rp = runparams;
171 // FIXME: the line below would fix bug 4182,
172 // but real_current_font moved to cursor.
173 //rp.local_font = &text_.real_current_font;
174 rp.moving_arg = true;
175 docstring const caption = getCaption(rp);
176 runparams.encoding = rp.encoding;
177 if (param_string.empty() && caption.empty())
178 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
180 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
181 if (!caption.empty()) {
182 os << "caption={" << caption << '}';
183 if (!param_string.empty())
186 os << from_utf8(param_string) << "]\n";
189 os << code << "\n\\end{lstlisting}\n\\endgroup\n";
197 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
199 switch (cmd.action) {
201 case LFUN_INSET_MODIFY: {
202 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
205 case LFUN_INSET_DIALOG_UPDATE:
206 InsetListingsMailer(*this).updateDialog(&cur.bv());
208 case LFUN_MOUSE_RELEASE: {
209 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
210 InsetListingsMailer(*this).showDialog(&cur.bv());
213 InsetCollapsable::doDispatch(cur, cmd);
217 InsetCollapsable::doDispatch(cur, cmd);
223 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
224 FuncStatus & status) const
226 switch (cmd.action) {
227 case LFUN_INSET_DIALOG_UPDATE:
228 status.enabled(true);
230 case LFUN_CAPTION_INSERT:
231 status.enabled(!params().isInline());
234 return InsetCollapsable::getStatus(cur, cmd, status);
239 void InsetListings::setButtonLabel()
242 if (decoration() == InsetLayout::Classic)
243 setLabel(isOpen() ? _("Listing") : getNewLabel(_("Listing")));
245 setLabel(getNewLabel(_("Listing")));
249 void InsetListings::validate(LaTeXFeatures & features) const
251 features.require("listings");
252 InsetCollapsable::validate(features);
256 bool InsetListings::showInsetDialog(BufferView * bv) const
258 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
263 docstring InsetListings::getCaption(OutputParams const & runparams) const
265 if (paragraphs().empty())
268 ParagraphList::const_iterator pit = paragraphs().begin();
269 for (; pit != paragraphs().end(); ++pit) {
270 InsetList::const_iterator it = pit->insetList().begin();
271 for (; it != pit->insetList().end(); ++it) {
272 Inset & inset = *it->inset;
273 if (inset.lyxCode() == CAPTION_CODE) {
274 odocstringstream ods;
276 static_cast<InsetCaption *>(it->inset);
277 ins->getOptArg(ods, runparams);
278 ins->getArgument(ods, runparams);
287 string const InsetListingsMailer::name_("listings");
289 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
294 string const InsetListingsMailer::inset2string(Buffer const &) const
296 return params2string(inset_.params());
300 void InsetListingsMailer::string2params(string const & in,
301 InsetListingsParams & params)
303 params = InsetListingsParams();
306 istringstream data(in);
309 // discard "listings", which is only used to determine inset
316 InsetListingsMailer::params2string(InsetListingsParams const & params)
319 data << name_ << " ";