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