2 * \file InsetListings.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetListings.h"
17 #include "DispatchResult.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
21 #include "support/lstrings.h"
30 using std::istringstream;
32 using std::ostringstream;
36 void InsetListings::init()
39 // FIXME: define Color::listing?
40 Font font(Font::ALL_SANE);
43 font.setColor(Color::foreground);
45 // FIXME: english_language?
46 text_.current_font.setLanguage(english_language);
47 text_.real_current_font.setLanguage(english_language);
48 // FIXME: why I can not make text of source code black with the following two lines?
49 text_.current_font.setColor(Color::foreground);
50 text_.real_current_font.setColor(Color::foreground);
54 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
55 : InsetERT(bp, par.status())
61 InsetListings::InsetListings(InsetListings const & in)
68 auto_ptr<Inset> InsetListings::doClone() const
70 return auto_ptr<Inset>(new InsetListings(*this));
74 InsetListings::~InsetListings()
76 InsetListingsMailer(*this).hideDialog();
80 bool InsetListings::display() const
82 return !params().isInline();
86 void InsetListings::write(Buffer const & buf, ostream & os) const
88 os << "listings" << "\n";
89 InsetListingsParams const & par = params();
90 // parameter string is encoded to be a valid lyx token.
91 string opt = par.encodedString();
93 os << "lstparams \"" << opt << "\"\n";
95 os << "inline true\n";
97 os << "inline false\n";
98 InsetCollapsable::write(buf, os);
102 void InsetListings::read(Buffer const & buf, Lexer & lex)
106 string const token = lex.getString();
107 if (token == "lstparams") {
109 string const value = lex.getString();
110 params().fromEncodedString(value);
111 } else if (token == "inline") {
113 params().setInline(lex.getBool());
115 // no special option, push back 'status' etc
116 lex.pushToken(token);
120 InsetCollapsable::read(buf, lex);
124 docstring const InsetListings::editMessage() const
126 return _("Opened Listings Inset");
130 int InsetListings::latex(Buffer const &, odocstream & os,
131 OutputParams const &) const
133 string param_string = params().encodedString();
134 // NOTE: I use {} to quote text, which is an experimental feature
135 // of the listings package (see page 25 of the manual)
137 bool lstinline = params().isInline();
139 if (param_string.empty())
140 os << "\\lstinline{";
142 os << "\\lstinline[" << from_ascii(param_string) << "]{";
144 if (param_string.empty())
145 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
147 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[" << from_ascii(param_string) << "]\n";
150 ParagraphList::const_iterator par = paragraphs().begin();
151 ParagraphList::const_iterator end = paragraphs().end();
154 pos_type siz = par->size();
155 for (pos_type i = 0; i < siz; ++i) {
156 // ignore all struck out text
157 if (par->isDeleted(i))
159 os.put(par->getChar(i));
162 // for the inline case, if there are multiple paragraphs
163 // they are simply joined. Otherwise, expect latex errors.
164 if (par != end && !lstinline) {
172 os << "\n\\end{lstlisting}\n\\endgroup\n";
180 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
182 switch (cmd.action) {
184 case LFUN_INSET_MODIFY: {
185 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
188 case LFUN_INSET_DIALOG_UPDATE:
189 InsetListingsMailer(*this).updateDialog(&cur.bv());
191 case LFUN_MOUSE_RELEASE: {
192 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
193 InsetListingsMailer(*this).showDialog(&cur.bv());
196 InsetERT::doDispatch(cur, cmd);
200 InsetERT::doDispatch(cur, cmd);
206 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
207 FuncStatus & status) const
209 switch (cmd.action) {
210 case LFUN_INSET_DIALOG_UPDATE:
211 status.enabled(true);
214 return InsetERT::getStatus(cur, cmd, status);
219 void InsetListings::setButtonLabel()
222 setLabel(isOpen() ? _("Listings") : getNewLabel(_("Listings")));
226 void InsetListings::validate(LaTeXFeatures & features) const
228 features.require("listings");
229 InsetERT::validate(features);
233 bool InsetListings::showInsetDialog(BufferView * bv) const
235 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
240 void InsetListings::getDrawFont(Font & font) const
242 font = Font(Font::ALL_INHERIT, english_language);
243 font.setFamily(Font::TYPEWRITER_FAMILY);
244 font.setColor(Color::foreground);
248 string const InsetListingsMailer::name_("listings");
250 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
255 string const InsetListingsMailer::inset2string(Buffer const &) const
257 return params2string(inset_.params());
261 void InsetListingsMailer::string2params(string const & in,
262 InsetListingsParams & params)
264 params = InsetListingsParams();
267 istringstream data(in);
270 // discard "listings", which is only used to determine inset
277 InsetListingsMailer::params2string(InsetListingsParams const & params)
280 data << name_ << " ";