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;
45 /** Information about each parameter
47 struct listings_param_info {
48 /// name of the parameter
52 // for option with value "true", "false",
56 // "other": option="other"
58 // "true": option=true
59 // "false": option=false
69 // info is a \n separated string with allowed values
71 // info is a string from which par is composed of
72 // (e.g. floatplacement can be one or more of tbph)
79 char const * allowed_languages =
80 "no language\nBAP\nACSL\nAda\nALGOL\nC\nC++\nCaml\nClean\nCobol\n"
81 "Comal 80\ncsh\nDelphi\nEiffel\nElan\nEuphoria\nFortran\nHaskell\n"
82 "HTML\nIDL\nJava\nLisp\nLogo\nmake\nMathematica\nMatlab\nMercury\n"
83 "Miranda\nML\nModula-2\nOberon-2\nOCL\nPascal\nPerl\nPHP\nPL/I\nPOV\n"
84 "Python\nProlog\nR\nS\nSAS\nSHELXL\nSimula\ntcl\nSQL\nTeX\nVBScript\n"
87 char const * style_hint = "Use \\footnotessize, \\small, \\itshape, \\ttfamily or something like that";
88 char const * frame_hint = "none, leftline, topline, bottomline, lines, single, shadowbox or subset of trblTRBL";
89 char const * frameround_hint = "The foru letters (t or f) attached to top right, bottom right, bottom left and top left corner.";
90 char const * color_hint = "Enter something like \\color{white}";
92 /// options copied from page 26 of listings manual
93 // FIXME: add default parameters ... (which is not used now)
94 listings_param_info const listings_param_table[] = {
95 { "float", "false", true, SUBSETOF, "tbph", "" },
96 { "floatplacement", "tbp", false, SUBSETOF, "tbph", "" },
97 { "aboveskip", "\\medskipamount", false, LENGTH, "", "" },
98 { "belowskip", "\\medskipamount", false, LENGTH, "", "" },
99 { "lineskip", "", false, LENGTH, "", "" },
100 { "boxpos", "", false, SUBSETOF, "bct", "" },
101 { "print", "", false, TRUEFALSE, "", "" },
102 { "firstline", "", false, INTEGER, "", "" },
103 { "lastline", "", false, INTEGER, "", "" },
104 { "showlines", "", false, TRUEFALSE, "", "" },
105 { "emptylines", "", false, ALL, "", "Expect a number with an optional * before it" },
106 { "gobble", "", false, INTEGER, "", "" },
107 { "style", "", false, ALL, "", "" },
108 { "language", "", false, ONEOF, allowed_languages, "" },
109 { "alsolanguage", "", false, ONEOF, allowed_languages, "" },
110 { "defaultdialect", "", false, ONEOF, allowed_languages, "" },
111 { "printpod", "", false, TRUEFALSE, "", "" },
112 { "usekeywordsintag", "", false, TRUEFALSE, "", "" },
113 { "tagstyle", "", false, ALL, "", style_hint },
114 { "markfirstintag", "", false, ALL, "", style_hint },
115 { "makemacrouse", "", false, TRUEFALSE, "", "" },
116 { "basicstyle", "", false, ALL, "", style_hint },
117 { "identifierstyle", "", false, ALL, "", style_hint },
118 { "commentstyle", "", false, ALL, "", style_hint },
119 { "stringstyle", "", false, ALL, "", style_hint },
120 { "keywordstyle", "", false, ALL, "", style_hint },
121 { "ndkeywordstyle", "", false, ALL, "", style_hint },
122 { "classoffset", "", false, INTEGER, "", "" },
123 { "texcsstyle", "", false, ALL, "", style_hint },
124 { "directivestyle", "", false, ALL, "", style_hint },
125 { "emph", "", false, ALL, "", "" },
126 { "moreemph", "", false, ALL, "", "" },
127 { "deleteemph", "", false, ALL, "", "" },
128 { "emphstyle", "", false, ALL, "", "" },
129 { "delim", "", false, ALL, "", "" },
130 { "moredelim", "", false, ALL, "", "" },
131 { "deletedelim", "", false, ALL, "", "" },
132 { "extendedchars", "", false, TRUEFALSE, "", "" },
133 { "inputencoding", "", false, ALL, "", "" },
134 { "upquote", "", false, TRUEFALSE, "", "" },
135 { "tabsize", "", false, INTEGER, "", "" },
136 { "showtabs", "", false, ALL, "", "" },
137 { "tab", "", false, ALL, "", "" },
138 { "showspaces", "", false, TRUEFALSE, "", "" },
139 { "showstringspaces", "", false, TRUEFALSE, "", "" },
140 { "formfeed", "", false, ALL, "", "" },
141 { "numbers", "", false, ONEOF, "none\nleft\nright", "" },
142 { "stepnumber", "", false, INTEGER, "", "" },
143 { "numberfirstline", "", false, TRUEFALSE, "", "" },
144 { "numberstyle", "", false, ALL, "", style_hint },
145 { "numbersep", "", false, LENGTH, "", "" },
146 { "numberblanklines", "", false, ALL, "", "" },
147 { "firstnumber", "", false, ALL, "", "auto, last or a number" },
148 { "name", "", false, ALL, "", "" },
149 { "thelstnumber", "", false, ALL, "", "" },
150 { "title", "", false, ALL, "", "" },
151 { "caption", "", false, ALL, "", "" },
152 { "label", "", false, ALL, "", "" },
153 { "nolol", "", false, TRUEFALSE, "", "" },
154 { "captionpos", "", false, SUBSETOF, "tb", "" },
155 { "abovecaptionskip", "", false, LENGTH, "", "" },
156 { "belowcaptionskip", "", false, LENGTH, "", "" },
157 { "linewidth", "", false, LENGTH, "", "" },
158 { "xleftmargin", "", false, LENGTH, "", "" },
159 { "xrightmargin", "", false, LENGTH, "", "" },
160 { "resetmargins", "", false, TRUEFALSE, "", "" },
161 { "breaklines", "", false, TRUEFALSE, "", "" },
162 { "prebreak", "", false, ALL, "", "" },
163 { "postbreak", "", false, ALL, "", "" },
164 { "breakindent", "", false, LENGTH, "", "" },
165 { "breakautoindent", "", false, TRUEFALSE, "", "" },
166 { "frame", "", false, ALL, "", frame_hint },
167 { "frameround", "", false, SUBSETOF, "tf", frameround_hint },
168 { "framesep", "", false, LENGTH, "", "" },
169 { "rulesep", "", false, LENGTH, "", "" },
170 { "framerule", "", false, LENGTH, "", "" },
171 { "framexleftmargin", "", false, LENGTH, "", "" },
172 { "framexrightmargin", "", false, LENGTH, "", "" },
173 { "framextopmargin", "", false, LENGTH, "", "" },
174 { "framexbottommargin", "", false, LENGTH, "", "" },
175 { "backgroundcolor", "", false, ALL, "", color_hint },
176 { "rulecolor", "", false, ALL, "", color_hint },
177 { "fillcolor", "", false, ALL, "", color_hint },
178 { "rulesepcolor", "", false, ALL, "", color_hint },
179 { "frameshape", "", false, ALL, "", "" },
180 { "index", "", false, ALL, "", "" },
181 { "moreindex", "", false, ALL, "", "" },
182 { "deleteindex", "", false, ALL, "", "" },
183 { "indexstyle", "", false, ALL, "", "" },
184 { "columns", "", false, ALL, "", "" },
185 { "flexiblecolumns", "", false, ALL, "", "" },
186 { "keepspaces", "", false, TRUEFALSE, "", "" },
187 { "basewidth", "", false, LENGTH, "", "" },
188 { "fontadjust", "", true, TRUEFALSE, "", "" },
189 { "texcl", "", false, TRUEFALSE, "", "" },
190 { "mathescape", "", false, TRUEFALSE, "", "" },
191 { "escapechar", "", false, ALL, "", "" },
192 { "escapeinside", "", false, ALL, "", "" },
193 { "escepeinside", "", false, ALL, "", "" },
194 { "escepebegin", "", false, ALL, "", "" },
195 { "escepeend", "", false, ALL, "", "" },
196 { "fancyvrb", "", false, TRUEFALSE, "", "" },
197 { "fvcmdparams", "", false, ALL, "", "" },
198 { "morefvcmdparams", "", false, ALL, "", "" },
199 { "keywordsprefix", "", false, ALL, "", "" },
200 { "keywords", "", false, ALL, "", "" },
201 { "morekeywords", "", false, ALL, "", "" },
202 { "deletekeywords", "", false, ALL, "", "" },
203 { "ndkeywords", "", false, ALL, "", "" },
204 { "morendkeywords", "", false, ALL, "", "" },
205 { "deletendkeywords", "", false, ALL, "", "" },
206 { "texcs", "", false, ALL, "", "" },
207 { "moretexcs", "", false, ALL, "", "" },
208 { "deletetexcs", "", false, ALL, "", "" },
209 { "directives", "", false, ALL, "", "" },
210 { "moredirectives", "", false, ALL, "", "" },
211 { "deletedirectives", "", false, ALL, "", "" },
212 { "sensitive", "", false, ALL, "", "" },
213 { "alsoletter", "", false, ALL, "", "" },
214 { "alsodigit", "", false, ALL, "", "" },
215 { "alsoother", "", false, ALL, "", "" },
216 { "otherkeywords", "", false, ALL, "", "" },
217 { "tag", "", false, ALL, "", "" },
218 { "string", "", false, ALL, "", "" },
219 { "morestring", "", false, ALL, "", "" },
220 { "deletestring", "", false, ALL, "", "" },
221 { "comment", "", false, ALL, "", "" },
222 { "morecomment", "", false, ALL, "", "" },
223 { "deletecomment", "", false, ALL, "", "" },
224 { "keywordcomment", "", false, ALL, "", "" },
225 { "morekeywordcomment", "", false, ALL, "", "" },
226 { "deletekeywordcomment", "", false, ALL, "", "" },
227 { "keywordcommentsemicolon", "", false, ALL, "", "" },
228 { "podcomment", "", false, ALL, "", "" },
229 { "", "", false, ALL, "", ""}
236 parValidator(string const & name);
238 /// validate given parameter
239 /// invalidParam will be thrown if invalid
240 /// parameter is found.
241 void validate(std::string const & par) const;
247 listings_param_info const * info;
251 parValidator::parValidator(string const & n)
255 throw invalidParam("Invalid (empty) listings param name.");
256 else if (name == "?") {
259 while (listings_param_table[idx].name != string()) {
262 pars += listings_param_table[idx].name;
265 throw invalidParam("Available listings parameters are " + pars);
267 // locate name in parameter table
269 while (listings_param_table[idx].name != name && listings_param_table[idx].name != string())
272 if (listings_param_table[idx].name != "") {
273 info = &listings_param_table[idx];
276 // otherwise, produce a meaningful error message.
277 string matching_names;
278 for (size_t i = 0; i < idx; ++i) {
279 string n(listings_param_table[i].name);
280 if (n.size() >= name.size() && n.substr(0, name.size()) == name) {
281 if (matching_names.empty())
284 matching_names += ", " + n;
287 if (matching_names.empty())
288 throw invalidParam("Unknown listings param name: " + name);
290 throw invalidParam("Parameters starting with '" + name +
291 "': " + matching_names);
295 void parValidator::validate(std::string const & par) const
297 switch (info->type) {
299 if (par.empty() && !info->onoff) {
300 if (info->hint != "")
301 throw invalidParam(info->hint);
303 throw invalidParam("An value is expected");
307 if (par.empty() && !info->onoff) {
308 if (info->hint != "")
309 throw invalidParam(info->hint);
311 throw invalidParam("Please specify true or false");
313 if (par != "true" && par != "false")
314 throw invalidParam("Only true or false is allowed for parameter" + name);
318 if (!isStrInt(par)) {
319 if (info->hint != "")
320 throw invalidParam(info->hint);
322 throw invalidParam("Please specify an integer value");
324 if (convert<int>(par) == 0 && par[0] != '0')
325 throw invalidParam("An integer is expected for parameter " + name);
329 if (par.empty() && !info->onoff) {
330 if (info->hint != "")
331 throw invalidParam(info->hint);
333 throw invalidParam("Please specify a latex length expression");
335 if (!isValidLength(par))
336 throw invalidParam("Invalid latex length expression for parameter " + name);
340 if (par.empty() && !info->onoff) {
341 if (info->hint != "")
342 throw invalidParam(info->hint);
344 throw invalidParam("Please specify one of " + string(info->info));
346 // break value to allowed strings
347 vector<string> lists;
349 for (size_t i = 0; info->info[i] != '\0'; ++i) {
350 if (info->info[i] == '\n') {
359 // good, find the string
360 if (std::find(lists.begin(), lists.end(), par) != lists.end())
362 // otherwise, produce a meaningful error message.
363 string matching_names;
364 for (vector<string>::iterator it = lists.begin();
365 it != lists.end(); ++it) {
366 if (it->size() >= par.size() && it->substr(0, par.size()) == par) {
367 if (matching_names.empty())
368 matching_names += *it;
370 matching_names += ", " + *it;
373 if (matching_names.empty())
374 throw invalidParam("Try one of " + string(info->info));
376 throw invalidParam("I guess you mean " + matching_names);
380 if (par.empty() && !info->onoff) {
381 if (info->hint != "")
382 throw invalidParam(info->hint);
384 throw invalidParam("Please specify one or more of " + string(info->info));
386 for (size_t i = 0; i < par.size(); ++i)
387 if (string(info->info).find(par[i], 0) == string::npos)
388 throw invalidParam("Parameter " + name +
389 " should be composed of one or more of " + info->info);
396 InsetListingsParams::InsetListingsParams() :
397 inline_(false), status_(InsetCollapsable::Open), params_()
402 InsetListingsParams::InsetListingsParams(string const & par, bool in, InsetCollapsable::CollapseStatus s)
403 : inline_(in), status_(s)
405 // this will activate parameter validation.
406 fromEncodedString(par);
410 void InsetListingsParams::write(ostream & os) const
416 os << status_ << " \"" << encodedString() << "\"";
420 void InsetListingsParams::read(Lexer & lex)
426 status_ = static_cast<InsetCollapsable::CollapseStatus>(s);
429 fromEncodedString(par);
433 void InsetListingsParams::addParam(string const & key, string const & value)
437 // exception may be thown.
438 parValidator(key.c_str()).validate(value);
439 // duplicate parameters!
440 if (find(keys_.begin(), keys_.end(), key) != keys_.end())
441 throw invalidParam("Parameter " + key + " has already been defined");
443 keys_.push_back(key);
444 if (!params_.empty())
451 while (listings_param_table[idx].name != key)
453 BOOST_ASSERT(listings_param_table[idx].name == key);
454 if (listings_param_table[idx].onoff && value == "false")
457 params_ += key + '=' + value;
462 void InsetListingsParams::addParams(string const & par)
466 bool isValue = false;
468 for (size_t i = 0; i < par.size(); ++i) {
470 if (par[i] == '\n') {
471 addParam(trim(key), trim(value));
476 } else if (par[i] == ',' && braces == 0) {
477 addParam(trim(key), trim(value));
482 } else if (par[i] == '=' && braces == 0) {
485 } else if (par[i] == '{' && par[i - 1] == '=')
487 else if (par[i] == '}' && (i == par.size() - 1 || par[i + 1] == ','))
495 if (!trim(key).empty())
496 addParam(trim(key), trim(value));
500 void InsetListingsParams::setParams(string const & par)
508 string InsetListingsParams::encodedString() const
512 // '"' should be handled differently because it will
513 // terminate a lyx token. Right now, it is silently ignored.
515 for (size_t i = 0; i < params_.size(); ++i) {
516 BOOST_ASSERT(params_[i] != '\n');
517 if (params_[i] != '"')
524 string InsetListingsParams::separatedParams(bool keepComma) const
526 // , might be used as regular parameter option so
527 // the prcess might be more complicated than what I am doing here
530 for (size_t i = 0; i < params_.size(); ++i)
531 if (params_[i] == ',' && braces == 0) {
536 } else if (params_[i] == '{' && params_[i - 1] == '=') {
539 } else if (params_[i] == '}' && (i == params_.size() -1 || params_[i + 1] == ',')) {
548 void InsetListingsParams::fromEncodedString(string const & in)
551 // Do nothing because " was silently ignored.