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::bformat;
30 using lyx::support::trim;
31 using lyx::support::isStrInt;
32 using lyx::support::prefixIs;
33 using lyx::support::suffixIs;
34 using lyx::support::getVectorFromString;
41 TRUEFALSE, // accept 'true' or 'false'
42 INTEGER, // accept an integer
43 LENGTH, // accept a latex length
44 ONEOF, // accept one of a few values
45 SUBSETOF, // accept a string composed of given characters
49 /** Information about each parameter
51 struct listings_param_info {
52 /// name of the parameter
56 // for option with value "true", "false",
60 // "other": option="other"
62 // "true": option=true
63 // "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)
77 /// parameter info, meaning depending on parameter type
79 /// a help message that is displayed in the gui
85 /// languages and language/dialect combinations
86 char const * allowed_languages =
87 "no language\nABAP\n[R/2 4.3]ABAP\n[R/2 5.0]ABAP\n[R/3 3.1]ABAP\n"
88 "[R/3 4.6C]ABAP\n[R/3 6.10]ABAP\nACSL\nAda\n[2005]Ada\n[83]Ada\n"
89 "[95]Ada\nALGOL\n[60]ALGOL\n[68]ALGOL\nAssembler\n"
90 "[Motorola68k]Assembler\n[x86masm]Assembler\nAwk\n[gnu]Awk\n[POSIX]Awk\n"
91 "bash\nBasic\n[Visual]Basic\nC\n[ANSI]C\n[Handel]C\n[Objective]C\n"
92 "[Sharp]C\nC++\n[ANSI]C++\n[GNU]C++\n[ISO]C++\n[Visual]C++\nCaml\n"
93 "[light]Caml\n[Objective]Caml\nClean\nCobol\n[1974]Cobol\n[1985]Cobol\n"
94 "[ibm]Cobol\nComal 80\ncommand.com\n[WinXP]command.com\nComsol\ncsh\n"
95 "Delphi\nEiffel\nElan\nEuphoria\nFortran\n[77]Fortran\n[90]Fortran\n"
96 "[95]Fortran\nGCL\nGnuplot\nHaskell\nHTML\nIDL\n[CORBA]IDL\ninform\n"
97 "Java\n[AspectJ]Java\nJVMIS\nksh\nLingo\nLisp\n[Auto]Lisp\nLogo\n"
98 "make\n[gnu]make\nMathematica\n[1.0]Mathematica\n[3.0]Mathematica\n"
99 "[5.2]Mathematica\nMatlab\nMercury\nMetaPost\nMiranda\nMizar\nML\n"
100 "Modula-2\nMuPAD\nNASTRAN\nOberon-2\nOCL\n[decorative]OCL\n[OMG]OCL\n"
101 "Octave\nOz\nPascal\n[Borland6]Pascal\n[Standard]Pascal\n[XSC]Pascal\n"
102 "Perl\nPHP\nPL/I\nPlasm\nPostScript\nPOV\nProlog\nPromela\nPSTricks\n"
103 "Python\nR\nReduce\nRexx\nRSL\nRuby\nS\n[PLUS]S\nSAS\nScilab\nsh\n"
104 "SHELXL\nSimula\n[67]Simula\n[CII]Simula\n[DEC]Simula\n[IBM]Simula\n"
105 "SPARQL\nSQL\ntcl\n[tk]tcl\nTeX\n[AlLaTeX]TeX\n[common]TeX\n[LaTeX]TeX\n"
106 "[plain]TeX\n[primitive]TeX\nVBScript\nVerilog\nVHDL\n[AMS]VHDL\nVRML\n"
107 "[97]VRML\nXML\nXSLT";
109 char const * style_hint = N_("Use \\footnotesize, \\small, \\itshape, \\ttfamily or something like that");
110 char const * frame_hint = N_("none, leftline, topline, bottomline, lines, single, shadowbox or subset of trblTRBL");
111 char const * frameround_hint =
112 N_("Enter four letters (either t = round or f = square) for top right, bottom right, bottom left and top left corner.");
113 char const * color_hint = N_("Enter something like \\color{white}");
115 /// options copied from page 26 of listings manual
116 // FIXME: add default parameters ... (which is not used now)
117 listings_param_info const listings_param_table[] = {
118 { "float", "false", true, SUBSETOF, "tbph", "" },
119 { "floatplacement", "tbp", false, SUBSETOF, "tbph", "" },
120 { "aboveskip", "\\medskipamount", false, LENGTH, "", "" },
121 { "belowskip", "\\medskipamount", false, LENGTH, "", "" },
122 { "lineskip", "", false, LENGTH, "", "" },
123 { "boxpos", "", false, SUBSETOF, "bct", "" },
124 { "print", "", false, TRUEFALSE, "", "" },
125 { "firstline", "", false, INTEGER, "", "" },
126 { "lastline", "", false, INTEGER, "", "" },
127 { "showlines", "", false, TRUEFALSE, "", "" },
128 { "emptylines", "", false, ALL, "", N_("Expect a number with an optional * before it") },
129 { "gobble", "", false, INTEGER, "", "" },
130 { "style", "", false, ALL, "", "" },
131 { "language", "", false, ONEOF, allowed_languages, "" },
132 { "alsolanguage", "", false, ONEOF, allowed_languages, "" },
133 { "defaultdialect", "", false, ONEOF, allowed_languages, "" },
134 { "printpod", "", false, TRUEFALSE, "", "" },
135 { "usekeywordsintag", "", false, TRUEFALSE, "", "" },
136 { "tagstyle", "", false, ALL, "", style_hint },
137 { "markfirstintag", "", false, ALL, "", style_hint },
138 { "makemacrouse", "", false, TRUEFALSE, "", "" },
139 { "basicstyle", "", false, ALL, "", style_hint },
140 { "identifierstyle", "", false, ALL, "", style_hint },
141 { "commentstyle", "", false, ALL, "", style_hint },
142 { "stringstyle", "", false, ALL, "", style_hint },
143 { "keywordstyle", "", false, ALL, "", style_hint },
144 { "ndkeywordstyle", "", false, ALL, "", style_hint },
145 { "classoffset", "", false, INTEGER, "", "" },
146 { "texcsstyle", "", false, ALL, "", style_hint },
147 { "directivestyle", "", false, ALL, "", style_hint },
148 { "emph", "", false, ALL, "", "" },
149 { "moreemph", "", false, ALL, "", "" },
150 { "deleteemph", "", false, ALL, "", "" },
151 { "emphstyle", "", false, ALL, "", "" },
152 { "delim", "", false, ALL, "", "" },
153 { "moredelim", "", false, ALL, "", "" },
154 { "deletedelim", "", false, ALL, "", "" },
155 { "extendedchars", "", false, TRUEFALSE, "", "" },
156 { "inputencoding", "", false, ALL, "", "" },
157 { "upquote", "", false, TRUEFALSE, "", "" },
158 { "tabsize", "", false, INTEGER, "", "" },
159 { "showtabs", "", false, ALL, "", "" },
160 { "tab", "", false, ALL, "", "" },
161 { "showspaces", "", false, TRUEFALSE, "", "" },
162 { "showstringspaces", "", false, TRUEFALSE, "", "" },
163 { "formfeed", "", false, ALL, "", "" },
164 { "numbers", "", false, ONEOF, "none\nleft\nright", "" },
165 { "stepnumber", "", false, INTEGER, "", "" },
166 { "numberfirstline", "", false, TRUEFALSE, "", "" },
167 { "numberstyle", "", false, ALL, "", style_hint },
168 { "numbersep", "", false, LENGTH, "", "" },
169 { "numberblanklines", "", false, ALL, "", "" },
170 { "firstnumber", "", false, ALL, "", N_("auto, last or a number") },
171 { "name", "", false, ALL, "", "" },
172 { "thelstnumber", "", false, ALL, "", "" },
173 { "title", "", false, ALL, "", "" },
174 // this option is not handled in the parameter box
175 { "caption", "", false, ALL, "", N_("This parameter should not be entered here. "
176 "Please use caption editbox (Include dialog) or insert->caption (listings inset)") },
177 // this option is not handled in the parameter box
178 { "label", "", false, ALL, "", N_("This parameter should not be entered here. "
179 "Please use label editbox (Include dialog) or insert->caption (listings inset)") },
180 { "nolol", "", false, TRUEFALSE, "", "" },
181 { "captionpos", "", false, SUBSETOF, "tb", "" },
182 { "abovecaptionskip", "", false, LENGTH, "", "" },
183 { "belowcaptionskip", "", false, LENGTH, "", "" },
184 { "linewidth", "", false, LENGTH, "", "" },
185 { "xleftmargin", "", false, LENGTH, "", "" },
186 { "xrightmargin", "", false, LENGTH, "", "" },
187 { "resetmargins", "", false, TRUEFALSE, "", "" },
188 { "breaklines", "", false, TRUEFALSE, "", "" },
189 { "prebreak", "", false, ALL, "", "" },
190 { "postbreak", "", false, ALL, "", "" },
191 { "breakindent", "", false, LENGTH, "", "" },
192 { "breakautoindent", "", false, TRUEFALSE, "", "" },
193 { "frame", "", false, ALL, "", frame_hint },
194 { "frameround", "", false, SUBSETOF, "tf", frameround_hint },
195 { "framesep", "", false, LENGTH, "", "" },
196 { "rulesep", "", false, LENGTH, "", "" },
197 { "framerule", "", false, LENGTH, "", "" },
198 { "framexleftmargin", "", false, LENGTH, "", "" },
199 { "framexrightmargin", "", false, LENGTH, "", "" },
200 { "framextopmargin", "", false, LENGTH, "", "" },
201 { "framexbottommargin", "", false, LENGTH, "", "" },
202 { "backgroundcolor", "", false, ALL, "", color_hint },
203 { "rulecolor", "", false, ALL, "", color_hint },
204 { "fillcolor", "", false, ALL, "", color_hint },
205 { "rulesepcolor", "", false, ALL, "", color_hint },
206 { "frameshape", "", false, ALL, "", "" },
207 { "index", "", false, ALL, "", "" },
208 { "moreindex", "", false, ALL, "", "" },
209 { "deleteindex", "", false, ALL, "", "" },
210 { "indexstyle", "", false, ALL, "", "" },
211 { "columns", "", false, ALL, "", "" },
212 { "flexiblecolumns", "", false, ALL, "", "" },
213 { "keepspaces", "", false, TRUEFALSE, "", "" },
214 { "basewidth", "", false, LENGTH, "", "" },
215 { "fontadjust", "", true, TRUEFALSE, "", "" },
216 { "texcl", "", false, TRUEFALSE, "", "" },
217 { "mathescape", "", false, TRUEFALSE, "", "" },
218 { "escapechar", "", false, ALL, "", "" },
219 { "escapeinside", "", false, ALL, "", "" },
220 { "escepeinside", "", false, ALL, "", "" },
221 { "escepebegin", "", false, ALL, "", "" },
222 { "escepeend", "", false, ALL, "", "" },
223 { "fancyvrb", "", false, TRUEFALSE, "", "" },
224 { "fvcmdparams", "", false, ALL, "", "" },
225 { "morefvcmdparams", "", false, ALL, "", "" },
226 { "keywordsprefix", "", false, ALL, "", "" },
227 { "keywords", "", false, ALL, "", "" },
228 { "morekeywords", "", false, ALL, "", "" },
229 { "deletekeywords", "", false, ALL, "", "" },
230 { "ndkeywords", "", false, ALL, "", "" },
231 { "morendkeywords", "", false, ALL, "", "" },
232 { "deletendkeywords", "", false, ALL, "", "" },
233 { "texcs", "", false, ALL, "", "" },
234 { "moretexcs", "", false, ALL, "", "" },
235 { "deletetexcs", "", false, ALL, "", "" },
236 { "directives", "", false, ALL, "", "" },
237 { "moredirectives", "", false, ALL, "", "" },
238 { "deletedirectives", "", false, ALL, "", "" },
239 { "sensitive", "", false, ALL, "", "" },
240 { "alsoletter", "", false, ALL, "", "" },
241 { "alsodigit", "", false, ALL, "", "" },
242 { "alsoother", "", false, ALL, "", "" },
243 { "otherkeywords", "", false, ALL, "", "" },
244 { "tag", "", false, ALL, "", "" },
245 { "string", "", false, ALL, "", "" },
246 { "morestring", "", false, ALL, "", "" },
247 { "deletestring", "", false, ALL, "", "" },
248 { "comment", "", false, ALL, "", "" },
249 { "morecomment", "", false, ALL, "", "" },
250 { "deletecomment", "", false, ALL, "", "" },
251 { "keywordcomment", "", false, ALL, "", "" },
252 { "morekeywordcomment", "", false, ALL, "", "" },
253 { "deletekeywordcomment", "", false, ALL, "", "" },
254 { "keywordcommentsemicolon", "", false, ALL, "", "" },
255 { "podcomment", "", false, ALL, "", "" },
256 { "", "", false, ALL, "", ""}
263 parValidator(string const & name);
265 /// validate given parameter
266 /// invalidParam will be thrown if invalid
267 /// parameter is found.
268 void validate(std::string const & par) const;
274 listings_param_info const * info;
278 parValidator::parValidator(string const & n)
282 throw invalidParam(_("Invalid (empty) listings param name."));
283 else if (name == "?") {
286 while (listings_param_table[idx].name != string()) {
289 pars += listings_param_table[idx].name;
292 throw invalidParam(bformat(
293 _("Available listings parameters are %1$s"), from_utf8(pars)));
295 // locate name in parameter table
297 while (listings_param_table[idx].name != name && listings_param_table[idx].name != string())
300 if (listings_param_table[idx].name != "") {
301 info = &listings_param_table[idx];
304 // otherwise, produce a meaningful error message.
305 string matching_names;
306 for (size_t i = 0; i < idx; ++i) {
307 string n(listings_param_table[i].name);
308 if (prefixIs(n, name)) {
309 if (matching_names.empty())
312 matching_names += ", " + n;
315 if (matching_names.empty())
316 throw invalidParam(bformat(_("Unknown listings param name: %1$s"),
319 throw invalidParam(bformat(_("Parameters starting with '%1$s': %2$s"),
320 from_utf8(name), from_utf8(matching_names)));
324 void parValidator::validate(std::string const & par) const
326 bool unclosed = false;
328 // braces are allowed
329 if (prefixIs(par, "{") && suffixIs(par, "}"))
330 par2 = par.substr(1, par.size() - 2);
331 else if (prefixIs(par, "{")) {
332 par2 = par.substr(1);
337 switch (info->type) {
339 if (par2.empty() && !info->onoff) {
340 if (info->hint != "")
341 throw invalidParam(from_utf8(info->hint));
343 throw invalidParam(_("A value is expected"));
346 throw invalidParam(_("Unbalanced braces!"));
349 if (par2.empty() && !info->onoff) {
350 if (info->hint != "")
351 throw invalidParam(from_utf8(info->hint));
353 throw invalidParam(_("Please specify true or false"));
355 if (par2 != "true" && par2 != "false")
356 throw invalidParam(bformat(_("Only true or false is allowed for parameter %1$s"),
359 throw invalidParam(_("Unbalanced braces!"));
363 if (!isStrInt(par2)) {
364 if (info->hint != "")
365 throw invalidParam(from_utf8(info->hint));
367 throw invalidParam(_("Please specify an integer value"));
369 if (convert<int>(par2) == 0 && par2[0] != '0')
370 throw invalidParam(bformat(_("An integer is expected for parameter %1$s"),
373 throw invalidParam(_("Unbalanced braces!"));
377 if (par2.empty() && !info->onoff) {
378 if (info->hint != "")
379 throw invalidParam(from_utf8(info->hint));
381 throw invalidParam(_("Please specify a latex length expression"));
383 if (!isValidLength(par2))
384 throw invalidParam(bformat(_("Invalid latex length expression for parameter %1$s"),
387 throw invalidParam(_("Unbalanced braces!"));
391 if (par2.empty() && !info->onoff) {
392 if (info->hint != "")
393 throw invalidParam(from_utf8(info->hint));
395 throw invalidParam(bformat(_("Please specify one of %1$s"),
396 from_utf8(string(info->info))));
398 // break value to allowed strings
399 vector<string> lists;
401 for (size_t i = 0; info->info[i] != '\0'; ++i) {
402 if (info->info[i] == '\n') {
411 // good, find the string
412 if (std::find(lists.begin(), lists.end(), par2) != lists.end()) {
414 throw invalidParam(_("Unbalanced braces!"));
417 // otherwise, produce a meaningful error message.
418 string matching_names;
419 for (vector<string>::iterator it = lists.begin();
420 it != lists.end(); ++it) {
421 if (it->size() >= par2.size() && it->substr(0, par2.size()) == par2) {
422 if (matching_names.empty())
423 matching_names += *it;
425 matching_names += ", " + *it;
428 if (matching_names.empty())
429 throw invalidParam(bformat(_("Try one of %1$s"),
430 from_utf8(string(info->info))));
432 throw invalidParam(bformat(_("I guess you mean %1$s"),
433 from_utf8(matching_names)));
437 if (par2.empty() && !info->onoff) {
438 if (info->hint != "")
439 throw invalidParam(from_utf8(info->hint));
441 throw invalidParam(bformat(_("Please specify one or more of '%1$s'"),
442 from_utf8(string(info->info))));
444 for (size_t i = 0; i < par2.size(); ++i)
445 if (string(info->info).find(par2[i], 0) == string::npos)
447 bformat(_("Parameter %1$s should be composed of one or more of %2$s"),
448 from_utf8(name), from_utf8(info->info)));
450 throw invalidParam(_("Unbalanced braces!"));
457 InsetListingsParams::InsetListingsParams() :
458 inline_(false), params_(), keys_(0), status_(InsetCollapsable::Open)
463 InsetListingsParams::InsetListingsParams(string const & par, bool in, InsetCollapsable::CollapseStatus s)
464 : inline_(in), params_(), keys_(0), status_(s)
466 // this will activate parameter validation.
467 fromEncodedString(par);
471 void InsetListingsParams::write(ostream & os) const
477 os << status_ << " \"" << encodedString() << "\"";
481 void InsetListingsParams::read(Lexer & lex)
487 status_ = static_cast<InsetCollapsable::CollapseStatus>(s);
490 fromEncodedString(par);
494 void InsetListingsParams::addParam(string const & key, string const & value)
498 // exception may be thown.
499 parValidator(key).validate(value);
500 // duplicate parameters!
501 if (find(keys_.begin(), keys_.end(), key) != keys_.end())
502 throw invalidParam(bformat(_("Parameter %1$s has already been defined"),
505 keys_.push_back(key);
506 if (!params_.empty())
513 while (listings_param_table[idx].name != key)
515 BOOST_ASSERT(listings_param_table[idx].name == key);
516 if (listings_param_table[idx].onoff && value == "false")
519 params_ += key + '=' + value;
524 void InsetListingsParams::addParams(string const & par)
528 bool isValue = false;
530 for (size_t i = 0; i < par.size(); ++i) {
532 if (par[i] == '\n') {
533 addParam(trim(key), trim(value));
538 } else if (par[i] == ',' && braces == 0) {
539 addParam(trim(key), trim(value));
544 } else if (par[i] == '=' && braces == 0) {
547 } else if (par[i] == '{' && par[i - 1] == '=')
549 else if (par[i] == '}' && (i == par.size() - 1 || par[i + 1] == ','))
557 if (!trim(key).empty())
558 addParam(trim(key), trim(value));
562 void InsetListingsParams::setParams(string const & par)
570 string InsetListingsParams::encodedString() const
574 // '"' should be handled differently because it will
575 // terminate a lyx token. Right now, it is silently ignored.
577 for (size_t i = 0; i < params_.size(); ++i) {
578 BOOST_ASSERT(params_[i] != '\n');
579 if (params_[i] != '"')
586 string InsetListingsParams::separatedParams(bool keepComma) const
588 // , might be used as regular parameter option so
589 // the prcess might be more complicated than what I am doing here
592 for (size_t i = 0; i < params_.size(); ++i)
593 if (params_[i] == ',' && braces == 0) {
598 } else if (params_[i] == '{' && params_[i - 1] == '=') {
601 } else if (params_[i] == '}' && (i == params_.size() -1 || params_[i + 1] == ',')) {
610 void InsetListingsParams::fromEncodedString(string const & in)
613 // Do nothing because " was silently ignored.
618 bool InsetListingsParams::isFloat() const
620 return find(keys_.begin(), keys_.end(), "float") != keys_.end();
624 string InsetListingsParams::getParamValue(string const & param) const
626 // is this parameter defined?
627 if (find(keys_.begin(), keys_.end(), param) == keys_.end())
629 // if so, search for it
630 vector<string> pars = getVectorFromString(separatedParams(), "\n");
631 for (vector<string>::iterator it = pars.begin(); it != pars.end(); ++it)
632 if (prefixIs(*it, param + "=")) {
633 string par = it->substr(param.size() + 1);
634 if (prefixIs(par, "{") && suffixIs(par, "}"))
635 return par.substr(1, par.size() - 2);
639 // if param= is not found, should be something like float, return ""