]> git.lyx.org Git - features.git/commitdiff
Native support for \lstlistoflistings
authorGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Mon, 16 Apr 2012 19:40:59 +0000 (21:40 +0200)
committerGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Mon, 16 Apr 2012 19:40:59 +0000 (21:40 +0200)
lib/doc/EmbeddedObjects.lyx
lib/layouts/stdinsets.inc
lib/lyx2lyx/lyx2lyx_tools.py
lib/lyx2lyx/lyx_2_1.py
lib/ui/stdmenus.inc
src/frontends/qt4/TocWidget.cpp
src/insets/InsetTOC.cpp
src/insets/InsetTOC.h
src/tex2lyx/test/test-insets.tex
src/tex2lyx/text.cpp
src/version.h

index 89c3636dd875daf38b0270deab93ddb8746bd825..d8c5a430f0330d5c16ac4c87dc202f0da9fee6f7 100644 (file)
@@ -39685,6 +39685,27 @@ reference "lst:Example-Listing"
 \end_inset
 
 
+\change_inserted -195340706 1334604968
+.
+ A list of listings which contains all listings with captions can be created
+ via the 
+\family sans
+Insert\SpecialChar \menuseparator
+List
+\begin_inset space ~
+\end_inset
+
+/
+\begin_inset space ~
+\end_inset
+
+TOC\SpecialChar \menuseparator
+List of Listings
+\family default
+ submenu.
+ The list entries are the listing caption and the listing number.
+\change_unchanged
+
 \end_layout
 
 \begin_layout Standard
index faa6d1c9d027015ded823cc4aa304b921688eba1..2000172f23f963920478b8582070314687cb2067 100644 (file)
@@ -170,25 +170,35 @@ InsetLayout Phantom
        ForcePlain            true
 End
 
-InsetLayout IncludeListings
+InsetLayout ListOfListings
        # We need the [[List of Listings]] context, since "Listings" is also
        # the name of the inset and translated differently.
        # "Listings[[List of Listings]]" is the name of the "List of listings"
        # ("Listings" is the predefined english name) in listings.sty, so it
        # must be used here as well.
        BabelPreamble
-               \addto\captions$$lang{\renewcommand{\lstlistingname}{_(Listing)}}
                \addto\captions$$lang{\renewcommand{\lstlistlistingname}{_(Listings[[List of Listings]])}}
        EndBabelPreamble
-       # The commands do not need to be defined in LangPreamble, since
-       # listings.sty does that already. However they need to be redefined
+       # The command does not need to be defined in LangPreamble, since
+       # listings.sty does that already. However it needs to be redefined
        # in order to be used for non-english single-language documents.
        LangPreamble
-               \renewcommand{\lstlistingname}{_(Listing)}
                \renewcommand{\lstlistlistingname}{_(Listings[[List of Listings]])}
        EndLangPreamble
 End
 
+InsetLayout IncludeListings
+       BabelPreamble
+               \addto\captions$$lang{\renewcommand{\lstlistingname}{_(Listing)}}
+       EndBabelPreamble
+       # The command does not need to be defined in LangPreamble, since
+       # listings.sty does that already. However it needs to be redefined
+       # in order to be used for non-english single-language documents.
+       LangPreamble
+               \renewcommand{\lstlistingname}{_(Listing)}
+       EndLangPreamble
+End
+
 InsetLayout Listings
        CopyStyle IncludeListings
        LabelString           Listings[[inset]]
index ac0b0a6c9ecf6fde0d7941db4990964e1298adee..75006baab54cbd000c13de46c5e16cf75e40e3b5 100644 (file)
@@ -60,7 +60,7 @@ latex_length(slen):
 '''
 
 import string
-from parser_tools import find_token
+from parser_tools import find_token, find_end_of_inset
 from unicode_symbols import unicode_reps
 
 
@@ -131,7 +131,38 @@ def put_cmd_in_ert(arg):
     ret += ["\\end_layout", "\\end_inset"]
     return ret
 
-            
+
+def get_ert(lines, i):
+    'Convert an ERT inset into LaTeX.'
+    if not lines[i].startswith("\\begin_inset ERT"):
+        return ""
+    j = find_end_of_inset(lines, i)
+    if j == -1:
+        return ""
+    while i < j and not lines[i].startswith("status"):
+        i = i + 1
+    i = i + 1
+    ret = ""
+    first = True
+    while i < j:
+        if lines[i] == "\\begin_layout Plain Layout":
+            if first:
+                first = False
+            else:
+                ret = ret + "\n"
+            while i + 1 < j and lines[i+1] == "":
+                i = i + 1
+        elif lines[i] == "\\end_layout":
+            while i + 1 < j and lines[i+1] == "":
+                i = i + 1
+        elif lines[i] == "\\backslash":
+            ret = ret + "\\"
+        else:
+            ret = ret + lines[i]
+        i = i + 1
+    return ret
+
+
 def lyx2latex(document, lines):
     'Convert some LyX stuff into corresponding LaTeX stuff, as best we can.'
 
index 704fef202136b5fe9a4d94f3de517cb85504fe4e..c76cf1baac12f93e058fe24bb4cf9526e15dc9ec 100644 (file)
@@ -34,10 +34,10 @@ from parser_tools import del_token, find_token, find_end_of, find_end_of_inset,
   #find_token_backwards, is_in_inset, get_value, get_quoted_value, \
   #del_token, check_token
 
-from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
+from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, get_ert
 
 #from lyx2lyx_tools import insert_to_preamble, \
-#  put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
+#  lyx2latex, latex_length, revert_flex_inset, \
 #  revert_font_attrs, hex2ratio, str2bool
 
 ####################################################################
@@ -690,6 +690,46 @@ def convert_table_rotation(document):
       i += 1
 
 
+def convert_listoflistings(document):
+    'Convert ERT \lstlistoflistings to TOC lstlistoflistings inset'
+    # We can support roundtrip because the command is so simple
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset ERT", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(document.body, i)
+        if j == -1:
+            document.warning("Malformed lyx document: Can't find end of ERT inset")
+            i += 1
+            continue
+        ert = get_ert(document.body, i)
+        if ert == "\\lstlistoflistings{}":
+            document.body[i:j] = ["\\begin_inset CommandInset toc", "LatexCommand lstlistoflistings", ""]
+            i = i + 4
+        else:
+            i = j + 1
+
+
+def revert_listoflistings(document):
+    'Convert TOC lstlistoflistings inset to ERT lstlistoflistings'
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset CommandInset toc", i)
+        if i == -1:
+            return
+        if document.body[i+1] == "LatexCommand lstlistoflistings":
+            j = find_end_of_inset(document.body, i)
+            if j == -1:
+                document.warning("Malformed lyx document: Can't find end of TOC inset")
+                i += 1
+                continue
+            subst = put_cmd_in_ert("\\lstlistoflistings{}")
+            document.body[i:j+1] = subst
+            add_to_preamble(document, ["\\usepackage{listings}"])
+        i = i + 1
+
+
 ##
 # Conversion hub
 #
@@ -711,10 +751,12 @@ convert = [
            [426, []],
            [427, []],
            [428, [convert_cell_rotation]],
-           [429, [convert_table_rotation]]
+           [429, [convert_table_rotation]],
+           [430, [convert_listoflistings]],
           ]
 
 revert =  [
+           [429, [revert_listoflistings]],
            [428, [revert_table_rotation]],
            [427, [revert_cell_rotation]],
            [426, [revert_tipa]],
index 6ab91f3a18ccd7cb2bbae546ee367b0aa5c94057..9a88185dde00428ae7f5b6601fffce7d8c4d0fac 100644 (file)
@@ -451,6 +451,7 @@ Menuset
                Item "Table of Contents|C" "inset-insert toc"
                FloatListInsert
                IndicesLists
+               Item "List of Listings|L" "inset-insert toc CommandInset toc LatexCommand lstlistoflistings \end_inset"
                Item "Nomenclature|N" "nomencl-print"
                Item "BibTeX Bibliography...|B" "dialog-show-new-inset bibtex"
        End
index 80c9db97d952a6ab773ca96eca3f0675fbc5f7a8..345b40ea21759a1ec20f42336a43b86fcd8b1b18 100644 (file)
@@ -496,9 +496,11 @@ void TocWidget::filterContents()
 static QString decodeType(QString const & str)
 {
        QString type = str;
-       if (type.contains("tableofcontents")) {
+       if (type.contains("tableofcontents"))
                type = "tableofcontents";
-       } else if (type.contains("floatlist")) {
+       else if (type.contains("lstlistoflistings"))
+               type = "listing";
+       else if (type.contains("floatlist")) {
                if (type.contains("\"figure"))
                        type = "figure";
                else if (type.contains("\"table"))
index 0ab4b65816f7d19e5da333fa2a9fae925f75c150..3b407b7eb4dd5f5429d9ce46544b1d2ad98a5bab 100644 (file)
@@ -37,6 +37,15 @@ using namespace std;
 
 namespace lyx {
 
+namespace {
+string cmd2type(string const & cmd)
+{
+       if (cmd == "lstlistoflistings")
+               return "listing";
+       return cmd;
+}
+}
+
 
 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
        : InsetCommand(buf, p)
@@ -53,10 +62,18 @@ ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
 }
 
 
+bool InsetTOC::isCompatibleCommand(string const & cmd)
+{
+       return cmd == defaultCommand() || cmd == "lstlistoflistings";
+}
+
+
 docstring InsetTOC::screenLabel() const
 {
        if (getCmdName() == "tableofcontents")
                return buffer().B_("Table of Contents");
+       if (getCmdName() == "lstlistoflistings")
+               return buffer().B_("List of Listings");
        return _("Unknown TOC type");
 }
 
@@ -76,10 +93,27 @@ void InsetTOC::doDispatch(Cursor & cur, FuncRequest & cmd) {
 }
 
 
+docstring InsetTOC::layoutName() const
+{
+       if (getCmdName() == "lstlistoflistings")
+               return from_ascii("ListOfListings");
+       return docstring();
+}
+
+
+void InsetTOC::validate(LaTeXFeatures & features) const
+{
+       InsetCommand::validate(features);
+       features.useInsetLayout(getLayout());
+       if (getCmdName() == "lstlistoflistings")
+               features.require("listings");
+}
+
+
 int InsetTOC::plaintext(odocstream & os, OutputParams const &) const
 {
        os << screenLabel() << "\n\n";
-       buffer().tocBackend().writePlaintextTocList(getCmdName(), os);
+       buffer().tocBackend().writePlaintextTocList(cmd2type(getCmdName()), os);
        return PLAINTEXT_NEWLINE;
 }
 
@@ -94,6 +128,9 @@ int InsetTOC::docbook(odocstream & os, OutputParams const &) const
 
 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
 {
+       if (getCmdName() != "tableofcontents")
+               return docstring();
+
        Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
        string const & tocclass = lay.defaultCSSClass();
        string const tocattr = "class='tochead " + tocclass + "'";
@@ -104,15 +141,14 @@ docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
        odocstringstream ods;
        XHTMLStream xs(ods);
 
-       Toc const & toc = buffer().tocBackend().toc("tableofcontents");
+       Toc const & toc = buffer().tocBackend().toc(cmd2type(getCmdName()));
        if (toc.empty())
                return docstring();
 
        xs << html::StartTag("div", "class='toc'");
 
        // Title of TOC
-       static string toctitle = N_("Table of Contents");
-       docstring title = buffer().B_(toctitle);
+       docstring title = screenLabel();
        xs << html::StartTag("div", tocattr)
                 << title
                 << html::EndTag("div");
index 9ba7cf06f3de98d527014932d087ed8c725ef435..e6e932b3f1b06d0baa3aee6549f0bfe3e02a3058 100644 (file)
@@ -19,8 +19,8 @@ namespace lyx {
 
 
 /// Used to insert table of contents and similar lists
-/// at present, supports only \tableofcontents. Other
-/// such commands, such as \listoffigures, are supported
+/// at present, supports only \tableofcontents and \listoflistings.
+/// Other such commands, such as \listoffigures, are supported
 /// by InsetFloatList.
 class InsetTOC : public InsetCommand {
 public:
@@ -32,8 +32,12 @@ public:
        ///
        InsetCode lyxCode() const { return TOC_CODE; }
        ///
+       docstring layoutName() const;
+       ///
        DisplayType display() const { return AlignCenter; }
        ///
+       virtual void validate(LaTeXFeatures &) const;
+       ///
        int plaintext(odocstream &, OutputParams const &) const;
        ///
        int docbook(odocstream &, OutputParams const &) const;
@@ -52,8 +56,7 @@ public:
        ///
        static std::string defaultCommand() { return "tableofcontents"; }
        ///
-       static bool isCompatibleCommand(std::string const & cmd)
-               { return cmd == defaultCommand(); }
+       static bool isCompatibleCommand(std::string const & cmd);
        //@}
 
 private:
index 9d9be0dbfbef62777fa9beefda8c9b8ce4162083..71a16802536977cf91d4b5604308ef870e911ce3 100644 (file)
@@ -43,6 +43,7 @@
 \begin{document}
 
 \tableofcontents
+\lstlistoflistings
 
 \noindent This paragraph is not indented.
 
index 2f59222ed7deeeaa7725ceb9d737fb31236f1a62..db7e7c6d35ed09b33f018ee55eb5bb3bbe53ac19 100644 (file)
@@ -2815,11 +2815,13 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
                                handle_ert(os, t.asInput(), context);
                }
 
-               else if (t.cs() == "tableofcontents") {
+               else if (t.cs() == "tableofcontents" || t.cs() == "lstlistoflistings") {
                        context.check_layout(os);
-                       begin_command_inset(os, "toc", "tableofcontents");
+                       begin_command_inset(os, "toc", t.cs());
                        end_inset(os);
                        skip_spaces_braces(p);
+                       if (t.cs() == "lstlistoflistings")
+                               preamble.registerAutomaticallyLoadedPackage("listings");
                }
 
                else if (t.cs() == "listoffigures") {
index 02bc516c63f9014b2f00654c46bb24278adca387..8329f8a2876c3731a5ae0109400418bb7ad9f93d 100644 (file)
@@ -30,8 +30,8 @@ extern char const * const lyx_version_info;
 
 // Do not remove the comment below, so we get merge conflict in
 // independent branches. Instead add your own.
-#define LYX_FORMAT_LYX 429 // uwestoehr: rotated tables
-#define LYX_FORMAT_TEX2LYX 429 // uwestoehr: rotated tables
+#define LYX_FORMAT_LYX 430 // gb: listoflistings
+#define LYX_FORMAT_TEX2LYX 430 // gb: listoflistings
 
 #if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX
 #ifndef _MSC_VER