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