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