]> git.lyx.org Git - lyx.git/blob - src/insets/InsetListingsParams.cpp
- Simplify prefs, graphics and external display options which are now true or false.
[lyx.git] / src / insets / InsetListingsParams.cpp
1 /**
2  * \file InsetListingsParams.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12 #include <algorithm>
13
14 #include "InsetListingsParams.h"
15
16 #include "support/gettext.h"
17 #include "Length.h"
18 #include "Lexer.h"
19
20 #include "support/lstrings.h"
21 #include "support/textutils.h"
22 #include "support/convert.h"
23
24 #include "support/lassert.h"
25
26 #include <sstream>
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32
33 namespace {
34
35 enum param_type {
36         ALL,  // accept all
37         TRUEFALSE, // accept 'true' or 'false'
38         INTEGER, // accept an integer
39         LENGTH,  // accept a latex length
40         ONEOF,  // accept one of a few values
41         SUBSETOF, // accept a string composed of given characters
42 };
43
44
45 /// Listings package parameter information.
46 // FIXME: make this class visible outside of this file so that
47 // FIXME: it can be used directly in the frontend and in the LyX format
48 // FIXME: parsing.
49 class ListingsParam {
50 public:
51         /// Default ctor for STL containers.
52         ListingsParam(): onoff_(false), type_(ALL)
53         {}
54         /// Main ctor.
55         ListingsParam(string const & v, bool o, param_type t,
56                 string const & i, docstring const & h)
57                 : value_(v), onoff_(o), type_(t), info_(i), hint_(h)
58         {}
59         /// Validate a paramater.
60         /// \retval an empty string if \c par is valid.
61         /// \retval otherwise an explanation WRT to \c par invalidity.
62         docstring validate(string const & par) const;
63 private:
64         /// default value
65         string value_;
66 public:
67         /// for option with value "true", "false".
68         /// if onoff is true,
69         ///   "true":  option
70         ///   "false":
71         ///   "other": option="other"
72         /// onoff is false,
73         ///   "true":  option=true
74         ///   "false": option=false
75         // FIXME: this is public because of InsetListingParam::addParam()
76         bool onoff_;
77 private:
78         /// validator type.
79         /// ALL:
80         /// TRUEFALSE:
81         /// INTEGER:
82         /// LENGTH:
83         ///     info is ignored.
84         /// ONEOF
85         ///     info is a \n separated string with allowed values
86         /// SUBSETOF
87         ///     info is a string from which par is composed of
88         ///     (e.g. floatplacement can be one or more of *tbph)
89         param_type type_;
90         /// information which meaning depends on parameter type.
91         /// \sa type_
92         string info_;
93         /// a help message that is displayed in the gui.
94         docstring hint_;
95 };
96
97
98 docstring ListingsParam::validate(string const & par) const
99 {
100         bool unclosed = false;
101         string par2 = par;
102         // braces are allowed
103         if (prefixIs(par, "{") && suffixIs(par, "}"))
104                 par2 = par.substr(1, par.size() - 2);
105         else if (prefixIs(par, "{")) {
106                 par2 = par.substr(1);
107                 unclosed = true;
108         }
109
110         switch (type_) {
111
112         case ALL:
113                 if (par2.empty() && !onoff_) {
114                         if (!hint_.empty())
115                                 return hint_;
116                         else
117                                 return _("A value is expected.");
118                 }
119                 if (unclosed)
120                                 return _("Unbalanced braces!");
121                 return docstring();
122
123         case TRUEFALSE:
124                 if (par2.empty() && !onoff_) {
125                         if (!hint_.empty())
126                                 return hint_;
127                         else
128                                 return _("Please specify true or false.");
129                 }
130                 if (par2 != "true" && par2 != "false")
131                         return _("Only true or false is allowed.");
132                 if (unclosed)
133                         return _("Unbalanced braces!");
134                 return docstring();
135
136         case INTEGER:
137                 if (!isStrInt(par2)) {
138                         if (!hint_.empty())
139                                 return hint_;
140                         else
141                                 return _("Please specify an integer value.");
142                 }
143                 if (convert<int>(par2) == 0 && par2[0] != '0')
144                         return _("An integer is expected.");
145                 if (unclosed)
146                         return _("Unbalanced braces!");
147                 return docstring();
148
149         case LENGTH:
150                 if (par2.empty() && !onoff_) {
151                         if (!hint_.empty())
152                                 return hint_;
153                         else
154                                 return _("Please specify a LaTeX length expression.");
155                 }
156                 if (!isValidLength(par2))
157                         return _("Invalid LaTeX length expression.");
158                 if (unclosed)
159                         return _("Unbalanced braces!");
160                 return docstring();
161
162         case ONEOF: {
163                 if (par2.empty() && !onoff_) {
164                         if (!hint_.empty())
165                                 return hint_;
166                         else
167                                 return bformat(_("Please specify one of %1$s."),
168                                                            from_utf8(info_));
169                 }
170                 // break value to allowed strings
171                 vector<string> lists;
172                 string v;
173                 for (size_t i = 0; i != info_.size(); ++i) {
174                         if (info_[i] == '\n') {
175                                 lists.push_back(v);
176                                 v = string();
177                         } else
178                                 v += info_[i];
179                 }
180                 if (!v.empty())
181                         lists.push_back(v);
182
183                 // good, find the string
184                 if (find(lists.begin(), lists.end(), par2) != lists.end()) {
185                         if (unclosed)
186                                 return _("Unbalanced braces!");
187                         return docstring();
188                 }
189                 // otherwise, produce a meaningful error message.
190                 string matching_names;
191                 for (vector<string>::iterator it = lists.begin();
192                         it != lists.end(); ++it) {
193                         if (it->size() >= par2.size() && it->substr(0, par2.size()) == par2) {
194                                 if (matching_names.empty())
195                                         matching_names += *it;
196                                 else
197                                         matching_names += ", " + *it;
198                         }
199                 }
200                 if (matching_names.empty())
201                         return bformat(_("Try one of %1$s."), from_utf8(info_));
202                 else
203                         return bformat(_("I guess you mean %1$s."), from_utf8(matching_names));
204                 return docstring();
205         }
206         case SUBSETOF:
207                 if (par2.empty() && !onoff_) {
208                         if (!hint_.empty())
209                                 return hint_;
210                         else
211                                 return bformat(_("Please specify one or more of '%1$s'."),
212                                                            from_utf8(info_));
213                 }
214                 for (size_t i = 0; i < par2.size(); ++i)
215                         if (info_.find(par2[i], 0) == string::npos)
216                                 return bformat(_("Should be composed of one or more of %1$s."),
217                                                 from_utf8(info_));
218                 if (unclosed)
219                         return _("Unbalanced braces!");
220                 return docstring();
221         }
222         return docstring();
223 }
224
225
226 /// languages and language/dialect combinations
227 char const * allowed_languages =
228         "no language\nABAP\n[R/2 4.3]ABAP\n[R/2 5.0]ABAP\n[R/3 3.1]ABAP\n"
229         "[R/3 4.6C]ABAP\n[R/3 6.10]ABAP\nACSL\nAda\n[2005]Ada\n[83]Ada\n"
230         "[95]Ada\nALGOL\n[60]ALGOL\n[68]ALGOL\nAssembler\n"
231         "[Motorola68k]Assembler\n[x86masm]Assembler\nAwk\n[gnu]Awk\n[POSIX]Awk\n"
232         "bash\nBasic\n[Visual]Basic\nC\n[ANSI]C\n[Handel]C\n[Objective]C\n"
233         "[Sharp]C\nC++\n[ANSI]C++\n[GNU]C++\n[ISO]C++\n[Visual]C++\nCaml\n"
234         "[light]Caml\n[Objective]Caml\nClean\nCobol\n[1974]Cobol\n[1985]Cobol\n"
235         "[ibm]Cobol\nComal 80\ncommand.com\n[WinXP]command.com\nComsol\ncsh\n"
236         "Delphi\nEiffel\nElan\nerlang\nEuphoria\nFortran\n[77]Fortran\n[90]Fortran\n"
237         "[95]Fortran\nGCL\nGnuplot\nHaskell\nHTML\nIDL\n[CORBA]IDL\ninform\n"
238         "Java\n[AspectJ]Java\nJVMIS\nksh\nLingo\nLisp\n[Auto]Lisp\nLogo\n"
239         "make\n[gnu]make\nMathematica\n[1.0]Mathematica\n[3.0]Mathematica\n"
240         "[5.2]Mathematica\nMatlab\nMercury\nMetaPost\nMiranda\nMizar\nML\n"
241         "Modula-2\nMuPAD\nNASTRAN\nOberon-2\nOCL\n[decorative]OCL\n[OMG]OCL\n"
242         "Octave\nOz\nPascal\n[Borland6]Pascal\n[Standard]Pascal\n[XSC]Pascal\n"
243         "Perl\nPHP\nPL/I\nPlasm\nPostScript\nPOV\nProlog\nPromela\nPSTricks\n"
244         "Python\nR\nReduce\nRexx\nRSL\nRuby\nS\n[PLUS]S\nSAS\nScilab\nsh\n"
245         "SHELXL\nSimula\n[67]Simula\n[CII]Simula\n[DEC]Simula\n[IBM]Simula\n"
246         "SPARQL\nSQL\ntcl\n[tk]tcl\nTeX\n[AlLaTeX]TeX\n[common]TeX\n[LaTeX]TeX\n"
247         "[plain]TeX\n[primitive]TeX\nVBScript\nVerilog\nVHDL\n[AMS]VHDL\nVRML\n"
248         "[97]VRML\nXML\nXSLT";
249
250
251 /// ListingsParam Validator.
252 /// This class is aimed to be a singleton which is instantiated in
253 /// \c InsetListingsParams::addParam().
254 // FIXME: transfer this validator to the frontend.
255 // FIXME: avoid the use of exception.
256 class ParValidator
257 {
258 public:
259         ParValidator();
260
261         /// validate a parameter for a given name.
262         /// return an error message if \c par is an invalid parameter.
263         docstring validate(string const & name, string const & par) const;
264
265         /// return the onoff status of a parameter \c key, if \c key is not found
266         /// return false
267         bool onoff(string const & key) const;
268
269 private:
270         /// key is the name of the parameter
271         typedef map<string, ListingsParam> ListingsParams;
272         ListingsParams all_params_;
273 };
274
275
276 ParValidator::ParValidator()
277 {
278         docstring const empty_hint;
279         docstring const style_hint = _("Use \\footnotesize, \\small, \\itshape, "
280                 "\\ttfamily or something like that");
281         docstring const frame_hint = _("none, leftline, topline, bottomline, lines, "
282                 "single, shadowbox or subset of trblTRBL");
283         docstring const frameround_hint = _("Enter four letters (either t = round "
284                 "or f = square) for top right, bottom "
285                 "right, bottom left and top left corner.");
286         docstring const color_hint = _("Enter something like \\color{white}");
287
288         /// options copied from page 26 of listings manual
289         // FIXME: add default parameters ... (which is not used now)
290         all_params_["float"] =
291                 ListingsParam("false", true, SUBSETOF, "*tbph", empty_hint);
292         all_params_["floatplacement"] =
293                 ListingsParam("tbp", false, SUBSETOF, "tbp", empty_hint);
294         all_params_["aboveskip"] =
295                 ListingsParam("\\medskipamount", false, LENGTH, "", empty_hint);
296         all_params_["belowskip"] =
297                 ListingsParam("\\medskipamount", false, LENGTH, "", empty_hint);
298         all_params_["lineskip"] =
299                 ListingsParam("", false, LENGTH, "", empty_hint);
300         all_params_["boxpos"] =
301                 ListingsParam("", false, SUBSETOF, "bct", empty_hint);
302         all_params_["print"] =
303                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
304         all_params_["firstline"] =
305                 ListingsParam("", false, INTEGER, "", empty_hint);
306         all_params_["lastline"] =
307                 ListingsParam("", false, INTEGER, "", empty_hint);
308         all_params_["linerange"] =
309                 ListingsParam("", false, ALL, "", empty_hint);
310         all_params_["showlines"] =
311                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
312         all_params_["emptylines"] =
313                 ListingsParam("", false, ALL, "", _(
314                 "Expect a number with an optional * before it"));
315         all_params_["gobble"] =
316                 ListingsParam("", false, INTEGER, "", empty_hint);
317         all_params_["style"] =
318                 ListingsParam("", false, ALL, "", empty_hint);
319         all_params_["language"] =
320                 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
321         all_params_["alsolanguage"] =
322                 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
323         all_params_["defaultdialect"] =
324                 ListingsParam("", false, ONEOF, allowed_languages, empty_hint);
325         all_params_["printpod"] =
326                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
327         all_params_["usekeywordsintag"] =
328                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
329         all_params_["tagstyle"] =
330                 ListingsParam("", false, ALL, "", style_hint);
331         all_params_["markfirstintag"] =
332                 ListingsParam("", false, ALL, "", style_hint);
333         all_params_["makemacrouse"] =
334                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
335         all_params_["basicstyle"] =
336                 ListingsParam("", false, ALL, "", style_hint);
337         all_params_["identifierstyle"] =
338                 ListingsParam("", false, ALL, "", style_hint);
339         all_params_["commentstyle"] =
340                 ListingsParam("", false, ALL, "", style_hint);
341         all_params_["stringstyle"] =
342                 ListingsParam("", false, ALL, "", style_hint);
343         all_params_["keywordstyle"] =
344                 ListingsParam("", false, ALL, "", style_hint);
345         all_params_["ndkeywordstyle"] =
346                 ListingsParam("", false, ALL, "", style_hint);
347         all_params_["classoffset"] =
348                 ListingsParam("", false, INTEGER, "", empty_hint);
349         all_params_["texcsstyle"] =
350                 ListingsParam("", false, ALL, "", style_hint);
351         all_params_["directivestyle"] =
352                 ListingsParam("", false, ALL, "", style_hint);
353         all_params_["emph"] =
354                 ListingsParam("", false, ALL, "", empty_hint);
355         all_params_["moreemph"] =
356                 ListingsParam("", false, ALL, "", empty_hint);
357         all_params_["deleteemph"] =
358                 ListingsParam("", false, ALL, "", empty_hint);
359         all_params_["emphstyle"] =
360                 ListingsParam("", false, ALL, "", empty_hint);
361         all_params_["delim"] =
362                 ListingsParam("", false, ALL, "", empty_hint);
363         all_params_["moredelim"] =
364                 ListingsParam("", false, ALL, "", empty_hint);
365         all_params_["deletedelim"] =
366                 ListingsParam("", false, ALL, "", empty_hint);
367         all_params_["extendedchars"] =
368                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
369         all_params_["inputencoding"] =
370                 ListingsParam("", false, ALL, "", empty_hint);
371         all_params_["upquote"] =
372                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
373         all_params_["tabsize"] =
374                 ListingsParam("", false, INTEGER, "", empty_hint);
375         all_params_["showtabs"] =
376                 ListingsParam("", false, ALL, "", empty_hint);
377         all_params_["tab"] =
378                 ListingsParam("", false, ALL, "", empty_hint);
379         all_params_["showspaces"] =
380                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
381         all_params_["showstringspaces"] =
382                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
383         all_params_["formfeed"] =
384                 ListingsParam("", false, ALL, "", empty_hint);
385         all_params_["numbers"] =
386                 ListingsParam("", false, ONEOF, "none\nleft\nright", empty_hint);
387         all_params_["stepnumber"] =
388                 ListingsParam("", false, INTEGER, "", empty_hint);
389         all_params_["numberfirstline"] =
390                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
391         all_params_["numberstyle"] =
392                 ListingsParam("", false, ALL, "", style_hint);
393         all_params_["numbersep"] =
394                 ListingsParam("", false, LENGTH, "", empty_hint);
395         all_params_["numberblanklines"] =
396                 ListingsParam("", false, ALL, "", empty_hint);
397         all_params_["firstnumber"] =
398                 ListingsParam("", false, ALL, "", _("auto, last or a number"));
399         all_params_["name"] =
400                 ListingsParam("", false, ALL, "", empty_hint);
401         all_params_["thelstnumber"] =
402                 ListingsParam("", false, ALL, "", empty_hint);
403         all_params_["title"] =
404                 ListingsParam("", false, ALL, "", empty_hint);
405         // this option is not handled in the parameter box
406         all_params_["caption"] =
407                 ListingsParam("", false, ALL, "", _(
408                 "This parameter should not be entered here. Please use the caption "
409                 "edit box (when using the child document dialog) or "
410                 "menu Insert->Caption (when defining a listing inset)"));
411         // this option is not handled in the parameter box
412         all_params_["label"] =
413                 ListingsParam("", false, ALL, "",_(
414                 "This parameter should not be entered here. Please use the label "
415                 "edit box (when using the child document dialog) or "
416                 "menu Insert->Label (when defining a listing inset)"));
417         all_params_["nolol"] =
418                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
419         all_params_["captionpos"] =
420                 ListingsParam("", false, SUBSETOF, "tb", empty_hint);
421         all_params_["abovecaptionskip"] =
422                 ListingsParam("", false, LENGTH, "", empty_hint);
423         all_params_["belowcaptionskip"] =
424                 ListingsParam("", false, LENGTH, "", empty_hint);
425         all_params_["linewidth"] =
426                 ListingsParam("", false, LENGTH, "", empty_hint);
427         all_params_["xleftmargin"] =
428                 ListingsParam("", false, LENGTH, "", empty_hint);
429         all_params_["xrightmargin"] =
430                 ListingsParam("", false, LENGTH, "", empty_hint);
431         all_params_["resetmargins"] =
432                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
433         all_params_["breaklines"] =
434                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
435         all_params_["prebreak"] =
436                 ListingsParam("", false, ALL, "", empty_hint);
437         all_params_["postbreak"] =
438                 ListingsParam("", false, ALL, "", empty_hint);
439         all_params_["breakindent"] =
440                 ListingsParam("", false, LENGTH, "", empty_hint);
441         all_params_["breakautoindent"] =
442                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
443         all_params_["frame"] =
444                 ListingsParam("", false, ALL, "", frame_hint);
445         all_params_["frameround"] =
446                 ListingsParam("", false, SUBSETOF, "tf", frameround_hint);
447         all_params_["framesep"] =
448                 ListingsParam("", false, LENGTH, "", empty_hint);
449         all_params_["rulesep"] =
450                 ListingsParam("", false, LENGTH, "", empty_hint);
451         all_params_["framerule"] =
452                 ListingsParam("", false, LENGTH, "", empty_hint);
453         all_params_["framexleftmargin"] =
454                 ListingsParam("", false, LENGTH, "", empty_hint);
455         all_params_["framexrightmargin"] =
456                 ListingsParam("", false, LENGTH, "", empty_hint);
457         all_params_["framextopmargin"] =
458                 ListingsParam("", false, LENGTH, "", empty_hint);
459         all_params_["framexbottommargin"] =
460                 ListingsParam("", false, LENGTH, "", empty_hint);
461         all_params_["backgroundcolor"] =
462                 ListingsParam("", false, ALL, "", color_hint );
463         all_params_["rulecolor"] =
464                 ListingsParam("", false, ALL, "", color_hint );
465         all_params_["fillcolor"] =
466                 ListingsParam("", false, ALL, "", color_hint );
467         all_params_["rulesepcolor"] =
468                 ListingsParam("", false, ALL, "", color_hint );
469         all_params_["frameshape"] =
470                 ListingsParam("", false, ALL, "", empty_hint);
471         all_params_["index"] =
472                 ListingsParam("", false, ALL, "", empty_hint);
473         all_params_["moreindex"] =
474                 ListingsParam("", false, ALL, "", empty_hint);
475         all_params_["deleteindex"] =
476                 ListingsParam("", false, ALL, "", empty_hint);
477         all_params_["indexstyle"] =
478                 ListingsParam("", false, ALL, "", empty_hint);
479         all_params_["columns"] =
480                 ListingsParam("", false, ALL, "", empty_hint);
481         all_params_["flexiblecolumns"] =
482                 ListingsParam("", false, ALL, "", empty_hint);
483         all_params_["keepspaces"] =
484                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
485         all_params_["basewidth"] =
486                 ListingsParam("", false, LENGTH, "", empty_hint);
487         all_params_["fontadjust"] =
488                 ListingsParam("", true, TRUEFALSE, "", empty_hint);
489         all_params_["texcl"] =
490                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
491         all_params_["mathescape"] =
492                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
493         all_params_["escapechar"] =
494                 ListingsParam("", false, ALL, "", empty_hint);
495         all_params_["escapeinside"] =
496                 ListingsParam("", false, ALL, "", empty_hint);
497         all_params_["escepeinside"] =
498                 ListingsParam("", false, ALL, "", empty_hint);
499         all_params_["escepebegin"] =
500                 ListingsParam("", false, ALL, "", empty_hint);
501         all_params_["escepeend"] =
502                 ListingsParam("", false, ALL, "", empty_hint);
503         all_params_["fancyvrb"] =
504                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
505         all_params_["fvcmdparams"] =
506                 ListingsParam("", false, ALL, "", empty_hint);
507         all_params_["morefvcmdparams"] =
508                 ListingsParam("", false, ALL, "", empty_hint);
509         all_params_["keywordsprefix"] =
510                 ListingsParam("", false, ALL, "", empty_hint);
511         all_params_["keywords"] =
512                 ListingsParam("", false, ALL, "", empty_hint);
513         all_params_["morekeywords"] =
514                 ListingsParam("", false, ALL, "", empty_hint);
515         all_params_["deletekeywords"] =
516                 ListingsParam("", false, ALL, "", empty_hint);
517         all_params_["ndkeywords"] =
518                 ListingsParam("", false, ALL, "", empty_hint);
519         all_params_["morendkeywords"] =
520                 ListingsParam("", false, ALL, "", empty_hint);
521         all_params_["deletendkeywords"] =
522                 ListingsParam("", false, ALL, "", empty_hint);
523         all_params_["texcs"] =
524                 ListingsParam("", false, ALL, "", empty_hint);
525         all_params_["moretexcs"] =
526                 ListingsParam("", false, ALL, "", empty_hint);
527         all_params_["deletetexcs"] =
528                 ListingsParam("", false, ALL, "", empty_hint);
529         all_params_["directives"] =
530                 ListingsParam("", false, ALL, "", empty_hint);
531         all_params_["moredirectives"] =
532                 ListingsParam("", false, ALL, "", empty_hint);
533         all_params_["deletedirectives"] =
534                 ListingsParam("", false, ALL, "", empty_hint);
535         all_params_["sensitive"] =
536                 ListingsParam("", false, ALL, "", empty_hint);
537         all_params_["alsoletter"] =
538                 ListingsParam("", false, ALL, "", empty_hint);
539         all_params_["alsodigit"] =
540                 ListingsParam("", false, ALL, "", empty_hint);
541         all_params_["alsoother"] =
542                 ListingsParam("", false, ALL, "", empty_hint);
543         all_params_["otherkeywords"] =
544                 ListingsParam("", false, ALL, "", empty_hint);
545         all_params_["tag"] =
546                 ListingsParam("", false, ALL, "", empty_hint);
547         all_params_["string"] =
548                 ListingsParam("", false, ALL, "", empty_hint);
549         all_params_["morestring"] =
550                 ListingsParam("", false, ALL, "", empty_hint);
551         all_params_["deletestring"] =
552                 ListingsParam("", false, ALL, "", empty_hint);
553         all_params_["comment"] =
554                 ListingsParam("", false, ALL, "", empty_hint);
555         all_params_["morecomment"] =
556                 ListingsParam("", false, ALL, "", empty_hint);
557         all_params_["deletecomment"] =
558                 ListingsParam("", false, ALL, "", empty_hint);
559         all_params_["keywordcomment"] =
560                 ListingsParam("", false, ALL, "", empty_hint);
561         all_params_["morekeywordcomment"] =
562                 ListingsParam("", false, ALL, "", empty_hint);
563         all_params_["deletekeywordcomment"] =
564                 ListingsParam("", false, ALL, "", empty_hint);
565         all_params_["keywordcommentsemicolon"] =
566                 ListingsParam("", false, ALL, "", empty_hint);
567         all_params_["podcomment"] =
568                 ListingsParam("", false, ALL, "", empty_hint);
569         // the following are experimental listings features
570         all_params_["procnamekeys"] =
571                 ListingsParam("", false, ALL, "", empty_hint);
572         all_params_["moreprocnamekeys"] =
573                 ListingsParam("", false, ALL, "", empty_hint);
574         all_params_["deleteprocnamekeys"] =
575                 ListingsParam("", false, ALL, "", empty_hint);
576         all_params_["procnamestyle"] =
577                 ListingsParam("", false, ALL, "", style_hint);
578         all_params_["indexprocnames"] =
579                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
580         all_params_["hyperref"] =
581                 ListingsParam("", false, ALL, "", empty_hint);
582         all_params_["morehyperref"] =
583                 ListingsParam("", false, ALL, "", empty_hint);
584         all_params_["deletehyperref"] =
585                 ListingsParam("", false, ALL, "", empty_hint);
586         all_params_["hyperanchor"] =
587                 ListingsParam("", false, ALL, "", empty_hint);
588         all_params_["hyperlink"] =
589                 ListingsParam("", false, ALL, "", empty_hint);
590         all_params_["literate"] =
591                 ListingsParam("", false, ALL, "", empty_hint);
592         all_params_["lgrindef"] =
593                 ListingsParam("", false, ALL, "", empty_hint);
594         all_params_["rangebeginprefix"] =
595                 ListingsParam("", false, ALL, "", empty_hint);
596         all_params_["rangebeginsuffix"] =
597                 ListingsParam("", false, ALL, "", empty_hint);
598         all_params_["rangeendprefix"] =
599                 ListingsParam("", false, ALL, "", empty_hint);
600         all_params_["rangeendsuffix"] =
601                 ListingsParam("", false, ALL, "", empty_hint);
602         all_params_["rangeprefix"] =
603                 ListingsParam("", false, ALL, "", empty_hint);
604         all_params_["rangesuffix"] =
605                 ListingsParam("", false, ALL, "", empty_hint);
606         all_params_["includerangemarker"] =
607                 ListingsParam("", false, TRUEFALSE, "", empty_hint);
608         all_params_["multicols"] =
609                 ListingsParam("", false, INTEGER, "", empty_hint);
610 }
611
612
613 docstring ParValidator::validate(string const & name,
614                 string const & par) const
615 {
616         if (name.empty())
617                 return _("Invalid (empty) listing parameter name.");
618
619         if (name[0] == '?') {
620                 string suffix = trim(string(name, 1));
621                 string param_names;
622                 ListingsParams::const_iterator it = all_params_.begin();
623                 ListingsParams::const_iterator end = all_params_.end();
624                 for (; it != end; ++it) {
625                         if (suffix.empty() || contains(it->first, suffix)) {
626                                 if (!param_names.empty())
627                                         param_names += ", ";
628                                 param_names += it->first;
629                         }
630                 }
631                 if (suffix.empty())
632                         return bformat(
633                                         _("Available listing parameters are %1$s"), from_ascii(param_names));
634                 else
635                         return bformat(
636                                         _("Available listings parameters containing string \"%1$s\" are %2$s"), 
637                                                 from_utf8(suffix), from_utf8(param_names));
638         }
639  
640         // locate name in parameter table
641         ListingsParams::const_iterator it = all_params_.find(name);
642         if (it != all_params_.end()) {
643                 docstring msg = it->second.validate(par);
644                 if (msg.empty())
645                         return msg;
646                 else
647                         return bformat(_("Parameter %1$s: "), from_utf8(name)) + msg;
648         } else {
649                 // otherwise, produce a meaningful error message.
650                 string matching_names;
651                 ListingsParams::const_iterator end = all_params_.end();
652                 for (it = all_params_.begin(); it != end; ++it) {
653                         if (prefixIs(it->first, name)) {
654                                 if (!matching_names.empty())
655                                         matching_names += ", ";
656                                 matching_names += it->first;
657                         }
658                 }
659                 if (matching_names.empty())
660                         return bformat(_("Unknown listing parameter name: %1$s"),
661                                                                 from_utf8(name));
662                 else
663                         return bformat(_("Parameters starting with '%1$s': %2$s"),
664                                                                 from_utf8(name), from_utf8(matching_names));
665         }
666         return docstring();
667 }
668
669
670 bool ParValidator::onoff(string const & name) const
671 {
672         // locate name in parameter table
673         ListingsParams::const_iterator it = all_params_.find(name);
674         if (it != all_params_.end())
675                 return it->second.onoff_;
676         else
677                 return false;
678 }
679
680 } // namespace anon.
681
682 // define a global ParValidator
683 ParValidator * par_validator = 0;
684
685 InsetListingsParams::InsetListingsParams()
686         : inline_(false), params_(), status_(InsetCollapsable::Open)
687 {
688 }
689
690
691 InsetListingsParams::InsetListingsParams(string const & par, bool in,
692                 InsetCollapsable::CollapseStatus s)
693         : inline_(in), params_(), status_(s)
694 {
695         // this will activate parameter validation.
696         fromEncodedString(par);
697 }
698
699
700 void InsetListingsParams::write(ostream & os) const
701 {
702         if (inline_)
703                 os << "true ";
704         else
705                 os << "false ";
706         os << status_ << " \""  << encodedString() << "\"";
707 }
708
709
710 void InsetListingsParams::read(Lexer & lex)
711 {
712         lex >> inline_;
713         int s = Inset::Collapsed;
714         lex >> s;
715         status_ = static_cast<InsetCollapsable::CollapseStatus>(s);
716         string par;
717         lex >> par;
718         fromEncodedString(par);
719 }
720
721
722 string InsetListingsParams::params(string const & sep) const
723 {
724         string par;
725         for (map<string, string>::const_iterator it = params_.begin();
726                 it != params_.end(); ++it) {
727                 if (!par.empty())
728                         par += sep;
729                 // key=value,key=value1 is stored in params_ as key=value,key_=value1. 
730                 if (it->second.empty())
731                         par += rtrim(it->first, "_");
732                 else
733                         par += rtrim(it->first, "_") + '=' + it->second;
734         }
735         return par;
736 }
737
738
739 void InsetListingsParams::addParam(string const & key, string const & value)
740 {
741         if (key.empty())
742                 return;
743
744         // duplicate parameters!
745         string keyname = key;
746         if (params_.find(key) != params_.end())
747                 // key=value,key=value1 is allowed in listings
748                 // use key_, key__, key___ etc to avoid name conflict
749                 while (params_.find(keyname += '_') != params_.end()) { }
750         // check onoff flag
751         // onoff parameter with value false
752         if (!par_validator)
753                 par_validator = new ParValidator();
754         if (par_validator->onoff(key) && (value == "false" || value == "{false}"))
755                 params_[keyname] = string();
756         // if the parameter is surrounded with {}, good
757         else if (prefixIs(value, "{") && suffixIs(value, "}"))
758                 params_[keyname] = value;
759         // otherwise, check if {} is needed. Add {} to all values with
760         // non-ascii/number characters, just to be safe
761         else {
762                 bool has_special_char = false;
763                 for (size_t i = 0; i < value.size(); ++i)
764                         if (!isAlphaASCII(value[i]) && !isDigit(value[i])) {
765                                 has_special_char = true;
766                                 break;
767                         }
768                 if (has_special_char)
769                         params_[keyname] = "{" + value + "}";
770                 else
771                         params_[keyname] = value;
772         }
773 }
774
775
776 void InsetListingsParams::addParams(string const & par)
777 {
778         string key;
779         string value;
780         bool isValue = false;
781         int braces = 0;
782         for (size_t i = 0; i < par.size(); ++i) {
783                 // end of par
784                 if (par[i] == '\n') {
785                         addParam(trim(key), trim(value));
786                         key = string();
787                         value = string();
788                         isValue = false;
789                         continue;
790                 } else if (par[i] == ',' && braces == 0) {
791                         addParam(trim(key), trim(value));
792                         key = string();
793                         value = string();
794                         isValue = false;
795                         continue;
796                 } else if (par[i] == '=' && braces == 0) {
797                         isValue = true;
798                         continue;
799                 } else if (par[i] == '{' && i > 0 && par[i - 1] == '=')
800                         braces ++;
801                 else if (par[i] == '}'
802                         && (i == par.size() - 1 || par[i + 1] == ',' || par[i + 1] == '\n'))
803                         braces --;
804
805                 if (isValue)
806                         value += par[i];
807                 else
808                         key += par[i];
809         }
810         if (!trim(key).empty())
811                 addParam(trim(key), trim(value));
812 }
813
814
815 void InsetListingsParams::setParams(string const & par)
816 {
817         params_.clear();
818         addParams(par);
819 }
820
821
822 string InsetListingsParams::encodedString() const
823 {
824         // Encode string!
825         // '"' is handled differently because it will
826         // terminate a lyx token.
827         string par = params();
828         // '"' is now &quot;  ==> '"' is now &amp;quot;
829         par = subst(par, "&", "&amp;");
830         // '"' is now &amp;quot; ==> '&quot;' is now &amp;quot;
831         par = subst(par, "\"", "&quot;");
832         return par;
833 }
834
835
836 string InsetListingsParams::separatedParams(bool keepComma) const
837 {
838         if (keepComma)
839                 return params(",\n");
840         else
841                 return params("\n");
842 }
843
844
845 void InsetListingsParams::fromEncodedString(string const & in)
846 {
847         // Decode string! Reversal of encodedString
848         string par = in;
849         // '&quot;' is now &amp;quot; ==> '"' is now &amp;quot;
850         par = subst(par, "&quot;", "\"");
851         //  '"' is now &amp;quot; ==> '"' is now &quot;
852         par = subst(par, "&amp;", "&");
853         setParams(par);
854 }
855
856
857 bool InsetListingsParams::isFloat() const
858 {
859         return params_.find("float") != params_.end();
860 }
861
862
863 string InsetListingsParams::getParamValue(string const & param) const
864 {
865         // is this parameter defined?
866         map<string, string>::const_iterator it = params_.find(param);
867         string par = (it == params_.end()) ? string() : it->second;
868         if (prefixIs(par, "{") && suffixIs(par, "}"))
869                 return par.substr(1, par.size() - 2);
870         else
871                 return par;
872 }
873
874
875 docstring InsetListingsParams::validate() const
876 {
877         docstring msg;
878         if (!par_validator)
879                 par_validator = new ParValidator();
880         for (map<string, string>::const_iterator it = params_.begin();
881                 it != params_.end(); ++it) {
882                 msg = par_validator->validate(it->first, it->second);
883                 if (!msg.empty())
884                         return msg;
885         }
886         return msg;
887 }
888
889 } // namespace lyx