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()
38 // FIXME: define Color::listing?
39 Font font(Font::ALL_SANE);
42 font.setColor(Color::foreground);
44 // FIXME: english_language?
45 text_.current_font.setLanguage(english_language);
46 text_.real_current_font.setLanguage(english_language);
47 // FIXME: why I can not make text of source code black with the following two lines?
48 text_.current_font.setColor(Color::foreground);
49 text_.real_current_font.setColor(Color::foreground);
53 InsetListings::InsetListings(BufferParams const & bp, InsetListingsParams const & par)
54 : InsetERT(bp, par.status())
60 InsetListings::InsetListings(InsetListings const & in)
67 auto_ptr<Inset> InsetListings::doClone() const
69 return auto_ptr<Inset>(new InsetListings(*this));
73 InsetListings::~InsetListings()
75 InsetListingsMailer(*this).hideDialog();
79 bool InsetListings::display() const
81 return !params().isInline();
85 void InsetListings::write(Buffer const & buf, ostream & os) const
87 os << "listings" << "\n";
88 InsetListingsParams const & par = params();
89 // parameter string is encoded to be a valid lyx token.
90 string opt = par.encodedString();
92 os << "lstparams \"" << opt << "\"\n";
94 os << "inline true\n";
96 os << "inline false\n";
97 InsetCollapsable::write(buf, os);
101 void InsetListings::read(Buffer const & buf, Lexer & lex)
105 string const token = lex.getString();
106 if (token == "lstparams") {
108 string const value = lex.getString();
109 params().fromEncodedString(value);
110 } else if (token == "inline") {
112 params().setInline(lex.getBool());
114 // no special option, push back 'status' etc
115 lex.pushToken(token);
119 InsetCollapsable::read(buf, lex);
123 docstring const InsetListings::editMessage() const
125 return _("Opened Listings Inset");
129 int InsetListings::latex(Buffer const &, odocstream & os,
130 OutputParams const &) const
132 string param_string = params().encodedString();
133 // NOTE: I use {} to quote text, which is an experimental feature
134 // of the listings package (see page 25 of the manual)
136 bool lstinline = params().isInline();
138 if (param_string.empty())
139 os << "\\lstinline{";
141 os << "\\lstinline[" << from_ascii(param_string) << "]{";
143 if (param_string.empty())
144 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
146 os << "\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[" << from_ascii(param_string) << "]\n";
149 ParagraphList::const_iterator par = paragraphs().begin();
150 ParagraphList::const_iterator end = paragraphs().end();
153 pos_type siz = par->size();
154 for (pos_type i = 0; i < siz; ++i) {
155 // ignore all struck out text
156 if (par->isDeleted(i))
158 os.put(par->getChar(i));
161 // for the inline case, if there are multiple paragraphs
162 // they are simply joined. Otherwise, expect latex errors.
163 if (par != end && !lstinline) {
171 os << "\n\\end{lstlisting}\n\\endgroup\n";
179 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
181 switch (cmd.action) {
183 case LFUN_INSET_MODIFY: {
184 InsetListingsMailer::string2params(to_utf8(cmd.argument()), params());
187 case LFUN_INSET_DIALOG_UPDATE:
188 InsetListingsMailer(*this).updateDialog(&cur.bv());
190 case LFUN_MOUSE_RELEASE: {
191 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
192 InsetListingsMailer(*this).showDialog(&cur.bv());
195 InsetERT::doDispatch(cur, cmd);
199 InsetERT::doDispatch(cur, cmd);
205 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
206 FuncStatus & status) const
208 switch (cmd.action) {
209 case LFUN_INSET_DIALOG_UPDATE:
210 status.enabled(true);
213 return InsetERT::getStatus(cur, cmd, status);
218 void InsetListings::setButtonLabel()
221 setLabel(isOpen() ? _("Listings") : getNewLabel(_("Listings")));
225 void InsetListings::validate(LaTeXFeatures & features) const
227 features.require("listings");
228 InsetERT::validate(features);
232 bool InsetListings::showInsetDialog(BufferView * bv) const
234 InsetListingsMailer(const_cast<InsetListings &>(*this)).showDialog(bv);
239 void InsetListings::getDrawFont(Font & font) const
241 font = Font(Font::ALL_INHERIT, english_language);
242 font.setFamily(Font::TYPEWRITER_FAMILY);
243 font.setColor(Color::foreground);
247 string const InsetListingsMailer::name_("listings");
249 InsetListingsMailer::InsetListingsMailer(InsetListings & inset)
254 string const InsetListingsMailer::inset2string(Buffer const &) const
256 return params2string(inset_.params());
260 void InsetListingsMailer::string2params(string const & in,
261 InsetListingsParams & params)
263 params = InsetListingsParams();
266 istringstream data(in);
269 // discard "listings", which is only used to determine inset
276 InsetListingsMailer::params2string(InsetListingsParams const & params)
279 data << name_ << " ";