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"
23 #include "support/lstrings.h"
32 using std::istringstream;
34 using std::ostringstream;
38 void InsetListings::init()
41 // FIXME: define Color::listing?
42 Font font(Font::ALL_SANE);
45 font.setColor(Color::foreground);
47 // FIXME: english_language?
48 text_.current_font.setLanguage(english_language);
49 text_.real_current_font.setLanguage(english_language);
50 // FIXME: why I can not make text of source code black with the following two lines?
51 text_.current_font.setColor(Color::foreground);
52 text_.real_current_font.setColor(Color::foreground);
56 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
57 : InsetERT(bp, par.status())
63 InsetListings::InsetListings(InsetListings const & in)
70 auto_ptr<Inset> InsetListings::doClone() const
72 return auto_ptr<Inset>(new InsetListings(*this));
76 InsetListings::~InsetListings()
78 InsetListingsMailer(*this).hideDialog();
82 bool InsetListings::display() const
84 return !params().isInline();
88 void InsetListings::write(Buffer const & buf, ostream & os) const
90 os << "listings" << "\n";
91 InsetListingsParams const & par = params();
92 // parameter string is encoded to be a valid lyx token.
93 string opt = par.encodedString();
95 os << "lstparams \"" << opt << "\"\n";
97 os << "inline true\n";
99 os << "inline false\n";
100 InsetCollapsable::write(buf, os);
104 void InsetListings::read(Buffer const & buf, Lexer & lex)
108 string const token = lex.getString();
109 if (token == "lstparams") {
111 string const value = lex.getString();
112 params().fromEncodedString(value);
113 } else if (token == "inline") {
115 params().setInline(lex.getBool());
117 // no special option, push back 'status' etc
118 lex.pushToken(token);
122 InsetCollapsable::read(buf, lex);
126 docstring const InsetListings::editMessage() const
128 return _("Opened Listings Inset");
132 int InsetListings::latex(Buffer const & buf, odocstream & os,
133 OutputParams const & runparams) const
135 string param_string = params().encodedString();
136 // NOTE: I use {} to quote text, which is an experimental feature
137 // of the listings package (see page 25 of the manual)
139 bool lstinline = params().isInline();
141 if (param_string.empty())
142 os << "\\lstinline{";
144 os << "\\lstinline[" << from_ascii(param_string) << "]{";
146 docstring const caption = getCaption(buf, runparams);
147 if (param_string.empty() && caption.empty())
148 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
150 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
151 if (!caption.empty()) {
152 os << "caption={" << caption << '}';
153 if (!param_string.empty())
156 os << from_ascii(param_string) << "]\n";
160 ParagraphList::const_iterator par = paragraphs().begin();
161 ParagraphList::const_iterator end = paragraphs().end();
164 pos_type siz = par->size();
165 bool captionline = false;
166 for (pos_type i = 0; i < siz; ++i) {
167 if (i == 0 && par->isInset(i) && i + 1 == siz)
169 // ignore all struck out text and (caption) insets
170 if (par->isDeleted(i) || par->isInset(i))
172 os.put(par->getChar(i));
175 // for the inline case, if there are multiple paragraphs
176 // they are simply joined. Otherwise, expect latex errors.
177 if (par != end && !lstinline && !captionline) {
185 os << "\n\\end{lstlisting}\n\\endgroup\n";
193 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
195 switch (cmd.action) {
197 case LFUN_INSET_MODIFY: {
198 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
201 case LFUN_INSET_DIALOG_UPDATE:
202 InsetListingsMailer(*this).updateDialog(&cur.bv());
204 case LFUN_MOUSE_RELEASE: {
205 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
206 InsetListingsMailer(*this).showDialog(&cur.bv());
209 InsetERT::doDispatch(cur, cmd);
213 InsetERT::doDispatch(cur, cmd);
219 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
220 FuncStatus & status) const
222 switch (cmd.action) {
223 case LFUN_INSET_DIALOG_UPDATE:
224 status.enabled(true);
226 case LFUN_CAPTION_INSERT:
227 status.enabled(!params().isInline());
230 return InsetERT::getStatus(cur, cmd, status);
235 void InsetListings::setButtonLabel()
238 setLabel(isOpen() ? _("Listings") : getNewLabel(_("Listings")));
242 void InsetListings::validate(LaTeXFeatures & features) const
244 features.require("listings");
245 InsetERT::validate(features);
249 bool InsetListings::showInsetDialog(BufferView * bv) const
251 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
256 void InsetListings::getDrawFont(Font & font) const
258 font = Font(Font::ALL_INHERIT, english_language);
259 font.setFamily(Font::TYPEWRITER_FAMILY);
260 font.setColor(Color::foreground);
264 docstring InsetListings::getCaption(Buffer const & buf,
265 OutputParams const & runparams) const
267 if (paragraphs().empty())
270 ParagraphList::const_iterator pit = paragraphs().begin();
271 for (; pit != paragraphs().end(); ++pit) {
272 InsetList::const_iterator it = pit->insetlist.begin();
273 for (; it != pit->insetlist.end(); ++it) {
274 Inset & inset = *it->inset;
275 if (inset.lyxCode() == Inset::CAPTION_CODE) {
276 odocstringstream ods;
278 static_cast<InsetCaption *>(it->inset);
279 ins->getOptArg(buf, ods, runparams);
280 ins->getArgument(buf, ods, runparams);
289 string const InsetListingsMailer::name_("listings");
291 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
296 string const InsetListingsMailer::inset2string(Buffer const &) const
298 return params2string(inset_.params());
302 void InsetListingsMailer::string2params(string const & in,
303 InsetListingsParams & params)
305 params = InsetListingsParams();
308 istringstream data(in);
311 // discard "listings", which is only used to determine inset
318 InsetListingsMailer::params2string(InsetListingsParams const & params)
321 data << name_ << " ";