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"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "MetricsInfo.h"
24 #include "support/lstrings.h"
33 using std::istringstream;
35 using std::ostringstream;
39 void InsetListings::init()
42 Font font(Font::ALL_SANE);
45 font.setColor(Color::none);
47 text_.current_font.setLanguage(latex_language);
48 text_.real_current_font.setLanguage(latex_language);
52 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
53 : InsetERT(bp, par.status())
59 InsetListings::InsetListings(InsetListings const & in)
60 : InsetERT(in), params_(in.params_)
66 auto_ptr<Inset> InsetListings::doClone() const
68 return auto_ptr<Inset>(new InsetListings(*this));
72 InsetListings::~InsetListings()
74 InsetListingsMailer(*this).hideDialog();
78 Inset::DisplayType InsetListings::display() const
80 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
84 void InsetListings::write(Buffer const & buf, ostream & os) const
86 os << "listings" << "\n";
87 InsetListingsParams const & par = params();
88 // parameter string is encoded to be a valid lyx token.
89 string opt = par.encodedString();
91 os << "lstparams \"" << opt << "\"\n";
93 os << "inline true\n";
95 os << "inline false\n";
96 InsetCollapsable::write(buf, os);
100 void InsetListings::read(Buffer const & buf, Lexer & lex)
104 string const token = lex.getString();
105 if (token == "lstparams") {
107 string const value = lex.getString();
108 params().fromEncodedString(value);
109 } else if (token == "inline") {
111 params().setInline(lex.getBool());
113 // no special option, push back 'status' etc
114 lex.pushToken(token);
118 InsetERT::read(buf, lex);
122 docstring const InsetListings::editMessage() const
124 return _("Opened Listings Inset");
128 int InsetListings::latex(Buffer const & buf, odocstream & os,
129 OutputParams const & runparams) const
131 string param_string = params().encodedString();
132 // NOTE: I use {} to quote text, which is an experimental feature
133 // of the listings package (see page 25 of the manual)
135 bool lstinline = params().isInline();
137 if (param_string.empty())
138 os << "\\lstinline{";
140 os << "\\lstinline[" << from_ascii(param_string) << "]{";
142 docstring const caption = getCaption(buf, runparams);
143 if (param_string.empty() && caption.empty())
144 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
146 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
147 if (!caption.empty()) {
148 os << "caption={" << caption << '}';
149 if (!param_string.empty())
152 os << from_utf8(param_string) << "]\n";
156 ParagraphList::const_iterator par = paragraphs().begin();
157 ParagraphList::const_iterator end = paragraphs().end();
160 pos_type siz = par->size();
161 bool captionline = false;
162 for (pos_type i = 0; i < siz; ++i) {
163 if (i == 0 && par->isInset(i) && i + 1 == siz)
165 // ignore all struck out text and (caption) insets
166 if (par->isDeleted(i) || par->isInset(i))
168 os.put(par->getChar(i));
171 // for the inline case, if there are multiple paragraphs
172 // they are simply joined. Otherwise, expect latex errors.
173 if (par != end && !lstinline && !captionline) {
181 os << "\n\\end{lstlisting}\n\\endgroup\n";
189 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
191 switch (cmd.action) {
193 case LFUN_INSET_MODIFY: {
194 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
197 case LFUN_INSET_DIALOG_UPDATE:
198 InsetListingsMailer(*this).updateDialog(&cur.bv());
200 case LFUN_MOUSE_RELEASE: {
201 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
202 InsetListingsMailer(*this).showDialog(&cur.bv());
205 InsetERT::doDispatch(cur, cmd);
209 InsetERT::doDispatch(cur, cmd);
215 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
216 FuncStatus & status) const
218 switch (cmd.action) {
219 case LFUN_INSET_DIALOG_UPDATE:
220 status.enabled(true);
222 case LFUN_CAPTION_INSERT:
223 status.enabled(!params().isInline());
226 return InsetERT::getStatus(cur, cmd, status);
231 void InsetListings::setButtonLabel()
234 setLabel(isOpen() ? _("Listings") : getNewLabel(_("Listings")));
238 bool InsetListings::metrics(MetricsInfo & mi, Dimension & dim) const
240 Font tmpfont = mi.base.font;
241 getDrawFont(mi.base.font);
242 mi.base.font.realize(tmpfont);
243 InsetCollapsable::metrics(mi, dim);
244 mi.base.font = tmpfont;
245 bool const changed = dim_ != dim;
251 void InsetListings::draw(PainterInfo & pi, int x, int y) const
253 Font tmpfont = pi.base.font;
254 getDrawFont(pi.base.font);
255 pi.base.font.realize(tmpfont);
256 InsetCollapsable::draw(pi, x, y);
257 pi.base.font = tmpfont;
261 void InsetListings::validate(LaTeXFeatures & features) const
263 features.require("listings");
264 InsetERT::validate(features);
268 bool InsetListings::showInsetDialog(BufferView * bv) const
270 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
275 void InsetListings::getDrawFont(Font & font) const
277 font = Font(Font::ALL_INHERIT, latex_language);
278 font.setFamily(Font::TYPEWRITER_FAMILY);
279 // FIXME: define Color::listing?
280 font.setColor(Color::foreground);
284 docstring InsetListings::getCaption(Buffer const & buf,
285 OutputParams const & runparams) const
287 if (paragraphs().empty())
290 ParagraphList::const_iterator pit = paragraphs().begin();
291 for (; pit != paragraphs().end(); ++pit) {
292 InsetList::const_iterator it = pit->insetlist.begin();
293 for (; it != pit->insetlist.end(); ++it) {
294 Inset & inset = *it->inset;
295 if (inset.lyxCode() == Inset::CAPTION_CODE) {
296 odocstringstream ods;
298 static_cast<InsetCaption *>(it->inset);
299 ins->getOptArg(buf, ods, runparams);
300 ins->getArgument(buf, ods, runparams);
309 string const InsetListingsMailer::name_("listings");
311 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
316 string const InsetListingsMailer::inset2string(Buffer const &) const
318 return params2string(inset_.params());
322 void InsetListingsMailer::string2params(string const & in,
323 InsetListingsParams & params)
325 params = InsetListingsParams();
328 istringstream data(in);
331 // discard "listings", which is only used to determine inset
338 InsetListingsMailer::params2string(InsetListingsParams const & params)
341 data << name_ << " ";