2 * \file InsetListingsParams.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetListingsParams.h"
19 #include "support/lstrings.h"
20 #include "support/textutils.h"
21 #include "support/convert.h"
23 #include <boost/assert.hpp>
36 using support::bformat;
39 using support::isStrInt;
40 using support::prefixIs;
41 using support::suffixIs;
42 using support::getVectorFromString;
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
56 /** Information about each parameter
58 struct listings_param_info {
59 /// name of the parameter
63 // for option with value "true", "false",
67 // "other": option="other"
69 // "true": option=true
70 // "false": option=false
79 // info is a \n separated string with allowed values
81 // info is a string from which par is composed of
82 // (e.g. floatplacement can be one or more of *tbph)
84 /// parameter info, meaning depending on parameter type
86 /// a help message that is displayed in the gui
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";
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}");
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}
270 parValidator(string const & name);
272 /// validate given parameter
273 /// invalidParam will be thrown if invalid
274 /// parameter is found.
275 void validate(std::string const & par) const;
281 listings_param_info const * info;
285 parValidator::parValidator(string const & n)
289 throw invalidParam(_("Invalid (empty) listings param name."));
290 else if (name == "?") {
293 while (listings_param_table[idx].name != string()) {
296 pars += listings_param_table[idx].name;
299 throw invalidParam(bformat(
300 _("Available listings parameters are %1$s"), from_utf8(pars)));
302 // locate name in parameter table
304 while (listings_param_table[idx].name != name && listings_param_table[idx].name != string())
307 if (!listings_param_table[idx].name.empty()) {
308 info = &listings_param_table[idx];
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())
319 matching_names += ", " + n;
322 if (matching_names.empty())
323 throw invalidParam(bformat(_("Unknown listings param name: %1$s"),
326 throw invalidParam(bformat(_("Parameters starting with '%1$s': %2$s"),
327 from_utf8(name), from_utf8(matching_names)));
331 void parValidator::validate(std::string const & par) const
333 bool unclosed = false;
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);
344 switch (info->type) {
346 if (par2.empty() && !info->onoff) {
347 if (!info->hint.empty())
348 throw invalidParam(info->hint);
350 throw invalidParam(_("A value is expected"));
353 throw invalidParam(_("Unbalanced braces!"));
356 if (par2.empty() && !info->onoff) {
357 if (!info->hint.empty())
358 throw invalidParam(info->hint);
360 throw invalidParam(_("Please specify true or false"));
362 if (par2 != "true" && par2 != "false")
363 throw invalidParam(bformat(_("Only true or false is allowed for parameter %1$s"),
366 throw invalidParam(_("Unbalanced braces!"));
370 if (!isStrInt(par2)) {
371 if (!info->hint.empty())
372 throw invalidParam(info->hint);
374 throw invalidParam(_("Please specify an integer value"));
376 if (convert<int>(par2) == 0 && par2[0] != '0')
377 throw invalidParam(bformat(_("An integer is expected for parameter %1$s"),
380 throw invalidParam(_("Unbalanced braces!"));
384 if (par2.empty() && !info->onoff) {
385 if (!info->hint.empty())
386 throw invalidParam(info->hint);
388 throw invalidParam(_("Please specify a latex length expression"));
390 if (!isValidLength(par2))
391 throw invalidParam(bformat(_("Invalid latex length expression for parameter %1$s"),
394 throw invalidParam(_("Unbalanced braces!"));
398 if (par2.empty() && !info->onoff) {
399 if (!info->hint.empty())
400 throw invalidParam(info->hint);
402 throw invalidParam(bformat(_("Please specify one of %1$s"),
403 from_utf8(string(info->info))));
405 // break value to allowed strings
406 vector<string> lists;
408 for (size_t i = 0; info->info[i] != '\0'; ++i) {
409 if (info->info[i] == '\n') {
418 // good, find the string
419 if (std::find(lists.begin(), lists.end(), par2) != lists.end()) {
421 throw invalidParam(_("Unbalanced braces!"));
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;
432 matching_names += ", " + *it;
435 if (matching_names.empty())
436 throw invalidParam(bformat(_("Try one of %1$s"),
437 from_utf8(string(info->info))));
439 throw invalidParam(bformat(_("I guess you mean %1$s"),
440 from_utf8(matching_names)));
444 if (par2.empty() && !info->onoff) {
445 if (!info->hint.empty())
446 throw invalidParam(info->hint);
448 throw invalidParam(bformat(_("Please specify one or more of '%1$s'"),
449 from_utf8(string(info->info))));
451 for (size_t i = 0; i < par2.size(); ++i)
452 if (string(info->info).find(par2[i], 0) == string::npos)
454 bformat(_("Parameter %1$s should be composed of one or more of %2$s"),
455 from_utf8(name), from_utf8(info->info)));
457 throw invalidParam(_("Unbalanced braces!"));
465 InsetListingsParams::InsetListingsParams() :
466 inline_(false), params_(), status_(InsetCollapsable::Open)
471 InsetListingsParams::InsetListingsParams(string const & par, bool in, InsetCollapsable::CollapseStatus s)
472 : inline_(in), params_(), status_(s)
474 // this will activate parameter validation.
475 fromEncodedString(par);
479 void InsetListingsParams::write(ostream & os) const
485 os << status_ << " \"" << encodedString() << "\"";
489 void InsetListingsParams::read(Lexer & lex)
495 status_ = static_cast<InsetCollapsable::CollapseStatus>(s);
498 fromEncodedString(par);
502 string InsetListingsParams::params(string const & sep) const
505 for (map<string, string>::const_iterator it = params_.begin();
506 it != params_.end(); ++it) {
509 if (it->second.empty())
512 par += it->first + '=' + it->second;
518 void InsetListingsParams::addParam(string const & key, string const & value)
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"),
530 while (listings_param_table[idx].name != key)
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
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;
548 if (has_special_char)
549 params_[key] = "{" + value + "}";
551 params_[key] = value;
556 void InsetListingsParams::addParams(string const & par)
560 bool isValue = false;
562 for (size_t i = 0; i < par.size(); ++i) {
564 if (par[i] == '\n') {
565 addParam(trim(key), trim(value));
570 } else if (par[i] == ',' && braces == 0) {
571 addParam(trim(key), trim(value));
576 } else if (par[i] == '=' && braces == 0) {
579 } else if (par[i] == '{' && par[i - 1] == '=')
581 else if (par[i] == '}' && (i == par.size() - 1 || par[i + 1] == ',' || par[i + 1] == '\n'))
589 if (!trim(key).empty())
590 addParam(trim(key), trim(value));
594 void InsetListingsParams::setParams(string const & par)
601 string InsetListingsParams::encodedString() const
604 // '"' is handled differently because it will
605 // terminate a lyx token.
606 string par = params();
607 // '"' is now " ==> '"' is now &quot;
608 par = subst(par, "&", "&");
609 // '"' is now &quot; ==> '"' is now &quot;
610 par = subst(par, "\"", """);
615 string InsetListingsParams::separatedParams(bool keepComma) const
618 return params(",\n");
624 void InsetListingsParams::fromEncodedString(string const & in)
626 // Decode string! Reversal of encodedString
628 // '"' is now &quot; ==> '"' is now &quot;
629 par = subst(par, """, "\"");
630 // '"' is now &quot; ==> '"' is now "
631 par = subst(par, "&", "&");
636 bool InsetListingsParams::isFloat() const
638 return params_.find("float") != params_.end();
642 string InsetListingsParams::getParamValue(string const & param) const
644 // is this parameter defined?
645 map<string, string>::const_iterator it = params_.find(param);
646 return (it == params_.end()) ? string() : it->second;