1 /**
2  * \file InsetListings.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11 
12 #include <config.h>
13 
14 #include "InsetListings.h"
15 
16 #include "Buffer.h"
17 #include "BufferView.h"
18 #include "BufferParams.h"
19 #include "Counters.h"
20 #include "Cursor.h"
21 #include "DispatchResult.h"
22 #include "Encoding.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetCaption.h"
26 #include "Language.h"
27 #include "LaTeXFeatures.h"
28 #include "Lexer.h"
29 #include "output_latex.h"
30 #include "output_xhtml.h"
31 #include "OutputParams.h"
32 #include "TextClass.h"
33 #include "TexRow.h"
34 #include "texstream.h"
35 
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"
41 
42 #include "frontends/alert.h"
43 #include "frontends/Application.h"
44 
45 #include "support/regex.h"
46 
47 #include <sstream>
48 
49 using namespace std;
50 using namespace lyx::support;
51 
52 namespace lyx {
53 
54 
InsetListings(Buffer * buf,InsetListingsParams const & par)55 InsetListings::InsetListings(Buffer * buf, InsetListingsParams const & par)
56 	: InsetCaptionable(buf,"listing")
57 {
58 	params_.setMinted(buffer().params().use_minted);
59 	status_ = par.status();
60 }
61 
62 
~InsetListings()63 InsetListings::~InsetListings()
64 {
65 	hideDialogs("listings", this);
66 }
67 
68 
display() const69 Inset::DisplayType InsetListings::display() const
70 {
71 	return params().isInline() || params().isFloat() ? Inline : AlignLeft;
72 }
73 
74 
layoutName() const75 docstring InsetListings::layoutName() const
76 {
77 	if (buffer().params().use_minted)
78 		return from_ascii("MintedListings");
79 	else
80 		return from_ascii("Listings");
81 }
82 
83 
write(ostream & os) const84 void InsetListings::write(ostream & os) const
85 {
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();
90 	if (!opt.empty())
91 		os << "lstparams \"" << opt << "\"\n";
92 	if (par.isInline())
93 		os << "inline true\n";
94 	else
95 		os << "inline false\n";
96 	InsetCaptionable::write(os);
97 }
98 
99 
read(Lexer & lex)100 void InsetListings::read(Lexer & lex)
101 {
102 	while (lex.isOK()) {
103 		lex.next();
104 		string token = lex.getString();
105 		if (token == "lstparams") {
106 			lex.next();
107 			string const value = lex.getString();
108 			params().fromEncodedString(value);
109 		} else if (token == "inline") {
110 			lex.next();
111 			params().setInline(lex.getBool());
112 		} else {
113 			// no special option, push back 'status' etc
114 			lex.pushToken(token);
115 			break;
116 		}
117 	}
118 	InsetCaptionable::read(lex);
119 }
120 
121 
forcedEncoding(Encoding const * inner_enc,Encoding const * outer_enc) const122 Encoding const * InsetListings::forcedEncoding(Encoding const * inner_enc,
123 											   Encoding const * outer_enc) const
124 {
125 	// The listings package cannot deal with multi-byte-encoded
126 	// glyphs, except if full-unicode aware backends
127 	// such as XeTeX or LuaTeX are used, and with pLaTeX.
128 	// Minted can deal with all encodings.
129 	if (buffer().params().use_minted
130 		|| inner_enc->name() == "utf8-plain"
131 		|| (buffer().params().encoding().package() == Encoding::japanese
132 			&& inner_enc->package() == Encoding::japanese)
133 		|| inner_enc->hasFixedWidth())
134 		return 0;
135 
136 	// We try if there's a singlebyte encoding for the outer
137 	// language; if not, fall back to latin1.
138 	return (outer_enc->hasFixedWidth()) ?
139 			outer_enc : encodings.fromLyXName("iso8859-1");
140 }
141 
142 
latex(otexstream & os,OutputParams const & runparams) const143 void InsetListings::latex(otexstream & os, OutputParams const & runparams) const
144 {
145 	string param_string = params().params();
146 	// NOTE: I use {} to quote text, which is an experimental feature
147 	// of the listings package (see page 25 of the manual)
148 	bool const isInline = params().isInline();
149 	bool const use_minted = buffer().params().use_minted;
150 	static regex const reg1("(.*)(basicstyle=\\{)([^\\}]*)(\\\\ttfamily)([^\\}]*)(\\})(.*)");
151 	static regex const reg2("(.*)(basicstyle=\\{)([^\\}]*)(\\\\rmfamily)([^\\}]*)(\\})(.*)");
152 	static regex const reg3("(.*)(basicstyle=\\{)([^\\}]*)(\\\\sffamily)([^\\}]*)(\\})(.*)");
153 	if (runparams.use_polyglossia && runparams.local_font->isRightToLeft()) {
154 		// We need to use the *latin switches (#11554)
155 		smatch sub;
156 		if (regex_match(param_string, sub, reg1))
157 			param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
158 					+ "latin"  + sub.str(5) + sub.str(6) + sub.str(7);
159 		if (regex_match(param_string, sub, reg2))
160 			param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
161 					+ "latin"  + sub.str(5) + sub.str(6) + sub.str(7);
162 		if (regex_match(param_string, sub, reg3))
163 			param_string = sub.str(1) + sub.str(2) + sub.str(3) + sub.str(4)
164 					+ "latin"  + sub.str(5) + sub.str(6) + sub.str(7);
165 	}
166 	string minted_language;
167 	string float_placement;
168 	bool const isfloat = params().isFloat();
169 	if (use_minted && (isfloat || contains(param_string, "language="))) {
170 		// Get float placement and/or language of the code,
171 		// then remove the relative options.
172 		vector<string> opts =
173 			getVectorFromString(param_string, ",", false);
174 		for (size_t i = 0; i < opts.size(); ++i) {
175 			if (prefixIs(opts[i], "float")) {
176 				if (prefixIs(opts[i], "float="))
177 					float_placement = opts[i].substr(6);
178 				opts.erase(opts.begin() + int(i--));
179 			}
180 			else if (prefixIs(opts[i], "language=")) {
181 				minted_language = opts[i].substr(9);
182 				opts.erase(opts.begin() + int(i--));
183 			}
184 		}
185 		param_string = getStringFromVector(opts, ",");
186 	}
187 	// Minted needs a language specification
188 	if (minted_language.empty()) {
189 		// If a language has been set globally, use that,
190 		// otherwise use TeX by default
191 		string const & blp = buffer().params().listings_params;
192 		size_t start = blp.find("language=");
193 		if (start != string::npos) {
194 			start += strlen("language=");
195 			size_t len = blp.find(",", start);
196 			if (len != string::npos)
197 				len -= start;
198 			minted_language = blp.substr(start, len);
199 		} else
200 			minted_language = "TeX";
201 	}
202 
203 	// get the paragraphs. We can not output them directly to given odocstream
204 	// because we can not yet determine the delimiter character of \lstinline
205 	docstring code;
206 	docstring uncodable;
207 	ParagraphList::const_iterator par = paragraphs().begin();
208 	ParagraphList::const_iterator end = paragraphs().end();
209 
210 	bool encoding_switched = false;
211 	Encoding const * const save_enc = runparams.encoding;
212 
213 	Encoding const * const outer_encoding =
214 		(runparams.local_font != 0) ?
215 			runparams.local_font->language()->encoding()
216 			: buffer().params().language->encoding();
217 	Encoding const * fixedlstenc = forcedEncoding(runparams.encoding, outer_encoding);
218 	if (fixedlstenc) {
219 		// We need to switch to a singlebyte encoding, due to
220 		// the restrictions of the listings package (see above).
221 		// This needs to be consistent with
222 		// LaTeXFeatures::getTClassI18nPreamble().
223 		switchEncoding(os.os(), buffer().params(), runparams, *fixedlstenc, true);
224 		runparams.encoding = fixedlstenc;
225 		encoding_switched = true;
226 	}
227 
228 	bool const captionfirst = !isfloat && par->isInset(0)
229 				&& par->getInset(0)->lyxCode() == CAPTION_CODE;
230 
231 	while (par != end) {
232 		pos_type siz = par->size();
233 		bool captionline = false;
234 		for (pos_type i = 0; i < siz; ++i) {
235 			if (i == 0 && par->isInset(i) && i + 1 == siz)
236 				captionline = true;
237 			// ignore all struck out text and (caption) insets
238 			if (par->isDeleted(i)
239 			    || (par->isInset(i) && par->getInset(i)->lyxCode() == CAPTION_CODE))
240 				continue;
241 			if (par->isInset(i)) {
242 				// Currently, this can only be a quote inset
243 				// that is output as plain quote here, but
244 				// we use more generic code anyway.
245 				otexstringstream ots;
246 				OutputParams rp = runparams;
247 				rp.pass_thru = true;
248 				par->getInset(i)->latex(ots, rp);
249 				code += ots.str();
250 				continue;
251 			}
252 			char_type c = par->getChar(i);
253 			// we can only output characters covered by the current
254 			// encoding!
255 			try {
256 				if (runparams.encoding->encodable(c))
257 					code += c;
258 				else if (runparams.dryrun) {
259 					code += "<" + _("LyX Warning: ")
260 					   + _("uncodable character") + " '";
261 					code += docstring(1, c);
262 					code += "'>";
263 				} else
264 					uncodable += c;
265 			} catch (EncodingException & /* e */) {
266 				if (runparams.dryrun) {
267 					code += "<" + _("LyX Warning: ")
268 					   + _("uncodable character") + " '";
269 					code += docstring(1, c);
270 					code += "'>";
271 				} else
272 					uncodable += c;
273 			}
274 		}
275 		++par;
276 		// for the inline case, if there are multiple paragraphs
277 		// they are simply joined. Otherwise, expect latex errors.
278 		if (par != end && !isInline && !captionline)
279 			code += "\n";
280 	}
281 	if (isInline) {
282 		static const docstring delimiters =
283 				from_utf8("!*()-=+|;:'\"`,<.>/?QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm");
284 
285 		size_t pos = delimiters.find_first_not_of(code);
286 
287 		// This code piece contains all possible special character? !!!
288 		// Replace ! with a warning message and use ! as delimiter.
289 		if (pos == string::npos) {
290 			docstring delim_error = "<" + _("LyX Warning: ")
291 				+ _("no more lstline delimiters available") + ">";
292 			code = subst(code, from_ascii("!"), delim_error);
293 			pos = 0;
294 			if (!runparams.dryrun && !runparams.silent) {
295 				// FIXME: warning should be passed to the error dialog
296 				frontend::Alert::warning(_("Running out of delimiters"),
297 				_("For inline program listings, one character must be reserved\n"
298 				  "as a delimiter. One of the listings, however, uses all available\n"
299 				  "characters, so none is left for delimiting purposes.\n"
300 				  "For the time being, I have replaced '!' by a warning, but you\n"
301 				  "must investigate!"));
302 			}
303 		}
304 		docstring const delim(1, delimiters[pos]);
305 		if (use_minted) {
306 			os << "\\mintinline";
307 			if (!param_string.empty())
308 				os << "[" << from_utf8(param_string) << "]";
309 			os << "{" << ascii_lowercase(minted_language) << "}";
310 		} else {
311 			os << "\\lstinline";
312 			if (!param_string.empty())
313 				os << "[" << from_utf8(param_string) << "]";
314 			else if (pos >= delimiters.find('Q'))
315 				// We need to terminate the command before
316 				// the delimiter
317 				os << " ";
318 		}
319 		os << delim << code << delim;
320 	} else if (use_minted) {
321 		OutputParams rp = runparams;
322 		rp.moving_arg = true;
323 		TexString caption = getCaption(rp);
324 		if (isfloat) {
325 			os << breakln << "\\begin{listing}";
326 			if (!float_placement.empty())
327 				os << '[' << float_placement << "]";
328 		} else if (captionfirst && !caption.str.empty()) {
329 			os << breakln << "\\lyxmintcaption[t]{"
330 			   << move(caption) << "}\n";
331 		}
332 		os << breakln << "\\begin{minted}";
333 		if (!param_string.empty())
334 			os << "[" << param_string << "]";
335 		os << "{" << ascii_lowercase(minted_language) << "}\n"
336 		   << code << breakln << "\\end{minted}\n";
337 		if (isfloat) {
338 			if (!caption.str.empty())
339 				os << "\\caption{" << move(caption) << "}\n";
340 			os << "\\end{listing}\n";
341 		} else if (!captionfirst && !caption.str.empty()) {
342 			os << breakln << "\\lyxmintcaption[b]{"
343 			   << move(caption) << "}";
344 		}
345 	} else {
346 		OutputParams rp = runparams;
347 		rp.moving_arg = true;
348 		TexString caption = getCaption(rp);
349 		os << breakln << "\\begin{lstlisting}";
350 		if (param_string.empty() && caption.str.empty())
351 			os << "\n";
352 		else {
353 			if (!runparams.nice)
354 				os << safebreakln;
355 			os << "[";
356 			if (!caption.str.empty()) {
357 				os << "caption={" << move(caption) << '}';
358 				if (!param_string.empty())
359 					os << ',';
360 			}
361 			os << from_utf8(param_string) << "]\n";
362 		}
363 		os << code << breakln << "\\end{lstlisting}\n";
364 	}
365 
366 	if (encoding_switched){
367 		// Switch back
368 		switchEncoding(os.os(), buffer().params(), runparams, *save_enc, true);
369 		runparams.encoding = save_enc;
370 	}
371 
372 	if (!uncodable.empty() && !runparams.silent) {
373 		// issue a warning about omitted characters
374 		// FIXME: should be passed to the error dialog
375 		if (fixedlstenc)
376 			frontend::Alert::warning(_("Uncodable characters in listings inset"),
377 				bformat(_("The following characters in one of the program listings are\n"
378 					  "not representable in the current encoding and have been omitted:\n%1$s.\n"
379 					  "This is due to a restriction of the listings package, which does\n"
380 					  "not support your encoding '%2$s'.\n"
381 					  "Toggling 'Use non-TeX fonts' in Document > Settings...\n"
382 					  "might help."),
383 				uncodable, _(runparams.encoding->guiName())));
384 		else
385 			frontend::Alert::warning(_("Uncodable characters in listings inset"),
386 				bformat(_("The following characters in one of the program listings are\n"
387 					  "not representable in the current encoding and have been omitted:\n%1$s."),
388 				uncodable));
389 	}
390 }
391 
392 
xhtml(XHTMLStream & os,OutputParams const & rp) const393 docstring InsetListings::xhtml(XHTMLStream & os, OutputParams const & rp) const
394 {
395 	odocstringstream ods;
396 	XHTMLStream out(ods);
397 
398 	bool const isInline = params().isInline();
399 	if (isInline)
400 		out << html::CompTag("br");
401 	else {
402 		out << html::StartTag("div", "class='float-listings'");
403 		docstring caption = getCaptionHTML(rp);
404 		if (!caption.empty())
405 			out << html::StartTag("div", "class='listings-caption'")
406 			    << XHTMLStream::ESCAPE_NONE
407 			    << caption << html::EndTag("div");
408 	}
409 
410 	InsetLayout const & il = getLayout();
411 	string const & tag = il.htmltag();
412 	string attr = "class ='listings";
413 	string const lang = params().getParamValue("language");
414 	if (!lang.empty())
415 		attr += " " + lang;
416 	attr += "'";
417 	out << html::StartTag(tag, attr);
418 	OutputParams newrp = rp;
419 	newrp.html_disable_captions = true;
420 	// We don't want to convert dashes here. That's the only conversion we
421 	// do for XHTML, so this is safe.
422 	newrp.pass_thru = true;
423 	docstring def = InsetText::insetAsXHTML(out, newrp, InsetText::JustText);
424 	out << html::EndTag(tag);
425 
426 	if (isInline) {
427 		out << html::CompTag("br");
428 		// escaping will already have been done
429 		os << XHTMLStream::ESCAPE_NONE << ods.str();
430 	} else {
431 		out << html::EndTag("div");
432 		// In this case, this needs to be deferred, but we'll put it
433 		// before anything the text itself deferred.
434 		def = ods.str() + '\n' + def;
435 	}
436 	return def;
437 }
438 
439 
contextMenuName() const440 string InsetListings::contextMenuName() const
441 {
442 	return "context-listings";
443 }
444 
445 
doDispatch(Cursor & cur,FuncRequest & cmd)446 void InsetListings::doDispatch(Cursor & cur, FuncRequest & cmd)
447 {
448 	switch (cmd.action()) {
449 
450 	case LFUN_INSET_MODIFY: {
451 		cur.recordUndoInset(this);
452 		InsetListings::string2params(to_utf8(cmd.argument()), params());
453 		break;
454 	}
455 
456 	case LFUN_INSET_DIALOG_UPDATE:
457 		cur.bv().updateDialog("listings", params2string(params()));
458 		break;
459 
460 	default:
461 		InsetCaptionable::doDispatch(cur, cmd);
462 		break;
463 	}
464 }
465 
466 
getStatus(Cursor & cur,FuncRequest const & cmd,FuncStatus & status) const467 bool InsetListings::getStatus(Cursor & cur, FuncRequest const & cmd,
468 	FuncStatus & status) const
469 {
470 	switch (cmd.action()) {
471 		case LFUN_INSET_MODIFY:
472 		case LFUN_INSET_DIALOG_UPDATE:
473 			status.setEnabled(true);
474 			return true;
475 		case LFUN_CAPTION_INSERT: {
476 			// the inset outputs at most one caption
477 			if (params().isInline() || getCaptionInset()) {
478 				status.setEnabled(false);
479 				return true;
480 			}
481 		}
482 		// fall through
483 		default:
484 			return InsetCaptionable::getStatus(cur, cmd, status);
485 	}
486 }
487 
488 
buttonLabel(BufferView const & bv) const489 docstring const InsetListings::buttonLabel(BufferView const & bv) const
490 {
491 	// FIXME UNICODE
492 	if (decoration() == InsetLayout::CLASSIC)
493 		return isOpen(bv) ? _("Listing") : getNewLabel(_("Listing"));
494 	else
495 		return getNewLabel(_("Listing"));
496 }
497 
498 
validate(LaTeXFeatures & features) const499 void InsetListings::validate(LaTeXFeatures & features) const
500 {
501 	features.useInsetLayout(getLayout());
502 	string param_string = params().params();
503 	if (buffer().params().use_minted) {
504 		features.require("minted");
505 		OutputParams rp = features.runparams();
506 		if (!params().isFloat() && !getCaption(rp).str.empty())
507 			features.require("lyxmintcaption");
508 		if (features.usePolyglossia() && features.hasRTLLanguage())
509 			// minted loads color, but color must be loaded before bidi
510 			// (i.e., polyglossia)
511 			features.require("color");
512 	} else {
513 		features.require("listings");
514 		if (contains(param_string, "\\color"))
515 			features.require("color");
516 	}
517 	InsetCaptionable::validate(features);
518 }
519 
520 
showInsetDialog(BufferView * bv) const521 bool InsetListings::showInsetDialog(BufferView * bv) const
522 {
523 	bv->showDialog("listings", params2string(params()),
524 		const_cast<InsetListings *>(this));
525 	return true;
526 }
527 
528 
getCaption(OutputParams const & runparams) const529 TexString InsetListings::getCaption(OutputParams const & runparams) const
530 {
531 	InsetCaption const * ins = getCaptionInset();
532 	if (ins == 0)
533 		return TexString();
534 
535 	otexstringstream os;
536 	ins->getArgs(os, runparams);
537 	ins->getArgument(os, runparams);
538 
539 	// TODO: The code below should be moved to support, and then the test
540 	//       in ../tests should be moved there as well.
541 
542 	// the caption may contain \label{} but the listings
543 	// package prefer caption={}, label={}
544 	TexString cap = os.release();
545 	if (buffer().params().use_minted
546 	    || !contains(cap.str, from_ascii("\\label{")))
547 		return cap;
548 	// convert from
549 	//     blah1\label{blah2} blah3
550 	// to
551 	//     blah1 blah3},label={blah2
552 	// to form options
553 	//     caption={blah1 blah3},label={blah2}
554 	//
555 	// NOTE that } is not allowed in blah2.
556 	regex const reg("(.*)\\\\label\\{(.*?)\\}(.*)");
557 	string const new_cap("$1$3},label={$2");
558 	// TexString validity: the substitution preserves the number of newlines.
559 	// Moreover we assume that $2 does not contain newlines, so that the texrow
560 	// information remains accurate.
561 	// Replace '\n' with an improbable character from Private Use Area-A
562 	// and then return to '\n' after the regex replacement.
563 	docstring const capstr = subst(cap.str, char_type('\n'), 0xffffd);
564 	cap.str = subst(from_utf8(regex_replace(to_utf8(capstr), reg, new_cap)),
565 			0xffffd, char_type('\n'));
566 	return cap;
567 }
568 
569 
string2params(string const & in,InsetListingsParams & params)570 void InsetListings::string2params(string const & in,
571 				   InsetListingsParams & params)
572 {
573 	params = InsetListingsParams();
574 	if (in.empty())
575 		return;
576 	istringstream data(in);
577 	Lexer lex;
578 	lex.setStream(data);
579 	// discard "listings", which is only used to determine inset
580 	lex.next();
581 	params.read(lex);
582 }
583 
584 
params2string(InsetListingsParams const & params)585 string InsetListings::params2string(InsetListingsParams const & params)
586 {
587 	ostringstream data;
588 	data << "listings" << ' ';
589 	params.write(data);
590 	return data.str();
591 }
592 
593 
594 } // namespace lyx
595