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