2 * \file InsetListingsParams.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 "InsetListingsParams.h"
19 #include "support/lstrings.h"
20 #include "support/textutils.h"
21 #include "support/convert.h"
23 #include <boost/assert.hpp>
36 using support::bformat;
40 using support::isStrInt;
41 using support::prefixIs;
42 using support::suffixIs;
43 using support::getVectorFromString;
44 using lyx::support::contains;
50 TRUEFALSE, // accept 'true' or 'false'
51 INTEGER, // accept an integer
52 LENGTH, // accept a latex length
53 ONEOF, // accept one of a few values
54 SUBSETOF, // accept a string composed of given characters
58 /// Listings package parameter information.
59 // FIXME: make this class visible outside of this file so that
60 // FIXME: it can be used directly in the frontend and in the LyX format
64 /// Default ctor for STL containers.
65 ListingsParam(): onoff_(false), type_(ALL)
68 ListingsParam(string const & v, bool o, param_type t,
69 string const & i, docstring const & h)
70 : value_(v), onoff_(o), type_(t), info_(i), hint_(h)
72 /// Validate a paramater.
73 /// \retval an empty string if \c par is valid.
74 /// \retval otherwise an explanation WRT to \c par invalidity.
75 docstring validate(string const & par) const;
80 /// for option with value "true", "false".
84 /// "other": option="other"
86 /// "true": option=true
87 /// "false": option=false
88 // FIXME: this is public because of InsetListingParam::addParam()
98 /// info is a \n separated string with allowed values
100 /// info is a string from which par is composed of
101 /// (e.g. floatplacement can be one or more of *tbph)
103 /// information which meaning depends on parameter type.
106 /// a help message that is displayed in the gui.
111 docstring ListingsParam::validate(string const & par) const
113 bool unclosed = false;
115 // braces are allowed
116 if (prefixIs(par, "{") && suffixIs(par, "}"))
117 par2 = par.substr(1, par.size() - 2);
118 else if (prefixIs(par, "{")) {
119 par2 = par.substr(1);
126 if (par2.empty() && !onoff_) {
130 return _("A value is expected.");
133 return _("Unbalanced braces!");
137 if (par2.empty() && !onoff_) {
141 return _("Please specify true or false.");
143 if (par2 != "true" && par2 != "false")
144 return _("Only true or false is allowed.");
146 return _("Unbalanced braces!");
150 if (!isStrInt(par2)) {
154 return _("Please specify an integer value.");
156 if (convert<int>(par2) == 0 && par2[0] != '0')
157 return _("An integer is expected.");
159 return _("Unbalanced braces!");
163 if (par2.empty() && !onoff_) {
167 return _("Please specify a LaTeX length expression.");
169 if (!isValidLength(par2))
170 return _("Invalid LaTeX length expression.");
172 return _("Unbalanced braces!");
176 if (par2.empty() && !onoff_) {
180 return bformat(_("Please specify one of %1$s."),
183 // break value to allowed strings
184 vector<string> lists;
186 for (size_t i = 0; i != info_.size(); ++i) {
187 if (info_[i] == '\n') {
196 // good, find the string
197 if (std::find(lists.begin(), lists.end(), par2) != lists.end()) {
199 return _("Unbalanced braces!");
202 // otherwise, produce a meaningful error message.
203 string matching_names;
204 for (vector<string>::iterator it = lists.begin();
205 it != lists.end(); ++it) {
206 if (it->size() >= par2.size() && it->substr(0, par2.size()) == par2) {
207 if (matching_names.empty())
208 matching_names += *it;
210 matching_names += ", " + *it;
213 if (matching_names.empty())
214 return bformat(_("Try one of %1$s."), from_utf8(info_));
216 return bformat(_("I guess you mean %1$s."), from_utf8(matching_names));
220 if (par2.empty() && !onoff_) {
224 return bformat(_("Please specify one or more of '%1$s'."),
227 for (size_t i = 0; i < par2.size(); ++i)
228 if (info_.find(par2[i], 0) == string::npos)
229 return bformat(_("Should be composed of one or more of %1$s."),
232 return _("Unbalanced braces!");
239 /// languages and language/dialect combinations
240 char const * allowed_languages =
241 "no language\nABAP\n[R/2 4.3]ABAP\n[R/2 5.0]ABAP\n[R/3 3.1]ABAP\n"
242 "[R/3 4.6C]ABAP\n[R/3 6.10]ABAP\nACSL\nAda\n[2005]Ada\n[83]Ada\n"
243 "[95]Ada\nALGOL\n[60]ALGOL\n[68]ALGOL\nAssembler\n"
244 "[Motorola68k]Assembler\n[x86masm]Assembler\nAwk\n[gnu]Awk\n[POSIX]Awk\n"
245 "bash\nBasic\n[Visual]Basic\nC\n[ANSI]C\n[Handel]C\n[Objective]C\n"
246 "[Sharp]C\nC++\n[ANSI]C++\n[GNU]C++\n[ISO]C++\n[Visual]C++\nCaml\n"
247 "[light]Caml\n[Objective]Caml\nClean\nCobol\n[1974]Cobol\n[1985]Cobol\n"
248 "[ibm]Cobol\nComal 80\ncommand.com\n[WinXP]command.com\nComsol\ncsh\n"
249 "Delphi\nEiffel\nElan\nEuphoria\nFortran\n[77]Fortran\n[90]Fortran\n"
250 "[95]Fortran\nGCL\nGnuplot\nHaskell\nHTML\nIDL\n[CORBA]IDL\ninform\n"
251 "Java\n[AspectJ]Java\nJVMIS\nksh\nLingo\nLisp\n[Auto]Lisp\nLogo\n"
252 "make\n[gnu]make\nMathematica\n[1.0]Mathematica\n[3.0]Mathematica\n"
253 "[5.2]Mathematica\nMatlab\nMercury\nMetaPost\nMiranda\nMizar\nML\n"
254 "Modula-2\nMuPAD\nNASTRAN\nOberon-2\nOCL\n[decorative]OCL\n[OMG]OCL\n"
255 "Octave\nOz\nPascal\n[Borland6]Pascal\n[Standard]Pascal\n[XSC]Pascal\n"
256 "Perl\nPHP\nPL/I\nPlasm\nPostScript\nPOV\nProlog\nPromela\nPSTricks\n"
257 "Python\nR\nReduce\nRexx\nRSL\nRuby\nS\n[PLUS]S\nSAS\nScilab\nsh\n"
258 "SHELXL\nSimula\n[67]Simula\n[CII]Simula\n[DEC]Simula\n[IBM]Simula\n"
259 "SPARQL\nSQL\ntcl\n[tk]tcl\nTeX\n[AlLaTeX]TeX\n[common]TeX\n[LaTeX]TeX\n"
260 "[plain]TeX\n[primitive]TeX\nVBScript\nVerilog\nVHDL\n[AMS]VHDL\nVRML\n"
261 "[97]VRML\nXML\nXSLT";
264 /// ListingsParam Validator.
265 /// This class is aimed to be a singleton which is instantiated in
266 /// \c InsetListingsParams::addParam().
267 // FIXME: transfer this validator to the frontend.
268 // FIXME: avoid the use of exception.
274 /// validate a parameter for a given name.
275 /// return an error message if \c par is an invalid parameter.
276 docstring validate(string const & name, string const & par) const;
278 /// return the onoff status of a parameter \c key, if \c key is not found
280 bool onoff(string const & key) const;
283 /// key is the name of the parameter
284 typedef map<string, ListingsParam> ListingsParams;
285 ListingsParams all_params_;
289 ParValidator::ParValidator()
291 docstring const empty_hint;
292 docstring const style_hint = _("Use \\footnotesize, \\small, \\itshape, "
293 "\\ttfamily or something like that");
294 docstring const frame_hint = _("none, leftline, topline, bottomline, lines, "
295 "single, shadowbox or subset of trblTRBL");
296 docstring const frameround_hint = _("Enter four letters (either t = round "
297 "or f = square) for top right, bottom "
298 "right, bottom left and top left corner.");
299 docstring const color_hint = _("Enter something like \\color{white}");
301 /// options copied from page 26 of listings manual
302 // FIXME: add default parameters ... (which is not used now)
303 all_params_["float"] =
304 ListingsParam("false", true, SUBSETOF, "*tbph", empty_hint);
305 all_params_["floatplacement"] =
306 ListingsParam("tbp", false, SUBSETOF, "tbp", empty_hint);
307 all_params_["aboveskip"] =
308 ListingsParam("\\medskipamount", false, LENGTH, "", empty_hint);
309 all_params_["belowskip"] =
310 ListingsParam("\\medskipamount", false, LENGTH, "", empty_hint);
311 all_params_["lineskip"] =
312 ListingsParam("", false, LENGTH, "", empty_hint);
313 all_params_["boxpos"] =
314 ListingsParam("", false, SUBSETOF, "bct", empty_hint);
315 all_params_["print"] =
316 ListingsParam("", false, TRUEFALSE, "", empty_hint);
317 all_params_["firstline"] =
318 ListingsParam("", false, INTEGER, "", empty_hint);
319 all_params_["lastline"] =
320 ListingsParam("", false, INTEGER, "", empty_hint);
321 all_params_["linerange"] =
322 ListingsParam("", false, ALL, "", empty_hint);
323 all_params_["showlines"] =
324 ListingsParam("", false, TRUEFALSE, "", empty_hint);
325 all_params_["emptylines"] =
326 ListingsParam("", false, ALL, "", _(
327 "Expect a number with an optional * before it"));
328 all_params_["gobble"] =
329 ListingsParam("", false, INTEGER, "", empty_hint);
330 all_params_["style"] =
331 ListingsParam("", false, ALL, "", empty_hint);
332 all_params_["language"] =
333 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
334 all_params_["alsolanguage"] =
335 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
336 all_params_["defaultdialect"] =
337 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
338 all_params_["printpod"] =
339 ListingsParam("", false, TRUEFALSE, "", empty_hint);
340 all_params_["usekeywordsintag"] =
341 ListingsParam("", false, TRUEFALSE, "", empty_hint);
342 all_params_["tagstyle"] =
343 ListingsParam("", false, ALL, "", style_hint);
344 all_params_["markfirstintag"] =
345 ListingsParam("", false, ALL, "", style_hint);
346 all_params_["makemacrouse"] =
347 ListingsParam("", false, TRUEFALSE, "", empty_hint);
348 all_params_["basicstyle"] =
349 ListingsParam("", false, ALL, "", style_hint);
350 all_params_["identifierstyle"] =
351 ListingsParam("", false, ALL, "", style_hint);
352 all_params_["commentstyle"] =
353 ListingsParam("", false, ALL, "", style_hint);
354 all_params_["stringstyle"] =
355 ListingsParam("", false, ALL, "", style_hint);
356 all_params_["keywordstyle"] =
357 ListingsParam("", false, ALL, "", style_hint);
358 all_params_["ndkeywordstyle"] =
359 ListingsParam("", false, ALL, "", style_hint);
360 all_params_["classoffset"] =
361 ListingsParam("", false, INTEGER, "", empty_hint);
362 all_params_["texcsstyle"] =
363 ListingsParam("", false, ALL, "", style_hint);
364 all_params_["directivestyle"] =
365 ListingsParam("", false, ALL, "", style_hint);
366 all_params_["emph"] =
367 ListingsParam("", false, ALL, "", empty_hint);
368 all_params_["moreemph"] =
369 ListingsParam("", false, ALL, "", empty_hint);
370 all_params_["deleteemph"] =
371 ListingsParam("", false, ALL, "", empty_hint);
372 all_params_["emphstyle"] =
373 ListingsParam("", false, ALL, "", empty_hint);
374 all_params_["delim"] =
375 ListingsParam("", false, ALL, "", empty_hint);
376 all_params_["moredelim"] =
377 ListingsParam("", false, ALL, "", empty_hint);
378 all_params_["deletedelim"] =
379 ListingsParam("", false, ALL, "", empty_hint);
380 all_params_["extendedchars"] =
381 ListingsParam("", false, TRUEFALSE, "", empty_hint);
382 all_params_["inputencoding"] =
383 ListingsParam("", false, ALL, "", empty_hint);
384 all_params_["upquote"] =
385 ListingsParam("", false, TRUEFALSE, "", empty_hint);
386 all_params_["tabsize"] =
387 ListingsParam("", false, INTEGER, "", empty_hint);
388 all_params_["showtabs"] =
389 ListingsParam("", false, ALL, "", empty_hint);
391 ListingsParam("", false, ALL, "", empty_hint);
392 all_params_["showspaces"] =
393 ListingsParam("", false, TRUEFALSE, "", empty_hint);
394 all_params_["showstringspaces"] =
395 ListingsParam("", false, TRUEFALSE, "", empty_hint);
396 all_params_["formfeed"] =
397 ListingsParam("", false, ALL, "", empty_hint);
398 all_params_["numbers"] =
399 ListingsParam("", false, ONEOF, "none\nleft\nright", empty_hint);
400 all_params_["stepnumber"] =
401 ListingsParam("", false, INTEGER, "", empty_hint);
402 all_params_["numberfirstline"] =
403 ListingsParam("", false, TRUEFALSE, "", empty_hint);
404 all_params_["numberstyle"] =
405 ListingsParam("", false, ALL, "", style_hint);
406 all_params_["numbersep"] =
407 ListingsParam("", false, LENGTH, "", empty_hint);
408 all_params_["numberblanklines"] =
409 ListingsParam("", false, ALL, "", empty_hint);
410 all_params_["firstnumber"] =
411 ListingsParam("", false, ALL, "", _("auto, last or a number"));
412 all_params_["name"] =
413 ListingsParam("", false, ALL, "", empty_hint);
414 all_params_["thelstnumber"] =
415 ListingsParam("", false, ALL, "", empty_hint);
416 all_params_["title"] =
417 ListingsParam("", false, ALL, "", empty_hint);
418 // this option is not handled in the parameter box
419 all_params_["caption"] =
420 ListingsParam("", false, ALL, "", _(
421 "This parameter should not be entered here. Please use the caption "
422 "edit box (when using the child document dialog) or "
423 "menu Insert->Caption (when defining a listing inset)"));
424 // this option is not handled in the parameter box
425 all_params_["label"] =
426 ListingsParam("", false, ALL, "",_(
427 "This parameter should not be entered here. Please use the label "
428 "edit box (when using the child document dialog) or "
429 "menu Insert->Label (when defining a listing inset)"));
430 all_params_["nolol"] =
431 ListingsParam("", false, TRUEFALSE, "", empty_hint);
432 all_params_["captionpos"] =
433 ListingsParam("", false, SUBSETOF, "tb", empty_hint);
434 all_params_["abovecaptionskip"] =
435 ListingsParam("", false, LENGTH, "", empty_hint);
436 all_params_["belowcaptionskip"] =
437 ListingsParam("", false, LENGTH, "", empty_hint);
438 all_params_["linewidth"] =
439 ListingsParam("", false, LENGTH, "", empty_hint);
440 all_params_["xleftmargin"] =
441 ListingsParam("", false, LENGTH, "", empty_hint);
442 all_params_["xrightmargin"] =
443 ListingsParam("", false, LENGTH, "", empty_hint);
444 all_params_["resetmargins"] =
445 ListingsParam("", false, TRUEFALSE, "", empty_hint);
446 all_params_["breaklines"] =
447 ListingsParam("", false, TRUEFALSE, "", empty_hint);
448 all_params_["prebreak"] =
449 ListingsParam("", false, ALL, "", empty_hint);
450 all_params_["postbreak"] =
451 ListingsParam("", false, ALL, "", empty_hint);
452 all_params_["breakindent"] =
453 ListingsParam("", false, LENGTH, "", empty_hint);
454 all_params_["breakautoindent"] =
455 ListingsParam("", false, TRUEFALSE, "", empty_hint);
456 all_params_["frame"] =
457 ListingsParam("", false, ALL, "", frame_hint);
458 all_params_["frameround"] =
459 ListingsParam("", false, SUBSETOF, "tf", frameround_hint);
460 all_params_["framesep"] =
461 ListingsParam("", false, LENGTH, "", empty_hint);
462 all_params_["rulesep"] =
463 ListingsParam("", false, LENGTH, "", empty_hint);
464 all_params_["framerule"] =
465 ListingsParam("", false, LENGTH, "", empty_hint);
466 all_params_["framexleftmargin"] =
467 ListingsParam("", false, LENGTH, "", empty_hint);
468 all_params_["framexrightmargin"] =
469 ListingsParam("", false, LENGTH, "", empty_hint);
470 all_params_["framextopmargin"] =
471 ListingsParam("", false, LENGTH, "", empty_hint);
472 all_params_["framexbottommargin"] =
473 ListingsParam("", false, LENGTH, "", empty_hint);
474 all_params_["backgroundcolor"] =
475 ListingsParam("", false, ALL, "", color_hint );
476 all_params_["rulecolor"] =
477 ListingsParam("", false, ALL, "", color_hint );
478 all_params_["fillcolor"] =
479 ListingsParam("", false, ALL, "", color_hint );
480 all_params_["rulesepcolor"] =
481 ListingsParam("", false, ALL, "", color_hint );
482 all_params_["frameshape"] =
483 ListingsParam("", false, ALL, "", empty_hint);
484 all_params_["index"] =
485 ListingsParam("", false, ALL, "", empty_hint);
486 all_params_["moreindex"] =
487 ListingsParam("", false, ALL, "", empty_hint);
488 all_params_["deleteindex"] =
489 ListingsParam("", false, ALL, "", empty_hint);
490 all_params_["indexstyle"] =
491 ListingsParam("", false, ALL, "", empty_hint);
492 all_params_["columns"] =
493 ListingsParam("", false, ALL, "", empty_hint);
494 all_params_["flexiblecolumns"] =
495 ListingsParam("", false, ALL, "", empty_hint);
496 all_params_["keepspaces"] =
497 ListingsParam("", false, TRUEFALSE, "", empty_hint);
498 all_params_["basewidth"] =
499 ListingsParam("", false, LENGTH, "", empty_hint);
500 all_params_["fontadjust"] =
501 ListingsParam("", true, TRUEFALSE, "", empty_hint);
502 all_params_["texcl"] =
503 ListingsParam("", false, TRUEFALSE, "", empty_hint);
504 all_params_["mathescape"] =
505 ListingsParam("", false, TRUEFALSE, "", empty_hint);
506 all_params_["escapechar"] =
507 ListingsParam("", false, ALL, "", empty_hint);
508 all_params_["escapeinside"] =
509 ListingsParam("", false, ALL, "", empty_hint);
510 all_params_["escepeinside"] =
511 ListingsParam("", false, ALL, "", empty_hint);
512 all_params_["escepebegin"] =
513 ListingsParam("", false, ALL, "", empty_hint);
514 all_params_["escepeend"] =
515 ListingsParam("", false, ALL, "", empty_hint);
516 all_params_["fancyvrb"] =
517 ListingsParam("", false, TRUEFALSE, "", empty_hint);
518 all_params_["fvcmdparams"] =
519 ListingsParam("", false, ALL, "", empty_hint);
520 all_params_["morefvcmdparams"] =
521 ListingsParam("", false, ALL, "", empty_hint);
522 all_params_["keywordsprefix"] =
523 ListingsParam("", false, ALL, "", empty_hint);
524 all_params_["keywords"] =
525 ListingsParam("", false, ALL, "", empty_hint);
526 all_params_["morekeywords"] =
527 ListingsParam("", false, ALL, "", empty_hint);
528 all_params_["deletekeywords"] =
529 ListingsParam("", false, ALL, "", empty_hint);
530 all_params_["ndkeywords"] =
531 ListingsParam("", false, ALL, "", empty_hint);
532 all_params_["morendkeywords"] =
533 ListingsParam("", false, ALL, "", empty_hint);
534 all_params_["deletendkeywords"] =
535 ListingsParam("", false, ALL, "", empty_hint);
536 all_params_["texcs"] =
537 ListingsParam("", false, ALL, "", empty_hint);
538 all_params_["moretexcs"] =
539 ListingsParam("", false, ALL, "", empty_hint);
540 all_params_["deletetexcs"] =
541 ListingsParam("", false, ALL, "", empty_hint);
542 all_params_["directives"] =
543 ListingsParam("", false, ALL, "", empty_hint);
544 all_params_["moredirectives"] =
545 ListingsParam("", false, ALL, "", empty_hint);
546 all_params_["deletedirectives"] =
547 ListingsParam("", false, ALL, "", empty_hint);
548 all_params_["sensitive"] =
549 ListingsParam("", false, ALL, "", empty_hint);
550 all_params_["alsoletter"] =
551 ListingsParam("", false, ALL, "", empty_hint);
552 all_params_["alsodigit"] =
553 ListingsParam("", false, ALL, "", empty_hint);
554 all_params_["alsoother"] =
555 ListingsParam("", false, ALL, "", empty_hint);
556 all_params_["otherkeywords"] =
557 ListingsParam("", false, ALL, "", empty_hint);
559 ListingsParam("", false, ALL, "", empty_hint);
560 all_params_["string"] =
561 ListingsParam("", false, ALL, "", empty_hint);
562 all_params_["morestring"] =
563 ListingsParam("", false, ALL, "", empty_hint);
564 all_params_["deletestring"] =
565 ListingsParam("", false, ALL, "", empty_hint);
566 all_params_["comment"] =
567 ListingsParam("", false, ALL, "", empty_hint);
568 all_params_["morecomment"] =
569 ListingsParam("", false, ALL, "", empty_hint);
570 all_params_["deletecomment"] =
571 ListingsParam("", false, ALL, "", empty_hint);
572 all_params_["keywordcomment"] =
573 ListingsParam("", false, ALL, "", empty_hint);
574 all_params_["morekeywordcomment"] =
575 ListingsParam("", false, ALL, "", empty_hint);
576 all_params_["deletekeywordcomment"] =
577 ListingsParam("", false, ALL, "", empty_hint);
578 all_params_["keywordcommentsemicolon"] =
579 ListingsParam("", false, ALL, "", empty_hint);
580 all_params_["podcomment"] =
581 ListingsParam("", false, ALL, "", empty_hint);
582 // the following are experimental listings features
583 all_params_["procnamekeys"] =
584 ListingsParam("", false, ALL, "", empty_hint);
585 all_params_["moreprocnamekeys"] =
586 ListingsParam("", false, ALL, "", empty_hint);
587 all_params_["deleteprocnamekeys"] =
588 ListingsParam("", false, ALL, "", empty_hint);
589 all_params_["procnamestyle"] =
590 ListingsParam("", false, ALL, "", style_hint);
591 all_params_["indexprocnames"] =
592 ListingsParam("", false, TRUEFALSE, "", empty_hint);
593 all_params_["hyperref"] =
594 ListingsParam("", false, ALL, "", empty_hint);
595 all_params_["morehyperref"] =
596 ListingsParam("", false, ALL, "", empty_hint);
597 all_params_["deletehyperref"] =
598 ListingsParam("", false, ALL, "", empty_hint);
599 all_params_["hyperanchor"] =
600 ListingsParam("", false, ALL, "", empty_hint);
601 all_params_["hyperlink"] =
602 ListingsParam("", false, ALL, "", empty_hint);
603 all_params_["literate"] =
604 ListingsParam("", false, ALL, "", empty_hint);
605 all_params_["lgrindef"] =
606 ListingsParam("", false, ALL, "", empty_hint);
607 all_params_["rangebeginprefix"] =
608 ListingsParam("", false, ALL, "", empty_hint);
609 all_params_["rangebeginsuffix"] =
610 ListingsParam("", false, ALL, "", empty_hint);
611 all_params_["rangeendprefix"] =
612 ListingsParam("", false, ALL, "", empty_hint);
613 all_params_["rangeendsuffix"] =
614 ListingsParam("", false, ALL, "", empty_hint);
615 all_params_["rangeprefix"] =
616 ListingsParam("", false, ALL, "", empty_hint);
617 all_params_["rangesuffix"] =
618 ListingsParam("", false, ALL, "", empty_hint);
619 all_params_["includerangemarker"] =
620 ListingsParam("", false, TRUEFALSE, "", empty_hint);
621 all_params_["multicols"] =
622 ListingsParam("", false, INTEGER, "", empty_hint);
626 docstring ParValidator::validate(string const & name,
627 string const & par) const
630 return _("Invalid (empty) listing parameter name.");
632 if (name[0] == '?') {
633 string suffix = trim(string(name, 1));
635 ListingsParams::const_iterator it = all_params_.begin();
636 ListingsParams::const_iterator end = all_params_.end();
637 for (; it != end; ++it) {
638 if (suffix.empty() || contains(it->first, suffix)) {
639 if (!param_names.empty())
641 param_names += it->first;
646 _("Available listing parameters are %1$s"), from_ascii(param_names));
649 _("Available listings parameters containing string \"%1$s\" are %2$s"),
650 from_utf8(suffix), from_utf8(param_names));
653 // locate name in parameter table
654 ListingsParams::const_iterator it = all_params_.find(name);
655 if (it != all_params_.end()) {
656 docstring msg = it->second.validate(par);
660 return bformat(_("Parameter %1$s: "), from_utf8(name)) + msg;
662 // otherwise, produce a meaningful error message.
663 string matching_names;
664 ListingsParams::const_iterator end = all_params_.end();
665 for (it = all_params_.begin(); it != end; ++it) {
666 if (prefixIs(it->first, name)) {
667 if (!matching_names.empty())
668 matching_names += ", ";
669 matching_names += it->first;
672 if (matching_names.empty())
673 return bformat(_("Unknown listing parameter name: %1$s"),
676 return bformat(_("Parameters starting with '%1$s': %2$s"),
677 from_utf8(name), from_utf8(matching_names));
683 bool ParValidator::onoff(string const & name) const
685 // locate name in parameter table
686 ListingsParams::const_iterator it = all_params_.find(name);
687 if (it != all_params_.end())
688 return it->second.onoff_;
695 // define a global ParValidator
696 ParValidator * par_validator = NULL;
698 InsetListingsParams::InsetListingsParams()
699 : inline_(false), params_(), status_(InsetCollapsable::Open)
704 InsetListingsParams::InsetListingsParams(string const & par, bool in,
705 InsetCollapsable::CollapseStatus s)
706 : inline_(in), params_(), status_(s)
708 // this will activate parameter validation.
709 fromEncodedString(par);
713 void InsetListingsParams::write(ostream & os) const
719 os << status_ << " \"" << encodedString() << "\"";
723 void InsetListingsParams::read(Lexer & lex)
729 status_ = static_cast<InsetCollapsable::CollapseStatus>(s);
732 fromEncodedString(par);
736 string InsetListingsParams::params(string const & sep) const
739 for (map<string, string>::const_iterator it = params_.begin();
740 it != params_.end(); ++it) {
743 // key=value,key=value1 is stored in params_ as key=value,key_=value1.
744 if (it->second.empty())
745 par += rtrim(it->first, "_");
747 par += rtrim(it->first, "_") + '=' + it->second;
753 void InsetListingsParams::addParam(string const & key, string const & value)
758 // duplicate parameters!
759 string keyname = key;
760 if (params_.find(key) != params_.end())
761 // key=value,key=value1 is allowed in listings
762 // use key_, key__, key___ etc to avoid name conflict
763 while (params_.find(keyname += '_') != params_.end());
765 // onoff parameter with value false
767 par_validator = new ParValidator();
768 if (par_validator->onoff(key) && (value == "false" || value == "{false}"))
769 params_[keyname] = string();
770 // if the parameter is surrounded with {}, good
771 else if (prefixIs(value, "{") && suffixIs(value, "}"))
772 params_[keyname] = value;
773 // otherwise, check if {} is needed. Add {} to all values with
774 // non-ascii/number characters, just to be safe
776 bool has_special_char = false;
777 for (size_t i = 0; i < value.size(); ++i)
778 if (!isAlphaASCII(value[i]) && !isDigit(value[i])) {
779 has_special_char = true;
782 if (has_special_char)
783 params_[keyname] = "{" + value + "}";
785 params_[keyname] = value;
790 void InsetListingsParams::addParams(string const & par)
794 bool isValue = false;
796 for (size_t i = 0; i < par.size(); ++i) {
798 if (par[i] == '\n') {
799 addParam(trim(key), trim(value));
804 } else if (par[i] == ',' && braces == 0) {
805 addParam(trim(key), trim(value));
810 } else if (par[i] == '=' && braces == 0) {
813 } else if (par[i] == '{' && par[i - 1] == '=')
815 else if (par[i] == '}'
816 && (i == par.size() - 1 || par[i + 1] == ',' || par[i + 1] == '\n'))
824 if (!trim(key).empty())
825 addParam(trim(key), trim(value));
829 void InsetListingsParams::setParams(string const & par)
836 string InsetListingsParams::encodedString() const
839 // '"' is handled differently because it will
840 // terminate a lyx token.
841 string par = params();
842 // '"' is now " ==> '"' is now &quot;
843 par = subst(par, "&", "&");
844 // '"' is now &quot; ==> '"' is now &quot;
845 par = subst(par, "\"", """);
850 string InsetListingsParams::separatedParams(bool keepComma) const
853 return params(",\n");
859 void InsetListingsParams::fromEncodedString(string const & in)
861 // Decode string! Reversal of encodedString
863 // '"' is now &quot; ==> '"' is now &quot;
864 par = subst(par, """, "\"");
865 // '"' is now &quot; ==> '"' is now "
866 par = subst(par, "&", "&");
871 bool InsetListingsParams::isFloat() const
873 return params_.find("float") != params_.end();
877 string InsetListingsParams::getParamValue(string const & param) const
879 // is this parameter defined?
880 map<string, string>::const_iterator it = params_.find(param);
881 string par = (it == params_.end()) ? string() : it->second;
882 if (prefixIs(par, "{") && suffixIs(par, "}"))
883 return par.substr(1, par.size() - 2);
889 docstring InsetListingsParams::validate() const
893 par_validator = new ParValidator();
894 for (map<string, string>::const_iterator it = params_.begin();
895 it != params_.end(); ++it) {
896 msg = par_validator->validate(it->first, it->second);