]> git.lyx.org Git - features.git/blob - src/insets/InsetListingsParams.cpp
- Convert char * to string and docstring for the hints, thus fixing the translation.
[features.git] / src / insets / InsetListingsParams.cpp
1 /**
2  * \file InsetListingsParams.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetListingsParams.h"
14
15 #include "gettext.h"
16 #include "Length.h"
17 #include "Lexer.h"
18
19 #include "support/lstrings.h"
20 #include "support/textutils.h"
21 #include "support/convert.h"
22
23 #include <boost/assert.hpp>
24
25 #include <sstream>
26
27 using std::map;
28 using std::vector;
29 using std::ostream;
30 using std::string;
31 using std::exception;
32
33 namespace lyx
34 {
35
36 using support::bformat;
37 using support::trim;
38 using support::subst;
39 using support::isStrInt;
40 using support::prefixIs;
41 using support::suffixIs;
42 using support::getVectorFromString;
43
44 namespace {
45
46 enum param_type {
47         ALL,  // accept all
48         TRUEFALSE, // accept 'true' or 'false'
49         INTEGER, // accept an integer
50         LENGTH,  // accept a latex length
51         ONEOF,  // accept one of a few values
52         SUBSETOF, // accept a string composed of given characters
53 };
54
55
56 /** Information about each parameter
57  */
58 struct listings_param_info {
59         /// name of the parameter
60         string name;
61         /// default value
62         string value;
63         // for option with value "true", "false",
64         // if onoff is true,
65         //   "true":  option
66         //   "false": 
67         //   "other": option="other"
68         // onoff is false,
69         //   "true":  option=true
70         //   "false": option=false
71         bool onoff;
72         /// validator type
73         // ALL:
74         // TRUEFALSE:
75         // INTEGER:
76         // LENGTH:
77         //     info is ignored.
78         // ONEOF
79         //     info is a \n separated string with allowed values
80         // SUBSETOF
81         //     info is a string from which par is composed of
82         //     (e.g. floatplacement can be one or more of *tbph)
83         param_type type;
84         /// parameter info, meaning depending on parameter type
85         string info;
86         /// a help message that is displayed in the gui
87         docstring hint;
88 };
89
90
91 /// languages and language/dialect combinations
92 char const * allowed_languages = 
93         "no language\nABAP\n[R/2 4.3]ABAP\n[R/2 5.0]ABAP\n[R/3 3.1]ABAP\n"
94         "[R/3 4.6C]ABAP\n[R/3 6.10]ABAP\nACSL\nAda\n[2005]Ada\n[83]Ada\n"
95         "[95]Ada\nALGOL\n[60]ALGOL\n[68]ALGOL\nAssembler\n"
96         "[Motorola68k]Assembler\n[x86masm]Assembler\nAwk\n[gnu]Awk\n[POSIX]Awk\n"
97         "bash\nBasic\n[Visual]Basic\nC\n[ANSI]C\n[Handel]C\n[Objective]C\n"
98         "[Sharp]C\nC++\n[ANSI]C++\n[GNU]C++\n[ISO]C++\n[Visual]C++\nCaml\n"
99         "[light]Caml\n[Objective]Caml\nClean\nCobol\n[1974]Cobol\n[1985]Cobol\n"
100         "[ibm]Cobol\nComal 80\ncommand.com\n[WinXP]command.com\nComsol\ncsh\n"
101         "Delphi\nEiffel\nElan\nEuphoria\nFortran\n[77]Fortran\n[90]Fortran\n"
102         "[95]Fortran\nGCL\nGnuplot\nHaskell\nHTML\nIDL\n[CORBA]IDL\ninform\n"
103         "Java\n[AspectJ]Java\nJVMIS\nksh\nLingo\nLisp\n[Auto]Lisp\nLogo\n"
104         "make\n[gnu]make\nMathematica\n[1.0]Mathematica\n[3.0]Mathematica\n"
105         "[5.2]Mathematica\nMatlab\nMercury\nMetaPost\nMiranda\nMizar\nML\n"
106         "Modula-2\nMuPAD\nNASTRAN\nOberon-2\nOCL\n[decorative]OCL\n[OMG]OCL\n"
107         "Octave\nOz\nPascal\n[Borland6]Pascal\n[Standard]Pascal\n[XSC]Pascal\n"
108         "Perl\nPHP\nPL/I\nPlasm\nPostScript\nPOV\nProlog\nPromela\nPSTricks\n"
109         "Python\nR\nReduce\nRexx\nRSL\nRuby\nS\n[PLUS]S\nSAS\nScilab\nsh\n"
110         "SHELXL\nSimula\n[67]Simula\n[CII]Simula\n[DEC]Simula\n[IBM]Simula\n"
111         "SPARQL\nSQL\ntcl\n[tk]tcl\nTeX\n[AlLaTeX]TeX\n[common]TeX\n[LaTeX]TeX\n"
112         "[plain]TeX\n[primitive]TeX\nVBScript\nVerilog\nVHDL\n[AMS]VHDL\nVRML\n"
113         "[97]VRML\nXML\nXSLT";
114
115 docstring empty_hint;
116 docstring style_hint = _("Use \\footnotesize, \\small, \\itshape, \\ttfamily or something like that");
117 docstring frame_hint = _("none, leftline, topline, bottomline, lines, single, shadowbox or subset of trblTRBL");
118 docstring frameround_hint =     _("Enter four letters (either t = round or f = square) for top right, bottom right, bottom left and top left corner.");
119 docstring color_hint = _("Enter something like \\color{white}");
120
121
122 /// options copied from page 26 of listings manual
123 // FIXME: add default parameters ... (which is not used now)
124 listings_param_info const listings_param_table[] = {
125         { "float", "false", true,  SUBSETOF, "*tbph", empty_hint},
126         { "floatplacement", "tbp", false, SUBSETOF, "tbp", empty_hint},
127         { "aboveskip", "\\medskipamount", false, LENGTH, "", empty_hint},
128         { "belowskip", "\\medskipamount", false, LENGTH, "", empty_hint},
129         { "lineskip", "", false, LENGTH, "", empty_hint},
130         { "boxpos", "", false, SUBSETOF, "bct", empty_hint},
131         { "print", "", false, TRUEFALSE, "", empty_hint},
132         { "firstline", "", false, INTEGER, "", empty_hint},
133         { "lastline", "", false, INTEGER, "", empty_hint},
134         { "showlines", "", false, TRUEFALSE, "", empty_hint},
135         { "emptylines", "", false, ALL, "", from_ascii("Expect a number with an optional * before it") },
136         { "gobble", "", false, INTEGER, "", empty_hint},
137         { "style", "", false, ALL, "", empty_hint},
138         { "language", "", false, ONEOF, allowed_languages, empty_hint},
139         { "alsolanguage", "", false, ONEOF, allowed_languages, empty_hint},
140         { "defaultdialect", "", false, ONEOF, allowed_languages, empty_hint},
141         { "printpod", "", false, TRUEFALSE, "", empty_hint},
142         { "usekeywordsintag", "", false, TRUEFALSE, "", empty_hint},
143         { "tagstyle", "", false, ALL, "", style_hint },
144         { "markfirstintag", "", false, ALL, "", style_hint },
145         { "makemacrouse", "", false, TRUEFALSE, "", empty_hint},
146         { "basicstyle", "", false, ALL, "", style_hint },
147         { "identifierstyle", "", false, ALL, "", style_hint },
148         { "commentstyle", "", false, ALL, "", style_hint },
149         { "stringstyle", "", false, ALL, "", style_hint },
150         { "keywordstyle", "", false, ALL, "", style_hint },
151         { "ndkeywordstyle", "", false, ALL, "", style_hint },
152         { "classoffset", "", false, INTEGER, "", empty_hint},
153         { "texcsstyle", "", false, ALL, "", style_hint },
154         { "directivestyle", "", false, ALL, "", style_hint },
155         { "emph", "", false, ALL, "", empty_hint},
156         { "moreemph", "", false, ALL, "", empty_hint},
157         { "deleteemph", "", false, ALL, "", empty_hint},
158         { "emphstyle", "", false, ALL, "", empty_hint},
159         { "delim", "", false, ALL, "", empty_hint},
160         { "moredelim", "", false, ALL, "", empty_hint},
161         { "deletedelim", "", false, ALL, "", empty_hint},
162         { "extendedchars", "", false, TRUEFALSE, "", empty_hint},
163         { "inputencoding", "", false, ALL, "", empty_hint},
164         { "upquote", "", false, TRUEFALSE, "", empty_hint},
165         { "tabsize", "", false, INTEGER, "", empty_hint},
166         { "showtabs", "", false, ALL, "", empty_hint},
167         { "tab", "", false, ALL, "", empty_hint},
168         { "showspaces", "", false, TRUEFALSE, "", empty_hint},
169         { "showstringspaces", "", false, TRUEFALSE, "", empty_hint},
170         { "formfeed", "", false, ALL, "", empty_hint},
171         { "numbers", "", false, ONEOF, "none\nleft\nright", empty_hint},
172         { "stepnumber", "", false, INTEGER, "", empty_hint},
173         { "numberfirstline", "", false, TRUEFALSE, "", empty_hint},
174         { "numberstyle", "", false, ALL, "", style_hint },
175         { "numbersep", "", false, LENGTH, "", empty_hint},
176         { "numberblanklines", "", false, ALL, "", empty_hint},
177         { "firstnumber", "", false, ALL, "", _("auto, last or a number") },
178         { "name", "", false, ALL, "", empty_hint},
179         { "thelstnumber", "", false, ALL, "", empty_hint},
180         { "title", "", false, ALL, "", empty_hint},
181         // this option is not handled in the parameter box
182         { "caption", "", false, ALL, "", _("This parameter should not be entered here. "
183                 "Please use caption editbox (Include dialog) or insert->caption (listings inset)") },
184         // this option is not handled in the parameter box
185         { "label", "", false, ALL, "", _("This parameter should not be entered here. "
186                 "Please use label editbox (Include dialog) or insert->caption (listings inset)") },
187         { "nolol", "", false, TRUEFALSE, "", empty_hint},
188         { "captionpos", "", false, SUBSETOF, "tb", empty_hint},
189         { "abovecaptionskip", "", false, LENGTH, "", empty_hint},
190         { "belowcaptionskip", "", false, LENGTH, "", empty_hint},
191         { "linewidth", "", false, LENGTH, "", empty_hint},
192         { "xleftmargin", "", false, LENGTH, "", empty_hint},
193         { "xrightmargin", "", false, LENGTH, "", empty_hint},
194         { "resetmargins", "", false, TRUEFALSE, "", empty_hint},
195         { "breaklines", "", false, TRUEFALSE, "", empty_hint},
196         { "prebreak", "", false, ALL, "", empty_hint},
197         { "postbreak", "", false, ALL, "", empty_hint},
198         { "breakindent", "", false, LENGTH, "", empty_hint},
199         { "breakautoindent", "", false, TRUEFALSE, "", empty_hint},
200         { "frame", "", false, ALL, "", frame_hint },
201         { "frameround", "", false, SUBSETOF, "tf", frameround_hint },
202         { "framesep", "", false, LENGTH, "", empty_hint},
203         { "rulesep", "", false, LENGTH, "", empty_hint},
204         { "framerule", "", false, LENGTH, "", empty_hint},
205         { "framexleftmargin", "", false, LENGTH, "", empty_hint},
206         { "framexrightmargin", "", false, LENGTH, "", empty_hint},
207         { "framextopmargin", "", false, LENGTH, "", empty_hint},
208         { "framexbottommargin", "", false, LENGTH, "", empty_hint},
209         { "backgroundcolor", "", false, ALL, "", color_hint },
210         { "rulecolor", "", false, ALL, "", color_hint },
211         { "fillcolor", "", false, ALL, "", color_hint },
212         { "rulesepcolor", "", false, ALL, "", color_hint },
213         { "frameshape", "", false, ALL, "", empty_hint},
214         { "index", "", false, ALL, "", empty_hint},
215         { "moreindex", "", false, ALL, "", empty_hint},
216         { "deleteindex", "", false, ALL, "", empty_hint},
217         { "indexstyle", "", false, ALL, "", empty_hint},
218         { "columns", "", false, ALL, "", empty_hint},
219         { "flexiblecolumns", "", false, ALL, "", empty_hint},
220         { "keepspaces", "", false, TRUEFALSE, "", empty_hint},
221         { "basewidth", "", false, LENGTH, "", empty_hint},
222         { "fontadjust", "", true, TRUEFALSE, "", empty_hint},
223         { "texcl", "", false, TRUEFALSE, "", empty_hint},
224         { "mathescape", "", false, TRUEFALSE, "", empty_hint},
225         { "escapechar", "", false, ALL, "", empty_hint},
226         { "escapeinside", "", false, ALL, "", empty_hint},
227         { "escepeinside", "", false, ALL, "", empty_hint},
228         { "escepebegin", "", false, ALL, "", empty_hint},
229         { "escepeend", "", false, ALL, "", empty_hint},
230         { "fancyvrb", "", false, TRUEFALSE, "", empty_hint},
231         { "fvcmdparams", "", false, ALL, "", empty_hint},
232         { "morefvcmdparams", "", false, ALL, "", empty_hint},
233         { "keywordsprefix", "", false, ALL, "", empty_hint},
234         { "keywords", "", false, ALL, "", empty_hint},
235         { "morekeywords", "", false, ALL, "", empty_hint},
236         { "deletekeywords", "", false, ALL, "", empty_hint},
237         { "ndkeywords", "", false, ALL, "", empty_hint},
238         { "morendkeywords", "", false, ALL, "", empty_hint},
239         { "deletendkeywords", "", false, ALL, "", empty_hint},
240         { "texcs", "", false, ALL, "", empty_hint},
241         { "moretexcs", "", false, ALL, "", empty_hint},
242         { "deletetexcs", "", false, ALL, "", empty_hint},
243         { "directives", "", false, ALL, "", empty_hint},
244         { "moredirectives", "", false, ALL, "", empty_hint},
245         { "deletedirectives", "", false, ALL, "", empty_hint},
246         { "sensitive", "", false, ALL, "", empty_hint},
247         { "alsoletter", "", false, ALL, "", empty_hint},
248         { "alsodigit", "", false, ALL, "", empty_hint},
249         { "alsoother", "", false, ALL, "", empty_hint},
250         { "otherkeywords", "", false, ALL, "", empty_hint},
251         { "tag", "", false, ALL, "", empty_hint},
252         { "string", "", false, ALL, "", empty_hint},
253         { "morestring", "", false, ALL, "", empty_hint},
254         { "deletestring", "", false, ALL, "", empty_hint},
255         { "comment", "", false, ALL, "", empty_hint},
256         { "morecomment", "", false, ALL, "", empty_hint},
257         { "deletecomment", "", false, ALL, "", empty_hint},
258         { "keywordcomment", "", false, ALL, "", empty_hint},
259         { "morekeywordcomment", "", false, ALL, "", empty_hint},
260         { "deletekeywordcomment", "", false, ALL, "", empty_hint},
261         { "keywordcommentsemicolon", "", false, ALL, "", empty_hint},
262         { "podcomment", "", false, ALL, "", empty_hint},
263         { "", "", false, ALL, "", empty_hint}
264 };
265
266
267 class parValidator
268 {
269 public:
270         parValidator(string const & name);
271
272         /// validate given parameter
273         /// invalidParam will be thrown if invalid 
274         /// parameter is found.
275         void validate(std::string const & par) const;
276
277 private:
278         /// parameter name
279         string const & name;
280         ///
281         listings_param_info const * info;
282 };
283
284
285 parValidator::parValidator(string const & n)
286         : name(n), info(0)
287 {
288         if (name.empty())
289                 throw invalidParam(_("Invalid (empty) listings param name."));
290         else if (name == "?") {
291                 string pars;
292                 size_t idx = 0;
293                 while (listings_param_table[idx].name != string()) {
294                         if (!pars.empty())
295                                 pars += ", ";
296                         pars += listings_param_table[idx].name;
297                         ++idx;
298                 }
299                 throw invalidParam(bformat(
300                         _("Available listings parameters are %1$s"), from_utf8(pars)));
301         }
302         // locate name in parameter table
303         size_t idx = 0;
304         while (listings_param_table[idx].name != name && listings_param_table[idx].name != string())
305                 ++idx;
306         // found the name
307         if (!listings_param_table[idx].name.empty()) {
308                 info = &listings_param_table[idx];
309                 return;
310         }
311         // otherwise, produce a meaningful error message.
312         string matching_names;
313         for (size_t i = 0; i < idx; ++i) {
314                 string n(listings_param_table[i].name);
315                 if (prefixIs(n, name)) {
316                         if (matching_names.empty())
317                                 matching_names += n;
318                         else
319                                 matching_names += ", " + n;
320                 }
321         }
322         if (matching_names.empty())
323                 throw invalidParam(bformat(_("Unknown listings param name: %1$s"),
324                                                     from_utf8(name)));
325         else
326                 throw invalidParam(bformat(_("Parameters starting with '%1$s': %2$s"),
327                                                     from_utf8(name), from_utf8(matching_names)));
328 }
329
330
331 void parValidator::validate(std::string const & par) const
332 {
333         bool unclosed = false;
334         string par2 = par;
335         // braces are allowed
336         if (prefixIs(par, "{") && suffixIs(par, "}"))
337                 par2 = par.substr(1, par.size() - 2);
338         else if (prefixIs(par, "{")) {
339                 par2 = par.substr(1);
340                 unclosed = true;
341         }
342         
343                 
344         switch (info->type) {
345         case ALL:
346                 if (par2.empty() && !info->onoff) {
347                         if (!info->hint.empty())
348                                 throw invalidParam(info->hint);
349                         else
350                                 throw invalidParam(_("A value is expected"));
351                 }
352                 if (unclosed)
353                                 throw invalidParam(_("Unbalanced braces!"));
354                 return;
355         case TRUEFALSE: {
356                 if (par2.empty() && !info->onoff) {
357                         if (!info->hint.empty())
358                                 throw invalidParam(info->hint);
359                         else
360                                 throw invalidParam(_("Please specify true or false"));
361                 }
362                 if (par2 != "true" && par2 != "false")
363                         throw invalidParam(bformat(_("Only true or false is allowed for parameter %1$s"),
364                                                    from_utf8(name)));
365                 if (unclosed)
366                                 throw invalidParam(_("Unbalanced braces!"));
367                 return;
368         }
369         case INTEGER: {
370                 if (!isStrInt(par2)) {
371                         if (!info->hint.empty())
372                                 throw invalidParam(info->hint);
373                         else
374                                 throw invalidParam(_("Please specify an integer value"));
375                 }
376                 if (convert<int>(par2) == 0 && par2[0] != '0')
377                         throw invalidParam(bformat(_("An integer is expected for parameter %1$s"),
378                                                    from_utf8(name)));
379                 if (unclosed)
380                                 throw invalidParam(_("Unbalanced braces!"));
381                 return;
382         }
383         case LENGTH: {
384                 if (par2.empty() && !info->onoff) {
385                         if (!info->hint.empty())
386                                 throw invalidParam(info->hint);
387                         else
388                                 throw invalidParam(_("Please specify a latex length expression"));
389                 }
390                 if (!isValidLength(par2))
391                         throw invalidParam(bformat(_("Invalid latex length expression for parameter %1$s"),
392                                                    from_utf8(name)));
393                 if (unclosed)
394                                 throw invalidParam(_("Unbalanced braces!"));
395                 return;
396         }
397         case ONEOF: {
398                 if (par2.empty() && !info->onoff) {
399                         if (!info->hint.empty())
400                                 throw invalidParam(info->hint);
401                         else
402                                 throw invalidParam(bformat(_("Please specify one of %1$s"),
403                                                            from_utf8(string(info->info))));
404                 }
405                 // break value to allowed strings
406                 vector<string> lists;
407                 string v;
408                 for (size_t i = 0; info->info[i] != '\0'; ++i) {
409                         if (info->info[i] == '\n') {
410                                 lists.push_back(v);
411                                 v = string();
412                         } else
413                                 v += info->info[i];
414                 }
415                 if (!v.empty())
416                         lists.push_back(v);
417
418                 // good, find the string
419                 if (std::find(lists.begin(), lists.end(), par2) != lists.end()) {
420                         if (unclosed)
421                                 throw invalidParam(_("Unbalanced braces!"));
422                         return;
423                 }
424                 // otherwise, produce a meaningful error message.
425                 string matching_names;
426                 for (vector<string>::iterator it = lists.begin(); 
427                         it != lists.end(); ++it) {
428                         if (it->size() >= par2.size() && it->substr(0, par2.size()) == par2) {
429                                 if (matching_names.empty())
430                                         matching_names += *it;
431                                 else
432                                         matching_names += ", " + *it;
433                         }
434                 }
435                 if (matching_names.empty())
436                         throw invalidParam(bformat(_("Try one of %1$s"),
437                                                    from_utf8(string(info->info))));
438                 else
439                         throw invalidParam(bformat(_("I guess you mean %1$s"),
440                                                    from_utf8(matching_names)));
441                 return;
442         }
443         case SUBSETOF: {
444                 if (par2.empty() && !info->onoff) {
445                         if (!info->hint.empty())
446                                 throw invalidParam(info->hint);
447                         else
448                                 throw invalidParam(bformat(_("Please specify one or more of '%1$s'"),
449                                                            from_utf8(string(info->info))));
450                 }
451                 for (size_t i = 0; i < par2.size(); ++i)
452                         if (string(info->info).find(par2[i], 0) == string::npos)
453                                 throw invalidParam(
454                                         bformat(_("Parameter %1$s should be composed of one or more of %2$s"),
455                                                 from_utf8(name), from_utf8(info->info)));
456                 if (unclosed)
457                                 throw invalidParam(_("Unbalanced braces!"));
458                 return;
459         }
460         }
461 }
462
463 } // namespace anon.
464
465 InsetListingsParams::InsetListingsParams() :
466         inline_(false), params_(), status_(InsetCollapsable::Open)
467 {
468 }
469
470
471 InsetListingsParams::InsetListingsParams(string const & par, bool in, InsetCollapsable::CollapseStatus s)
472         : inline_(in), params_(), status_(s)
473 {
474         // this will activate parameter validation.
475         fromEncodedString(par);
476 }
477
478
479 void InsetListingsParams::write(ostream & os) const
480 {
481         if (inline_)
482                 os << "true ";
483         else
484                 os << "false ";
485         os << status_ << " \""  << encodedString() << "\"";
486 }
487
488
489 void InsetListingsParams::read(Lexer & lex)
490 {
491         lex >> inline_;
492         int s;
493         lex >> s;
494         if (lex)
495                 status_ = static_cast<InsetCollapsable::CollapseStatus>(s);
496         string par;
497         lex >> par;
498         fromEncodedString(par);
499 }
500
501
502 string InsetListingsParams::params(string const & sep) const
503 {
504         string par;
505         for (map<string, string>::const_iterator it = params_.begin();
506                 it != params_.end(); ++it) {
507                 if (!par.empty())
508                         par += sep;
509                 if (it->second.empty())
510                         par += it->first;
511                 else
512                         par += it->first + '=' + it->second;
513         }
514         return par;
515 }
516
517
518 void InsetListingsParams::addParam(string const & key, string const & value)
519 {       
520         if (key.empty())
521                 return;
522         // exception may be thown.
523         parValidator(key).validate(value);
524         // duplicate parameters!
525         if (params_.find(key) != params_.end())
526                 throw invalidParam(bformat(_("Parameter %1$s has already been defined"),
527                                           from_utf8(key)));
528         // check onoff flag
529         size_t idx = 0;
530         while (listings_param_table[idx].name != key)
531                 ++idx;
532         BOOST_ASSERT(listings_param_table[idx].name == key);
533         // onoff parameter with value false
534         if (listings_param_table[idx].onoff && (value == "false" || value == "{false}"))
535                 params_[key] = string();
536         // if the parameter is surrounded with {}, good
537         else if (prefixIs(value, "{") && suffixIs(value, "}"))
538                 params_[key] = value;
539         // otherwise, check if {} is needed. Add {} to all values with 
540         // non-ascii/number characters, just to be safe
541         else {
542                 bool has_special_char = false;
543                 for (size_t i = 0; i < value.size(); ++i)
544                         if (!isAlphaASCII(value[i]) && !isDigit(value[i])) {
545                                 has_special_char = true;
546                                 break;
547                         }
548                 if (has_special_char)
549                         params_[key] = "{" + value + "}";
550                 else
551                         params_[key] = value;
552         }
553 }
554
555
556 void InsetListingsParams::addParams(string const & par)
557 {
558         string key;
559         string value;
560         bool isValue = false;
561         int braces = 0;
562         for (size_t i = 0; i < par.size(); ++i) {
563                 // end of par
564                 if (par[i] == '\n') {
565                         addParam(trim(key), trim(value));
566                         key = string();
567                         value = string();
568                         isValue = false;
569                         continue;
570                 } else if (par[i] == ',' && braces == 0) {
571                         addParam(trim(key), trim(value));
572                         key = string();
573                         value = string();
574                         isValue = false;
575                         continue;
576                 } else if (par[i] == '=' && braces == 0) {
577                         isValue = true;
578                         continue;
579                 } else if (par[i] == '{' && par[i - 1] == '=')
580                         braces ++;
581                 else if (par[i] == '}' && (i == par.size() - 1 || par[i + 1] == ',' || par[i + 1] == '\n'))
582                         braces --;
583                 
584                 if (isValue)
585                         value += par[i];
586                 else
587                         key += par[i];
588         }
589         if (!trim(key).empty())
590                 addParam(trim(key), trim(value));
591 }
592
593
594 void InsetListingsParams::setParams(string const & par)
595 {
596         params_.clear();
597         addParams(par);
598 }
599
600
601 string InsetListingsParams::encodedString() const
602 {
603         // Encode string!
604         // '"' is handled differently because it will 
605         // terminate a lyx token.
606         string par = params();
607         // '"' is now &quot;  ==> '"' is now &amp;quot;
608         par = subst(par, "&", "&amp;");
609         // '"' is now &amp;quot; ==> '&quot;' is now &amp;quot;
610         par = subst(par, "\"", "&quot;");
611         return par;     
612 }
613
614
615 string InsetListingsParams::separatedParams(bool keepComma) const
616 {
617         if (keepComma)
618                 return params(",\n");
619         else
620                 return params("\n");
621 }
622
623
624 void InsetListingsParams::fromEncodedString(string const & in)
625 {
626         // Decode string! Reversal of encodedString
627         string par = in;
628         // '&quot;' is now &amp;quot; ==> '"' is now &amp;quot;
629         par = subst(par, "&quot;", "\"");
630         //  '"' is now &amp;quot; ==> '"' is now &quot;
631         par = subst(par, "&amp;", "&");
632         setParams(par);
633 }
634
635
636 bool InsetListingsParams::isFloat() const
637 {
638         return params_.find("float") != params_.end();
639 }
640
641
642 string InsetListingsParams::getParamValue(string const & param) const
643 {
644         // is this parameter defined?
645         map<string, string>::const_iterator it = params_.find(param);
646         return (it == params_.end()) ? string() : it->second;
647 }
648
649
650 } // namespace lyx