]> git.lyx.org Git - features.git/blob - src/insets/InsetListingsParams.cpp
Fix bug #9101
[features.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 "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 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;
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 (size_t i = 0; i < par2.size(); ++i)
236                         if (info_.find(par2[i], 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\nACSL\nAda\n[2005]Ada\n[83]Ada\n"
251         "[95]Ada\nALGOL\n[60]ALGOL\n[68]ALGOL\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\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\nerlang\nEuphoria\nFortran\n[77]Fortran\n[90]Fortran\n"
258         "[95]Fortran\nGCL\nGnuplot\nHaskell\nHTML\nIDL\n[CORBA]IDL\ninform\n"
259         "Java\n[AspectJ]Java\nJVMIS\nksh\nLingo\nLisp\n[Auto]Lisp\nLogo\n"
260         "make\n[gnu]make\nMathematica\n[1.0]Mathematica\n[3.0]Mathematica\n"
261         "[5.2]Mathematica\nMatlab\nMercury\nMetaPost\nMiranda\nMizar\nML\n"
262         "Modula-2\nMuPAD\nNASTRAN\nOberon-2\nOCL\n[decorative]OCL\n[OMG]OCL\n"
263         "Octave\nOz\nPascal\n[Borland6]Pascal\n[Standard]Pascal\n[XSC]Pascal\n"
264         "Perl\nPHP\nPL/I\nPlasm\nPostScript\nPOV\nProlog\nPromela\nPSTricks\n"
265         "Python\nR\nReduce\nRexx\nRSL\nRuby\nS\n[PLUS]S\nSAS\nScilab\nsh\n"
266         "SHELXL\nSimula\n[67]Simula\n[CII]Simula\n[DEC]Simula\n[IBM]Simula\n"
267         "SPARQL\nSQL\ntcl\n[tk]tcl\nTeX\n[AlLaTeX]TeX\n[common]TeX\n[LaTeX]TeX\n"
268         "[plain]TeX\n[primitive]TeX\nVBScript\nVerilog\nVHDL\n[AMS]VHDL\nVRML\n"
269         "[97]VRML\nXML\nXSLT";
270
271
272 /// ListingsParam Validator.
273 /// This class is aimed to be a singleton which is instantiated in
274 /// \c InsetListingsParams::addParam().
275 // FIXME: transfer this validator to the frontend.
276 // FIXME: avoid the use of exception.
277 class ParValidator
278 {
279 public:
280         ParValidator();
281
282         /// validate a parameter for a given name.
283         /// return an error message if \c par is an invalid parameter.
284         docstring validate(string const & name, string const & par) const;
285
286         /// return the onoff status of a parameter \c key, if \c key is not found
287         /// return false
288         bool onoff(string const & key) const;
289
290 private:
291         /// key is the name of the parameter
292         typedef map<string, ListingsParam> ListingsParams;
293         ListingsParams all_params_[2];
294 };
295
296
297 ParValidator::ParValidator()
298 {
299         docstring const empty_hint;
300         docstring const style_hint = _("Use \\footnotesize, \\small, \\itshape, "
301                 "\\ttfamily or something like that");
302         docstring const frame_hint_mint =
303                 _("none, leftline, topline, bottomline, lines, single");
304         docstring const frame_hint_lst =
305                 _("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_mint =
311                         _("Previously defined color name as a string");
312         docstring const color_hint_lst =
313                         _("Enter something like \\color{white}");
314
315         // Listings package
316
317         /// options copied from page 26 of listings manual
318         // FIXME: add default parameters ... (which is not used now)
319         all_params_[0]["float"] =
320                 ListingsParam("false", true, SUBSETOF, "*tbph", empty_hint);
321         all_params_[0]["floatplacement"] =
322                 ListingsParam("tbp", false, SUBSETOF, "tbp", empty_hint);
323         all_params_[0]["aboveskip"] =
324                 ListingsParam("\\medskipamount", false, SKIP, "", empty_hint);
325         all_params_[0]["belowskip"] =
326                 ListingsParam("\\medskipamount", false, SKIP, "", empty_hint);
327         all_params_[0]["lineskip"] =
328                 ListingsParam("", false, SKIP, "", empty_hint);
329         all_params_[0]["boxpos"] =
330                 ListingsParam("", false, SUBSETOF, "bct", empty_hint);
331         all_params_[0]["print"] =
332                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
333         all_params_[0]["firstline"] =
334                 ListingsParam("", false, INTEGER, "", empty_hint);
335         all_params_[0]["lastline"] =
336                 ListingsParam("", false, INTEGER, "", empty_hint);
337         all_params_[0]["linerange"] =
338                 ListingsParam("", false, ALL, "", empty_hint);
339         all_params_[0]["showlines"] =
340                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
341         all_params_[0]["emptylines"] =
342                 ListingsParam("", false, ALL, "", _(
343                 "Expect a number with an optional * before it"));
344         all_params_[0]["gobble"] =
345                 ListingsParam("", false, INTEGER, "", empty_hint);
346         all_params_[0]["style"] =
347                 ListingsParam("", false, ALL, "", empty_hint);
348         all_params_[0]["language"] =
349                 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
350         all_params_[0]["alsolanguage"] =
351                 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
352         all_params_[0]["defaultdialect"] =
353                 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
354         all_params_[0]["printpod"] =
355                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
356         all_params_[0]["usekeywordsintag"] =
357                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
358         all_params_[0]["tagstyle"] =
359                 ListingsParam("", false, ALL, "", style_hint);
360         all_params_[0]["markfirstintag"] =
361                 ListingsParam("", false, ALL, "", style_hint);
362         all_params_[0]["makemacrouse"] =
363                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
364         all_params_[0]["basicstyle"] =
365                 ListingsParam("", false, ALL, "", style_hint);
366         all_params_[0]["identifierstyle"] =
367                 ListingsParam("", false, ALL, "", style_hint);
368         all_params_[0]["commentstyle"] =
369                 ListingsParam("", false, ALL, "", style_hint);
370         all_params_[0]["stringstyle"] =
371                 ListingsParam("", false, ALL, "", style_hint);
372         all_params_[0]["keywordstyle"] =
373                 ListingsParam("", false, ALL, "", style_hint);
374         all_params_[0]["ndkeywordstyle"] =
375                 ListingsParam("", false, ALL, "", style_hint);
376         all_params_[0]["classoffset"] =
377                 ListingsParam("", false, INTEGER, "", empty_hint);
378         all_params_[0]["texcsstyle"] =
379                 ListingsParam("", false, ALL, "", style_hint);
380         all_params_[0]["directivestyle"] =
381                 ListingsParam("", false, ALL, "", style_hint);
382         all_params_[0]["emph"] =
383                 ListingsParam("", false, ALL, "", empty_hint);
384         all_params_[0]["moreemph"] =
385                 ListingsParam("", false, ALL, "", empty_hint);
386         all_params_[0]["deleteemph"] =
387                 ListingsParam("", false, ALL, "", empty_hint);
388         all_params_[0]["emphstyle"] =
389                 ListingsParam("", false, ALL, "", empty_hint);
390         all_params_[0]["delim"] =
391                 ListingsParam("", false, ALL, "", empty_hint);
392         all_params_[0]["moredelim"] =
393                 ListingsParam("", false, ALL, "", empty_hint);
394         all_params_[0]["deletedelim"] =
395                 ListingsParam("", false, ALL, "", empty_hint);
396         all_params_[0]["extendedchars"] =
397                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
398         all_params_[0]["inputencoding"] =
399                 ListingsParam("", false, ALL, "", empty_hint);
400         all_params_[0]["upquote"] =
401                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
402         all_params_[0]["tabsize"] =
403                 ListingsParam("", false, INTEGER, "", empty_hint);
404         all_params_[0]["showtabs"] =
405                 ListingsParam("", false, ALL, "", empty_hint);
406         all_params_[0]["tab"] =
407                 ListingsParam("", false, ALL, "", empty_hint);
408         all_params_[0]["showspaces"] =
409                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
410         all_params_[0]["showstringspaces"] =
411                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
412         all_params_[0]["formfeed"] =
413                 ListingsParam("", false, ALL, "", empty_hint);
414         all_params_[0]["numbers"] =
415                 ListingsParam("", false, ONEOF, "none\nleft\nright", empty_hint);
416         all_params_[0]["stepnumber"] =
417                 ListingsParam("", false, INTEGER, "", empty_hint);
418         all_params_[0]["numberfirstline"] =
419                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
420         all_params_[0]["numberstyle"] =
421                 ListingsParam("", false, ALL, "", style_hint);
422         all_params_[0]["numbersep"] =
423                 ListingsParam("", false, LENGTH, "", empty_hint);
424         all_params_[0]["numberblanklines"] =
425                 ListingsParam("", false, ALL, "", empty_hint);
426         all_params_[0]["firstnumber"] =
427                 ListingsParam("", false, ALL, "", _("auto, last or a number"));
428         all_params_[0]["name"] =
429                 ListingsParam("", false, ALL, "", empty_hint);
430         all_params_[0]["thelstnumber"] =
431                 ListingsParam("", false, ALL, "", empty_hint);
432         all_params_[0]["title"] =
433                 ListingsParam("", false, ALL, "", empty_hint);
434         // this option is not handled in the parameter box
435         all_params_[0]["caption"] =
436                 ListingsParam("", false, ALL, "", _(
437                 "This parameter should not be entered here. Please use the caption "
438                 "edit box (when using the child document dialog) or "
439                 "menu Insert->Caption (when defining a listing inset)"));
440         // this option is not handled in the parameter box
441         all_params_[0]["label"] =
442                 ListingsParam("", false, ALL, "",_(
443                 "This parameter should not be entered here. Please use the label "
444                 "edit box (when using the child document dialog) or "
445                 "menu Insert->Label (when defining a listing inset)"));
446         all_params_[0]["nolol"] =
447                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
448         all_params_[0]["captionpos"] =
449                 ListingsParam("", false, SUBSETOF, "tb", empty_hint);
450         all_params_[0]["abovecaptionskip"] =
451                 ListingsParam("", false, SKIP, "", empty_hint);
452         all_params_[0]["belowcaptionskip"] =
453                 ListingsParam("", false, SKIP, "", empty_hint);
454         all_params_[0]["linewidth"] =
455                 ListingsParam("", false, LENGTH, "", empty_hint);
456         all_params_[0]["xleftmargin"] =
457                 ListingsParam("", false, LENGTH, "", empty_hint);
458         all_params_[0]["xrightmargin"] =
459                 ListingsParam("", false, LENGTH, "", empty_hint);
460         all_params_[0]["resetmargins"] =
461                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
462         all_params_[0]["breaklines"] =
463                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
464         all_params_[0]["breakatwhitespace"] =
465                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
466         all_params_[0]["prebreak"] =
467                 ListingsParam("", false, ALL, "", empty_hint);
468         all_params_[0]["postbreak"] =
469                 ListingsParam("", false, ALL, "", empty_hint);
470         all_params_[0]["breakindent"] =
471                 ListingsParam("", false, LENGTH, "", empty_hint);
472         all_params_[0]["breakautoindent"] =
473                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
474         all_params_[0]["frame"] =
475                 ListingsParam("", false, ALL, "", frame_hint_lst);
476         all_params_[0]["frameround"] =
477                 ListingsParam("", false, SUBSETOF, "tf", frameround_hint);
478         all_params_[0]["framesep"] =
479                 ListingsParam("", false, LENGTH, "", empty_hint);
480         all_params_[0]["rulesep"] =
481                 ListingsParam("", false, LENGTH, "", empty_hint);
482         all_params_[0]["framerule"] =
483                 ListingsParam("", false, LENGTH, "", empty_hint);
484         all_params_[0]["framexleftmargin"] =
485                 ListingsParam("", false, LENGTH, "", empty_hint);
486         all_params_[0]["framexrightmargin"] =
487                 ListingsParam("", false, LENGTH, "", empty_hint);
488         all_params_[0]["framextopmargin"] =
489                 ListingsParam("", false, LENGTH, "", empty_hint);
490         all_params_[0]["framexbottommargin"] =
491                 ListingsParam("", false, LENGTH, "", empty_hint);
492         all_params_[0]["backgroundcolor"] =
493                 ListingsParam("", false, ALL, "", color_hint_lst);
494         all_params_[0]["rulecolor"] =
495                 ListingsParam("", false, ALL, "", color_hint_lst);
496         all_params_[0]["fillcolor"] =
497                 ListingsParam("", false, ALL, "", color_hint_lst);
498         all_params_[0]["rulesepcolor"] =
499                 ListingsParam("", false, ALL, "", color_hint_lst);
500         all_params_[0]["frameshape"] =
501                 ListingsParam("", false, ALL, "", empty_hint);
502         all_params_[0]["index"] =
503                 ListingsParam("", false, ALL, "", empty_hint);
504         all_params_[0]["moreindex"] =
505                 ListingsParam("", false, ALL, "", empty_hint);
506         all_params_[0]["deleteindex"] =
507                 ListingsParam("", false, ALL, "", empty_hint);
508         all_params_[0]["indexstyle"] =
509                 ListingsParam("", false, ALL, "", empty_hint);
510         all_params_[0]["columns"] =
511                 ListingsParam("", false, ALL, "", empty_hint);
512         all_params_[0]["flexiblecolumns"] =
513                 ListingsParam("", false, ALL, "", empty_hint);
514         all_params_[0]["keepspaces"] =
515                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
516         all_params_[0]["basewidth"] =
517                 ListingsParam("", false, LENGTH, "", empty_hint);
518         all_params_[0]["fontadjust"] =
519                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
520         all_params_[0]["texcl"] =
521                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
522         all_params_[0]["mathescape"] =
523                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
524         all_params_[0]["escapechar"] =
525                 ListingsParam("", false, ALL, "", empty_hint);
526         all_params_[0]["escapeinside"] =
527                 ListingsParam("", false, ALL, "", empty_hint);
528         all_params_[0]["escapebegin"] =
529                 ListingsParam("", false, ALL, "", empty_hint);
530         all_params_[0]["escapeend"] =
531                 ListingsParam("", false, ALL, "", empty_hint);
532         all_params_[0]["fancyvrb"] =
533                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
534         all_params_[0]["fvcmdparams"] =
535                 ListingsParam("", false, ALL, "", empty_hint);
536         all_params_[0]["morefvcmdparams"] =
537                 ListingsParam("", false, ALL, "", empty_hint);
538         all_params_[0]["keywordsprefix"] =
539                 ListingsParam("", false, ALL, "", empty_hint);
540         all_params_[0]["keywords"] =
541                 ListingsParam("", false, ALL, "", empty_hint);
542         all_params_[0]["morekeywords"] =
543                 ListingsParam("", false, ALL, "", empty_hint);
544         all_params_[0]["deletekeywords"] =
545                 ListingsParam("", false, ALL, "", empty_hint);
546         all_params_[0]["ndkeywords"] =
547                 ListingsParam("", false, ALL, "", empty_hint);
548         all_params_[0]["morendkeywords"] =
549                 ListingsParam("", false, ALL, "", empty_hint);
550         all_params_[0]["deletendkeywords"] =
551                 ListingsParam("", false, ALL, "", empty_hint);
552         all_params_[0]["texcs"] =
553                 ListingsParam("", false, ALL, "", empty_hint);
554         all_params_[0]["moretexcs"] =
555                 ListingsParam("", false, ALL, "", empty_hint);
556         all_params_[0]["deletetexcs"] =
557                 ListingsParam("", false, ALL, "", empty_hint);
558         all_params_[0]["directives"] =
559                 ListingsParam("", false, ALL, "", empty_hint);
560         all_params_[0]["moredirectives"] =
561                 ListingsParam("", false, ALL, "", empty_hint);
562         all_params_[0]["deletedirectives"] =
563                 ListingsParam("", false, ALL, "", empty_hint);
564         all_params_[0]["sensitive"] =
565                 ListingsParam("", false, ALL, "", empty_hint);
566         all_params_[0]["alsoletter"] =
567                 ListingsParam("", false, ALL, "", empty_hint);
568         all_params_[0]["alsodigit"] =
569                 ListingsParam("", false, ALL, "", empty_hint);
570         all_params_[0]["alsoother"] =
571                 ListingsParam("", false, ALL, "", empty_hint);
572         all_params_[0]["otherkeywords"] =
573                 ListingsParam("", false, ALL, "", empty_hint);
574         all_params_[0]["tag"] =
575                 ListingsParam("", false, ALL, "", empty_hint);
576         all_params_[0]["string"] =
577                 ListingsParam("", false, ALL, "", empty_hint);
578         all_params_[0]["morestring"] =
579                 ListingsParam("", false, ALL, "", empty_hint);
580         all_params_[0]["deletestring"] =
581                 ListingsParam("", false, ALL, "", empty_hint);
582         all_params_[0]["comment"] =
583                 ListingsParam("", false, ALL, "", empty_hint);
584         all_params_[0]["morecomment"] =
585                 ListingsParam("", false, ALL, "", empty_hint);
586         all_params_[0]["deletecomment"] =
587                 ListingsParam("", false, ALL, "", empty_hint);
588         all_params_[0]["keywordcomment"] =
589                 ListingsParam("", false, ALL, "", empty_hint);
590         all_params_[0]["morekeywordcomment"] =
591                 ListingsParam("", false, ALL, "", empty_hint);
592         all_params_[0]["deletekeywordcomment"] =
593                 ListingsParam("", false, ALL, "", empty_hint);
594         all_params_[0]["keywordcommentsemicolon"] =
595                 ListingsParam("", false, ALL, "", empty_hint);
596         all_params_[0]["podcomment"] =
597                 ListingsParam("", false, ALL, "", empty_hint);
598         // the following are experimental listings features
599         all_params_[0]["procnamekeys"] =
600                 ListingsParam("", false, ALL, "", empty_hint);
601         all_params_[0]["moreprocnamekeys"] =
602                 ListingsParam("", false, ALL, "", empty_hint);
603         all_params_[0]["deleteprocnamekeys"] =
604                 ListingsParam("", false, ALL, "", empty_hint);
605         all_params_[0]["procnamestyle"] =
606                 ListingsParam("", false, ALL, "", style_hint);
607         all_params_[0]["indexprocnames"] =
608                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
609         all_params_[0]["hyperref"] =
610                 ListingsParam("", false, ALL, "", empty_hint);
611         all_params_[0]["morehyperref"] =
612                 ListingsParam("", false, ALL, "", empty_hint);
613         all_params_[0]["deletehyperref"] =
614                 ListingsParam("", false, ALL, "", empty_hint);
615         all_params_[0]["hyperanchor"] =
616                 ListingsParam("", false, ALL, "", empty_hint);
617         all_params_[0]["hyperlink"] =
618                 ListingsParam("", false, ALL, "", empty_hint);
619         all_params_[0]["literate"] =
620                 ListingsParam("", false, ALL, "", empty_hint);
621         all_params_[0]["lgrindef"] =
622                 ListingsParam("", false, ALL, "", empty_hint);
623         all_params_[0]["rangebeginprefix"] =
624                 ListingsParam("", false, ALL, "", empty_hint);
625         all_params_[0]["rangebeginsuffix"] =
626                 ListingsParam("", false, ALL, "", empty_hint);
627         all_params_[0]["rangeendprefix"] =
628                 ListingsParam("", false, ALL, "", empty_hint);
629         all_params_[0]["rangeendsuffix"] =
630                 ListingsParam("", false, ALL, "", empty_hint);
631         all_params_[0]["rangeprefix"] =
632                 ListingsParam("", false, ALL, "", empty_hint);
633         all_params_[0]["rangesuffix"] =
634                 ListingsParam("", false, ALL, "", empty_hint);
635         all_params_[0]["includerangemarker"] =
636                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
637         all_params_[0]["multicols"] =
638                 ListingsParam("", false, INTEGER, "", empty_hint);
639
640         // Minted package
641
642         // This is not a real minted option and its only purpose
643         // is to get a caption for a floating listing.
644         all_params_[1]["caption"] =
645                 ListingsParam("", false, ALL, "", _(
646                 "This parameter should not be entered here. Please use the caption "
647                 "edit box (when using the child document dialog) or "
648                 "menu Insert->Caption (when defining a listing inset)"));
649         // The "label" minted option is being subverted here for the
650         // sake of getting a label for a floating listing.
651         all_params_[1]["label"] =
652                 ListingsParam("", false, ALL, "",_(
653                 "This parameter should not be entered here. Please use the label "
654                 "edit box (when using the child document dialog) or "
655                 "menu Insert->Label (when defining a listing inset)"));
656         // This is not a real minted option and its only purpose
657         // is to signal that this is a floating listing.
658         all_params_[1]["float"] =
659                 ListingsParam("false", true, SUBSETOF, "*tbph", empty_hint);
660         all_params_[1]["chapter"] =
661                 ListingsParam("", true, TRUEFALSE, "", _(
662                                         "Number floats by chapter"));
663         all_params_[1]["section"] =
664                 ListingsParam("", true, TRUEFALSE, "", _(
665                                         "Number floats by section"));
666         all_params_[1]["cache"] =
667                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
668         all_params_[1]["cachedir"] =
669                 ListingsParam("", false, ALL, "", _(
670                                         "default: _minted-<jobname>"));
671         all_params_[1]["finalizecache"] =
672                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
673         all_params_[1]["frozencache"] =
674                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
675         all_params_[1]["draft"] =
676                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
677         all_params_[1]["final"] =
678                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
679         all_params_[1]["kpsewhich"] =
680                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
681         all_params_[1]["langlinenos"] =
682                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
683         all_params_[1]["newfloat"] =
684                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
685         all_params_[1]["outputdir"] =
686                 ListingsParam("", false, ALL, "", empty_hint);
687         all_params_[1]["autogobble"] =
688                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
689         all_params_[1]["baselinestretch"] =
690                 ListingsParam("", false, LENGTH, "", empty_hint);
691         all_params_[1]["breakafter"] =
692                 ListingsParam("", false, ALL, "", empty_hint);
693         all_params_[1]["breakaftergroup"] =
694                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
695         all_params_[1]["breakaftersymbolpre"] =
696                 ListingsParam("", false, ALL, "", empty_hint);
697         all_params_[1]["breakaftersymbolpost"] =
698                 ListingsParam("", false, ALL, "", empty_hint);
699         all_params_[1]["breakanywhere"] =
700                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
701         all_params_[1]["breakanywheresymbolpre"] =
702                 ListingsParam("", false, ALL, "", empty_hint);
703         all_params_[1]["breakanywheresymbolpost"] =
704                 ListingsParam("", false, ALL, "", empty_hint);
705         all_params_[1]["breakautoindent"] =
706                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
707         all_params_[1]["breakbefore"] =
708                 ListingsParam("", false, ALL, "", empty_hint);
709         all_params_[1]["breakbeforegroup"] =
710                 ListingsParam("", true, ALL, "", empty_hint);
711         all_params_[1]["breakbeforesymbolpre"] =
712                 ListingsParam("", false, ALL, "", empty_hint);
713         all_params_[1]["breakbeforesymbolpost"] =
714                 ListingsParam("", false, ALL, "", empty_hint);
715         all_params_[1]["breakbytoken"] =
716                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
717         all_params_[1]["breakbytokenanywhere"] =
718                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
719         all_params_[1]["breakindent"] =
720                 ListingsParam("", false, LENGTH, "", empty_hint);
721         all_params_[1]["breaklines"] =
722                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
723         all_params_[1]["breaksymbol"] =
724                 ListingsParam("", false, ALL, "", empty_hint);
725         all_params_[1]["breaksymbolleft"] =
726                 ListingsParam("", false, ALL, "", empty_hint);
727         all_params_[1]["breaksymbolright"] =
728                 ListingsParam("", false, ALL, "", empty_hint);
729         all_params_[1]["breaksymbolindent"] =
730                 ListingsParam("", false, LENGTH, "", empty_hint);
731         all_params_[1]["breaksymbolindentleft"] =
732                 ListingsParam("", false, LENGTH, "", empty_hint);
733         all_params_[1]["breaksymbolindentright"] =
734                 ListingsParam("", false, LENGTH, "", empty_hint);
735         all_params_[1]["breaksymbolsep"] =
736                 ListingsParam("", false, LENGTH, "", empty_hint);
737         all_params_[1]["breaksymbolsepleft"] =
738                 ListingsParam("", false, LENGTH, "", empty_hint);
739         all_params_[1]["breaksymbolsepright"] =
740                 ListingsParam("", false, LENGTH, "", empty_hint);
741         all_params_[1]["bgcolor"] =
742                 ListingsParam("", false, ALL, "", color_hint_mint);
743         all_params_[1]["codetagify"] =
744                 ListingsParam("", false, ALL, "", empty_hint);
745         all_params_[1]["curlyquotes"] =
746                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
747         all_params_[1]["encoding"] =
748                 ListingsParam("", false, ALL, "", _(
749                                 "Sets encoding expected by Pygments"));
750         all_params_[1]["escapeinside"] =
751                 ListingsParam("", false, ALL, "", empty_hint);
752         all_params_[1]["firstline"] =
753                 ListingsParam("", false, INTEGER, "", empty_hint);
754         all_params_[1]["firstnumber"] =
755                 ListingsParam("", false, ALL, "", _(
756                                         "(auto | last | integer)"));
757         all_params_[1]["fontfamily"] =
758                 ListingsParam("", false, ALL, "", _(
759                                 "A latex family such as tt, sf, rm"));
760         all_params_[1]["fontseries"] =
761                 ListingsParam("", false, ALL, "", _(
762                                 "A latex series such as m, b, c, bx, sb"));
763         all_params_[1]["fontsize"] =
764                 ListingsParam("", false, ALL, "", _(
765                                 "A latex name such as \\small"));
766         all_params_[1]["fontshape"] =
767                 ListingsParam("", false, ALL, "", _(
768                                 "A latex shape such as n, it, sl, sc"));
769         all_params_[1]["formatcom"] =
770                 ListingsParam("", false, ALL, "", empty_hint);
771         all_params_[1]["frame"] =
772                 ListingsParam("", false, ONEOF,
773                   "none\nleftline\ntopline\nbottomline\nlines\nsingle",
774                   frame_hint_mint);
775         all_params_[1]["framerule"] =
776                 ListingsParam("", false, LENGTH, "", empty_hint);
777         all_params_[1]["framesep"] =
778                 ListingsParam("", false, LENGTH, "", empty_hint);
779         all_params_[1]["funcnamehighlighting"] =
780                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
781         all_params_[1]["gobble"] =
782                 ListingsParam("", false, INTEGER, "", empty_hint);
783         all_params_[1]["highlightcolor"] =
784                 ListingsParam("", false, ALL, "", color_hint_mint);
785         all_params_[1]["highlightlines"] =
786                 ListingsParam("", false, ALL, "", _(
787                                 "A range of lines such as {1,3-4}"));
788         all_params_[1]["keywordcase"] =
789                 ListingsParam("", false, ONEOF,
790                                 "lower\nupper\ncapitalize", empty_hint);
791         all_params_[1]["labelposition"] =
792                 ListingsParam("", false, ONEOF,
793                         "none\ntopline\nbottomline\nall", empty_hint);
794         all_params_[1]["language"] =
795                 ListingsParam("", false, ONEOF,
796                                 allowed_languages, empty_hint);
797         all_params_[1]["language"] =
798                 ListingsParam("", false, ALL, "", _(
799                 "This parameter should not be entered here. Please "
800                 "use the language combo box in the listings inset "
801                 "settings dialog, unless you need to enter a language "
802                 "not offered there."));
803         all_params_[1]["lastline"] =
804                 ListingsParam("", false, INTEGER, "", empty_hint);
805         all_params_[1]["linenos"] =
806                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
807         all_params_[1]["numberfirstline"] =
808                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
809         all_params_[1]["numbers"] =
810                 ListingsParam("", false, ONEOF,
811                                 "left\nright\nboth\nnone", empty_hint);
812         all_params_[1]["mathescape"] =
813                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
814         all_params_[1]["numberblanklines"] =
815                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
816         all_params_[1]["numbersep"] =
817                 ListingsParam("", false, LENGTH, "", empty_hint);
818         all_params_[1]["obeytabs"] =
819                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
820         all_params_[1]["outencoding"] =
821                 ListingsParam("", false, ALL, "", _(
822                   "File encoding used by Pygments for highlighting"));
823         all_params_[1]["python3"] =
824                 ListingsParam("", true, TRUEFALSE, "", _(
825                                         "Apply Python 3 highlighting"));
826         all_params_[1]["resetsmargins"] =
827                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
828         all_params_[1]["rulecolor"] =
829                 ListingsParam("", false, ALL, "", color_hint_mint);
830         all_params_[1]["samepage"] =
831                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
832         all_params_[1]["showspaces"] =
833                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
834         all_params_[1]["showtabs"] =
835                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
836         all_params_[1]["space"] =
837                 ListingsParam("", false, ALL, "", _(
838                                 "A macro. Default: \textvisiblespace"));
839         all_params_[1]["spacecolor"] =
840                 ListingsParam("", false, ALL, "", color_hint_mint);
841         all_params_[1]["startinline"] =
842                 ListingsParam("", true, TRUEFALSE, "", _("For PHP only"));
843         all_params_[1]["style"] =
844                 ListingsParam("", false, ALL, "", _(
845                                         "The style used by Pygments"));
846         all_params_[1]["stepnumber"] =
847                 ListingsParam("", false, INTEGER, "", empty_hint);
848         all_params_[1]["stepnumberfromfirst"] =
849                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
850         all_params_[1]["stepnumberoffsetvalues"] =
851                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
852         all_params_[1]["stripall"] =
853                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
854         all_params_[1]["stripnl"] =
855                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
856         all_params_[1]["tab"] =
857                 ListingsParam("", false, ALL, "", _(
858                                 "A macro to redefine visible tabs"));
859         all_params_[1]["tabcolor"] =
860                 ListingsParam("", false, ALL, "", color_hint_mint);
861         all_params_[1]["tabsize"] =
862                 ListingsParam("", false, INTEGER, "", empty_hint);
863         all_params_[1]["texcl"] =
864                 ListingsParam("", true, TRUEFALSE, "", _(
865                                 "Enables latex code in comments"));
866         all_params_[1]["texcomments"] =
867                 ListingsParam("", true, TRUEFALSE, "", _(
868                                 "Enables latex code in comments"));
869         all_params_[1]["xleftmargin"] =
870                 ListingsParam("", false, LENGTH, "", empty_hint);
871         all_params_[1]["xrightmargin"] =
872                 ListingsParam("", false, LENGTH, "", empty_hint);
873 }
874
875
876 docstring ParValidator::validate(string const & name,
877                 string const & par) const
878 {
879         int p = InsetListingsParams::package();
880
881         if (name.empty())
882                 return _("Invalid (empty) listing parameter name.");
883
884         if (name[0] == '?') {
885                 string suffix = trim(string(name, 1));
886                 string param_names;
887                 ListingsParams::const_iterator it = all_params_[p].begin();
888                 ListingsParams::const_iterator end = all_params_[p].end();
889                 for (; it != end; ++it) {
890                         if (suffix.empty() || contains(it->first, suffix)) {
891                                 if (!param_names.empty())
892                                         param_names += ", ";
893                                 param_names += it->first;
894                         }
895                 }
896                 if (suffix.empty())
897                         return bformat(
898                                         _("Available listing parameters are %1$s"), from_ascii(param_names));
899                 else
900                         return bformat(
901                                         _("Available listings parameters containing string \"%1$s\" are %2$s"), 
902                                                 from_utf8(suffix), from_utf8(param_names));
903         }
904  
905         // locate name in parameter table
906         ListingsParams::const_iterator it = all_params_[p].find(name);
907         if (it != all_params_[p].end()) {
908                 docstring msg = it->second.validate(par);
909                 if (msg.empty())
910                         return msg;
911                 else
912                         return bformat(_("Parameter %1$s: "), from_utf8(name)) + msg;
913         } else {
914                 // otherwise, produce a meaningful error message.
915                 string matching_names;
916                 ListingsParams::const_iterator end = all_params_[p].end();
917                 for (it = all_params_[p].begin(); it != end; ++it) {
918                         if (prefixIs(it->first, name)) {
919                                 if (!matching_names.empty())
920                                         matching_names += ", ";
921                                 matching_names += it->first;
922                         }
923                 }
924                 if (matching_names.empty())
925                         return bformat(_("Unknown listing parameter name: %1$s"),
926                                                                 from_utf8(name));
927                 else
928                         return bformat(_("Parameters starting with '%1$s': %2$s"),
929                                                                 from_utf8(name), from_utf8(matching_names));
930         }
931 }
932
933
934 bool ParValidator::onoff(string const & name) const
935 {
936         int p = InsetListingsParams::package();
937
938         // locate name in parameter table
939         ListingsParams::const_iterator it = all_params_[p].find(name);
940         if (it != all_params_[p].end())
941                 return it->second.onoff_;
942         else
943                 return false;
944 }
945
946 } // namespace anon.
947
948 // define a global ParValidator
949 ParValidator * par_validator = 0;
950
951 // The package to be used by the global ParValidator
952 // (0 for listings, 1 for minted)
953 int InsetListingsParams::package_ = 0;
954
955 InsetListingsParams::InsetListingsParams()
956         : inline_(false), params_(), status_(InsetCollapsable::Open)
957 {
958 }
959
960
961 InsetListingsParams::InsetListingsParams(string const & par, bool in,
962                 InsetCollapsable::CollapseStatus s)
963         : inline_(in), params_(), status_(s)
964 {
965         // this will activate parameter validation.
966         fromEncodedString(par);
967 }
968
969
970 void InsetListingsParams::write(ostream & os) const
971 {
972         if (inline_)
973                 os << "true ";
974         else
975                 os << "false ";
976         os << status_ << " \""  << encodedString() << "\"";
977 }
978
979
980 void InsetListingsParams::read(Lexer & lex)
981 {
982         lex >> inline_;
983         int s = InsetCollapsable::Collapsed;
984         lex >> s;
985         status_ = static_cast<InsetCollapsable::CollapseStatus>(s);
986         string par;
987         lex >> par;
988         fromEncodedString(par);
989 }
990
991
992 string InsetListingsParams::params(string const & sep) const
993 {
994         string par;
995         keyValuePair::const_iterator it = params_.begin();
996         for (; it != params_.end(); ++it) {
997                 if (!par.empty())
998                         par += sep;
999                 // key=value,key=value1 is stored in params_ as key=value,key_=value1.
1000                 if (it->second.empty())
1001                         par += rtrim(it->first, "_");
1002                 else
1003                         par += rtrim(it->first, "_") + '=' + it->second;
1004         }
1005         return par;
1006 }
1007
1008
1009 bool InsetListingsParams::hasParam(string const & key) const
1010 {
1011         keyValuePair::const_iterator it = params_.begin();
1012         for (; it != params_.end(); ++it) {
1013                 if (it->first == key)
1014                         return true;
1015         }
1016         return false;
1017 }
1018
1019
1020 string InsetListingsParams::getValue(string const & key) const
1021 {
1022         keyValuePair::const_iterator it = params_.begin();
1023         for (; it != params_.end(); ++it) {
1024                 if (it->first == key)
1025                         return it->second;
1026         }
1027         return string();
1028 }
1029
1030
1031 void InsetListingsParams::addParam(string const & key, 
1032                 string const & value, bool replace)
1033 {
1034         if (key.empty())
1035                 return;
1036
1037         // duplicate parameters!
1038         string keyname = key;
1039         if (!replace && hasParam(key))
1040                 // key=value,key=value1 is allowed in listings
1041                 // use key_, key__, key___ etc to avoid name conflict
1042                 while (hasParam(keyname += '_')) { }
1043         // check onoff flag
1044         // onoff parameter with value false
1045         if (!par_validator)
1046                 par_validator = new ParValidator;
1047         if (par_validator->onoff(key) && (value == "false" || value == "{false}"))
1048                 params_.push_back(make_pair(keyname, string()));
1049         // if the parameter is surrounded with {}, good
1050         else if (prefixIs(value, "{") && suffixIs(value, "}"))
1051                 params_.push_back(make_pair(keyname, value));
1052         // otherwise, check if {} is needed. Add {} to all values with
1053         // non-ascii/number characters, just to be safe
1054         else {
1055                 bool has_special_char = false;
1056                 for (size_t i = 0; i < value.size(); ++i)
1057                         if (!isAlnumASCII(value[i])) {
1058                                 has_special_char = true;
1059                                 break;
1060                         }
1061                 if (has_special_char)
1062                         params_.push_back(make_pair(keyname, "{" + value + "}"));
1063                 else
1064                         params_.push_back(make_pair(keyname, value));
1065         }
1066 }
1067
1068
1069 void InsetListingsParams::addParams(string const & par)
1070 {
1071         string key;
1072         string value;
1073         bool isValue = false;
1074         int braces = 0;
1075         for (size_t i = 0; i < par.size(); ++i) {
1076                 // end of par
1077                 if (par[i] == '\n') {
1078                         addParam(trim(key), trim(value));
1079                         key = string();
1080                         value = string();
1081                         isValue = false;
1082                         continue;
1083                 } else if (par[i] == ',' && braces == 0) {
1084                         addParam(trim(key), trim(value));
1085                         key = string();
1086                         value = string();
1087                         isValue = false;
1088                         continue;
1089                 } else if (par[i] == '=' && braces == 0) {
1090                         isValue = true;
1091                         continue;
1092                 } else if (par[i] == '{' && i > 0 && par[i-1] != '\\')
1093                         // don't count a brace in first position
1094                         ++braces;
1095                 else if (par[i] == '}' && i != par.size() - 1 
1096                          && (i == 0 || (i > 0 && par[i-1] != '\\')))
1097                         --braces;
1098
1099                 if (isValue)
1100                         value += par[i];
1101                 else
1102                         key += par[i];
1103         }
1104         if (!trim(key).empty())
1105                 addParam(trim(key), trim(value));
1106 }
1107
1108
1109 void InsetListingsParams::setParams(string const & par)
1110 {
1111         params_.clear();
1112         addParams(par);
1113 }
1114
1115
1116 string InsetListingsParams::encodedString() const
1117 {
1118         // Encode string!
1119         // '"' is handled differently because it will
1120         // terminate a lyx token.
1121         string par = params();
1122         // '"' is now &quot;  ==> '"' is now &amp;quot;
1123         par = subst(par, "&", "&amp;");
1124         // '"' is now &amp;quot; ==> '&quot;' is now &amp;quot;
1125         par = subst(par, "\"", "&quot;");
1126         return par;
1127 }
1128
1129
1130 string InsetListingsParams::separatedParams(bool keepComma) const
1131 {
1132         if (keepComma)
1133                 return params(",\n");
1134         else
1135                 return params("\n");
1136 }
1137
1138
1139 void InsetListingsParams::fromEncodedString(string const & in)
1140 {
1141         // Decode string! Reversal of encodedString
1142         string par = in;
1143         // '&quot;' is now &amp;quot; ==> '"' is now &amp;quot;
1144         par = subst(par, "&quot;", "\"");
1145         //  '"' is now &amp;quot; ==> '"' is now &quot;
1146         par = subst(par, "&amp;", "&");
1147         setParams(par);
1148 }
1149
1150
1151 bool InsetListingsParams::isFloat() const
1152 {
1153         return hasParam("float");
1154 }
1155
1156
1157 string InsetListingsParams::getParamValue(string const & param) const
1158 {
1159         string par = getValue(param);
1160         if (prefixIs(par, "{") && suffixIs(par, "}"))
1161                 return par.substr(1, par.size() - 2);
1162         else
1163                 return par;
1164 }
1165
1166
1167 docstring InsetListingsParams::validate() const
1168 {
1169         docstring msg;
1170         if (!par_validator)
1171                 par_validator = new ParValidator;
1172         // return msg for first key=value pair which is incomplete or has an error
1173         keyValuePair::const_iterator it = params_.begin();
1174         for (; it != params_.end(); ++it) {
1175                 // key trimmed
1176                 msg = par_validator->validate(rtrim(it->first, "_"), it->second);
1177                 if (!msg.empty())
1178                         return msg;
1179         }
1180         return msg;
1181 }
1182
1183 } // namespace lyx