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