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"
19 #include "support/convert.h"
20 #include "support/gettext.h"
21 #include "support/lstrings.h"
22 #include "support/textutils.h"
27 using namespace lyx::support;
35 TRUEFALSE, // accept 'true' or 'false'
36 INTEGER, // accept an integer
37 LENGTH, // accept a latex length
38 SKIP, // accept a skip or a length
39 ONEOF, // accept one of a few values
40 SUBSETOF // accept a string composed of given characters
44 /// Listings package parameter information.
45 // FIXME: make this class visible outside of this file so that
46 // FIXME: it can be used directly in the frontend and in the LyX format
50 /// Default ctor for STL containers.
51 ListingsParam(): onoff_(false), type_(ALL)
54 ListingsParam(string const & v, bool o, param_type t,
55 string const & i, docstring const & h)
56 : value_(v), onoff_(o), type_(t), info_(i), hint_(h)
58 /// Validate a paramater.
59 /// \retval an empty string if \c par is valid.
60 /// \retval otherwise an explanation WRT to \c par invalidity.
61 docstring validate(string const & par) const;
66 /// for option with value "true", "false".
70 /// "other": option="other"
72 /// "true": option=true
73 /// "false": option=false
74 // FIXME: this is public because of InsetListingParam::addParam()
84 /// info is a \n separated string with allowed values
86 /// info is a string from which par is composed of
87 /// (e.g. floatplacement can be one or more of *tbph)
89 /// information which meaning depends on parameter type.
92 /// a help message that is displayed in the gui.
97 char const * allowed_skips = "\\smallskipamount,\\medskipamount,\\bigskipamount";
100 docstring ListingsParam::validate(string const & par) const
102 bool unclosed = false;
104 // braces are allowed
105 if (prefixIs(par, "{") && suffixIs(par, "}") && !suffixIs(par, "\\}"))
106 par2 = par.substr(1, par.size() - 2);
108 // check for unmatched braces
110 for (size_t i = 0; i < par2.size(); ++i) {
111 if (par2[i] == '{' && (i == 0 || par2[i-1] != '\\'))
113 else if (par2[i] == '}' && (i == 0 || par2[i-1] != '\\'))
116 unclosed = braces != 0;
121 if (par2.empty() && !onoff_) {
125 return _("A value is expected.");
128 return _("Unbalanced braces!");
132 if (par2.empty() && !onoff_) {
136 return _("Please specify true or false.");
138 if (par2 != "true" && par2 != "false")
139 return _("Only true or false is allowed.");
141 return _("Unbalanced braces!");
145 if (!isStrInt(par2)) {
149 return _("Please specify an integer value.");
151 if (convert<int>(par2) == 0 && par2[0] != '0')
152 return _("An integer is expected.");
154 return _("Unbalanced braces!");
158 if (par2.empty() && !onoff_) {
162 return _("Please specify a LaTeX length expression.");
164 if (!isValidLength(par2))
165 return _("Invalid LaTeX length expression.");
167 return _("Unbalanced braces!");
171 if (par2.empty() && !onoff_) {
175 return bformat(_("Please specify a LaTeX length expression or a skip amount (%1$s)"),
176 from_ascii(subst(allowed_skips, ",", ", ")));
178 if (!isValidLength(par2) && tokenPos(allowed_skips, ',', par2) == -1)
179 return _("Not a valid LaTeX length expression or skip amount.");
181 return _("Unbalanced braces!");
185 if (par2.empty() && !onoff_) {
189 return bformat(_("Please specify one of %1$s."),
192 // break value to allowed strings
193 vector<string> lists;
195 for (size_t i = 0; i != info_.size(); ++i) {
196 if (info_[i] == '\n') {
205 // good, find the string
206 if (find(lists.begin(), lists.end(), par2) != lists.end()) {
208 return _("Unbalanced braces!");
211 // otherwise, produce a meaningful error message.
212 string matching_names;
213 for (vector<string>::iterator it = lists.begin();
214 it != lists.end(); ++it) {
215 if (it->size() >= par2.size() && it->substr(0, par2.size()) == par2) {
216 if (matching_names.empty())
217 matching_names += *it;
219 matching_names += ", " + *it;
222 if (matching_names.empty())
223 return bformat(_("Try one of %1$s."), from_utf8(info_));
225 return bformat(_("I guess you mean %1$s."), from_utf8(matching_names));
226 // this stifles a warning but upsets coverity
227 // coverity[UNREACHABLE]
231 if (par2.empty() && !onoff_) {
235 return bformat(_("Please specify one or more of '%1$s'."),
238 for (size_t i = 0; i < par2.size(); ++i)
239 if (info_.find(par2[i], 0) == string::npos)
240 return bformat(_("Should be composed of one or more of %1$s."),
243 return _("Unbalanced braces!");
250 /// languages and language/dialect combinations
251 char const * allowed_languages =
252 "no language\nABAP\n[R/2 4.3]ABAP\n[R/2 5.0]ABAP\n[R/3 3.1]ABAP\n"
253 "[R/3 4.6C]ABAP\n[R/3 6.10]ABAP\nACSL\nAda\n[2005]Ada\n[83]Ada\n"
254 "[95]Ada\nALGOL\n[60]ALGOL\n[68]ALGOL\nAssembler\n"
255 "[Motorola68k]Assembler\n[x86masm]Assembler\nAwk\n[gnu]Awk\n[POSIX]Awk\n"
256 "bash\nBasic\n[Visual]Basic\nC\n[ANSI]C\n[Handel]C\n[Objective]C\n"
257 "[Sharp]C\nC++\n[ANSI]C++\n[GNU]C++\n[ISO]C++\n[Visual]C++\nCaml\n"
258 "[light]Caml\n[Objective]Caml\nClean\nCobol\n[1974]Cobol\n[1985]Cobol\n"
259 "[ibm]Cobol\nComal 80\ncommand.com\n[WinXP]command.com\nComsol\ncsh\n"
260 "Delphi\nEiffel\nElan\nerlang\nEuphoria\nFortran\n[77]Fortran\n[90]Fortran\n"
261 "[95]Fortran\nGCL\nGnuplot\nHaskell\nHTML\nIDL\n[CORBA]IDL\ninform\n"
262 "Java\n[AspectJ]Java\nJVMIS\nksh\nLingo\nLisp\n[Auto]Lisp\nLogo\n"
263 "make\n[gnu]make\nMathematica\n[1.0]Mathematica\n[3.0]Mathematica\n"
264 "[5.2]Mathematica\nMatlab\nMercury\nMetaPost\nMiranda\nMizar\nML\n"
265 "Modula-2\nMuPAD\nNASTRAN\nOberon-2\nOCL\n[decorative]OCL\n[OMG]OCL\n"
266 "Octave\nOz\nPascal\n[Borland6]Pascal\n[Standard]Pascal\n[XSC]Pascal\n"
267 "Perl\nPHP\nPL/I\nPlasm\nPostScript\nPOV\nProlog\nPromela\nPSTricks\n"
268 "Python\nR\nReduce\nRexx\nRSL\nRuby\nS\n[PLUS]S\nSAS\nScilab\nsh\n"
269 "SHELXL\nSimula\n[67]Simula\n[CII]Simula\n[DEC]Simula\n[IBM]Simula\n"
270 "SPARQL\nSQL\ntcl\n[tk]tcl\nTeX\n[AlLaTeX]TeX\n[common]TeX\n[LaTeX]TeX\n"
271 "[plain]TeX\n[primitive]TeX\nVBScript\nVerilog\nVHDL\n[AMS]VHDL\nVRML\n"
272 "[97]VRML\nXML\nXSLT";
275 /// ListingsParam Validator.
276 /// This class is aimed to be a singleton which is instantiated in
277 /// \c InsetListingsParams::addParam().
278 // FIXME: transfer this validator to the frontend.
279 // FIXME: avoid the use of exception.
285 /// validate a parameter for a given name.
286 /// return an error message if \c par is an invalid parameter.
287 docstring validate(string const & name, string const & par) const;
289 /// return the onoff status of a parameter \c key, if \c key is not found
291 bool onoff(string const & key) const;
294 /// key is the name of the parameter
295 typedef map<string, ListingsParam> ListingsParams;
296 ListingsParams all_params_;
300 ParValidator::ParValidator()
302 docstring const empty_hint;
303 docstring const style_hint = _("Use \\footnotesize, \\small, \\itshape, "
304 "\\ttfamily or something like that");
305 docstring const frame_hint = _("none, leftline, topline, bottomline, lines, "
306 "single, shadowbox or subset of trblTRBL");
307 docstring const frameround_hint = _("Enter four letters (either t = round "
308 "or f = square) for top right, bottom "
309 "right, bottom left and top left corner.");
310 docstring const color_hint = _("Enter something like \\color{white}");
312 /// options copied from page 26 of listings manual
313 // FIXME: add default parameters ... (which is not used now)
314 all_params_["float"] =
315 ListingsParam("false", true, SUBSETOF, "*tbph", empty_hint);
316 all_params_["floatplacement"] =
317 ListingsParam("tbp", false, SUBSETOF, "tbp", empty_hint);
318 all_params_["aboveskip"] =
319 ListingsParam("\\medskipamount", false, SKIP, "", empty_hint);
320 all_params_["belowskip"] =
321 ListingsParam("\\medskipamount", false, SKIP, "", empty_hint);
322 all_params_["lineskip"] =
323 ListingsParam("", false, SKIP, "", empty_hint);
324 all_params_["boxpos"] =
325 ListingsParam("", false, SUBSETOF, "bct", empty_hint);
326 all_params_["print"] =
327 ListingsParam("", false, TRUEFALSE, "", empty_hint);
328 all_params_["firstline"] =
329 ListingsParam("", false, INTEGER, "", empty_hint);
330 all_params_["lastline"] =
331 ListingsParam("", false, INTEGER, "", empty_hint);
332 all_params_["linerange"] =
333 ListingsParam("", false, ALL, "", empty_hint);
334 all_params_["showlines"] =
335 ListingsParam("", false, TRUEFALSE, "", empty_hint);
336 all_params_["emptylines"] =
337 ListingsParam("", false, ALL, "", _(
338 "Expect a number with an optional * before it"));
339 all_params_["gobble"] =
340 ListingsParam("", false, INTEGER, "", empty_hint);
341 all_params_["style"] =
342 ListingsParam("", false, ALL, "", empty_hint);
343 all_params_["language"] =
344 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
345 all_params_["alsolanguage"] =
346 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
347 all_params_["defaultdialect"] =
348 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
349 all_params_["printpod"] =
350 ListingsParam("", false, TRUEFALSE, "", empty_hint);
351 all_params_["usekeywordsintag"] =
352 ListingsParam("", false, TRUEFALSE, "", empty_hint);
353 all_params_["tagstyle"] =
354 ListingsParam("", false, ALL, "", style_hint);
355 all_params_["markfirstintag"] =
356 ListingsParam("", false, ALL, "", style_hint);
357 all_params_["makemacrouse"] =
358 ListingsParam("", false, TRUEFALSE, "", empty_hint);
359 all_params_["basicstyle"] =
360 ListingsParam("", false, ALL, "", style_hint);
361 all_params_["identifierstyle"] =
362 ListingsParam("", false, ALL, "", style_hint);
363 all_params_["commentstyle"] =
364 ListingsParam("", false, ALL, "", style_hint);
365 all_params_["stringstyle"] =
366 ListingsParam("", false, ALL, "", style_hint);
367 all_params_["keywordstyle"] =
368 ListingsParam("", false, ALL, "", style_hint);
369 all_params_["ndkeywordstyle"] =
370 ListingsParam("", false, ALL, "", style_hint);
371 all_params_["classoffset"] =
372 ListingsParam("", false, INTEGER, "", empty_hint);
373 all_params_["texcsstyle"] =
374 ListingsParam("", false, ALL, "", style_hint);
375 all_params_["directivestyle"] =
376 ListingsParam("", false, ALL, "", style_hint);
377 all_params_["emph"] =
378 ListingsParam("", false, ALL, "", empty_hint);
379 all_params_["moreemph"] =
380 ListingsParam("", false, ALL, "", empty_hint);
381 all_params_["deleteemph"] =
382 ListingsParam("", false, ALL, "", empty_hint);
383 all_params_["emphstyle"] =
384 ListingsParam("", false, ALL, "", empty_hint);
385 all_params_["delim"] =
386 ListingsParam("", false, ALL, "", empty_hint);
387 all_params_["moredelim"] =
388 ListingsParam("", false, ALL, "", empty_hint);
389 all_params_["deletedelim"] =
390 ListingsParam("", false, ALL, "", empty_hint);
391 all_params_["extendedchars"] =
392 ListingsParam("", false, TRUEFALSE, "", empty_hint);
393 all_params_["inputencoding"] =
394 ListingsParam("", false, ALL, "", empty_hint);
395 all_params_["upquote"] =
396 ListingsParam("", false, TRUEFALSE, "", empty_hint);
397 all_params_["tabsize"] =
398 ListingsParam("", false, INTEGER, "", empty_hint);
399 all_params_["showtabs"] =
400 ListingsParam("", false, ALL, "", empty_hint);
402 ListingsParam("", false, ALL, "", empty_hint);
403 all_params_["showspaces"] =
404 ListingsParam("", false, TRUEFALSE, "", empty_hint);
405 all_params_["showstringspaces"] =
406 ListingsParam("", false, TRUEFALSE, "", empty_hint);
407 all_params_["formfeed"] =
408 ListingsParam("", false, ALL, "", empty_hint);
409 all_params_["numbers"] =
410 ListingsParam("", false, ONEOF, "none\nleft\nright", empty_hint);
411 all_params_["stepnumber"] =
412 ListingsParam("", false, INTEGER, "", empty_hint);
413 all_params_["numberfirstline"] =
414 ListingsParam("", false, TRUEFALSE, "", empty_hint);
415 all_params_["numberstyle"] =
416 ListingsParam("", false, ALL, "", style_hint);
417 all_params_["numbersep"] =
418 ListingsParam("", false, LENGTH, "", empty_hint);
419 all_params_["numberblanklines"] =
420 ListingsParam("", false, ALL, "", empty_hint);
421 all_params_["firstnumber"] =
422 ListingsParam("", false, ALL, "", _("auto, last or a number"));
423 all_params_["name"] =
424 ListingsParam("", false, ALL, "", empty_hint);
425 all_params_["thelstnumber"] =
426 ListingsParam("", false, ALL, "", empty_hint);
427 all_params_["title"] =
428 ListingsParam("", false, ALL, "", empty_hint);
429 // this option is not handled in the parameter box
430 all_params_["caption"] =
431 ListingsParam("", false, ALL, "", _(
432 "This parameter should not be entered here. Please use the caption "
433 "edit box (when using the child document dialog) or "
434 "menu Insert->Caption (when defining a listing inset)"));
435 // this option is not handled in the parameter box
436 all_params_["label"] =
437 ListingsParam("", false, ALL, "",_(
438 "This parameter should not be entered here. Please use the label "
439 "edit box (when using the child document dialog) or "
440 "menu Insert->Label (when defining a listing inset)"));
441 all_params_["nolol"] =
442 ListingsParam("", false, TRUEFALSE, "", empty_hint);
443 all_params_["captionpos"] =
444 ListingsParam("", false, SUBSETOF, "tb", empty_hint);
445 all_params_["abovecaptionskip"] =
446 ListingsParam("", false, SKIP, "", empty_hint);
447 all_params_["belowcaptionskip"] =
448 ListingsParam("", false, SKIP, "", empty_hint);
449 all_params_["linewidth"] =
450 ListingsParam("", false, LENGTH, "", empty_hint);
451 all_params_["xleftmargin"] =
452 ListingsParam("", false, LENGTH, "", empty_hint);
453 all_params_["xrightmargin"] =
454 ListingsParam("", false, LENGTH, "", empty_hint);
455 all_params_["resetmargins"] =
456 ListingsParam("", false, TRUEFALSE, "", empty_hint);
457 all_params_["breaklines"] =
458 ListingsParam("", false, TRUEFALSE, "", empty_hint);
459 all_params_["breakatwhitespace"] =
460 ListingsParam("", false, TRUEFALSE, "", empty_hint);
461 all_params_["prebreak"] =
462 ListingsParam("", false, ALL, "", empty_hint);
463 all_params_["postbreak"] =
464 ListingsParam("", false, ALL, "", empty_hint);
465 all_params_["breakindent"] =
466 ListingsParam("", false, LENGTH, "", empty_hint);
467 all_params_["breakautoindent"] =
468 ListingsParam("", false, TRUEFALSE, "", empty_hint);
469 all_params_["frame"] =
470 ListingsParam("", false, ALL, "", frame_hint);
471 all_params_["frameround"] =
472 ListingsParam("", false, SUBSETOF, "tf", frameround_hint);
473 all_params_["framesep"] =
474 ListingsParam("", false, LENGTH, "", empty_hint);
475 all_params_["rulesep"] =
476 ListingsParam("", false, LENGTH, "", empty_hint);
477 all_params_["framerule"] =
478 ListingsParam("", false, LENGTH, "", empty_hint);
479 all_params_["framexleftmargin"] =
480 ListingsParam("", false, LENGTH, "", empty_hint);
481 all_params_["framexrightmargin"] =
482 ListingsParam("", false, LENGTH, "", empty_hint);
483 all_params_["framextopmargin"] =
484 ListingsParam("", false, LENGTH, "", empty_hint);
485 all_params_["framexbottommargin"] =
486 ListingsParam("", false, LENGTH, "", empty_hint);
487 all_params_["backgroundcolor"] =
488 ListingsParam("", false, ALL, "", color_hint );
489 all_params_["rulecolor"] =
490 ListingsParam("", false, ALL, "", color_hint );
491 all_params_["fillcolor"] =
492 ListingsParam("", false, ALL, "", color_hint );
493 all_params_["rulesepcolor"] =
494 ListingsParam("", false, ALL, "", color_hint );
495 all_params_["frameshape"] =
496 ListingsParam("", false, ALL, "", empty_hint);
497 all_params_["index"] =
498 ListingsParam("", false, ALL, "", empty_hint);
499 all_params_["moreindex"] =
500 ListingsParam("", false, ALL, "", empty_hint);
501 all_params_["deleteindex"] =
502 ListingsParam("", false, ALL, "", empty_hint);
503 all_params_["indexstyle"] =
504 ListingsParam("", false, ALL, "", empty_hint);
505 all_params_["columns"] =
506 ListingsParam("", false, ALL, "", empty_hint);
507 all_params_["flexiblecolumns"] =
508 ListingsParam("", false, ALL, "", empty_hint);
509 all_params_["keepspaces"] =
510 ListingsParam("", false, TRUEFALSE, "", empty_hint);
511 all_params_["basewidth"] =
512 ListingsParam("", false, LENGTH, "", empty_hint);
513 all_params_["fontadjust"] =
514 ListingsParam("", true, TRUEFALSE, "", empty_hint);
515 all_params_["texcl"] =
516 ListingsParam("", false, TRUEFALSE, "", empty_hint);
517 all_params_["mathescape"] =
518 ListingsParam("", false, TRUEFALSE, "", empty_hint);
519 all_params_["escapechar"] =
520 ListingsParam("", false, ALL, "", empty_hint);
521 all_params_["escapeinside"] =
522 ListingsParam("", false, ALL, "", empty_hint);
523 all_params_["escapebegin"] =
524 ListingsParam("", false, ALL, "", empty_hint);
525 all_params_["escapeend"] =
526 ListingsParam("", false, ALL, "", empty_hint);
527 all_params_["fancyvrb"] =
528 ListingsParam("", false, TRUEFALSE, "", empty_hint);
529 all_params_["fvcmdparams"] =
530 ListingsParam("", false, ALL, "", empty_hint);
531 all_params_["morefvcmdparams"] =
532 ListingsParam("", false, ALL, "", empty_hint);
533 all_params_["keywordsprefix"] =
534 ListingsParam("", false, ALL, "", empty_hint);
535 all_params_["keywords"] =
536 ListingsParam("", false, ALL, "", empty_hint);
537 all_params_["morekeywords"] =
538 ListingsParam("", false, ALL, "", empty_hint);
539 all_params_["deletekeywords"] =
540 ListingsParam("", false, ALL, "", empty_hint);
541 all_params_["ndkeywords"] =
542 ListingsParam("", false, ALL, "", empty_hint);
543 all_params_["morendkeywords"] =
544 ListingsParam("", false, ALL, "", empty_hint);
545 all_params_["deletendkeywords"] =
546 ListingsParam("", false, ALL, "", empty_hint);
547 all_params_["texcs"] =
548 ListingsParam("", false, ALL, "", empty_hint);
549 all_params_["moretexcs"] =
550 ListingsParam("", false, ALL, "", empty_hint);
551 all_params_["deletetexcs"] =
552 ListingsParam("", false, ALL, "", empty_hint);
553 all_params_["directives"] =
554 ListingsParam("", false, ALL, "", empty_hint);
555 all_params_["moredirectives"] =
556 ListingsParam("", false, ALL, "", empty_hint);
557 all_params_["deletedirectives"] =
558 ListingsParam("", false, ALL, "", empty_hint);
559 all_params_["sensitive"] =
560 ListingsParam("", false, ALL, "", empty_hint);
561 all_params_["alsoletter"] =
562 ListingsParam("", false, ALL, "", empty_hint);
563 all_params_["alsodigit"] =
564 ListingsParam("", false, ALL, "", empty_hint);
565 all_params_["alsoother"] =
566 ListingsParam("", false, ALL, "", empty_hint);
567 all_params_["otherkeywords"] =
568 ListingsParam("", false, ALL, "", empty_hint);
570 ListingsParam("", false, ALL, "", empty_hint);
571 all_params_["string"] =
572 ListingsParam("", false, ALL, "", empty_hint);
573 all_params_["morestring"] =
574 ListingsParam("", false, ALL, "", empty_hint);
575 all_params_["deletestring"] =
576 ListingsParam("", false, ALL, "", empty_hint);
577 all_params_["comment"] =
578 ListingsParam("", false, ALL, "", empty_hint);
579 all_params_["morecomment"] =
580 ListingsParam("", false, ALL, "", empty_hint);
581 all_params_["deletecomment"] =
582 ListingsParam("", false, ALL, "", empty_hint);
583 all_params_["keywordcomment"] =
584 ListingsParam("", false, ALL, "", empty_hint);
585 all_params_["morekeywordcomment"] =
586 ListingsParam("", false, ALL, "", empty_hint);
587 all_params_["deletekeywordcomment"] =
588 ListingsParam("", false, ALL, "", empty_hint);
589 all_params_["keywordcommentsemicolon"] =
590 ListingsParam("", false, ALL, "", empty_hint);
591 all_params_["podcomment"] =
592 ListingsParam("", false, ALL, "", empty_hint);
593 // the following are experimental listings features
594 all_params_["procnamekeys"] =
595 ListingsParam("", false, ALL, "", empty_hint);
596 all_params_["moreprocnamekeys"] =
597 ListingsParam("", false, ALL, "", empty_hint);
598 all_params_["deleteprocnamekeys"] =
599 ListingsParam("", false, ALL, "", empty_hint);
600 all_params_["procnamestyle"] =
601 ListingsParam("", false, ALL, "", style_hint);
602 all_params_["indexprocnames"] =
603 ListingsParam("", false, TRUEFALSE, "", empty_hint);
604 all_params_["hyperref"] =
605 ListingsParam("", false, ALL, "", empty_hint);
606 all_params_["morehyperref"] =
607 ListingsParam("", false, ALL, "", empty_hint);
608 all_params_["deletehyperref"] =
609 ListingsParam("", false, ALL, "", empty_hint);
610 all_params_["hyperanchor"] =
611 ListingsParam("", false, ALL, "", empty_hint);
612 all_params_["hyperlink"] =
613 ListingsParam("", false, ALL, "", empty_hint);
614 all_params_["literate"] =
615 ListingsParam("", false, ALL, "", empty_hint);
616 all_params_["lgrindef"] =
617 ListingsParam("", false, ALL, "", empty_hint);
618 all_params_["rangebeginprefix"] =
619 ListingsParam("", false, ALL, "", empty_hint);
620 all_params_["rangebeginsuffix"] =
621 ListingsParam("", false, ALL, "", empty_hint);
622 all_params_["rangeendprefix"] =
623 ListingsParam("", false, ALL, "", empty_hint);
624 all_params_["rangeendsuffix"] =
625 ListingsParam("", false, ALL, "", empty_hint);
626 all_params_["rangeprefix"] =
627 ListingsParam("", false, ALL, "", empty_hint);
628 all_params_["rangesuffix"] =
629 ListingsParam("", false, ALL, "", empty_hint);
630 all_params_["includerangemarker"] =
631 ListingsParam("", false, TRUEFALSE, "", empty_hint);
632 all_params_["multicols"] =
633 ListingsParam("", false, INTEGER, "", empty_hint);
637 docstring ParValidator::validate(string const & name,
638 string const & par) const
641 return _("Invalid (empty) listing parameter name.");
643 if (name[0] == '?') {
644 string suffix = trim(string(name, 1));
646 ListingsParams::const_iterator it = all_params_.begin();
647 ListingsParams::const_iterator end = all_params_.end();
648 for (; it != end; ++it) {
649 if (suffix.empty() || contains(it->first, suffix)) {
650 if (!param_names.empty())
652 param_names += it->first;
657 _("Available listing parameters are %1$s"), from_ascii(param_names));
660 _("Available listings parameters containing string \"%1$s\" are %2$s"),
661 from_utf8(suffix), from_utf8(param_names));
664 // locate name in parameter table
665 ListingsParams::const_iterator it = all_params_.find(name);
666 if (it != all_params_.end()) {
667 docstring msg = it->second.validate(par);
671 return bformat(_("Parameter %1$s: "), from_utf8(name)) + msg;
673 // otherwise, produce a meaningful error message.
674 string matching_names;
675 ListingsParams::const_iterator end = all_params_.end();
676 for (it = all_params_.begin(); it != end; ++it) {
677 if (prefixIs(it->first, name)) {
678 if (!matching_names.empty())
679 matching_names += ", ";
680 matching_names += it->first;
683 if (matching_names.empty())
684 return bformat(_("Unknown listing parameter name: %1$s"),
687 return bformat(_("Parameters starting with '%1$s': %2$s"),
688 from_utf8(name), from_utf8(matching_names));
693 bool ParValidator::onoff(string const & name) const
695 // locate name in parameter table
696 ListingsParams::const_iterator it = all_params_.find(name);
697 if (it != all_params_.end())
698 return it->second.onoff_;
705 // define a global ParValidator
706 ParValidator * par_validator = 0;
708 InsetListingsParams::InsetListingsParams()
709 : inline_(false), params_(), status_(InsetCollapsable::Open)
714 InsetListingsParams::InsetListingsParams(string const & par, bool in,
715 InsetCollapsable::CollapseStatus s)
716 : inline_(in), params_(), status_(s)
718 // this will activate parameter validation.
719 fromEncodedString(par);
723 void InsetListingsParams::write(ostream & os) const
729 os << status_ << " \"" << encodedString() << "\"";
733 void InsetListingsParams::read(Lexer & lex)
736 int s = InsetCollapsable::Collapsed;
738 status_ = static_cast<InsetCollapsable::CollapseStatus>(s);
741 fromEncodedString(par);
745 string InsetListingsParams::params(string const & sep) const
748 keyValuePair::const_iterator it = params_.begin();
749 for (; it != params_.end(); ++it) {
752 // key=value,key=value1 is stored in params_ as key=value,key_=value1.
753 if (it->second.empty())
754 par += rtrim(it->first, "_");
756 par += rtrim(it->first, "_") + '=' + it->second;
762 bool InsetListingsParams::hasParam(string const & key) const
764 keyValuePair::const_iterator it = params_.begin();
765 for (; it != params_.end(); ++it) {
766 if (it->first == key)
773 string InsetListingsParams::getValue(string const & key) const
775 keyValuePair::const_iterator it = params_.begin();
776 for (; it != params_.end(); ++it) {
777 if (it->first == key)
784 void InsetListingsParams::addParam(string const & key,
785 string const & value, bool replace)
790 // duplicate parameters!
791 string keyname = key;
792 if (!replace && hasParam(key))
793 // key=value,key=value1 is allowed in listings
794 // use key_, key__, key___ etc to avoid name conflict
795 while (hasParam(keyname += '_')) { }
797 // onoff parameter with value false
799 par_validator = new ParValidator;
800 if (par_validator->onoff(key) && (value == "false" || value == "{false}"))
801 params_.push_back(make_pair(keyname, string()));
802 // if the parameter is surrounded with {}, good
803 else if (prefixIs(value, "{") && suffixIs(value, "}"))
804 params_.push_back(make_pair(keyname, value));
805 // otherwise, check if {} is needed. Add {} to all values with
806 // non-ascii/number characters, just to be safe
808 bool has_special_char = false;
809 for (size_t i = 0; i < value.size(); ++i)
810 if (!isAlnumASCII(value[i])) {
811 has_special_char = true;
814 if (has_special_char)
815 params_.push_back(make_pair(keyname, "{" + value + "}"));
817 params_.push_back(make_pair(keyname, value));
822 void InsetListingsParams::addParams(string const & par)
826 bool isValue = false;
828 for (size_t i = 0; i < par.size(); ++i) {
830 if (par[i] == '\n') {
831 addParam(trim(key), trim(value));
836 } else if (par[i] == ',' && braces == 0) {
837 addParam(trim(key), trim(value));
842 } else if (par[i] == '=' && braces == 0) {
845 } else if (par[i] == '{' && i > 0 && par[i-1] != '\\')
846 // don't count a brace in first position
848 else if (par[i] == '}' && i != par.size() - 1
849 && (i == 0 || (i > 0 && par[i-1] != '\\')))
857 if (!trim(key).empty())
858 addParam(trim(key), trim(value));
862 void InsetListingsParams::setParams(string const & par)
869 string InsetListingsParams::encodedString() const
872 // '"' is handled differently because it will
873 // terminate a lyx token.
874 string par = params();
875 // '"' is now " ==> '"' is now &quot;
876 par = subst(par, "&", "&");
877 // '"' is now &quot; ==> '"' is now &quot;
878 par = subst(par, "\"", """);
883 string InsetListingsParams::separatedParams(bool keepComma) const
886 return params(",\n");
892 void InsetListingsParams::fromEncodedString(string const & in)
894 // Decode string! Reversal of encodedString
896 // '"' is now &quot; ==> '"' is now &quot;
897 par = subst(par, """, "\"");
898 // '"' is now &quot; ==> '"' is now "
899 par = subst(par, "&", "&");
904 bool InsetListingsParams::isFloat() const
906 return hasParam("float");
910 string InsetListingsParams::getParamValue(string const & param) const
912 string par = getValue(param);
913 if (prefixIs(par, "{") && suffixIs(par, "}"))
914 return par.substr(1, par.size() - 2);
920 docstring InsetListingsParams::validate() const
924 par_validator = new ParValidator;
925 // return msg for first key=value pair which is incomplete or has an error
926 keyValuePair::const_iterator it = params_.begin();
927 for (; it != params_.end(); ++it) {
929 msg = par_validator->validate(rtrim(it->first, "_"), it->second);