]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiListings.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / GuiListings.cpp
index da128829e69bd3cbb0446f4d379af34f7f79f480..c957cb0b75bf0857e9cef14c3c9a4adce6f2f521 100644 (file)
@@ -15,6 +15,8 @@
 
 #include "qt_helpers.h"
 
+#include "Buffer.h"
+#include "BufferParams.h"
 #include "FuncRequest.h"
 
 #include "insets/InsetListings.h"
@@ -45,26 +47,28 @@ namespace frontend {
 
 
 char const * languages_supported[] =
-{ "no language", "ABAP", "ACSL", "Ada", "ALGOL", "Assembler", "Awk", "bash", "Basic", "C",
-  "C++", "Caml", "Clean", "Cobol", "Comal 80", "command.com", "Comsol", "csh", "Delphi",
-  "Eiffel", "Elan", "erlang", "Euphoria", "Fortran", "Gnuplot", "Haskell", "HTML", "IDL", "inform",
-  "Java", "JVMIS", "ksh", "Lingo", "Lisp", "Logo", "make", "Mathematica", "Matlab", "Mercury",
-  "MetaPost", "Miranda", "ML", "Modula-2", "MuPAD", "NASTRAN", "Oberon-2", "OCL", "Octave",
+{ "no language", "ABAP", "ACM", "ACMscript", "ACSL", "Ada", "ALGOL", "Ant", "Assembler", "Awk", "bash", "Basic", "C",
+  "C++", "Caml", "CIL", "Clean", "Cobol", "Comal 80", "command.com", "Comsol", "csh", "Delphi",
+  "Eiffel", "Elan", "elisp", "erlang", "Euphoria", "Fortran", "GAP", "GCL", "Gnuplot", "Go",
+  "hansl", "Haskell", "HTML", "IDL", "inform",
+  "Java", "JVMIS", "ksh", "Lingo", "Lisp", "LLVM", "Logo", "Lua", "make", "Mathematica", "Matlab", "Mercury",
+  "MetaPost", "Miranda", "Mizar", "ML", "Modula-2", "MuPAD", "NASTRAN", "Oberon-2", "OCL", "Octave", "OORexx",
   "Oz", "Pascal", "Perl", "PHP", "PL/I", "Plasm", "PostScript", "POV", "Prolog", "Promela",
-  "PSTricks", "Python", "R", "Reduce", "Rexx", "RSL", "Ruby", "S", "SAS", "Scilab", "sh",
-  "SHELXL", "Simula", "tcl", "SPARQL", "SQL", "tcl", "TeX", "VBScript", "Verilog", "VHDL",
+  "PSTricks", "Python", "R", "Reduce", "Rexx", "RSL", "Ruby", "S", "SAS", "Scala", "Scilab", "sh",
+  "SHELXL", "Simula", "SPARQL", "SQL", "Swift", "tcl", "TeX", "VBScript", "Verilog", "VHDL",
   "VRML", "XML", "XSLT", "" };
 
 
 char const * languages_gui[] =
-{ N_("No language"), "ABAP", "ACSL", "Ada", "ALGOL", "Assembler", "Awk", "bash", "Basic",
-  "C", "C++", "Caml", "Clean", "Cobol", "Comal 80", "command.com", "Comsol", "csh", "Delphi",
-  "Eiffel", "Elan", "Erlang", "Euphoria", "Fortran", "Gnuplot", "Haskell", "HTML", "IDL", "inform",
-  "Java", "JVMIS", "ksh", "Lingo", "Lisp", "Logo", "make", "Mathematica", "Matlab", "Mercury",
-  "MetaPost", "Miranda", "ML", "Modula-2", "MuPAD", "NASTRAN", "Oberon-2", "OCL", "Octave",
+{ N_("No language"), "ABAP", "ACM", "ACMscript", "ACSL", "Ada", "ALGOL", "Ant", "Assembler", "Awk", "bash", "Basic", "C",
+  "C++", "Caml", "CIL", "Clean", "Cobol", "Comal 80", "command.com", "Comsol", "csh", "Delphi",
+  "Eiffel", "Elan", "elisp", "erlang", "Euphoria", "Fortran", "GAP", "GCL", "Gnuplot", "Go",
+  "hansl", "Haskell", "HTML", "IDL", "inform",
+  "Java", "JVMIS", "ksh", "Lingo", "Lisp", "LLVM", "Logo", "Lua", "make", "Mathematica", "Matlab", "Mercury",
+  "MetaPost", "Miranda", "Mizar", "ML", "Modula-2", "MuPAD", "NASTRAN", "Oberon-2", "OCL", "Octave", "OORexx",
   "Oz", "Pascal", "Perl", "PHP", "PL/I", "Plasm", "PostScript", "POV", "Prolog", "Promela",
-  "PSTricks", "Python", "R", "Reduce", "Rexx", "RSL", "Ruby", "S", "SAS", "Scilab", "sh",
-  "SHELXL", "Simula", "tcl", "SPARQL", "SQL", "tcl", "TeX", "VBScript", "Verilog", "VHDL",
+  "PSTricks", "Python", "R", "Reduce", "Rexx", "RSL", "Ruby", "S", "SAS", "Scala", "Scilab", "sh",
+  "SHELXL", "Simula", "SPARQL", "SQL", "Swift", "tcl", "TeX", "VBScript", "Verilog", "VHDL",
   "VRML", "XML", "XSLT", "" };
 
 
@@ -165,9 +169,8 @@ GuiListings::GuiListings(GuiView & lv)
 {
        setupUi(this);
 
-       connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
-       connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
-       connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
+       connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
+               this, SLOT(slotButtonBox(QAbstractButton *)));
 
        connect(languageCO, SIGNAL(currentIndexChanged(int)),
                this, SLOT(change_adaptor()));
@@ -232,9 +235,9 @@ GuiListings::GuiListings(GuiView & lv)
        placementLE->setValidator(new QRegExpValidator(QRegExp("[\\*tbph]*"), this));
 
        bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
-       bc().setOK(okPB);
-       bc().setApply(applyPB);
-       bc().setCancel(closePB);
+       bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
+       bc().setApply(buttonBox->button(QDialogButtonBox::Apply));
+       bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
        listingsTB->setPlainText(
                qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
 
@@ -293,10 +296,23 @@ string GuiListings::construct_params()
        string fontsize = font_sizes[qMax(0, fontsizeCO->currentIndex())];
        string fontstyle = font_styles[qMax(0, fontstyleCO->currentIndex())];
        string basicstyle;
-       if (fontsize != "default")
-               basicstyle = "\\" + fontsize;
-       if (fontstyle != "default")
-               basicstyle += "\\" + fontstyle;
+       string mintedsize;
+       // FIXME: We should not compose listings- or minted-dependant string here
+       //        This breaks if a users switches the backend without opening and
+       //        re-applying all listings insets. Use a backend-abstract syntax!
+       bool const use_minted = buffer().params().use_minted;
+       if (fontsize != "default") {
+               if (use_minted)
+                       mintedsize = "\\" + fontsize;
+               else
+                       basicstyle = "\\" + fontsize;
+       }
+       if (fontstyle != "default") {
+               if (use_minted)
+                       basicstyle = fontstyle.substr(0, 2);
+               else
+                       basicstyle += "\\" + fontstyle;
+       }
        bool breakline = breaklinesCB->isChecked();
        bool space = spaceCB->isChecked();
        int tabsize = tabsizeSB->value();
@@ -306,7 +322,22 @@ string GuiListings::construct_params()
 
        // compose a string
        InsetListingsParams par;
-       if (language != "no language" && !contains(extra, "language=")) {
+       par.setMinted(use_minted);
+       if (use_minted) {
+               if (language == "no language" && !contains(extra, "language=")) {
+                       string const & blp = buffer().params().listings_params;
+                       size_t start = blp.find("language=");
+                       if (start != string::npos) {
+                               start += strlen("language=");
+                               size_t len = blp.find(",", start);
+                               if (len != string::npos)
+                                       len -= start;
+                               par.addParam("language", blp.substr(start, len));
+                       } else
+                               par.addParam("language", "TeX");
+               } else
+                       par.addParam("language", language);
+       } else if (language != "no language" && !contains(extra, "language=")) {
                if (dialect.empty())
                        par.addParam("language", language);
                else
@@ -319,7 +350,7 @@ string GuiListings::construct_params()
                par.addParam("float", placement);
        if (numberSide != "none")
                par.addParam("numbers", numberSide);
-       if (numberfontsize != "default" && numberSide != "none")
+       if (numberfontsize != "default" && numberSide != "none" && !use_minted)
                par.addParam("numberstyle", "\\" + numberfontsize);
        if (!stepnumber.empty() && numberSide != "none")
                par.addParam("stepnumber", stepnumber);
@@ -328,16 +359,18 @@ string GuiListings::construct_params()
        if (!lastline.empty())
                par.addParam("lastline", lastline);
        if (!basicstyle.empty())
-               par.addParam("basicstyle", basicstyle);
+               par.addParam(use_minted ? "fontfamily" : "basicstyle", basicstyle);
+       if (!mintedsize.empty())
+               par.addParam("fontsize", mintedsize);
        if (breakline)
                par.addParam("breaklines", "true");
        if (space)
                par.addParam("showspaces", "true");
-       if (!spaceInString)
+       if (!spaceInString && !use_minted)
                par.addParam("showstringspaces", "false");
        if (tabsize != 8)
                par.addParam("tabsize", convert<string>(tabsize));
-       if (extendedchars)
+       if (extendedchars && !use_minted)
                par.addParam("extendedchars", "true");
        par.addParams(extra);
        return par.params();
@@ -346,21 +379,9 @@ string GuiListings::construct_params()
 
 docstring GuiListings::validate_listings_params()
 {
-       // use a cache here to avoid repeated validation
-       // of the same parameters
-       // FIXME THREAD
-       static string param_cache;
-       static docstring msg_cache;
-       
        if (bypassCB->isChecked())
                return docstring();
-
-       string params = construct_params();
-       if (params != param_cache) {
-               param_cache = params;
-               msg_cache = InsetListingsParams(params).validate();
-       }
-       return msg_cache;
+       return InsetListingsParams(construct_params()).validate();
 }
 
 
@@ -425,12 +446,14 @@ void GuiListings::on_languageCO_currentIndexChanged(int index)
                }
        }
        dialectCO->setCurrentIndex(default_dialect);
-       dialectCO->setEnabled(dialectCO->count() > 1);
+       dialectCO->setEnabled(dialectCO->count() > 1
+                             && !buffer().params().use_minted);
 }
 
 
 void GuiListings::applyView()
 {
+       params_.setMinted(buffer().params().use_minted);
        params_.setInline(inlineCB->isChecked());
        params_.setParams(construct_params());
 }
@@ -447,6 +470,7 @@ static string plainParam(string const & par)
 
 void GuiListings::updateContents()
 {
+       bool const use_minted = buffer().params().use_minted;
        // set default values
        listingsTB->setPlainText(
                qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
@@ -515,7 +539,7 @@ void GuiListings::updateContents()
                        if (in_gui)
                                *it = "";
                        languageCO->setEnabled(in_gui);
-                       dialectCO->setEnabled(
+                       dialectCO->setEnabled(!use_minted &&
                                in_gui && dialectCO->count() > 1);
                } else if (prefixIs(*it, "float")) {
                        floatCB->setChecked(true);
@@ -583,6 +607,36 @@ void GuiListings::updateContents()
                                }
                                *it = "";
                        }
+               } else if (prefixIs(*it, "fontsize=")) {
+                       string size;
+                       for (int n = 0; font_sizes[n][0]; ++n) {
+                               string const s = font_sizes[n];
+                               if (contains(*it, "\\" + s)) {
+                                       size = "\\" + s;
+                                       break;
+                               }
+                       }
+                       if (!size.empty()) {
+                               int n = findToken(font_sizes, size.substr(1));
+                               if (n >= 0)
+                                       fontsizeCO->setCurrentIndex(n);
+                       }
+                       *it = "";
+               } else if (prefixIs(*it, "fontfamily=")) {
+                       string style;
+                       for (int n = 0; font_styles[n][0]; ++n) {
+                               string const s = font_styles[n];
+                               if (contains(*it, "=" + s.substr(0,2))) {
+                                       style = "\\" + s;
+                                       break;
+                               }
+                       }
+                       if (!style.empty()) {
+                               int n = findToken(font_styles, style.substr(1));
+                               if (n >= 0)
+                                       fontstyleCO->setCurrentIndex(n);
+                       }
+                       *it = "";
                } else if (prefixIs(*it, "breaklines=")) {
                        breaklinesCB->setChecked(contains(*it, "true"));
                        *it = "";
@@ -602,11 +656,15 @@ void GuiListings::updateContents()
        }
 
        numberStepLE->setEnabled(numberSideCO->currentIndex() > 0);
-       numberFontSizeCO->setEnabled(numberSideCO->currentIndex() > 0);
+       numberFontSizeCO->setEnabled(numberSideCO->currentIndex() > 0
+                                    && !use_minted);
+       spaceInStringCB->setEnabled(!use_minted);
+       extendedcharsCB->setEnabled(!use_minted);
        // parameters that can be handled by widgets are cleared
        // the rest is put to the extra edit box.
        string extra = getStringFromVector(pars);
        listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
+       params_.setMinted(use_minted);
 }
 
 
@@ -616,9 +674,9 @@ bool GuiListings::isValid()
 }
 
 
-bool GuiListings::initialiseParams(string const & data)
+bool GuiListings::initialiseParams(string const & sdata)
 {
-       InsetListings::string2params(data, params_);
+       InsetListings::string2params(sdata, params_);
        return true;
 }
 
@@ -626,6 +684,7 @@ bool GuiListings::initialiseParams(string const & data)
 void GuiListings::clearParams()
 {
        params_.clear();
+       params_.setMinted(buffer().params().use_minted);
 }
 
 
@@ -639,6 +698,7 @@ void GuiListings::dispatchParams()
 void GuiListings::setParams(InsetListingsParams const & params)
 {
        params_ = params;
+       params_.setMinted(buffer().params().use_minted);
 }