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