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