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