]> git.lyx.org Git - lyx.git/commitdiff
Extend minted support to listings as child documents
authorEnrico Forestieri <forenr@lyx.org>
Thu, 8 Jun 2017 17:33:54 +0000 (19:33 +0200)
committerEnrico Forestieri <forenr@lyx.org>
Thu, 8 Jun 2017 17:35:48 +0000 (19:35 +0200)
This was slipping through the cracks, apparently...

src/frontends/qt4/GuiInclude.cpp
src/insets/InsetInclude.cpp
src/insets/InsetListingsParams.cpp

index b02fd9181f0384e52e7f31fe8ed7a9c22062c297..78c62031cab11d78f31d5e0414552807bc662f70 100644 (file)
@@ -15,6 +15,7 @@
 #include "GuiInclude.h"
 
 #include "Buffer.h"
+#include "BufferParams.h"
 #include "FuncRequest.h"
 #include "LyXRC.h"
 
@@ -94,7 +95,9 @@ docstring GuiInclude::validate_listings_params()
        if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
                return docstring();
        string params = fromqstr(listingsED->toPlainText());
-       return InsetListingsParams(params).validate();
+       InsetListingsParams lstparams(params);
+       lstparams.setMinted(buffer().params().use_minted);
+       return lstparams.validate();
 }
 
 
@@ -176,7 +179,8 @@ void GuiInclude::paramsToDialog(InsetCommandParams const & params_)
        if (cmdname != "include" &&
            cmdname != "verbatiminput" &&
            cmdname != "verbatiminput*" &&
-               cmdname != "lstinputlisting")
+           cmdname != "lstinputlisting" &&
+           cmdname != "inputminted")
                cmdname = "input";
 
        if (cmdname == "include") {
@@ -196,7 +200,7 @@ void GuiInclude::paramsToDialog(InsetCommandParams const & params_)
                typeCO->setCurrentIndex(2);
                visiblespaceCB->setEnabled(true);
 
-       } else if (cmdname == "lstinputlisting") {
+       } else if (cmdname == "lstinputlisting" || cmdname == "inputminted") {
                typeCO->setCurrentIndex(3);
                listingsGB->setEnabled(true);
                listingsED->setEnabled(true);
@@ -241,7 +245,10 @@ void GuiInclude::applyView()
        } else if (item == 1) {
                params_.setCmdName("input");
        } else if (item == 3) {
-               params_.setCmdName("lstinputlisting");
+               if (buffer().params().use_minted)
+                       params_.setCmdName("inputminted");
+               else
+                       params_.setCmdName("lstinputlisting");
                // the parameter string should have passed validation
                InsetListingsParams par(fromqstr(listingsED->toPlainText()));
                string caption = fromqstr(captionLE->text());
index 3ae1b48965805cc76ef6ae9a339d2d18e76d99d2..6ae77a657cc69d233e2302f1bd28134aca90e84b 100644 (file)
@@ -99,7 +99,7 @@ Types type(string const & s)
                return VERB;
        if (s == "verbatiminput*")
                return VERBAST;
-       if (s == "lstinputlisting")
+       if (s == "lstinputlisting" || s == "inputminted")
                return LISTINGS;
        if (s == "include")
                return INCLUDE;
@@ -598,15 +598,81 @@ void InsetInclude::latex(otexstream & os, OutputParams const & runparams) const
                break;
        }
        case LISTINGS: {
+               // Here, listings and minted have sligthly different behaviors.
+               // Using listings, it is always possible to have a caption,
+               // even for non-floats. Using minted, only floats can have a
+               // caption. So, with minted we use the following strategy.
+               // If a caption or the float parameter are specified, we
+               // assume that the listing is floating. In this case, the
+               // label parameter is taken as the label by which the float
+               // can be referenced, otherwise it will have the meaning
+               // intended by minted. In this last case, the label will
+               // serve as a sort of caption that, however, will be shown
+               // by minted only if the frame parameter is also specified.
+               bool const use_minted = buffer().params().use_minted;
                runparams.exportdata->addExternalFile(tex_format, writefile,
                                                      exportfile);
-               os << '\\' << from_ascii(params().getCmdName());
                string const opt = to_utf8(params()["lstparams"]);
                // opt is set in QInclude dialog and should have passed validation.
-               InsetListingsParams params(opt);
-               if (!params.params().empty())
-                       os << "[" << from_utf8(params.params()) << "]";
-               os << '{'  << from_utf8(incfile) << '}';
+               InsetListingsParams lstparams(opt);
+               string parameters = lstparams.params();
+               string language;
+               string caption;
+               string label;
+               string placement;
+               bool isfloat = false;
+               if (use_minted) {
+                       // Get float placement, language, caption, and
+                       // label, then remove the relative options.
+                       vector<string> opts =
+                               getVectorFromString(parameters, ",", false);
+                       for (size_t i = 0; i < opts.size(); ++i) {
+                               if (prefixIs(opts[i], "float")) {
+                                       isfloat = true;
+                                       if (prefixIs(opts[i], "float="))
+                                               placement = opts[i].substr(6);
+                                       opts.erase(opts.begin() + i--);
+                               } else if (prefixIs(opts[i], "language=")) {
+                                       language = opts[i].substr(9);
+                                       opts.erase(opts.begin() + i--);
+                               } else if (prefixIs(opts[i], "caption=")) {
+                                       isfloat = true;
+                                       caption = opts[i].substr(8);
+                                       opts.erase(opts.begin() + i--);
+                               } else if (prefixIs(opts[i], "label=")) {
+                                       label = opts[i].substr(6);
+                                       opts.erase(opts.begin() + i--);
+                               }
+                       }
+                       if (!label.empty()) {
+                               if (isfloat)
+                                       label = trim(label, "{}");
+                               else
+                                       opts.push_back("label=" + label);
+                       }
+                       parameters = getStringFromVector(opts, ",");
+               }
+               if (language.empty())
+                       language = "TeX";
+               if (use_minted && isfloat) {
+                       os << breakln << "\\begin{listing}";
+                       if (!placement.empty())
+                               os << '[' << placement << "]";
+                       os << breakln;
+               }
+               os << (use_minted ? "\\inputminted" : "\\lstinputlisting");
+               if (!parameters.empty())
+                       os << "[" << parameters << "]";
+               if (use_minted)
+                       os << '{'  << language << '}';
+               os << '{'  << incfile << '}';
+               if (use_minted && isfloat) {
+                       if (!caption.empty())
+                               os << breakln << "\\caption{" << caption << "}";
+                       if (!label.empty())
+                               os << breakln << "\\label{" << label << "}";
+                       os << breakln << "\\end{listing}\n";
+               }
                break;
        }
        case INCLUDE: {
index bab238ae1ee91a6e728da67c627aea074c9237e2..a2d2cea3c734b807cff32943a7ae341345264e71 100644 (file)
@@ -687,7 +687,7 @@ ParValidator::ParValidator()
        all_params_[1]["autogobble"] =
                ListingsParam("", true, 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"] =
@@ -791,15 +791,12 @@ 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"] =