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