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