]> git.lyx.org Git - features.git/blobdiff - src/insets/InsetListingsParams.cpp
Regenerate previews after zoom (#11919)
[features.git] / src / insets / InsetListingsParams.cpp
index bab238ae1ee91a6e728da67c627aea074c9237e2..9390d6bbe4549867005cfe5e2ed3b2c8add7cecf 100644 (file)
@@ -13,7 +13,7 @@
 
 #include "InsetListingsParams.h"
 
-#include "Length.h"
+#include "support/Length.h"
 #include "Lexer.h"
 
 #include "support/convert.h"
@@ -55,7 +55,7 @@ public:
                string const & i, docstring const & h)
                : value_(v), onoff_(o), type_(t), info_(i), hint_(h)
        {}
-       /// Validate a paramater.
+       /// Validate a parameter.
        /// \retval an empty string if \c par is valid.
        /// \retval otherwise an explanation WRT to \c par invalidity.
        docstring validate(string const & par) const;
@@ -102,7 +102,7 @@ docstring ListingsParam::validate(string const & par) const
        bool unclosed = false;
        string par2 = par;
        // braces are allowed
-       if (prefixIs(par, "{") && suffixIs(par, "}") && !suffixIs(par, "\\}"))  
+       if (prefixIs(par, "{") && suffixIs(par, "}") && !suffixIs(par, "\\}"))
                par2 = par.substr(1, par.size() - 2);
 
        // check for unmatched braces
@@ -114,7 +114,7 @@ docstring ListingsParam::validate(string const & par) const
                        --braces;
        }
        unclosed = braces != 0;
-       
+
        switch (type_) {
 
        case ALL:
@@ -232,8 +232,8 @@ docstring ListingsParam::validate(string const & par) const
                                return bformat(_("Please specify one or more of '%1$s'."),
                                                           from_utf8(info_));
                }
-               for (size_t i = 0; i < par2.size(); ++i)
-                       if (info_.find(par2[i], 0) == string::npos)
+               for (char c : par2)
+                       if (info_.find(c, 0) == string::npos)
                                return bformat(_("Should be composed of one or more of %1$s."),
                                                from_utf8(info_));
                if (unclosed)
@@ -247,28 +247,54 @@ docstring ListingsParam::validate(string const & par) const
 /// languages and language/dialect combinations
 char const * allowed_languages =
        "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"
+       "[R/3 4.6C]ABAP\n[R/3 6.10]ABAP\nACM\nACMscript\nACSL\nAda\n[2005]Ada\n[83]Ada\n"
+       "[95]Ada\nALGOL\n[60]ALGOL\n[68]ALGOL\nAnt\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"
+       "[light]Caml\n[Objective]Caml\nCIL\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\nerlang\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"
+       "Delphi\nEiffel\nElan\nelisp\nerlang\nEuphoria\nFortran\n[77]Fortran\n[90]Fortran\n"
+       "[95]Fortran\n[03]Fortran\n[08]Fortran\nGAP\nGCL\nGnuplot\nGo\nhansl\nHaskell\nHTML\nIDL\n[CORBA]IDL\ninform\n"
+       "Java\n[AspectJ]Java\nJVMIS\nksh\nLingo\nLisp\n[Auto]Lisp\nLLVM\nLogo\n"
+       "Lua\n[5.0]Lua\n[5.1]Lua\n[5.2]Lua\n[5.3]Lua\n"
+       "make\n[gnu]make\nMathematica\n[1.0]Mathematica\n[3.0]Mathematica\n[11.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"
+       "Octave\nOORexx\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"
+       "Python\nR\nReduce\nRexx\n[VM/XA]Rexx\nRSL\nRuby\nS\n[PLUS]S\nSAS\nScala\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";
 
 
+/// Return language allowed in the GUI without dialect and proper casing
+string const languageonly(string const & lang)
+{
+       string const locase = ascii_lowercase(trim(lang, "{}"));
+       string const all_languages = ascii_lowercase(allowed_languages) + "\n";
+       string language = (lang.at(0) == '[') ? locase + "\n"
+                                             : string("]") + locase + "\n";
+       size_t i = all_languages.find(language);
+       if (i == string::npos && lang.at(0) != '[') {
+               language[0] = '\n';
+               i = all_languages.find(language);
+       }
+       if (i == string::npos)
+               return lang;
+       if (all_languages.at(i) == '[')
+               i = all_languages.find(']', i);
+       if (i == string::npos)
+               return lang;
+       size_t j = all_languages.find('\n', i + 1);
+       if (j == string::npos)
+               return lang;
+       return string(allowed_languages).substr(i + 1, j - i - 1);
+}
+
+
 /// ListingsParam Validator.
 /// This class is aimed to be a singleton which is instantiated in
 /// \c InsetListingsParams::addParam().
@@ -657,12 +683,6 @@ ParValidator::ParValidator()
        // is to signal that this is a floating listing.
        all_params_[1]["float"] =
                ListingsParam("false", true, SUBSETOF, "*tbph", empty_hint);
-       all_params_[1]["chapter"] =
-               ListingsParam("", true, TRUEFALSE, "", _(
-                                       "Number floats by chapter"));
-       all_params_[1]["section"] =
-               ListingsParam("", true, TRUEFALSE, "", _(
-                                       "Number floats by section"));
        all_params_[1]["cache"] =
                ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["cachedir"] =
@@ -685,25 +705,25 @@ ParValidator::ParValidator()
        all_params_[1]["outputdir"] =
                ListingsParam("", false, ALL, "", empty_hint);
        all_params_[1]["autogobble"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["baselinestretch"] =
-               ListingsParam("", false, LENGTH, "", empty_hint);
+               ListingsParam("", false, ALL, "", empty_hint);
        all_params_[1]["breakafter"] =
                ListingsParam("", false, ALL, "", empty_hint);
        all_params_[1]["breakaftergroup"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["breakaftersymbolpre"] =
                ListingsParam("", false, ALL, "", empty_hint);
        all_params_[1]["breakaftersymbolpost"] =
                ListingsParam("", false, ALL, "", empty_hint);
        all_params_[1]["breakanywhere"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["breakanywheresymbolpre"] =
                ListingsParam("", false, ALL, "", empty_hint);
        all_params_[1]["breakanywheresymbolpost"] =
                ListingsParam("", false, ALL, "", empty_hint);
        all_params_[1]["breakautoindent"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["breakbefore"] =
                ListingsParam("", false, ALL, "", empty_hint);
        all_params_[1]["breakbeforegroup"] =
@@ -713,13 +733,13 @@ ParValidator::ParValidator()
        all_params_[1]["breakbeforesymbolpost"] =
                ListingsParam("", false, ALL, "", empty_hint);
        all_params_[1]["breakbytoken"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["breakbytokenanywhere"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["breakindent"] =
                ListingsParam("", false, LENGTH, "", empty_hint);
        all_params_[1]["breaklines"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["breaksymbol"] =
                ListingsParam("", false, ALL, "", empty_hint);
        all_params_[1]["breaksymbolleft"] =
@@ -743,7 +763,7 @@ ParValidator::ParValidator()
        all_params_[1]["codetagify"] =
                ListingsParam("", false, ALL, "", empty_hint);
        all_params_[1]["curlyquotes"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["encoding"] =
                ListingsParam("", false, ALL, "", _(
                                "Sets encoding expected by Pygments"));
@@ -753,7 +773,7 @@ ParValidator::ParValidator()
                ListingsParam("", false, INTEGER, "", empty_hint);
        all_params_[1]["firstnumber"] =
                ListingsParam("", false, ALL, "", _(
-                                       "(auto | last | integer)"));
+                                       "auto, last or a number"));
        all_params_[1]["fontfamily"] =
                ListingsParam("", false, ALL, "", _(
                                "A latex family such as tt, sf, rm"));
@@ -777,7 +797,7 @@ ParValidator::ParValidator()
        all_params_[1]["framesep"] =
                ListingsParam("", false, LENGTH, "", empty_hint);
        all_params_[1]["funcnamehighlighting"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["gobble"] =
                ListingsParam("", false, INTEGER, "", empty_hint);
        all_params_[1]["highlightcolor"] =
@@ -791,68 +811,65 @@ ParValidator::ParValidator()
        all_params_[1]["labelposition"] =
                ListingsParam("", false, ONEOF,
                        "none\ntopline\nbottomline\nall", empty_hint);
-       all_params_[1]["language"] =
-               ListingsParam("", false, ONEOF,
-                               allowed_languages, empty_hint);
        all_params_[1]["language"] =
                ListingsParam("", false, ALL, "", _(
-               "This parameter should not be entered here. Please "
-               "use the language combo box in the listings inset "
-               "settings dialog, unless you need to enter a language "
-               "not offered there."));
+               "Enter one of the supported languages. However, if you "
+               "are defining a listing inset, it is better using the  "
+               "language combo box, unless you need to enter a language not "
+               "offered there, otherwise the combo box will be disabled."));
        all_params_[1]["lastline"] =
                ListingsParam("", false, INTEGER, "", empty_hint);
        all_params_[1]["linenos"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["numberfirstline"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["numbers"] =
                ListingsParam("", false, ONEOF,
                                "left\nright\nboth\nnone", empty_hint);
        all_params_[1]["mathescape"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["numberblanklines"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["numbersep"] =
                ListingsParam("", false, LENGTH, "", empty_hint);
        all_params_[1]["obeytabs"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["outencoding"] =
                ListingsParam("", false, ALL, "", _(
                  "File encoding used by Pygments for highlighting"));
        all_params_[1]["python3"] =
-               ListingsParam("", true, TRUEFALSE, "", _(
+               ListingsParam("", false, TRUEFALSE, "", _(
                                        "Apply Python 3 highlighting"));
-       all_params_[1]["resetsmargins"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+       all_params_[1]["resetmargins"] =
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["rulecolor"] =
                ListingsParam("", false, ALL, "", color_hint_mint);
        all_params_[1]["samepage"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["showspaces"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["showtabs"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["space"] =
                ListingsParam("", false, ALL, "", _(
-                               "A macro. Default: \textvisiblespace"));
+                               "A macro. Default: \\textvisiblespace"));
        all_params_[1]["spacecolor"] =
                ListingsParam("", false, ALL, "", color_hint_mint);
        all_params_[1]["startinline"] =
-               ListingsParam("", true, TRUEFALSE, "", _("For PHP only"));
+               ListingsParam("", false, TRUEFALSE, "", _("For PHP only"));
        all_params_[1]["style"] =
                ListingsParam("", false, ALL, "", _(
                                        "The style used by Pygments"));
        all_params_[1]["stepnumber"] =
                ListingsParam("", false, INTEGER, "", empty_hint);
        all_params_[1]["stepnumberfromfirst"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["stepnumberoffsetvalues"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["stripall"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["stripnl"] =
-               ListingsParam("", true, TRUEFALSE, "", empty_hint);
+               ListingsParam("", false, TRUEFALSE, "", empty_hint);
        all_params_[1]["tab"] =
                ListingsParam("", false, ALL, "", _(
                                "A macro to redefine visible tabs"));
@@ -861,10 +878,10 @@ ParValidator::ParValidator()
        all_params_[1]["tabsize"] =
                ListingsParam("", false, INTEGER, "", empty_hint);
        all_params_[1]["texcl"] =
-               ListingsParam("", true, TRUEFALSE, "", _(
+               ListingsParam("", false, TRUEFALSE, "", _(
                                "Enables latex code in comments"));
        all_params_[1]["texcomments"] =
-               ListingsParam("", true, TRUEFALSE, "", _(
+               ListingsParam("", false, TRUEFALSE, "", _(
                                "Enables latex code in comments"));
        all_params_[1]["xleftmargin"] =
                ListingsParam("", false, LENGTH, "", empty_hint);
@@ -898,10 +915,10 @@ docstring ParValidator::validate(string const & name,
                                        _("Available listing parameters are %1$s"), from_ascii(param_names));
                else
                        return bformat(
-                                       _("Available listings parameters containing string \"%1$s\" are %2$s"), 
+                                       _("Available listings parameters containing string \"%1$s\" are %2$s"),
                                                from_utf8(suffix), from_utf8(param_names));
        }
+
        // locate name in parameter table
        ListingsParams::const_iterator it = all_params_[p].find(name);
        if (it != all_params_[p].end()) {
@@ -931,19 +948,19 @@ docstring ParValidator::validate(string const & name,
 }
 
 
-bool ParValidator::onoff(string const & name) const
+bool ParValidator::onoff(string const & key) const
 {
        int p = InsetListingsParams::package();
 
        // locate name in parameter table
-       ListingsParams::const_iterator it = all_params_[p].find(name);
+       ListingsParams::const_iterator it = all_params_[p].find(key);
        if (it != all_params_[p].end())
                return it->second.onoff_;
        else
                return false;
 }
 
-} // namespace anon.
+} // namespace
 
 // define a global ParValidator
 ParValidator * par_validator = 0;
@@ -953,13 +970,13 @@ ParValidator * par_validator = 0;
 int InsetListingsParams::package_ = 0;
 
 InsetListingsParams::InsetListingsParams()
-       : inline_(false), params_(), status_(InsetCollapsable::Open)
+       : inline_(false), params_(), status_(InsetCollapsible::Open)
 {
 }
 
 
 InsetListingsParams::InsetListingsParams(string const & par, bool in,
-               InsetCollapsable::CollapseStatus s)
+               InsetCollapsible::CollapseStatus s)
        : inline_(in), params_(), status_(s)
 {
        // this will activate parameter validation.
@@ -980,9 +997,9 @@ void InsetListingsParams::write(ostream & os) const
 void InsetListingsParams::read(Lexer & lex)
 {
        lex >> inline_;
-       int s = InsetCollapsable::Collapsed;
+       int s = InsetCollapsible::Collapsed;
        lex >> s;
-       status_ = static_cast<InsetCollapsable::CollapseStatus>(s);
+       status_ = static_cast<InsetCollapsible::CollapseStatus>(s);
        string par;
        lex >> par;
        fromEncodedString(par);
@@ -1028,12 +1045,15 @@ string InsetListingsParams::getValue(string const & key) const
 }
 
 
-void InsetListingsParams::addParam(string const & key, 
-               string const & value, bool replace)
+void InsetListingsParams::addParam(string const & key,
+               string const & val, bool replace)
 {
        if (key.empty())
                return;
 
+       bool const is_minted_language = minted() && key == "language";
+       string const value = (is_minted_language && !val.empty())
+                               ? languageonly(val) : val;
        // duplicate parameters!
        string keyname = key;
        if (!replace && hasParam(key))
@@ -1053,12 +1073,12 @@ void InsetListingsParams::addParam(string const & key,
        // non-ascii/number characters, just to be safe
        else {
                bool has_special_char = false;
-               for (size_t i = 0; i < value.size(); ++i)
-                       if (!isAlnumASCII(value[i])) {
+               for (char c : value)
+                       if (!isAlnumASCII(c)) {
                                has_special_char = true;
                                break;
                        }
-               if (has_special_char)
+               if (has_special_char && !is_minted_language)
                        params_.push_back(make_pair(keyname, "{" + value + "}"));
                else
                        params_.push_back(make_pair(keyname, value));
@@ -1092,7 +1112,7 @@ void InsetListingsParams::addParams(string const & par)
                } else if (par[i] == '{' && i > 0 && par[i-1] != '\\')
                        // don't count a brace in first position
                        ++braces;
-               else if (par[i] == '}' && i != par.size() - 1 
+               else if (par[i] == '}' && i != par.size() - 1
                         && (i == 0 || (i > 0 && par[i-1] != '\\')))
                        --braces;