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/convert.h"
29 using lyx::support::trim;
30 using lyx::support::isStrInt;
31 using lyx::support::prefixIs;
32 using lyx::support::suffixIs;
33 using lyx::support::getVectorFromString;
40 TRUEFALSE, // accept 'true' or 'false'
41 INTEGER, // accept an integer
42 LENGTH, // accept an latex length
43 ONEOF, // accept one of a few values
44 SUBSETOF, // accept a string composed of given characters
48 /** Information about each parameter
50 struct listings_param_info {
51 /// name of the parameter
55 // for option with value "true", "false",
59 // "other": option="other"
61 // "true": option=true
62 // "false": option=false
72 // info is a \n separated string with allowed values
74 // info is a string from which par is composed of
75 // (e.g. floatplacement can be one or more of tbph)
83 /// languages and language/dialect combinations
84 char const * allowed_languages =
85 "no language\nABAP\n[R/2 4.3]ABAP\n[R/2 5.0]ABAP\n[R/3 3.1]ABAP\n"
86 "[R/3 4.6C]ABAP\n[R/3 6.10]ABAP\nACSL\nAda\n[2005]Ada\n[83]Ada\n"
87 "[95]Ada\nALGOL\n[60]ALGOL\n[68]ALGOL\nAssembler\n"
88 "[Motorola68k]Assembler\n[x86masm]Assembler\nAwk\n[gnu]Awk\n[POSIX]Awk\n"
89 "bash\nBasic\n[Visual]Basic\nC\n[ANSI]C\n[Handel]C\n[Objective]C\n"
90 "[Sharp]C\nC++\n[ANSI]C++\n[GNU]C++\n[ISO]C++\n[Visual]C++\nCaml\n"
91 "[light]Caml\n[Objective]Caml\nClean\nCobol\n[1974]Cobol\n[1985]Cobol\n"
92 "[ibm]Cobol\nComal 80\ncommand.com\n[WinXP]command.com\nComsol\ncsh\n"
93 "Delphi\nEiffel\nElan\nEuphoria\nFortran\n[77]Fortran\n[90]Fortran\n"
94 "[95]Fortran\nGCL\nGnuplot\nHaskell\nHTML\nIDL\n[CORBA]IDL\ninform\n"
95 "Java\n[AspectJ]Java\nJVMIS\nksh\nLingo\nLisp\n[Auto]Lisp\nLogo\n"
96 "make\n[gnu]make\nMathematica\n[1.0]Mathematica\n[3.0]Mathematica\n"
97 "[5.2]Mathematica\nMatlab\nMercury\nMetaPost\nMiranda\nMizar\nML\n"
98 "Modula-2\nMuPAD\nNASTRAN\nOberon-2\nOCL\n[decorative]OCL\n[OMG]OCL\n"
99 "Octave\nOz\nPascal\n[Borland6]Pascal\n[Standard]Pascal\n[XSC]Pascal\n"
100 "Perl\nPHP\nPL/I\nPlasm\nPostScript\nPOV\nProlog\nPromela\nPSTricks\n"
101 "Python\nR\nReduce\nRexx\nRSL\nRuby\nS\n[PLUS]S\nSAS\nScilab\nsh\n"
102 "SHELXL\nSimula\n[67]Simula\n[CII]Simula\n[DEC]Simula\n[IBM]Simula\n"
103 "SPARQL\nSQL\ntcl\n[tk]tcl\nTeX\n[AlLaTeX]TeX\n[common]TeX\n[LaTeX]TeX\n"
104 "[plain]TeX\n[primitive]TeX\nVBScript\nVerilog\nVHDL\n[AMS]VHDL\nVRML\n"
105 "[97]VRML\nXML\nXSLT";
107 char const * style_hint = "Use \\footnotesize, \\small, \\itshape, \\ttfamily or something like that";
108 char const * frame_hint = "none, leftline, topline, bottomline, lines, single, shadowbox or subset of trblTRBL";
109 char const * frameround_hint =
110 "Enter four letters (either t = round or f = square) for top right, bottom right, bottom left and top left corner.";
111 char const * color_hint = "Enter something like \\color{white}";
113 /// options copied from page 26 of listings manual
114 // FIXME: add default parameters ... (which is not used now)
115 listings_param_info const listings_param_table[] = {
116 { "float", "false", true, SUBSETOF, "tbph", "" },
117 { "floatplacement", "tbp", false, SUBSETOF, "tbph", "" },
118 { "aboveskip", "\\medskipamount", false, LENGTH, "", "" },
119 { "belowskip", "\\medskipamount", false, LENGTH, "", "" },
120 { "lineskip", "", false, LENGTH, "", "" },
121 { "boxpos", "", false, SUBSETOF, "bct", "" },
122 { "print", "", false, TRUEFALSE, "", "" },
123 { "firstline", "", false, INTEGER, "", "" },
124 { "lastline", "", false, INTEGER, "", "" },
125 { "showlines", "", false, TRUEFALSE, "", "" },
126 { "emptylines", "", false, ALL, "", "Expect a number with an optional * before it" },
127 { "gobble", "", false, INTEGER, "", "" },
128 { "style", "", false, ALL, "", "" },
129 { "language", "", false, ONEOF, allowed_languages, "" },
130 { "alsolanguage", "", false, ONEOF, allowed_languages, "" },
131 { "defaultdialect", "", false, ONEOF, allowed_languages, "" },
132 { "printpod", "", false, TRUEFALSE, "", "" },
133 { "usekeywordsintag", "", false, TRUEFALSE, "", "" },
134 { "tagstyle", "", false, ALL, "", style_hint },
135 { "markfirstintag", "", false, ALL, "", style_hint },
136 { "makemacrouse", "", false, TRUEFALSE, "", "" },
137 { "basicstyle", "", false, ALL, "", style_hint },
138 { "identifierstyle", "", false, ALL, "", style_hint },
139 { "commentstyle", "", false, ALL, "", style_hint },
140 { "stringstyle", "", false, ALL, "", style_hint },
141 { "keywordstyle", "", false, ALL, "", style_hint },
142 { "ndkeywordstyle", "", false, ALL, "", style_hint },
143 { "classoffset", "", false, INTEGER, "", "" },
144 { "texcsstyle", "", false, ALL, "", style_hint },
145 { "directivestyle", "", false, ALL, "", style_hint },
146 { "emph", "", false, ALL, "", "" },
147 { "moreemph", "", false, ALL, "", "" },
148 { "deleteemph", "", false, ALL, "", "" },
149 { "emphstyle", "", false, ALL, "", "" },
150 { "delim", "", false, ALL, "", "" },
151 { "moredelim", "", false, ALL, "", "" },
152 { "deletedelim", "", false, ALL, "", "" },
153 { "extendedchars", "", false, TRUEFALSE, "", "" },
154 { "inputencoding", "", false, ALL, "", "" },
155 { "upquote", "", false, TRUEFALSE, "", "" },
156 { "tabsize", "", false, INTEGER, "", "" },
157 { "showtabs", "", false, ALL, "", "" },
158 { "tab", "", false, ALL, "", "" },
159 { "showspaces", "", false, TRUEFALSE, "", "" },
160 { "showstringspaces", "", false, TRUEFALSE, "", "" },
161 { "formfeed", "", false, ALL, "", "" },
162 { "numbers", "", false, ONEOF, "none\nleft\nright", "" },
163 { "stepnumber", "", false, INTEGER, "", "" },
164 { "numberfirstline", "", false, TRUEFALSE, "", "" },
165 { "numberstyle", "", false, ALL, "", style_hint },
166 { "numbersep", "", false, LENGTH, "", "" },
167 { "numberblanklines", "", false, ALL, "", "" },
168 { "firstnumber", "", false, ALL, "", "auto, last or a number" },
169 { "name", "", false, ALL, "", "" },
170 { "thelstnumber", "", false, ALL, "", "" },
171 { "title", "", false, ALL, "", "" },
172 // this option is not handled in the parameter box
173 { "caption", "", false, ALL, "", "This parameter should not be entered here. "
174 "Please use caption editbox (Include dialog) or insert->caption (listings inset)" },
175 // this option is not handled in the parameter box
176 { "label", "", false, ALL, "", "This parameter should not be entered here."
177 "Please use label editbox (Include dialog) or insert->caption (listings inset)"},
178 { "nolol", "", false, TRUEFALSE, "", "" },
179 { "captionpos", "", false, SUBSETOF, "tb", "" },
180 { "abovecaptionskip", "", false, LENGTH, "", "" },
181 { "belowcaptionskip", "", false, LENGTH, "", "" },
182 { "linewidth", "", false, LENGTH, "", "" },
183 { "xleftmargin", "", false, LENGTH, "", "" },
184 { "xrightmargin", "", false, LENGTH, "", "" },
185 { "resetmargins", "", false, TRUEFALSE, "", "" },
186 { "breaklines", "", false, TRUEFALSE, "", "" },
187 { "prebreak", "", false, ALL, "", "" },
188 { "postbreak", "", false, ALL, "", "" },
189 { "breakindent", "", false, LENGTH, "", "" },
190 { "breakautoindent", "", false, TRUEFALSE, "", "" },
191 { "frame", "", false, ALL, "", frame_hint },
192 { "frameround", "", false, SUBSETOF, "tf", frameround_hint },
193 { "framesep", "", false, LENGTH, "", "" },
194 { "rulesep", "", false, LENGTH, "", "" },
195 { "framerule", "", false, LENGTH, "", "" },
196 { "framexleftmargin", "", false, LENGTH, "", "" },
197 { "framexrightmargin", "", false, LENGTH, "", "" },
198 { "framextopmargin", "", false, LENGTH, "", "" },
199 { "framexbottommargin", "", false, LENGTH, "", "" },
200 { "backgroundcolor", "", false, ALL, "", color_hint },
201 { "rulecolor", "", false, ALL, "", color_hint },
202 { "fillcolor", "", false, ALL, "", color_hint },
203 { "rulesepcolor", "", false, ALL, "", color_hint },
204 { "frameshape", "", false, ALL, "", "" },
205 { "index", "", false, ALL, "", "" },
206 { "moreindex", "", false, ALL, "", "" },
207 { "deleteindex", "", false, ALL, "", "" },
208 { "indexstyle", "", false, ALL, "", "" },
209 { "columns", "", false, ALL, "", "" },
210 { "flexiblecolumns", "", false, ALL, "", "" },
211 { "keepspaces", "", false, TRUEFALSE, "", "" },
212 { "basewidth", "", false, LENGTH, "", "" },
213 { "fontadjust", "", true, TRUEFALSE, "", "" },
214 { "texcl", "", false, TRUEFALSE, "", "" },
215 { "mathescape", "", false, TRUEFALSE, "", "" },
216 { "escapechar", "", false, ALL, "", "" },
217 { "escapeinside", "", false, ALL, "", "" },
218 { "escepeinside", "", false, ALL, "", "" },
219 { "escepebegin", "", false, ALL, "", "" },
220 { "escepeend", "", false, ALL, "", "" },
221 { "fancyvrb", "", false, TRUEFALSE, "", "" },
222 { "fvcmdparams", "", false, ALL, "", "" },
223 { "morefvcmdparams", "", false, ALL, "", "" },
224 { "keywordsprefix", "", false, ALL, "", "" },
225 { "keywords", "", false, ALL, "", "" },
226 { "morekeywords", "", false, ALL, "", "" },
227 { "deletekeywords", "", false, ALL, "", "" },
228 { "ndkeywords", "", false, ALL, "", "" },
229 { "morendkeywords", "", false, ALL, "", "" },
230 { "deletendkeywords", "", false, ALL, "", "" },
231 { "texcs", "", false, ALL, "", "" },
232 { "moretexcs", "", false, ALL, "", "" },
233 { "deletetexcs", "", false, ALL, "", "" },
234 { "directives", "", false, ALL, "", "" },
235 { "moredirectives", "", false, ALL, "", "" },
236 { "deletedirectives", "", false, ALL, "", "" },
237 { "sensitive", "", false, ALL, "", "" },
238 { "alsoletter", "", false, ALL, "", "" },
239 { "alsodigit", "", false, ALL, "", "" },
240 { "alsoother", "", false, ALL, "", "" },
241 { "otherkeywords", "", false, ALL, "", "" },
242 { "tag", "", false, ALL, "", "" },
243 { "string", "", false, ALL, "", "" },
244 { "morestring", "", false, ALL, "", "" },
245 { "deletestring", "", false, ALL, "", "" },
246 { "comment", "", false, ALL, "", "" },
247 { "morecomment", "", false, ALL, "", "" },
248 { "deletecomment", "", false, ALL, "", "" },
249 { "keywordcomment", "", false, ALL, "", "" },
250 { "morekeywordcomment", "", false, ALL, "", "" },
251 { "deletekeywordcomment", "", false, ALL, "", "" },
252 { "keywordcommentsemicolon", "", false, ALL, "", "" },
253 { "podcomment", "", false, ALL, "", "" },
254 { "", "", false, ALL, "", ""}
261 parValidator(string const & name);
263 /// validate given parameter
264 /// invalidParam will be thrown if invalid
265 /// parameter is found.
266 void validate(std::string const & par) const;
272 listings_param_info const * info;
276 parValidator::parValidator(string const & n)
280 throw invalidParam("Invalid (empty) listings param name.");
281 else if (name == "?") {
284 while (listings_param_table[idx].name != string()) {
287 pars += listings_param_table[idx].name;
290 throw invalidParam("Available listings parameters are " + pars);
292 // locate name in parameter table
294 while (listings_param_table[idx].name != name && listings_param_table[idx].name != string())
297 if (listings_param_table[idx].name != "") {
298 info = &listings_param_table[idx];
301 // otherwise, produce a meaningful error message.
302 string matching_names;
303 for (size_t i = 0; i < idx; ++i) {
304 string n(listings_param_table[i].name);
305 if (n.size() >= name.size() && n.substr(0, name.size()) == name) {
306 if (matching_names.empty())
309 matching_names += ", " + n;
312 if (matching_names.empty())
313 throw invalidParam("Unknown listings param name: " + name);
315 throw invalidParam("Parameters starting with '" + name +
316 "': " + matching_names);
320 void parValidator::validate(std::string const & par) const
322 bool unclosed = false;
324 // braces are allowed
325 if (prefixIs(par, "{") && suffixIs(par, "}"))
326 par2 = par.substr(1, par.size() - 2);
327 else if (prefixIs(par, "{")) {
328 par2 = par.substr(1);
333 switch (info->type) {
335 if (par2.empty() && !info->onoff) {
336 if (info->hint != "")
337 throw invalidParam(info->hint);
339 throw invalidParam("A value is expected");
342 throw invalidParam("Unbalanced braces!");
345 if (par2.empty() && !info->onoff) {
346 if (info->hint != "")
347 throw invalidParam(info->hint);
349 throw invalidParam("Please specify true or false");
351 if (par2 != "true" && par2 != "false")
352 throw invalidParam("Only true or false is allowed for parameter" + name);
354 throw invalidParam("Unbalanced braces!");
358 if (!isStrInt(par2)) {
359 if (info->hint != "")
360 throw invalidParam(info->hint);
362 throw invalidParam("Please specify an integer value");
364 if (convert<int>(par2) == 0 && par2[0] != '0')
365 throw invalidParam("An integer is expected for parameter " + name);
367 throw invalidParam("Unbalanced braces!");
371 if (par2.empty() && !info->onoff) {
372 if (info->hint != "")
373 throw invalidParam(info->hint);
375 throw invalidParam("Please specify a latex length expression");
377 if (!isValidLength(par2))
378 throw invalidParam("Invalid latex length expression for parameter " + name);
380 throw invalidParam("Unbalanced braces!");
384 if (par2.empty() && !info->onoff) {
385 if (info->hint != "")
386 throw invalidParam(info->hint);
388 throw invalidParam("Please specify one of " + string(info->info));
390 // break value to allowed strings
391 vector<string> lists;
393 for (size_t i = 0; info->info[i] != '\0'; ++i) {
394 if (info->info[i] == '\n') {
403 // good, find the string
404 if (std::find(lists.begin(), lists.end(), par2) != lists.end()) {
406 throw invalidParam("Unbalanced braces!");
409 // otherwise, produce a meaningful error message.
410 string matching_names;
411 for (vector<string>::iterator it = lists.begin();
412 it != lists.end(); ++it) {
413 if (it->size() >= par2.size() && it->substr(0, par2.size()) == par2) {
414 if (matching_names.empty())
415 matching_names += *it;
417 matching_names += ", " + *it;
420 if (matching_names.empty())
421 throw invalidParam("Try one of " + string(info->info));
423 throw invalidParam("I guess you mean " + matching_names);
427 if (par2.empty() && !info->onoff) {
428 if (info->hint != "")
429 throw invalidParam(info->hint);
431 throw invalidParam("Please specify one or more of " + string(info->info));
433 for (size_t i = 0; i < par2.size(); ++i)
434 if (string(info->info).find(par2[i], 0) == string::npos)
435 throw invalidParam("Parameter " + name +
436 " should be composed of one or more of " + info->info);
438 throw invalidParam("Unbalanced braces!");
445 InsetListingsParams::InsetListingsParams() :
446 inline_(false), params_(), keys_(0), status_(InsetCollapsable::Open)
451 InsetListingsParams::InsetListingsParams(string const & par, bool in, InsetCollapsable::CollapseStatus s)
452 : inline_(in), params_(), keys_(0), status_(s)
454 // this will activate parameter validation.
455 fromEncodedString(par);
459 void InsetListingsParams::write(ostream & os) const
465 os << status_ << " \"" << encodedString() << "\"";
469 void InsetListingsParams::read(Lexer & lex)
475 status_ = static_cast<InsetCollapsable::CollapseStatus>(s);
478 fromEncodedString(par);
482 void InsetListingsParams::addParam(string const & key, string const & value)
486 // exception may be thown.
487 parValidator(key.c_str()).validate(value);
488 // duplicate parameters!
489 if (find(keys_.begin(), keys_.end(), key) != keys_.end())
490 throw invalidParam("Parameter " + key + " has already been defined");
492 keys_.push_back(key);
493 if (!params_.empty())
500 while (listings_param_table[idx].name != key)
502 BOOST_ASSERT(listings_param_table[idx].name == key);
503 if (listings_param_table[idx].onoff && value == "false")
506 params_ += key + '=' + value;
511 void InsetListingsParams::addParams(string const & par)
515 bool isValue = false;
517 for (size_t i = 0; i < par.size(); ++i) {
519 if (par[i] == '\n') {
520 addParam(trim(key), trim(value));
525 } else if (par[i] == ',' && braces == 0) {
526 addParam(trim(key), trim(value));
531 } else if (par[i] == '=' && braces == 0) {
534 } else if (par[i] == '{' && par[i - 1] == '=')
536 else if (par[i] == '}' && (i == par.size() - 1 || par[i + 1] == ','))
544 if (!trim(key).empty())
545 addParam(trim(key), trim(value));
549 void InsetListingsParams::setParams(string const & par)
557 string InsetListingsParams::encodedString() const
561 // '"' should be handled differently because it will
562 // terminate a lyx token. Right now, it is silently ignored.
564 for (size_t i = 0; i < params_.size(); ++i) {
565 BOOST_ASSERT(params_[i] != '\n');
566 if (params_[i] != '"')
573 string InsetListingsParams::separatedParams(bool keepComma) const
575 // , might be used as regular parameter option so
576 // the prcess might be more complicated than what I am doing here
579 for (size_t i = 0; i < params_.size(); ++i)
580 if (params_[i] == ',' && braces == 0) {
585 } else if (params_[i] == '{' && params_[i - 1] == '=') {
588 } else if (params_[i] == '}' && (i == params_.size() -1 || params_[i + 1] == ',')) {
597 void InsetListingsParams::fromEncodedString(string const & in)
600 // Do nothing because " was silently ignored.
605 bool InsetListingsParams::isFloat() const
607 return find(keys_.begin(), keys_.end(), "float") != keys_.end();
611 string InsetListingsParams::getParamValue(string const & param) const
613 // is this parameter defined?
614 if (find(keys_.begin(), keys_.end(), param) == keys_.end())
616 // if so, search for it
617 vector<string> pars = getVectorFromString(separatedParams(), "\n");
618 for (vector<string>::iterator it = pars.begin(); it != pars.end(); ++it)
619 if (prefixIs(*it, param + "=")) {
620 string par = it->substr(param.size() + 1);
621 if (prefixIs(par, "{") && suffixIs(par, "}"))
622 return par.substr(1, par.size() - 2);
626 // if param= is not found, should be something like float, return ""