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"
17 #include "BufferView.h"
18 #include "BufferParams.h"
21 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetCaption.h"
27 #include "LaTeXFeatures.h"
29 #include "output_latex.h"
30 #include "output_xhtml.h"
31 #include "OutputParams.h"
32 #include "TextClass.h"
34 #include "texstream.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40 #include "support/lassert.h"
42 #include "frontends/alert.h"
43 #include "frontends/Application.h"
45 #include "support/regex.h"
50 using namespace lyx::support;
55 InsetListings::InsetListings(Buffer * buf, InsetListingsParams const & par)
56 : InsetCaptionable(buf,"listing")
58 status_ = par.status();
62 InsetListings::~InsetListings()
64 hideDialogs("listings", this);
68 Inset::DisplayType InsetListings::display() const
70 return params().isInline() || params().isFloat() ? Inline : AlignLeft;
74 void InsetListings::write(ostream & os) const
76 os << "listings" << "\n";
77 InsetListingsParams const & par = params();
78 // parameter string is encoded to be a valid lyx token.
79 string opt = par.encodedString();
81 os << "lstparams \"" << opt << "\"\n";
83 os << "inline true\n";
85 os << "inline false\n";
86 InsetCaptionable::write(os);
90 void InsetListings::read(Lexer & lex)
94 string token = lex.getString();
95 if (token == "lstparams") {
97 string const value = lex.getString();
98 params().fromEncodedString(value);
99 } else if (token == "inline") {
101 params().setInline(lex.getBool());
103 // no special option, push back 'status' etc
104 lex.pushToken(token);
108 InsetCaptionable::read(lex);
112 void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
114 string param_string = params().params();
115 // NOTE: I use {} to quote text, which is an experimental feature
116 // of the listings package (see page 25 of the manual)
117 bool const isInline = params().isInline();
118 // get the paragraphs. We can not output them directly to given odocstream
119 // because we can not yet determine the delimiter character of \lstinline
122 ParagraphList::const_iterator par = paragraphs().begin();
123 ParagraphList::const_iterator end = paragraphs().end();
125 bool encoding_switched = false;
126 Encoding const * const save_enc = runparams.encoding;
127 // The listings package cannot deal with multi-byte-encoded
128 // glyphs, except if full-unicode aware backends
129 // such as XeTeX or LuaTeX are used, and with pLaTeX.
130 bool const multibyte_possible = runparams.isFullUnicode()
131 || (buffer().params().encoding().package() == Encoding::japanese
132 && runparams.encoding->package() == Encoding::japanese);
134 if (!multibyte_possible && !runparams.encoding->hasFixedWidth()) {
135 // We need to switch to a singlebyte encoding, due to
136 // the restrictions of the listings package (see above).
137 // This needs to be consistent with
138 // LaTeXFeatures::getTClassI18nPreamble().
139 Language const * const outer_language =
140 (runparams.local_font != 0) ?
141 runparams.local_font->language()
142 : buffer().params().language;
143 // We try if there's a singlebyte encoding for the current
144 // language; if not, fall back to latin1.
145 Encoding const * const lstenc =
146 (outer_language->encoding()->hasFixedWidth()) ?
147 outer_language->encoding()
148 : encodings.fromLyXName("iso8859-1");
149 switchEncoding(os.os(), buffer().params(), runparams, *lstenc, true);
150 runparams.encoding = lstenc;
151 encoding_switched = true;
155 pos_type siz = par->size();
156 bool captionline = false;
157 for (pos_type i = 0; i < siz; ++i) {
158 if (i == 0 && par->isInset(i) && i + 1 == siz)
160 // ignore all struck out text and (caption) insets
161 if (par->isDeleted(i)
162 || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
164 if (par->isInset(i)) {
165 // Currently, this can only be a quote inset
166 // that is output as plain quote here, but
167 // we use more generic code anyway.
168 otexstringstream ots;
169 OutputParams rp = runparams;
171 par->getInset(i)->latex(ots, rp);
175 char_type c = par->getChar(i);
176 // we can only output characters covered by the current
179 if (runparams.encoding->encodable(c))
181 else if (runparams.dryrun) {
182 code += "<" + _("LyX Warning: ")
183 + _("uncodable character") + " '";
184 code += docstring(1, c);
188 } catch (EncodingException & /* e */) {
189 if (runparams.dryrun) {
190 code += "<" + _("LyX Warning: ")
191 + _("uncodable character") + " '";
192 code += docstring(1, c);
199 // for the inline case, if there are multiple paragraphs
200 // they are simply joined. Otherwise, expect latex errors.
201 if (par != end && !isInline && !captionline)
205 static const docstring delimiters =
206 from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
208 size_t pos = delimiters.find_first_not_of(code);
210 // This code piece contains all possible special character? !!!
211 // Replace ! with a warning message and use ! as delimiter.
212 if (pos == string::npos) {
213 docstring delim_error = "<" + _("LyX Warning: ")
214 + _("no more lstline delimiters available") + ">";
215 code = subst(code, from_ascii("!"), delim_error);
217 if (!runparams.dryrun && !runparams.silent) {
218 // FIXME: warning should be passed to the error dialog
219 frontend::Alert::warning(_("Running out of delimiters"),
220 _("For inline program listings, one character must be reserved\n"
221 "as a delimiter. One of the listings, however, uses all available\n"
222 "characters, so none is left for delimiting purposes.\n"
223 "For the time being, I have replaced '!' by a warning, but you\n"
224 "must investigate!"));
227 docstring const delim(1, delimiters[pos]);
229 if (!param_string.empty())
230 os << "[" << from_utf8(param_string) << "]";
231 else if (pos >= delimiters.find('Q'))
232 // We need to terminate the command before the delimiter
234 os << delim << code << delim;
236 OutputParams rp = runparams;
237 rp.moving_arg = true;
238 TexString caption = getCaption(rp);
239 os << breakln << "\\begin{lstlisting}";
240 if (param_string.empty() && caption.str.empty())
246 if (!caption.str.empty()) {
247 os << "caption={" << move(caption) << '}';
248 if (!param_string.empty())
251 os << from_utf8(param_string) << "]\n";
253 os << code << breakln << "\\end{lstlisting}\n";
256 if (encoding_switched){
258 switchEncoding(os.os(), buffer().params(), runparams, *save_enc, true);
259 runparams.encoding = save_enc;
262 if (!uncodable.empty() && !runparams.silent) {
263 // issue a warning about omitted characters
264 // FIXME: should be passed to the error dialog
265 if (!multibyte_possible && !runparams.encoding->hasFixedWidth())
266 frontend::Alert::warning(_("Uncodable characters in listings inset"),
267 bformat(_("The following characters in one of the program listings are\n"
268 "not representable in the current encoding and have been omitted:\n%1$s.\n"
269 "This is due to a restriction of the listings package, which does\n"
270 "not support your encoding '%2$s'.\n"
271 "Toggling 'Use non-TeX fonts' in Document > Settings...\n"
273 uncodable, _(runparams.encoding->guiName())));
275 frontend::Alert::warning(_("Uncodable characters in listings inset"),
276 bformat(_("The following characters in one of the program listings are\n"
277 "not representable in the current encoding and have been omitted:\n%1$s."),
283 docstring InsetListings::xhtml(XHTMLStream & os, OutputParams const & rp) const
285 odocstringstream ods;
286 XHTMLStream out(ods);
288 bool const isInline = params().isInline();
290 out << html::CompTag("br");
292 out << html::StartTag("div", "class='float-listings'");
293 docstring caption = getCaptionHTML(rp);
294 if (!caption.empty())
295 out << html::StartTag("div", "class='listings-caption'")
296 << XHTMLStream::ESCAPE_NONE
297 << caption << html::EndTag("div");
300 InsetLayout const & il = getLayout();
301 string const & tag = il.htmltag();
302 string attr = "class ='listings";
303 string const lang = params().getParamValue("language");
307 out << html::StartTag(tag, attr);
308 OutputParams newrp = rp;
309 newrp.html_disable_captions = true;
310 // We don't want to convert dashes here. That's the only conversion we
311 // do for XHTML, so this is safe.
312 newrp.pass_thru = true;
313 docstring def = InsetText::insetAsXHTML(out, newrp, InsetText::JustText);
314 out << html::EndTag(tag);
317 out << html::CompTag("br");
318 // escaping will already have been done
319 os << XHTMLStream::ESCAPE_NONE << ods.str();
321 out << html::EndTag("div");
322 // In this case, this needs to be deferred, but we'll put it
323 // before anything the text itself deferred.
324 def = ods.str() + '\n' + def;
330 string InsetListings::contextMenuName() const
332 return "context-listings";
336 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
338 switch (cmd.action()) {
340 case LFUN_INSET_MODIFY: {
341 cur.recordUndoInset(this);
342 InsetListings::string2params(to_utf8(cmd.argument()), params());
346 case LFUN_INSET_DIALOG_UPDATE:
347 cur.bv().updateDialog("listings", params2string(params()));
351 InsetCaptionable::doDispatch(cur, cmd);
357 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
358 FuncStatus & status) const
360 switch (cmd.action()) {
361 case LFUN_INSET_MODIFY:
362 case LFUN_INSET_DIALOG_UPDATE:
363 status.setEnabled(true);
365 case LFUN_CAPTION_INSERT: {
366 // the inset outputs at most one caption
367 if (params().isInline() || getCaptionInset()) {
368 status.setEnabled(false);
373 return InsetCaptionable::getStatus(cur, cmd, status);
378 docstring const InsetListings::buttonLabel(BufferView const & bv) const
381 if (decoration() == InsetLayout::CLASSIC)
382 return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
384 return getNewLabel(_("Listing"));
388 void InsetListings::validate(LaTeXFeatures & features) const
390 features.require("listings");
391 features.useInsetLayout(getLayout());
392 string param_string = params().params();
393 if (param_string.find("\\color") != string::npos)
394 features.require("color");
395 InsetCaptionable::validate(features);
399 bool InsetListings::showInsetDialog(BufferView * bv) const
401 bv->showDialog("listings", params2string(params()),
402 const_cast<InsetListings *>(this));
407 TexString InsetListings::getCaption(OutputParams const & runparams) const
409 InsetCaption const * ins = getCaptionInset();
414 ins->getArgs(os, runparams);
415 ins->getArgument(os, runparams);
417 // TODO: The code below should be moved to support, and then the test
418 // in ../tests should be moved there as well.
420 // the caption may contain \label{} but the listings
421 // package prefer caption={}, label={}
422 TexString cap = os.release();
423 if (!contains(cap.str, from_ascii("\\label{")))
426 // blah1\label{blah2} blah3
428 // blah1 blah3},label={blah2
430 // caption={blah1 blah3},label={blah2}
432 // NOTE that } is not allowed in blah2.
433 regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
434 string const new_cap("$1$3},label={$2");
435 // TexString validity: the substitution preserves the number of newlines.
436 // Moreover we assume that $2 does not contain newlines, so that the texrow
437 // information remains accurate.
438 cap.str = from_utf8(regex_replace(to_utf8(cap.str), reg, new_cap));
443 void InsetListings::string2params(string const & in,
444 InsetListingsParams & params)
446 params = InsetListingsParams();
449 istringstream data(in);
452 // discard "listings", which is only used to determine inset
458 string InsetListings::params2string(InsetListingsParams const & params)
461 data << "listings" << ' ';