]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetListingsParams.cpp
Forgot to restore this before committing 18494. This fixes bug 3725.
[lyx.git] / src / insets / InsetListingsParams.cpp
index dac5309e92d032dae7840f8d6d24a86f66c3bebc..424fdcf4f5d194adc9255890ee3a2932111c8d1a 100644 (file)
@@ -26,18 +26,23 @@ using std::vector;
 using std::ostream;
 using std::string;
 using std::exception;
+using lyx::support::bformat;
 using lyx::support::trim;
+using lyx::support::isStrInt;
+using lyx::support::prefixIs;
+using lyx::support::suffixIs;
+using lyx::support::getVectorFromString;
 
 namespace lyx
 {
 
 enum param_type {
-       ALL,
-       TRUEFALSE,
-       INTEGER,
-       LENGTH,
-       ONEOF,
-       SUBSETOF,
+       ALL,  // accept all
+       TRUEFALSE, // accept 'true' or 'false'
+       INTEGER, // accept an integer
+       LENGTH,  // accept a latex length
+       ONEOF,  // accept one of a few values
+       SUBSETOF, // accept a string composed of given characters
 };
 
 
@@ -48,7 +53,7 @@ struct listings_param_info {
        char const * name;
        /// default value
        char const * value;
-       // for option with value "true", "false", 
+       // for option with value "true", "false",
        // if onoff is true,
        //   "true":  option
        //   "false": 
@@ -58,7 +63,6 @@ struct listings_param_info {
        //   "false": option=false
        bool onoff;
        /// validator type
-       param_type type;
        // ALL:
        // TRUEFALSE:
        // INTEGER:
@@ -69,24 +73,44 @@ struct listings_param_info {
        // SUBSETOF
        //     info is a string from which par is composed of
        //     (e.g. floatplacement can be one or more of tbph)
+       param_type type;
+       /// parameter info, meaning depending on parameter type
        char const * info;
-       //
+       /// a help message that is displayed in the gui
        char const * hint;
 };
 
 
+
+/// languages and language/dialect combinations
 char const * allowed_languages = 
-       "no language\nBAP\nACSL\nAda\nALGOL\nC\nC++\nCaml\nClean\nCobol\n"
-       "Comal 80\ncsh\nDelphi\nEiffel\nElan\nEuphoria\nFortran\nHaskell\n"
-       "HTML\nIDL\nJava\nLisp\nLogo\nmake\nMathematica\nMatlab\nMercury\n"
-       "Miranda\nML\nModula-2\nOberon-2\nOCL\nPascal\nPerl\nPHP\nPL/I\nPOV\n"
-       "Python\nProlog\nR\nS\nSAS\nSHELXL\nSimula\ntcl\nSQL\nTeX\nVBScript\n"
-       "VHDL\nXML";
-
-char const * style_hint = "Use \\footnotessize, \\small, \\itshape, \\ttfamily or something like that";
-char const * frame_hint = "none, leftline, topline, bottomline, lines, single, shadowbox or subset of trblTRBL";
-char const * frameround_hint = "The foru letters (t or f) attached to top right, bottom right, bottom left and top left corner.";
-char const * color_hint = "Enter something like \\color{white}";
+       "no language\nABAP\n[R/2 4.3]ABAP\n[R/2 5.0]ABAP\n[R/3 3.1]ABAP\n"
+       "[R/3 4.6C]ABAP\n[R/3 6.10]ABAP\nACSL\nAda\n[2005]Ada\n[83]Ada\n"
+       "[95]Ada\nALGOL\n[60]ALGOL\n[68]ALGOL\nAssembler\n"
+       "[Motorola68k]Assembler\n[x86masm]Assembler\nAwk\n[gnu]Awk\n[POSIX]Awk\n"
+       "bash\nBasic\n[Visual]Basic\nC\n[ANSI]C\n[Handel]C\n[Objective]C\n"
+       "[Sharp]C\nC++\n[ANSI]C++\n[GNU]C++\n[ISO]C++\n[Visual]C++\nCaml\n"
+       "[light]Caml\n[Objective]Caml\nClean\nCobol\n[1974]Cobol\n[1985]Cobol\n"
+       "[ibm]Cobol\nComal 80\ncommand.com\n[WinXP]command.com\nComsol\ncsh\n"
+       "Delphi\nEiffel\nElan\nEuphoria\nFortran\n[77]Fortran\n[90]Fortran\n"
+       "[95]Fortran\nGCL\nGnuplot\nHaskell\nHTML\nIDL\n[CORBA]IDL\ninform\n"
+       "Java\n[AspectJ]Java\nJVMIS\nksh\nLingo\nLisp\n[Auto]Lisp\nLogo\n"
+       "make\n[gnu]make\nMathematica\n[1.0]Mathematica\n[3.0]Mathematica\n"
+       "[5.2]Mathematica\nMatlab\nMercury\nMetaPost\nMiranda\nMizar\nML\n"
+       "Modula-2\nMuPAD\nNASTRAN\nOberon-2\nOCL\n[decorative]OCL\n[OMG]OCL\n"
+       "Octave\nOz\nPascal\n[Borland6]Pascal\n[Standard]Pascal\n[XSC]Pascal\n"
+       "Perl\nPHP\nPL/I\nPlasm\nPostScript\nPOV\nProlog\nPromela\nPSTricks\n"
+       "Python\nR\nReduce\nRexx\nRSL\nRuby\nS\n[PLUS]S\nSAS\nScilab\nsh\n"
+       "SHELXL\nSimula\n[67]Simula\n[CII]Simula\n[DEC]Simula\n[IBM]Simula\n"
+       "SPARQL\nSQL\ntcl\n[tk]tcl\nTeX\n[AlLaTeX]TeX\n[common]TeX\n[LaTeX]TeX\n"
+       "[plain]TeX\n[primitive]TeX\nVBScript\nVerilog\nVHDL\n[AMS]VHDL\nVRML\n"
+       "[97]VRML\nXML\nXSLT";
+
+char const * style_hint = N_("Use \\footnotesize, \\small, \\itshape, \\ttfamily or something like that");
+char const * frame_hint = N_("none, leftline, topline, bottomline, lines, single, shadowbox or subset of trblTRBL");
+char const * frameround_hint =
+       N_("Enter four letters (either t = round or f = square) for top right, bottom right, bottom left and top left corner.");
+char const * color_hint = N_("Enter something like \\color{white}");
 
 /// options copied from page 26 of listings manual
 // FIXME: add default parameters ... (which is not used now)
@@ -101,7 +125,7 @@ listings_param_info const listings_param_table[] = {
        { "firstline", "", false, INTEGER, "", "" },
        { "lastline", "", false, INTEGER, "", "" },
        { "showlines", "", false, TRUEFALSE, "", "" },
-       { "emptylines", "", false, ALL, "", "Expect a number with an optional * before it" },
+       { "emptylines", "", false, ALL, "", N_("Expect a number with an optional * before it") },
        { "gobble", "", false, INTEGER, "", "" },
        { "style", "", false, ALL, "", "" },
        { "language", "", false, ONEOF, allowed_languages, "" },
@@ -143,12 +167,16 @@ listings_param_info const listings_param_table[] = {
        { "numberstyle", "", false, ALL, "", style_hint },
        { "numbersep", "", false, LENGTH, "", "" },
        { "numberblanklines", "", false, ALL, "", "" },
-       { "firstnumber", "", false, ALL, "", "auto, last or a number" },
+       { "firstnumber", "", false, ALL, "", N_("auto, last or a number") },
        { "name", "", false, ALL, "", "" },
        { "thelstnumber", "", false, ALL, "", "" },
        { "title", "", false, ALL, "", "" },
-       { "caption", "", false, ALL, "", "" },
-       { "label", "", false, ALL, "", "" },
+       // this option is not handled in the parameter box
+       { "caption", "", false, ALL, "", N_("This parameter should not be entered here. "
+               "Please use caption editbox (Include dialog) or insert->caption (listings inset)") },
+       // this option is not handled in the parameter box
+       { "label", "", false, ALL, "", N_("This parameter should not be entered here. "
+               "Please use label editbox (Include dialog) or insert->caption (listings inset)") },
        { "nolol", "", false, TRUEFALSE, "", "" },
        { "captionpos", "", false, SUBSETOF, "tb", "" },
        { "abovecaptionskip", "", false, LENGTH, "", "" },
@@ -251,7 +279,7 @@ parValidator::parValidator(string const & n)
        : name(n), info(0)
 {
        if (name.empty())
-               throw invalidParam("Invalid (empty) listings param name.");
+               throw invalidParam(_("Invalid (empty) listings param name."));
        else if (name == "?") {
                string pars;
                size_t idx = 0;
@@ -261,7 +289,8 @@ parValidator::parValidator(string const & n)
                        pars += listings_param_table[idx].name;
                        ++idx;
                }
-               throw invalidParam("Available listings parameters are " + pars);
+               throw invalidParam(bformat(
+                       _("Available listings parameters are %1$s"), from_utf8(pars)));
        }
        // locate name in parameter table
        size_t idx = 0;
@@ -276,7 +305,7 @@ parValidator::parValidator(string const & n)
        string matching_names;
        for (size_t i = 0; i < idx; ++i) {
                string n(listings_param_table[i].name);
-               if (n.size() >= name.size() && n.substr(0, name.size()) == name) {
+               if (prefixIs(n, name)) {
                        if (matching_names.empty())
                                matching_names += n;
                        else
@@ -284,63 +313,87 @@ parValidator::parValidator(string const & n)
                }
        }
        if (matching_names.empty())
-               throw invalidParam("Unknown listings param name: " + name);
+               throw invalidParam(bformat(_("Unknown listings param name: %1$s"),
+                                                   from_utf8(name)));
        else
-               throw invalidParam("Parameters starting with '" + name + 
-                       "': " + matching_names);
+               throw invalidParam(bformat(_("Parameters starting with '%1$s': %2$s"),
+                                                   from_utf8(name), from_utf8(matching_names)));
 }
 
 
 void parValidator::validate(std::string const & par) const
 {
+       bool unclosed = false;
+       string par2 = par;
+       // braces are allowed
+       if (prefixIs(par, "{") && suffixIs(par, "}"))
+               par2 = par.substr(1, par.size() - 2);
+       else if (prefixIs(par, "{")) {
+               par2 = par.substr(1);
+               unclosed = true;
+       }
+       
+               
        switch (info->type) {
        case ALL:
-               if (par.empty() && !info->onoff) {
+               if (par2.empty() && !info->onoff) {
                        if (info->hint != "")
-                               throw invalidParam(info->hint);
+                               throw invalidParam(from_utf8(info->hint));
                        else
-                               throw invalidParam("An value is expected");
+                               throw invalidParam(_("A value is expected"));
                }
+               if (unclosed)
+                               throw invalidParam(_("Unbalanced braces!"));
                return;
        case TRUEFALSE: {
-               if (par.empty() && !info->onoff) {
+               if (par2.empty() && !info->onoff) {
                        if (info->hint != "")
-                               throw invalidParam(info->hint);
+                               throw invalidParam(from_utf8(info->hint));
                        else
-                               throw invalidParam("Please specify true or false");
+                               throw invalidParam(_("Please specify true or false"));
                }
-               if (par != "true" && par != "false")
-                       throw invalidParam("Only true or false is allowed for parameter" + name);
+               if (par2 != "true" && par2 != "false")
+                       throw invalidParam(bformat(_("Only true or false is allowed for parameter %1$s"),
+                                                  from_utf8(name)));
+               if (unclosed)
+                               throw invalidParam(_("Unbalanced braces!"));
                return;
        }
        case INTEGER: {
-               if (par.empty() && !info->onoff) {
+               if (!isStrInt(par2)) {
                        if (info->hint != "")
-                               throw invalidParam(info->hint);
+                               throw invalidParam(from_utf8(info->hint));
                        else
-                               throw invalidParam("Please specify an integer value");
+                               throw invalidParam(_("Please specify an integer value"));
                }
-               if (convert<int>(par) == 0 && par[0] != '0')
-                       throw invalidParam("An integer is expected for parameter " + name);
+               if (convert<int>(par2) == 0 && par2[0] != '0')
+                       throw invalidParam(bformat(_("An integer is expected for parameter %1$s"),
+                                                  from_utf8(name)));
+               if (unclosed)
+                               throw invalidParam(_("Unbalanced braces!"));
                return;
        }
        case LENGTH: {
-               if (par.empty() && !info->onoff) {
+               if (par2.empty() && !info->onoff) {
                        if (info->hint != "")
-                               throw invalidParam(info->hint);
+                               throw invalidParam(from_utf8(info->hint));
                        else
-                               throw invalidParam("Please specify a latex length expression");
+                               throw invalidParam(_("Please specify a latex length expression"));
                }
-               if (!isValidLength(par))
-                       throw invalidParam("Invalid latex length expression for parameter " + name);
+               if (!isValidLength(par2))
+                       throw invalidParam(bformat(_("Invalid latex length expression for parameter %1$s"),
+                                                  from_utf8(name)));
+               if (unclosed)
+                               throw invalidParam(_("Unbalanced braces!"));
                return;
        }
        case ONEOF: {
-               if (par.empty() && !info->onoff) {
+               if (par2.empty() && !info->onoff) {
                        if (info->hint != "")
-                               throw invalidParam(info->hint);
+                               throw invalidParam(from_utf8(info->hint));
                        else
-                               throw invalidParam("Please specify one of " + string(info->info));
+                               throw invalidParam(bformat(_("Please specify one of %1$s"),
+                                                          from_utf8(string(info->info))));
                }
                // break value to allowed strings
                vector<string> lists;
@@ -356,13 +409,16 @@ void parValidator::validate(std::string const & par) const
                        lists.push_back(v);
 
                // good, find the string
-               if (std::find(lists.begin(), lists.end(), par) != lists.end())
+               if (std::find(lists.begin(), lists.end(), par2) != lists.end()) {
+                       if (unclosed)
+                               throw invalidParam(_("Unbalanced braces!"));
                        return;
+               }
                // otherwise, produce a meaningful error message.
                string matching_names;
                for (vector<string>::iterator it = lists.begin(); 
                        it != lists.end(); ++it) {
-                       if (it->size() >= par.size() && it->substr(0, par.size()) == par) {
+                       if (it->size() >= par2.size() && it->substr(0, par2.size()) == par2) {
                                if (matching_names.empty())
                                        matching_names += *it;
                                else
@@ -370,22 +426,28 @@ void parValidator::validate(std::string const & par) const
                        }
                }
                if (matching_names.empty())
-                       throw invalidParam("Try one of " + string(info->info));
+                       throw invalidParam(bformat(_("Try one of %1$s"),
+                                                  from_utf8(string(info->info))));
                else
-                       throw invalidParam("I guess you mean " + matching_names);
+                       throw invalidParam(bformat(_("I guess you mean %1$s"),
+                                                  from_utf8(matching_names)));
                return;
        }
        case SUBSETOF: {
-               if (par.empty() && !info->onoff) {
+               if (par2.empty() && !info->onoff) {
                        if (info->hint != "")
-                               throw invalidParam(info->hint);
+                               throw invalidParam(from_utf8(info->hint));
                        else
-                               throw invalidParam("Please specify one or more of " + string(info->info));
+                               throw invalidParam(bformat(_("Please specify one or more of '%1$s'"),
+                                                          from_utf8(string(info->info))));
                }
-               for (size_t i = 0; i < par.size(); ++i)
-                       if (string(info->info).find(par[i], 0) == string::npos)
-                               throw invalidParam("Parameter " + name + 
-                                       " should be composed of one or more of " + info->info);
+               for (size_t i = 0; i < par2.size(); ++i)
+                       if (string(info->info).find(par2[i], 0) == string::npos)
+                               throw invalidParam(
+                                       bformat(_("Parameter %1$s should be composed of one or more of %2$s"),
+                                               from_utf8(name), from_utf8(info->info)));
+               if (unclosed)
+                               throw invalidParam(_("Unbalanced braces!"));
                return;
        }
        }
@@ -393,13 +455,13 @@ void parValidator::validate(std::string const & par) const
 
 
 InsetListingsParams::InsetListingsParams() :
-       inline_(false), status_(InsetCollapsable::Open), params_()
+       inline_(false), params_(), keys_(0), status_(InsetCollapsable::Open)
 {
 }
 
 
 InsetListingsParams::InsetListingsParams(string const & par, bool in, InsetCollapsable::CollapseStatus s)
-       : inline_(in), status_(s)
+       : inline_(in), params_(), keys_(0), status_(s)
 {
        // this will activate parameter validation.
        fromEncodedString(par);
@@ -434,7 +496,13 @@ void InsetListingsParams::addParam(string const & key, string const & value)
        if (key.empty())
                return;
        // exception may be thown.
-       parValidator(key.c_str()).validate(value);
+       parValidator(key).validate(value);
+       // duplicate parameters!
+       if (find(keys_.begin(), keys_.end(), key) != keys_.end())
+               throw invalidParam(bformat(_("Parameter %1$s has already been defined"),
+                                         from_utf8(key)));
+       else
+               keys_.push_back(key);
        if (!params_.empty())
                params_ += ',';
        if (value.empty())
@@ -453,22 +521,35 @@ void InsetListingsParams::addParam(string const & key, string const & value)
 }
 
 
-void InsetListingsParams::setParams(string const & par)
+void InsetListingsParams::addParams(string const & par)
 {
        string key;
        string value;
        bool isValue = false;
-       params_.clear();
+       int braces = 0;
        for (size_t i = 0; i < par.size(); ++i) {
                // end of par
-               if (par[i] == '\n' || par[i] == ',') {
+               if (par[i] == '\n') {
                        addParam(trim(key), trim(value));
                        key = string();
                        value = string();
                        isValue = false;
-               } else if (par[i] == '=')
+                       continue;
+               } else if (par[i] == ',' && braces == 0) {
+                       addParam(trim(key), trim(value));
+                       key = string();
+                       value = string();
+                       isValue = false;
+                       continue;
+               } else if (par[i] == '=' && braces == 0) {
                        isValue = true;
-               else if (isValue)
+                       continue;
+               } else if (par[i] == '{' && par[i - 1] == '=')
+                       braces ++;
+               else if (par[i] == '}' && (i == par.size() - 1 || par[i + 1] == ','))
+                       braces --;
+               
+               if (isValue)
                        value += par[i];
                else
                        key += par[i];
@@ -478,6 +559,14 @@ void InsetListingsParams::setParams(string const & par)
 }
 
 
+void InsetListingsParams::setParams(string const & par)
+{
+       params_.clear();
+       keys_.clear();
+       addParams(par);
+}
+
+
 string InsetListingsParams::encodedString() const
 {
        // Encode string!
@@ -499,12 +588,19 @@ string InsetListingsParams::separatedParams(bool keepComma) const
        // , might be used as regular parameter option so 
        // the prcess might be more complicated than what I am doing here
        string opt;
+       int braces = 0;
        for (size_t i = 0; i < params_.size(); ++i)
-               if (params_[i] == ',') {
+               if (params_[i] == ',' && braces == 0) {
                        if (keepComma)
                                opt += ",\n";
                        else
                                opt += "\n";
+               } else if (params_[i] == '{' && params_[i - 1] == '=') {
+                       braces ++;
+                       opt += params_[i];
+               } else if (params_[i] == '}' && (i == params_.size() -1 || params_[i + 1] == ',')) {
+                       braces --;
+                       opt += params_[i];
                } else
                        opt += params_[i];
        return opt;
@@ -519,5 +615,30 @@ void InsetListingsParams::fromEncodedString(string const & in)
 }
 
 
+bool InsetListingsParams::isFloat() const
+{
+       return find(keys_.begin(), keys_.end(), "float") != keys_.end();
+}
+
+
+string InsetListingsParams::getParamValue(string const & param) const
+{
+       // is this parameter defined?
+       if (find(keys_.begin(), keys_.end(), param) == keys_.end())
+               return string();
+       // if so, search for it
+       vector<string> pars = getVectorFromString(separatedParams(), "\n");
+       for (vector<string>::iterator it = pars.begin(); it != pars.end(); ++it)
+               if (prefixIs(*it, param + "=")) {
+                       string par = it->substr(param.size() + 1);
+                       if (prefixIs(par, "{") && suffixIs(par, "}"))
+                               return par.substr(1, par.size() - 2);
+                       else
+                               return par;
+               }
+       // if param= is not found, should be something like float, return ""
+       return string();
+}
+
 
 } // namespace lyx