]> git.lyx.org Git - lyx.git/blob - src/insets/InsetListingsParams.cpp
Change cursor shape for clickable math insets
[lyx.git] / src / insets / InsetListingsParams.cpp
1 /**
2  * \file InsetListingsParams.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12 #include <algorithm>
13
14 #include "InsetListingsParams.h"
15
16 #include "support/Length.h"
17 #include "Lexer.h"
18
19 #include "support/convert.h"
20 #include "support/gettext.h"
21 #include "support/lstrings.h"
22 #include "support/textutils.h"
23
24 #include <sstream>
25
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30
31 namespace {
32
33 enum param_type {
34         ALL,  // accept all
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
41 };
42
43
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
47 // FIXME: parsing.
48 class ListingsParam {
49 public:
50         /// Default ctor for STL containers.
51         ListingsParam(): onoff_(false), type_(ALL)
52         {}
53         /// Main ctor.
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)
57         {}
58         /// Validate a parameter.
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;
62 private:
63         /// default value
64         string value_;
65 public:
66         /// for option with value "true", "false".
67         /// if onoff is true,
68         ///   "true":  option
69         ///   "false":
70         ///   "other": option="other"
71         /// onoff is false,
72         ///   "true":  option=true
73         ///   "false": option=false
74         // FIXME: this is public because of InsetListingParam::addParam()
75         bool onoff_;
76 private:
77         /// validator type.
78         /// ALL:
79         /// TRUEFALSE:
80         /// INTEGER:
81         /// LENGTH:
82         ///     info is ignored.
83         /// ONEOF
84         ///     info is a \n separated string with allowed values
85         /// SUBSETOF
86         ///     info is a string from which par is composed of
87         ///     (e.g. floatplacement can be one or more of *tbph)
88         param_type type_;
89         /// information which meaning depends on parameter type.
90         /// \sa type_
91         string info_;
92         /// a help message that is displayed in the gui.
93         docstring hint_;
94 };
95
96
97 char const * allowed_skips = "\\smallskipamount,\\medskipamount,\\bigskipamount";
98
99
100 docstring ListingsParam::validate(string const & par) const
101 {
102         bool unclosed = false;
103         string par2 = par;
104         // braces are allowed
105         if (prefixIs(par, "{") && suffixIs(par, "}") && !suffixIs(par, "\\}"))
106                 par2 = par.substr(1, par.size() - 2);
107
108         // check for unmatched braces
109         int braces = 0;
110         for (size_t i = 0; i < par2.size(); ++i) {
111                 if (par2[i] == '{' && (i == 0 || par2[i-1] != '\\'))
112                         ++braces;
113                 else if (par2[i] == '}' && (i == 0 || par2[i-1] != '\\'))
114                         --braces;
115         }
116         unclosed = braces != 0;
117
118         switch (type_) {
119
120         case ALL:
121                 if (par2.empty() && !onoff_) {
122                         if (!hint_.empty())
123                                 return hint_;
124                         else
125                                 return _("A value is expected.");
126                 }
127                 if (unclosed)
128                         return _("Unbalanced braces!");
129                 return docstring();
130
131         case TRUEFALSE:
132                 if (par2.empty() && !onoff_) {
133                         if (!hint_.empty())
134                                 return hint_;
135                         else
136                                 return _("Please specify true or false.");
137                 }
138                 if (par2 != "true" && par2 != "false")
139                         return _("Only true or false is allowed.");
140                 if (unclosed)
141                         return _("Unbalanced braces!");
142                 return docstring();
143
144         case INTEGER:
145                 if (!isStrInt(par2)) {
146                         if (!hint_.empty())
147                                 return hint_;
148                         else
149                                 return _("Please specify an integer value.");
150                 }
151                 if (convert<int>(par2) == 0 && par2[0] != '0')
152                         return _("An integer is expected.");
153                 if (unclosed)
154                         return _("Unbalanced braces!");
155                 return docstring();
156
157         case LENGTH:
158                 if (par2.empty() && !onoff_) {
159                         if (!hint_.empty())
160                                 return hint_;
161                         else
162                                 return _("Please specify a LaTeX length expression.");
163                 }
164                 if (!isValidLength(par2))
165                         return _("Invalid LaTeX length expression.");
166                 if (unclosed)
167                         return _("Unbalanced braces!");
168                 return docstring();
169
170         case SKIP:
171                 if (par2.empty() && !onoff_) {
172                         if (!hint_.empty())
173                                 return hint_;
174                         else
175                                 return bformat(_("Please specify a LaTeX length expression or a skip amount (%1$s)"),
176                                                from_ascii(subst(allowed_skips, ",", ", ")));
177                 }
178                 if (!isValidLength(par2) && tokenPos(allowed_skips, ',', par2) == -1)
179                         return _("Not a valid LaTeX length expression or skip amount.");
180                 if (unclosed)
181                         return _("Unbalanced braces!");
182                 return docstring();
183
184         case ONEOF: {
185                 if (par2.empty() && !onoff_) {
186                         if (!hint_.empty())
187                                 return hint_;
188                         else
189                                 return bformat(_("Please specify one of %1$s."),
190                                                            from_utf8(info_));
191                 }
192                 // break value to allowed strings
193                 vector<string> lists;
194                 string v;
195                 for (size_t i = 0; i != info_.size(); ++i) {
196                         if (info_[i] == '\n') {
197                                 lists.push_back(v);
198                                 v = string();
199                         } else
200                                 v += info_[i];
201                 }
202                 if (!v.empty())
203                         lists.push_back(v);
204
205                 // good, find the string
206                 if (find(lists.begin(), lists.end(), par2) != lists.end()) {
207                         if (unclosed)
208                                 return _("Unbalanced braces!");
209                         return docstring();
210                 }
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;
218                                 else
219                                         matching_names += ", " + *it;
220                         }
221                 }
222                 if (matching_names.empty())
223                         return bformat(_("Try one of %1$s."), from_utf8(info_));
224                 else
225                         return bformat(_("I guess you mean %1$s."), from_utf8(matching_names));
226         }
227         case SUBSETOF:
228                 if (par2.empty() && !onoff_) {
229                         if (!hint_.empty())
230                                 return hint_;
231                         else
232                                 return bformat(_("Please specify one or more of '%1$s'."),
233                                                            from_utf8(info_));
234                 }
235                 for (char c : par2)
236                         if (info_.find(c, 0) == string::npos)
237                                 return bformat(_("Should be composed of one or more of %1$s."),
238                                                 from_utf8(info_));
239                 if (unclosed)
240                         return _("Unbalanced braces!");
241                 return docstring();
242         }
243         return docstring();
244 }
245
246
247 /// languages and language/dialect combinations
248 char const * allowed_languages =
249         "no language\nABAP\n[R/2 4.3]ABAP\n[R/2 5.0]ABAP\n[R/3 3.1]ABAP\n"
250         "[R/3 4.6C]ABAP\n[R/3 6.10]ABAP\nACM\nACMscript\nACSL\nAda\n[2005]Ada\n[83]Ada\n"
251         "[95]Ada\nALGOL\n[60]ALGOL\n[68]ALGOL\nAnt\nAssembler\n"
252         "[Motorola68k]Assembler\n[x86masm]Assembler\nAwk\n[gnu]Awk\n[POSIX]Awk\n"
253         "bash\nBasic\n[Visual]Basic\nC\n[ANSI]C\n[Handel]C\n[Objective]C\n"
254         "[Sharp]C\nC++\n[ANSI]C++\n[GNU]C++\n[ISO]C++\n[Visual]C++\nCaml\n"
255         "[light]Caml\n[Objective]Caml\nCIL\nClean\nCobol\n[1974]Cobol\n[1985]Cobol\n"
256         "[ibm]Cobol\nComal 80\ncommand.com\n[WinXP]command.com\nComsol\ncsh\n"
257         "Delphi\nEiffel\nElan\nelisp\nerlang\nEuphoria\nFortran\n[77]Fortran\n[90]Fortran\n"
258         "[95]Fortran\n[03]Fortran\n[08]Fortran\nGAP\nGCL\nGnuplot\nGo\nhansl\nHaskell\nHTML\nIDL\n[CORBA]IDL\ninform\n"
259         "Java\n[AspectJ]Java\nJVMIS\nksh\nLingo\nLisp\n[Auto]Lisp\nLLVM\nLogo\n"
260         "Lua\n[5.0]Lua\n[5.1]Lua\n[5.2]Lua\n[5.3]Lua\n"
261         "make\n[gnu]make\nMathematica\n[1.0]Mathematica\n[3.0]Mathematica\n[11.0]Mathematica\n"
262         "[5.2]Mathematica\nMatlab\nMercury\nMetaPost\nMiranda\nMizar\nML\n"
263         "Modula-2\nMuPAD\nNASTRAN\nOberon-2\nOCL\n[decorative]OCL\n[OMG]OCL\n"
264         "Octave\nOORexx\nOz\nPascal\n[Borland6]Pascal\n[Standard]Pascal\n[XSC]Pascal\n"
265         "Perl\nPHP\nPL/I\nPlasm\nPostScript\nPOV\nProlog\nPromela\nPSTricks\n"
266         "Python\nR\nReduce\nRexx\n[VM/XA]Rexx\nRSL\nRuby\nS\n[PLUS]S\nSAS\nScala\nScilab\nsh\n"
267         "SHELXL\nSimula\n[67]Simula\n[CII]Simula\n[DEC]Simula\n[IBM]Simula\n"
268         "SPARQL\nSQL\ntcl\n[tk]tcl\nTeX\n[AlLaTeX]TeX\n[common]TeX\n[LaTeX]TeX\n"
269         "[plain]TeX\n[primitive]TeX\nVBScript\nVerilog\nVHDL\n[AMS]VHDL\nVRML\n"
270         "[97]VRML\nXML\nXSLT";
271
272
273 /// Return language allowed in the GUI without dialect and proper casing
274 string const languageonly(string const & lang)
275 {
276         string const locase = ascii_lowercase(trim(lang, "{}"));
277         string const all_languages = ascii_lowercase(allowed_languages) + "\n";
278         string language = (lang.at(0) == '[') ? locase + "\n"
279                                               : string("]") + locase + "\n";
280         size_t i = all_languages.find(language);
281         if (i == string::npos && lang.at(0) != '[') {
282                 language[0] = '\n';
283                 i = all_languages.find(language);
284         }
285         if (i == string::npos)
286                 return lang;
287         if (all_languages.at(i) == '[')
288                 i = all_languages.find(']', i);
289         if (i == string::npos)
290                 return lang;
291         size_t j = all_languages.find('\n', i + 1);
292         if (j == string::npos)
293                 return lang;
294         return string(allowed_languages).substr(i + 1, j - i - 1);
295 }
296
297
298 /// ListingsParam Validator.
299 /// This class is aimed to be a singleton which is instantiated in
300 /// \c InsetListingsParams::addParam().
301 // FIXME: transfer this validator to the frontend.
302 // FIXME: avoid the use of exception.
303 class ParValidator
304 {
305 public:
306         ParValidator();
307
308         /// validate a parameter for a given name.
309         /// return an error message if \c par is an invalid parameter.
310         docstring validate(string const & name, string const & par) const;
311
312         /// return the onoff status of a parameter \c key, if \c key is not found
313         /// return false
314         bool onoff(string const & key) const;
315
316 private:
317         /// key is the name of the parameter
318         typedef map<string, ListingsParam> ListingsParams;
319         ListingsParams all_params_[2];
320 };
321
322
323 ParValidator::ParValidator()
324 {
325         docstring const empty_hint;
326         docstring const style_hint = _("Use \\footnotesize, \\small, \\itshape, "
327                 "\\ttfamily or something like that");
328         docstring const frame_hint_mint =
329                 _("none, leftline, topline, bottomline, lines, single");
330         docstring const frame_hint_lst =
331                 _("none, leftline, topline, bottomline, lines, "
332                 "single, shadowbox or subset of trblTRBL");
333         docstring const frameround_hint = _("Enter four letters (either t = round "
334                 "or f = square) for top right, bottom "
335                 "right, bottom left and top left corner.");
336         docstring const color_hint_mint =
337                         _("Previously defined color name as a string");
338         docstring const color_hint_lst =
339                         _("Enter something like \\color{white}");
340
341         // Listings package
342
343         /// options copied from page 26 of listings manual
344         // FIXME: add default parameters ... (which is not used now)
345         all_params_[0]["float"] =
346                 ListingsParam("false", true, SUBSETOF, "*tbph", empty_hint);
347         all_params_[0]["floatplacement"] =
348                 ListingsParam("tbp", false, SUBSETOF, "tbp", empty_hint);
349         all_params_[0]["aboveskip"] =
350                 ListingsParam("\\medskipamount", false, SKIP, "", empty_hint);
351         all_params_[0]["belowskip"] =
352                 ListingsParam("\\medskipamount", false, SKIP, "", empty_hint);
353         all_params_[0]["lineskip"] =
354                 ListingsParam("", false, SKIP, "", empty_hint);
355         all_params_[0]["boxpos"] =
356                 ListingsParam("", false, SUBSETOF, "bct", empty_hint);
357         all_params_[0]["print"] =
358                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
359         all_params_[0]["firstline"] =
360                 ListingsParam("", false, INTEGER, "", empty_hint);
361         all_params_[0]["lastline"] =
362                 ListingsParam("", false, INTEGER, "", empty_hint);
363         all_params_[0]["linerange"] =
364                 ListingsParam("", false, ALL, "", empty_hint);
365         all_params_[0]["showlines"] =
366                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
367         all_params_[0]["emptylines"] =
368                 ListingsParam("", false, ALL, "", _(
369                 "Expect a number with an optional * before it"));
370         all_params_[0]["gobble"] =
371                 ListingsParam("", false, INTEGER, "", empty_hint);
372         all_params_[0]["style"] =
373                 ListingsParam("", false, ALL, "", empty_hint);
374         all_params_[0]["language"] =
375                 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
376         all_params_[0]["alsolanguage"] =
377                 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
378         all_params_[0]["defaultdialect"] =
379                 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
380         all_params_[0]["printpod"] =
381                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
382         all_params_[0]["usekeywordsintag"] =
383                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
384         all_params_[0]["tagstyle"] =
385                 ListingsParam("", false, ALL, "", style_hint);
386         all_params_[0]["markfirstintag"] =
387                 ListingsParam("", false, ALL, "", style_hint);
388         all_params_[0]["makemacrouse"] =
389                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
390         all_params_[0]["basicstyle"] =
391                 ListingsParam("", false, ALL, "", style_hint);
392         all_params_[0]["identifierstyle"] =
393                 ListingsParam("", false, ALL, "", style_hint);
394         all_params_[0]["commentstyle"] =
395                 ListingsParam("", false, ALL, "", style_hint);
396         all_params_[0]["stringstyle"] =
397                 ListingsParam("", false, ALL, "", style_hint);
398         all_params_[0]["keywordstyle"] =
399                 ListingsParam("", false, ALL, "", style_hint);
400         all_params_[0]["ndkeywordstyle"] =
401                 ListingsParam("", false, ALL, "", style_hint);
402         all_params_[0]["classoffset"] =
403                 ListingsParam("", false, INTEGER, "", empty_hint);
404         all_params_[0]["texcsstyle"] =
405                 ListingsParam("", false, ALL, "", style_hint);
406         all_params_[0]["directivestyle"] =
407                 ListingsParam("", false, ALL, "", style_hint);
408         all_params_[0]["emph"] =
409                 ListingsParam("", false, ALL, "", empty_hint);
410         all_params_[0]["moreemph"] =
411                 ListingsParam("", false, ALL, "", empty_hint);
412         all_params_[0]["deleteemph"] =
413                 ListingsParam("", false, ALL, "", empty_hint);
414         all_params_[0]["emphstyle"] =
415                 ListingsParam("", false, ALL, "", empty_hint);
416         all_params_[0]["delim"] =
417                 ListingsParam("", false, ALL, "", empty_hint);
418         all_params_[0]["moredelim"] =
419                 ListingsParam("", false, ALL, "", empty_hint);
420         all_params_[0]["deletedelim"] =
421                 ListingsParam("", false, ALL, "", empty_hint);
422         all_params_[0]["extendedchars"] =
423                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
424         all_params_[0]["inputencoding"] =
425                 ListingsParam("", false, ALL, "", empty_hint);
426         all_params_[0]["upquote"] =
427                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
428         all_params_[0]["tabsize"] =
429                 ListingsParam("", false, INTEGER, "", empty_hint);
430         all_params_[0]["showtabs"] =
431                 ListingsParam("", false, ALL, "", empty_hint);
432         all_params_[0]["tab"] =
433                 ListingsParam("", false, ALL, "", empty_hint);
434         all_params_[0]["showspaces"] =
435                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
436         all_params_[0]["showstringspaces"] =
437                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
438         all_params_[0]["formfeed"] =
439                 ListingsParam("", false, ALL, "", empty_hint);
440         all_params_[0]["numbers"] =
441                 ListingsParam("", false, ONEOF, "none\nleft\nright", empty_hint);
442         all_params_[0]["stepnumber"] =
443                 ListingsParam("", false, INTEGER, "", empty_hint);
444         all_params_[0]["numberfirstline"] =
445                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
446         all_params_[0]["numberstyle"] =
447                 ListingsParam("", false, ALL, "", style_hint);
448         all_params_[0]["numbersep"] =
449                 ListingsParam("", false, LENGTH, "", empty_hint);
450         all_params_[0]["numberblanklines"] =
451                 ListingsParam("", false, ALL, "", empty_hint);
452         all_params_[0]["firstnumber"] =
453                 ListingsParam("", false, ALL, "", _("auto, last or a number"));
454         all_params_[0]["name"] =
455                 ListingsParam("", false, ALL, "", empty_hint);
456         all_params_[0]["thelstnumber"] =
457                 ListingsParam("", false, ALL, "", empty_hint);
458         all_params_[0]["title"] =
459                 ListingsParam("", false, ALL, "", empty_hint);
460         // this option is not handled in the parameter box
461         all_params_[0]["caption"] =
462                 ListingsParam("", false, ALL, "", _(
463                 "This parameter should not be entered here. Please use the caption "
464                 "edit box (when using the child document dialog) or "
465                 "menu Insert->Caption (when defining a listing inset)"));
466         // this option is not handled in the parameter box
467         all_params_[0]["label"] =
468                 ListingsParam("", false, ALL, "",_(
469                 "This parameter should not be entered here. Please use the label "
470                 "edit box (when using the child document dialog) or "
471                 "menu Insert->Label (when defining a listing inset)"));
472         all_params_[0]["nolol"] =
473                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
474         all_params_[0]["captionpos"] =
475                 ListingsParam("", false, SUBSETOF, "tb", empty_hint);
476         all_params_[0]["abovecaptionskip"] =
477                 ListingsParam("", false, SKIP, "", empty_hint);
478         all_params_[0]["belowcaptionskip"] =
479                 ListingsParam("", false, SKIP, "", empty_hint);
480         all_params_[0]["linewidth"] =
481                 ListingsParam("", false, LENGTH, "", empty_hint);
482         all_params_[0]["xleftmargin"] =
483                 ListingsParam("", false, LENGTH, "", empty_hint);
484         all_params_[0]["xrightmargin"] =
485                 ListingsParam("", false, LENGTH, "", empty_hint);
486         all_params_[0]["resetmargins"] =
487                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
488         all_params_[0]["breaklines"] =
489                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
490         all_params_[0]["breakatwhitespace"] =
491                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
492         all_params_[0]["prebreak"] =
493                 ListingsParam("", false, ALL, "", empty_hint);
494         all_params_[0]["postbreak"] =
495                 ListingsParam("", false, ALL, "", empty_hint);
496         all_params_[0]["breakindent"] =
497                 ListingsParam("", false, LENGTH, "", empty_hint);
498         all_params_[0]["breakautoindent"] =
499                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
500         all_params_[0]["frame"] =
501                 ListingsParam("", false, ALL, "", frame_hint_lst);
502         all_params_[0]["frameround"] =
503                 ListingsParam("", false, SUBSETOF, "tf", frameround_hint);
504         all_params_[0]["framesep"] =
505                 ListingsParam("", false, LENGTH, "", empty_hint);
506         all_params_[0]["rulesep"] =
507                 ListingsParam("", false, LENGTH, "", empty_hint);
508         all_params_[0]["framerule"] =
509                 ListingsParam("", false, LENGTH, "", empty_hint);
510         all_params_[0]["framexleftmargin"] =
511                 ListingsParam("", false, LENGTH, "", empty_hint);
512         all_params_[0]["framexrightmargin"] =
513                 ListingsParam("", false, LENGTH, "", empty_hint);
514         all_params_[0]["framextopmargin"] =
515                 ListingsParam("", false, LENGTH, "", empty_hint);
516         all_params_[0]["framexbottommargin"] =
517                 ListingsParam("", false, LENGTH, "", empty_hint);
518         all_params_[0]["backgroundcolor"] =
519                 ListingsParam("", false, ALL, "", color_hint_lst);
520         all_params_[0]["rulecolor"] =
521                 ListingsParam("", false, ALL, "", color_hint_lst);
522         all_params_[0]["fillcolor"] =
523                 ListingsParam("", false, ALL, "", color_hint_lst);
524         all_params_[0]["rulesepcolor"] =
525                 ListingsParam("", false, ALL, "", color_hint_lst);
526         all_params_[0]["frameshape"] =
527                 ListingsParam("", false, ALL, "", empty_hint);
528         all_params_[0]["index"] =
529                 ListingsParam("", false, ALL, "", empty_hint);
530         all_params_[0]["moreindex"] =
531                 ListingsParam("", false, ALL, "", empty_hint);
532         all_params_[0]["deleteindex"] =
533                 ListingsParam("", false, ALL, "", empty_hint);
534         all_params_[0]["indexstyle"] =
535                 ListingsParam("", false, ALL, "", empty_hint);
536         all_params_[0]["columns"] =
537                 ListingsParam("", false, ALL, "", empty_hint);
538         all_params_[0]["flexiblecolumns"] =
539                 ListingsParam("", false, ALL, "", empty_hint);
540         all_params_[0]["keepspaces"] =
541                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
542         all_params_[0]["basewidth"] =
543                 ListingsParam("", false, LENGTH, "", empty_hint);
544         all_params_[0]["fontadjust"] =
545                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
546         all_params_[0]["texcl"] =
547                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
548         all_params_[0]["mathescape"] =
549                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
550         all_params_[0]["escapechar"] =
551                 ListingsParam("", false, ALL, "", empty_hint);
552         all_params_[0]["escapeinside"] =
553                 ListingsParam("", false, ALL, "", empty_hint);
554         all_params_[0]["escapebegin"] =
555                 ListingsParam("", false, ALL, "", empty_hint);
556         all_params_[0]["escapeend"] =
557                 ListingsParam("", false, ALL, "", empty_hint);
558         all_params_[0]["fancyvrb"] =
559                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
560         all_params_[0]["fvcmdparams"] =
561                 ListingsParam("", false, ALL, "", empty_hint);
562         all_params_[0]["morefvcmdparams"] =
563                 ListingsParam("", false, ALL, "", empty_hint);
564         all_params_[0]["keywordsprefix"] =
565                 ListingsParam("", false, ALL, "", empty_hint);
566         all_params_[0]["keywords"] =
567                 ListingsParam("", false, ALL, "", empty_hint);
568         all_params_[0]["morekeywords"] =
569                 ListingsParam("", false, ALL, "", empty_hint);
570         all_params_[0]["deletekeywords"] =
571                 ListingsParam("", false, ALL, "", empty_hint);
572         all_params_[0]["ndkeywords"] =
573                 ListingsParam("", false, ALL, "", empty_hint);
574         all_params_[0]["morendkeywords"] =
575                 ListingsParam("", false, ALL, "", empty_hint);
576         all_params_[0]["deletendkeywords"] =
577                 ListingsParam("", false, ALL, "", empty_hint);
578         all_params_[0]["texcs"] =
579                 ListingsParam("", false, ALL, "", empty_hint);
580         all_params_[0]["moretexcs"] =
581                 ListingsParam("", false, ALL, "", empty_hint);
582         all_params_[0]["deletetexcs"] =
583                 ListingsParam("", false, ALL, "", empty_hint);
584         all_params_[0]["directives"] =
585                 ListingsParam("", false, ALL, "", empty_hint);
586         all_params_[0]["moredirectives"] =
587                 ListingsParam("", false, ALL, "", empty_hint);
588         all_params_[0]["deletedirectives"] =
589                 ListingsParam("", false, ALL, "", empty_hint);
590         all_params_[0]["sensitive"] =
591                 ListingsParam("", false, ALL, "", empty_hint);
592         all_params_[0]["alsoletter"] =
593                 ListingsParam("", false, ALL, "", empty_hint);
594         all_params_[0]["alsodigit"] =
595                 ListingsParam("", false, ALL, "", empty_hint);
596         all_params_[0]["alsoother"] =
597                 ListingsParam("", false, ALL, "", empty_hint);
598         all_params_[0]["otherkeywords"] =
599                 ListingsParam("", false, ALL, "", empty_hint);
600         all_params_[0]["tag"] =
601                 ListingsParam("", false, ALL, "", empty_hint);
602         all_params_[0]["string"] =
603                 ListingsParam("", false, ALL, "", empty_hint);
604         all_params_[0]["morestring"] =
605                 ListingsParam("", false, ALL, "", empty_hint);
606         all_params_[0]["deletestring"] =
607                 ListingsParam("", false, ALL, "", empty_hint);
608         all_params_[0]["comment"] =
609                 ListingsParam("", false, ALL, "", empty_hint);
610         all_params_[0]["morecomment"] =
611                 ListingsParam("", false, ALL, "", empty_hint);
612         all_params_[0]["deletecomment"] =
613                 ListingsParam("", false, ALL, "", empty_hint);
614         all_params_[0]["keywordcomment"] =
615                 ListingsParam("", false, ALL, "", empty_hint);
616         all_params_[0]["morekeywordcomment"] =
617                 ListingsParam("", false, ALL, "", empty_hint);
618         all_params_[0]["deletekeywordcomment"] =
619                 ListingsParam("", false, ALL, "", empty_hint);
620         all_params_[0]["keywordcommentsemicolon"] =
621                 ListingsParam("", false, ALL, "", empty_hint);
622         all_params_[0]["podcomment"] =
623                 ListingsParam("", false, ALL, "", empty_hint);
624         // the following are experimental listings features
625         all_params_[0]["procnamekeys"] =
626                 ListingsParam("", false, ALL, "", empty_hint);
627         all_params_[0]["moreprocnamekeys"] =
628                 ListingsParam("", false, ALL, "", empty_hint);
629         all_params_[0]["deleteprocnamekeys"] =
630                 ListingsParam("", false, ALL, "", empty_hint);
631         all_params_[0]["procnamestyle"] =
632                 ListingsParam("", false, ALL, "", style_hint);
633         all_params_[0]["indexprocnames"] =
634                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
635         all_params_[0]["hyperref"] =
636                 ListingsParam("", false, ALL, "", empty_hint);
637         all_params_[0]["morehyperref"] =
638                 ListingsParam("", false, ALL, "", empty_hint);
639         all_params_[0]["deletehyperref"] =
640                 ListingsParam("", false, ALL, "", empty_hint);
641         all_params_[0]["hyperanchor"] =
642                 ListingsParam("", false, ALL, "", empty_hint);
643         all_params_[0]["hyperlink"] =
644                 ListingsParam("", false, ALL, "", empty_hint);
645         all_params_[0]["literate"] =
646                 ListingsParam("", false, ALL, "", empty_hint);
647         all_params_[0]["lgrindef"] =
648                 ListingsParam("", false, ALL, "", empty_hint);
649         all_params_[0]["rangebeginprefix"] =
650                 ListingsParam("", false, ALL, "", empty_hint);
651         all_params_[0]["rangebeginsuffix"] =
652                 ListingsParam("", false, ALL, "", empty_hint);
653         all_params_[0]["rangeendprefix"] =
654                 ListingsParam("", false, ALL, "", empty_hint);
655         all_params_[0]["rangeendsuffix"] =
656                 ListingsParam("", false, ALL, "", empty_hint);
657         all_params_[0]["rangeprefix"] =
658                 ListingsParam("", false, ALL, "", empty_hint);
659         all_params_[0]["rangesuffix"] =
660                 ListingsParam("", false, ALL, "", empty_hint);
661         all_params_[0]["includerangemarker"] =
662                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
663         all_params_[0]["multicols"] =
664                 ListingsParam("", false, INTEGER, "", empty_hint);
665
666         // Minted package
667
668         // This is not a real minted option and its only purpose
669         // is to get a caption for a floating listing.
670         all_params_[1]["caption"] =
671                 ListingsParam("", false, ALL, "", _(
672                 "This parameter should not be entered here. Please use the caption "
673                 "edit box (when using the child document dialog) or "
674                 "menu Insert->Caption (when defining a listing inset)"));
675         // The "label" minted option is being subverted here for the
676         // sake of getting a label for a floating listing.
677         all_params_[1]["label"] =
678                 ListingsParam("", false, ALL, "",_(
679                 "This parameter should not be entered here. Please use the label "
680                 "edit box (when using the child document dialog) or "
681                 "menu Insert->Label (when defining a listing inset)"));
682         // This is not a real minted option and its only purpose
683         // is to signal that this is a floating listing.
684         all_params_[1]["float"] =
685                 ListingsParam("false", true, SUBSETOF, "*tbph", empty_hint);
686         all_params_[1]["cache"] =
687                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
688         all_params_[1]["cachedir"] =
689                 ListingsParam("", false, ALL, "", _(
690                                         "default: _minted-<jobname>"));
691         all_params_[1]["finalizecache"] =
692                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
693         all_params_[1]["frozencache"] =
694                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
695         all_params_[1]["draft"] =
696                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
697         all_params_[1]["final"] =
698                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
699         all_params_[1]["kpsewhich"] =
700                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
701         all_params_[1]["langlinenos"] =
702                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
703         all_params_[1]["newfloat"] =
704                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
705         all_params_[1]["outputdir"] =
706                 ListingsParam("", false, ALL, "", empty_hint);
707         all_params_[1]["autogobble"] =
708                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
709         all_params_[1]["baselinestretch"] =
710                 ListingsParam("", false, ALL, "", empty_hint);
711         all_params_[1]["breakafter"] =
712                 ListingsParam("", false, ALL, "", empty_hint);
713         all_params_[1]["breakaftergroup"] =
714                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
715         all_params_[1]["breakaftersymbolpre"] =
716                 ListingsParam("", false, ALL, "", empty_hint);
717         all_params_[1]["breakaftersymbolpost"] =
718                 ListingsParam("", false, ALL, "", empty_hint);
719         all_params_[1]["breakanywhere"] =
720                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
721         all_params_[1]["breakanywheresymbolpre"] =
722                 ListingsParam("", false, ALL, "", empty_hint);
723         all_params_[1]["breakanywheresymbolpost"] =
724                 ListingsParam("", false, ALL, "", empty_hint);
725         all_params_[1]["breakautoindent"] =
726                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
727         all_params_[1]["breakbefore"] =
728                 ListingsParam("", false, ALL, "", empty_hint);
729         all_params_[1]["breakbeforegroup"] =
730                 ListingsParam("", true, ALL, "", empty_hint);
731         all_params_[1]["breakbeforesymbolpre"] =
732                 ListingsParam("", false, ALL, "", empty_hint);
733         all_params_[1]["breakbeforesymbolpost"] =
734                 ListingsParam("", false, ALL, "", empty_hint);
735         all_params_[1]["breakbytoken"] =
736                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
737         all_params_[1]["breakbytokenanywhere"] =
738                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
739         all_params_[1]["breakindent"] =
740                 ListingsParam("", false, LENGTH, "", empty_hint);
741         all_params_[1]["breaklines"] =
742                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
743         all_params_[1]["breaksymbol"] =
744                 ListingsParam("", false, ALL, "", empty_hint);
745         all_params_[1]["breaksymbolleft"] =
746                 ListingsParam("", false, ALL, "", empty_hint);
747         all_params_[1]["breaksymbolright"] =
748                 ListingsParam("", false, ALL, "", empty_hint);
749         all_params_[1]["breaksymbolindent"] =
750                 ListingsParam("", false, LENGTH, "", empty_hint);
751         all_params_[1]["breaksymbolindentleft"] =
752                 ListingsParam("", false, LENGTH, "", empty_hint);
753         all_params_[1]["breaksymbolindentright"] =
754                 ListingsParam("", false, LENGTH, "", empty_hint);
755         all_params_[1]["breaksymbolsep"] =
756                 ListingsParam("", false, LENGTH, "", empty_hint);
757         all_params_[1]["breaksymbolsepleft"] =
758                 ListingsParam("", false, LENGTH, "", empty_hint);
759         all_params_[1]["breaksymbolsepright"] =
760                 ListingsParam("", false, LENGTH, "", empty_hint);
761         all_params_[1]["bgcolor"] =
762                 ListingsParam("", false, ALL, "", color_hint_mint);
763         all_params_[1]["codetagify"] =
764                 ListingsParam("", false, ALL, "", empty_hint);
765         all_params_[1]["curlyquotes"] =
766                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
767         all_params_[1]["encoding"] =
768                 ListingsParam("", false, ALL, "", _(
769                                 "Sets encoding expected by Pygments"));
770         all_params_[1]["escapeinside"] =
771                 ListingsParam("", false, ALL, "", empty_hint);
772         all_params_[1]["firstline"] =
773                 ListingsParam("", false, INTEGER, "", empty_hint);
774         all_params_[1]["firstnumber"] =
775                 ListingsParam("", false, ALL, "", _(
776                                         "auto, last or a number"));
777         all_params_[1]["fontfamily"] =
778                 ListingsParam("", false, ALL, "", _(
779                                 "A latex family such as tt, sf, rm"));
780         all_params_[1]["fontseries"] =
781                 ListingsParam("", false, ALL, "", _(
782                                 "A latex series such as m, b, c, bx, sb"));
783         all_params_[1]["fontsize"] =
784                 ListingsParam("", false, ALL, "", _(
785                                 "A latex name such as \\small"));
786         all_params_[1]["fontshape"] =
787                 ListingsParam("", false, ALL, "", _(
788                                 "A latex shape such as n, it, sl, sc"));
789         all_params_[1]["formatcom"] =
790                 ListingsParam("", false, ALL, "", empty_hint);
791         all_params_[1]["frame"] =
792                 ListingsParam("", false, ONEOF,
793                   "none\nleftline\ntopline\nbottomline\nlines\nsingle",
794                   frame_hint_mint);
795         all_params_[1]["framerule"] =
796                 ListingsParam("", false, LENGTH, "", empty_hint);
797         all_params_[1]["framesep"] =
798                 ListingsParam("", false, LENGTH, "", empty_hint);
799         all_params_[1]["funcnamehighlighting"] =
800                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
801         all_params_[1]["gobble"] =
802                 ListingsParam("", false, INTEGER, "", empty_hint);
803         all_params_[1]["highlightcolor"] =
804                 ListingsParam("", false, ALL, "", color_hint_mint);
805         all_params_[1]["highlightlines"] =
806                 ListingsParam("", false, ALL, "", _(
807                                 "A range of lines such as {1,3-4}"));
808         all_params_[1]["keywordcase"] =
809                 ListingsParam("", false, ONEOF,
810                                 "lower\nupper\ncapitalize", empty_hint);
811         all_params_[1]["labelposition"] =
812                 ListingsParam("", false, ONEOF,
813                         "none\ntopline\nbottomline\nall", empty_hint);
814         all_params_[1]["language"] =
815                 ListingsParam("", false, ALL, "", _(
816                 "Enter one of the supported languages. However, if you "
817                 "are defining a listing inset, it is better using the  "
818                 "language combo box, unless you need to enter a language not "
819                 "offered there, otherwise the combo box will be disabled."));
820         all_params_[1]["lastline"] =
821                 ListingsParam("", false, INTEGER, "", empty_hint);
822         all_params_[1]["linenos"] =
823                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
824         all_params_[1]["numberfirstline"] =
825                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
826         all_params_[1]["numbers"] =
827                 ListingsParam("", false, ONEOF,
828                                 "left\nright\nboth\nnone", empty_hint);
829         all_params_[1]["mathescape"] =
830                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
831         all_params_[1]["numberblanklines"] =
832                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
833         all_params_[1]["numbersep"] =
834                 ListingsParam("", false, LENGTH, "", empty_hint);
835         all_params_[1]["obeytabs"] =
836                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
837         all_params_[1]["outencoding"] =
838                 ListingsParam("", false, ALL, "", _(
839                   "File encoding used by Pygments for highlighting"));
840         all_params_[1]["python3"] =
841                 ListingsParam("", false, TRUEFALSE, "", _(
842                                         "Apply Python 3 highlighting"));
843         all_params_[1]["resetmargins"] =
844                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
845         all_params_[1]["rulecolor"] =
846                 ListingsParam("", false, ALL, "", color_hint_mint);
847         all_params_[1]["samepage"] =
848                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
849         all_params_[1]["showspaces"] =
850                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
851         all_params_[1]["showtabs"] =
852                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
853         all_params_[1]["space"] =
854                 ListingsParam("", false, ALL, "", _(
855                                 "A macro. Default: \\textvisiblespace"));
856         all_params_[1]["spacecolor"] =
857                 ListingsParam("", false, ALL, "", color_hint_mint);
858         all_params_[1]["startinline"] =
859                 ListingsParam("", false, TRUEFALSE, "", _("For PHP only"));
860         all_params_[1]["style"] =
861                 ListingsParam("", false, ALL, "", _(
862                                         "The style used by Pygments"));
863         all_params_[1]["stepnumber"] =
864                 ListingsParam("", false, INTEGER, "", empty_hint);
865         all_params_[1]["stepnumberfromfirst"] =
866                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
867         all_params_[1]["stepnumberoffsetvalues"] =
868                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
869         all_params_[1]["stripall"] =
870                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
871         all_params_[1]["stripnl"] =
872                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
873         all_params_[1]["tab"] =
874                 ListingsParam("", false, ALL, "", _(
875                                 "A macro to redefine visible tabs"));
876         all_params_[1]["tabcolor"] =
877                 ListingsParam("", false, ALL, "", color_hint_mint);
878         all_params_[1]["tabsize"] =
879                 ListingsParam("", false, INTEGER, "", empty_hint);
880         all_params_[1]["texcl"] =
881                 ListingsParam("", false, TRUEFALSE, "", _(
882                                 "Enables latex code in comments"));
883         all_params_[1]["texcomments"] =
884                 ListingsParam("", false, TRUEFALSE, "", _(
885                                 "Enables latex code in comments"));
886         all_params_[1]["xleftmargin"] =
887                 ListingsParam("", false, LENGTH, "", empty_hint);
888         all_params_[1]["xrightmargin"] =
889                 ListingsParam("", false, LENGTH, "", empty_hint);
890 }
891
892
893 docstring ParValidator::validate(string const & name,
894                 string const & par) const
895 {
896         int p = InsetListingsParams::package();
897
898         if (name.empty())
899                 return _("Invalid (empty) listing parameter name.");
900
901         if (name[0] == '?') {
902                 string suffix = trim(string(name, 1));
903                 string param_names;
904                 ListingsParams::const_iterator it = all_params_[p].begin();
905                 ListingsParams::const_iterator end = all_params_[p].end();
906                 for (; it != end; ++it) {
907                         if (suffix.empty() || contains(it->first, suffix)) {
908                                 if (!param_names.empty())
909                                         param_names += ", ";
910                                 param_names += it->first;
911                         }
912                 }
913                 if (suffix.empty())
914                         return bformat(
915                                         _("Available listing parameters are %1$s"), from_ascii(param_names));
916                 else
917                         return bformat(
918                                         _("Available listings parameters containing string \"%1$s\" are %2$s"),
919                                                 from_utf8(suffix), from_utf8(param_names));
920         }
921
922         // locate name in parameter table
923         ListingsParams::const_iterator it = all_params_[p].find(name);
924         if (it != all_params_[p].end()) {
925                 docstring msg = it->second.validate(par);
926                 if (msg.empty())
927                         return msg;
928                 else
929                         return bformat(_("Parameter %1$s: "), from_utf8(name)) + msg;
930         } else {
931                 // otherwise, produce a meaningful error message.
932                 string matching_names;
933                 ListingsParams::const_iterator end = all_params_[p].end();
934                 for (it = all_params_[p].begin(); it != end; ++it) {
935                         if (prefixIs(it->first, name)) {
936                                 if (!matching_names.empty())
937                                         matching_names += ", ";
938                                 matching_names += it->first;
939                         }
940                 }
941                 if (matching_names.empty())
942                         return bformat(_("Unknown listing parameter name: %1$s"),
943                                                                 from_utf8(name));
944                 else
945                         return bformat(_("Parameters starting with '%1$s': %2$s"),
946                                                                 from_utf8(name), from_utf8(matching_names));
947         }
948 }
949
950
951 bool ParValidator::onoff(string const & key) const
952 {
953         int p = InsetListingsParams::package();
954
955         // locate name in parameter table
956         ListingsParams::const_iterator it = all_params_[p].find(key);
957         if (it != all_params_[p].end())
958                 return it->second.onoff_;
959         else
960                 return false;
961 }
962
963 } // namespace
964
965 // define a global ParValidator
966 ParValidator * par_validator = 0;
967
968 // The package to be used by the global ParValidator
969 // (0 for listings, 1 for minted)
970 int InsetListingsParams::package_ = 0;
971
972 InsetListingsParams::InsetListingsParams()
973         : inline_(false), params_(), status_(InsetCollapsible::Open)
974 {
975 }
976
977
978 InsetListingsParams::InsetListingsParams(string const & par, bool in,
979                 InsetCollapsible::CollapseStatus s)
980         : inline_(in), params_(), status_(s)
981 {
982         // this will activate parameter validation.
983         fromEncodedString(par);
984 }
985
986
987 void InsetListingsParams::write(ostream & os) const
988 {
989         if (inline_)
990                 os << "true ";
991         else
992                 os << "false ";
993         os << status_ << " \""  << encodedString() << "\"";
994 }
995
996
997 void InsetListingsParams::read(Lexer & lex)
998 {
999         lex >> inline_;
1000         int s = InsetCollapsible::Collapsed;
1001         lex >> s;
1002         status_ = static_cast<InsetCollapsible::CollapseStatus>(s);
1003         string par;
1004         lex >> par;
1005         fromEncodedString(par);
1006 }
1007
1008
1009 string InsetListingsParams::params(string const & sep) const
1010 {
1011         string par;
1012         keyValuePair::const_iterator it = params_.begin();
1013         for (; it != params_.end(); ++it) {
1014                 if (!par.empty())
1015                         par += sep;
1016                 // key=value,key=value1 is stored in params_ as key=value,key_=value1.
1017                 if (it->second.empty())
1018                         par += rtrim(it->first, "_");
1019                 else
1020                         par += rtrim(it->first, "_") + '=' + it->second;
1021         }
1022         return par;
1023 }
1024
1025
1026 bool InsetListingsParams::hasParam(string const & key) const
1027 {
1028         keyValuePair::const_iterator it = params_.begin();
1029         for (; it != params_.end(); ++it) {
1030                 if (it->first == key)
1031                         return true;
1032         }
1033         return false;
1034 }
1035
1036
1037 string InsetListingsParams::getValue(string const & key) const
1038 {
1039         keyValuePair::const_iterator it = params_.begin();
1040         for (; it != params_.end(); ++it) {
1041                 if (it->first == key)
1042                         return it->second;
1043         }
1044         return string();
1045 }
1046
1047
1048 void InsetListingsParams::addParam(string const & key,
1049                 string const & val, bool replace)
1050 {
1051         if (key.empty())
1052                 return;
1053
1054         bool const is_minted_language = minted() && key == "language";
1055         string const value = (is_minted_language && !val.empty())
1056                                 ? languageonly(val) : val;
1057         // duplicate parameters!
1058         string keyname = key;
1059         if (!replace && hasParam(key))
1060                 // key=value,key=value1 is allowed in listings
1061                 // use key_, key__, key___ etc to avoid name conflict
1062                 while (hasParam(keyname += '_')) { }
1063         // check onoff flag
1064         // onoff parameter with value false
1065         if (!par_validator)
1066                 par_validator = new ParValidator;
1067         if (par_validator->onoff(key) && (value == "false" || value == "{false}"))
1068                 params_.push_back(make_pair(keyname, string()));
1069         // if the parameter is surrounded with {}, good
1070         else if (prefixIs(value, "{") && suffixIs(value, "}"))
1071                 params_.push_back(make_pair(keyname, value));
1072         // otherwise, check if {} is needed. Add {} to all values with
1073         // non-ascii/number characters, just to be safe
1074         else {
1075                 bool has_special_char = false;
1076                 for (char c : value)
1077                         if (!isAlnumASCII(c)) {
1078                                 has_special_char = true;
1079                                 break;
1080                         }
1081                 if (has_special_char && !is_minted_language)
1082                         params_.push_back(make_pair(keyname, "{" + value + "}"));
1083                 else
1084                         params_.push_back(make_pair(keyname, value));
1085         }
1086 }
1087
1088
1089 void InsetListingsParams::addParams(string const & par)
1090 {
1091         string key;
1092         string value;
1093         bool isValue = false;
1094         int braces = 0;
1095         for (size_t i = 0; i < par.size(); ++i) {
1096                 // end of par
1097                 if (par[i] == '\n') {
1098                         addParam(trim(key), trim(value));
1099                         key = string();
1100                         value = string();
1101                         isValue = false;
1102                         continue;
1103                 } else if (par[i] == ',' && braces == 0) {
1104                         addParam(trim(key), trim(value));
1105                         key = string();
1106                         value = string();
1107                         isValue = false;
1108                         continue;
1109                 } else if (par[i] == '=' && braces == 0) {
1110                         isValue = true;
1111                         continue;
1112                 } else if (par[i] == '{' && i > 0 && par[i-1] != '\\')
1113                         // don't count a brace in first position
1114                         ++braces;
1115                 else if (par[i] == '}' && i != par.size() - 1
1116                          && (i == 0 || (i > 0 && par[i-1] != '\\')))
1117                         --braces;
1118
1119                 if (isValue)
1120                         value += par[i];
1121                 else
1122                         key += par[i];
1123         }
1124         if (!trim(key).empty())
1125                 addParam(trim(key), trim(value));
1126 }
1127
1128
1129 void InsetListingsParams::setParams(string const & par)
1130 {
1131         params_.clear();
1132         addParams(par);
1133 }
1134
1135
1136 string InsetListingsParams::encodedString() const
1137 {
1138         // Encode string!
1139         // '"' is handled differently because it will
1140         // terminate a lyx token.
1141         string par = params();
1142         // '"' is now &quot;  ==> '"' is now &amp;quot;
1143         par = subst(par, "&", "&amp;");
1144         // '"' is now &amp;quot; ==> '&quot;' is now &amp;quot;
1145         par = subst(par, "\"", "&quot;");
1146         return par;
1147 }
1148
1149
1150 string InsetListingsParams::separatedParams(bool keepComma) const
1151 {
1152         if (keepComma)
1153                 return params(",\n");
1154         else
1155                 return params("\n");
1156 }
1157
1158
1159 void InsetListingsParams::fromEncodedString(string const & in)
1160 {
1161         // Decode string! Reversal of encodedString
1162         string par = in;
1163         // '&quot;' is now &amp;quot; ==> '"' is now &amp;quot;
1164         par = subst(par, "&quot;", "\"");
1165         //  '"' is now &amp;quot; ==> '"' is now &quot;
1166         par = subst(par, "&amp;", "&");
1167         setParams(par);
1168 }
1169
1170
1171 bool InsetListingsParams::isFloat() const
1172 {
1173         return hasParam("float");
1174 }
1175
1176
1177 string InsetListingsParams::getParamValue(string const & param) const
1178 {
1179         string par = getValue(param);
1180         if (prefixIs(par, "{") && suffixIs(par, "}"))
1181                 return par.substr(1, par.size() - 2);
1182         else
1183                 return par;
1184 }
1185
1186
1187 docstring InsetListingsParams::validate() const
1188 {
1189         docstring msg;
1190         if (!par_validator)
1191                 par_validator = new ParValidator;
1192         // return msg for first key=value pair which is incomplete or has an error
1193         keyValuePair::const_iterator it = params_.begin();
1194         for (; it != params_.end(); ++it) {
1195                 // key trimmed
1196                 msg = par_validator->validate(rtrim(it->first, "_"), it->second);
1197                 if (!msg.empty())
1198                         return msg;
1199         }
1200         return msg;
1201 }
1202
1203 } // namespace lyx