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.
14 #include "InsetListingsParams.h"
20 #include <boost/assert.hpp>
22 #include "support/lstrings.h"
23 #include "support/textutils.h"
24 #include "support/convert.h"
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;
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
54 /** Information about each parameter
56 struct listings_param_info {
57 /// name of the parameter
61 // for option with value "true", "false",
65 // "other": option="other"
67 // "true": option=true
68 // "false": option=false
77 // info is a \n separated string with allowed values
79 // info is a string from which par is composed of
80 // (e.g. floatplacement can be one or more of tbph)
82 /// parameter info, meaning depending on parameter type
84 /// a help message that is displayed in the gui
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";
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}");
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, "", ""}
268 parValidator(string const & name);
270 /// validate given parameter
271 /// invalidParam will be thrown if invalid
272 /// parameter is found.
273 void validate(std::string const & par) const;
279 listings_param_info const * info;
283 parValidator::parValidator(string const & n)
287 throw invalidParam(_("Invalid (empty) listings param name."));
288 else if (name == "?") {
291 while (listings_param_table[idx].name != string()) {
294 pars += listings_param_table[idx].name;
297 throw invalidParam(bformat(
298 _("Available listings parameters are %1$s"), from_utf8(pars)));
300 // locate name in parameter table
302 while (listings_param_table[idx].name != name && listings_param_table[idx].name != string())
305 if (listings_param_table[idx].name != "") {
306 info = &listings_param_table[idx];
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())
317 matching_names += ", " + n;
320 if (matching_names.empty())
321 throw invalidParam(bformat(_("Unknown listings param name: %1$s"),
324 throw invalidParam(bformat(_("Parameters starting with '%1$s': %2$s"),
325 from_utf8(name), from_utf8(matching_names)));
329 void parValidator::validate(std::string const & par) const
331 bool unclosed = false;
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);
342 switch (info->type) {
344 if (par2.empty() && !info->onoff) {
345 if (info->hint != "")
346 throw invalidParam(from_utf8(info->hint));
348 throw invalidParam(_("A value is expected"));
351 throw invalidParam(_("Unbalanced braces!"));
354 if (par2.empty() && !info->onoff) {
355 if (info->hint != "")
356 throw invalidParam(from_utf8(info->hint));
358 throw invalidParam(_("Please specify true or false"));
360 if (par2 != "true" && par2 != "false")
361 throw invalidParam(bformat(_("Only true or false is allowed for parameter %1$s"),
364 throw invalidParam(_("Unbalanced braces!"));
368 if (!isStrInt(par2)) {
369 if (info->hint != "")
370 throw invalidParam(from_utf8(info->hint));
372 throw invalidParam(_("Please specify an integer value"));
374 if (convert<int>(par2) == 0 && par2[0] != '0')
375 throw invalidParam(bformat(_("An integer is expected for parameter %1$s"),
378 throw invalidParam(_("Unbalanced braces!"));
382 if (par2.empty() && !info->onoff) {
383 if (info->hint != "")
384 throw invalidParam(from_utf8(info->hint));
386 throw invalidParam(_("Please specify a latex length expression"));
388 if (!isValidLength(par2))
389 throw invalidParam(bformat(_("Invalid latex length expression for parameter %1$s"),
392 throw invalidParam(_("Unbalanced braces!"));
396 if (par2.empty() && !info->onoff) {
397 if (info->hint != "")
398 throw invalidParam(from_utf8(info->hint));
400 throw invalidParam(bformat(_("Please specify one of %1$s"),
401 from_utf8(string(info->info))));
403 // break value to allowed strings
404 vector<string> lists;
406 for (size_t i = 0; info->info[i] != '\0'; ++i) {
407 if (info->info[i] == '\n') {
416 // good, find the string
417 if (std::find(lists.begin(), lists.end(), par2) != lists.end()) {
419 throw invalidParam(_("Unbalanced braces!"));
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;
430 matching_names += ", " + *it;
433 if (matching_names.empty())
434 throw invalidParam(bformat(_("Try one of %1$s"),
435 from_utf8(string(info->info))));
437 throw invalidParam(bformat(_("I guess you mean %1$s"),
438 from_utf8(matching_names)));
442 if (par2.empty() && !info->onoff) {
443 if (info->hint != "")
444 throw invalidParam(from_utf8(info->hint));
446 throw invalidParam(bformat(_("Please specify one or more of '%1$s'"),
447 from_utf8(string(info->info))));
449 for (size_t i = 0; i < par2.size(); ++i)
450 if (string(info->info).find(par2[i], 0) == string::npos)
452 bformat(_("Parameter %1$s should be composed of one or more of %2$s"),
453 from_utf8(name), from_utf8(info->info)));
455 throw invalidParam(_("Unbalanced braces!"));
462 InsetListingsParams::InsetListingsParams() :
463 inline_(false), params_(), status_(InsetCollapsable::Open)
468 InsetListingsParams::InsetListingsParams(string const & par, bool in, InsetCollapsable::CollapseStatus s)
469 : inline_(in), params_(), status_(s)
471 // this will activate parameter validation.
472 fromEncodedString(par);
476 void InsetListingsParams::write(ostream & os) const
482 os << status_ << " \"" << encodedString() << "\"";
486 void InsetListingsParams::read(Lexer & lex)
492 status_ = static_cast<InsetCollapsable::CollapseStatus>(s);
495 fromEncodedString(par);
499 string InsetListingsParams::params(string sep) const
502 for (map<string, string>::const_iterator it = params_.begin();
503 it != params_.end(); ++it) {
506 if (it->second.empty())
509 par += it->first + '=' + it->second;
515 void InsetListingsParams::addParam(string const & key, string const & value)
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"),
527 while (listings_param_table[idx].name != key)
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
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;
545 if (has_special_char)
546 params_[key] = "{" + value + "}";
548 params_[key] = value;
553 void InsetListingsParams::addParams(string const & par)
557 bool isValue = false;
559 for (size_t i = 0; i < par.size(); ++i) {
561 if (par[i] == '\n') {
562 addParam(trim(key), trim(value));
567 } else if (par[i] == ',' && braces == 0) {
568 addParam(trim(key), trim(value));
573 } else if (par[i] == '=' && braces == 0) {
576 } else if (par[i] == '{' && par[i - 1] == '=')
578 else if (par[i] == '}' && (i == par.size() - 1 || par[i + 1] == ',' || par[i + 1] == '\n'))
586 if (!trim(key).empty())
587 addParam(trim(key), trim(value));
591 void InsetListingsParams::setParams(string const & par)
598 string InsetListingsParams::encodedString() const
601 // '"' is handled differently because it will
602 // terminate a lyx token.
603 string par = params();
604 // '"' is now " ==> '"' is now &quot;
605 par = subst(par, "&", "&");
606 // '"' is now &quot; ==> '"' is now &quot;
607 par = subst(par, "\"", """);
612 string InsetListingsParams::separatedParams(bool keepComma) const
615 return params(",\n");
621 void InsetListingsParams::fromEncodedString(string const & in)
623 // Decode string! Reversal of encodedString
625 // '"' is now &quot; ==> '"' is now &quot;
626 par = subst(par, """, "\"");
627 // '"' is now &quot; ==> '"' is now "
628 par = subst(par, "&", "&");
633 bool InsetListingsParams::isFloat() const
635 return params_.find("float") != params_.end();
639 string InsetListingsParams::getParamValue(string const & param) const
641 // is this parameter defined?
642 map<string, string>::const_iterator it = params_.find(param);
643 return (it == params_.end()) ? string() : it->second;