The attached contains everything including the lyx2lyx stuff.

OK to commit?

- Martin
Index: src/insets/InsetIndex.cpp
===================================================================
--- src/insets/InsetIndex.cpp	(revision 20607)
+++ src/insets/InsetIndex.cpp	(working copy)
@@ -27,41 +27,75 @@
 using std::ostream;
 
 
-InsetIndex::InsetIndex(InsetCommandParams const & p)
-	: InsetCommand(p, "index")
+InsetIndex::InsetIndex(BufferParams const & bp)
+	: InsetCollapsable(bp)
+{
+	setLayout(bp);
+}
+
+
+InsetIndex::InsetIndex(InsetIndex const & in)
+	: InsetCollapsable(in)
 {}
 
 
-docstring const InsetIndex::getScreenLabel(Buffer const &) const
+int InsetIndex::docbook(Buffer const & buf, odocstream & os,
+			OutputParams const & runparams) const
 {
-	size_t const maxLabelChars = 25;
+	os << "<indexterm><primary>";
+	int const i = InsetText::docbook(buf, os, runparams);
+	os << "</primary></indexterm>";
+	return i;
+}
 
-	docstring label = _("Idx: ") + getParam("name");
-	if (label.size() > maxLabelChars) {
-		label.erase(maxLabelChars - 3);
-		label += "...";
-	}
-	return label;
+
+Inset::Code InsetIndex::lyxCode() const
+{
+	return Inset::INDEX_CODE;
 }
 
 
-int InsetIndex::docbook(Buffer const &, odocstream & os,
-			OutputParams const &) const
+Inset * InsetIndex::clone() const
 {
-	os << "<indexterm><primary>"
-	   << sgml::escapeString(getParam("name"))
-	   << "</primary></indexterm>";
-	return 0;
+	return new InsetIndex(*this);
 }
 
 
-Inset::Code InsetIndex::lyxCode() const
+void InsetIndex::write(Buffer const & buf, std::ostream & os) const
 {
-	return Inset::INDEX_CODE;
+	os << to_utf8(name()) << "\n";
+	InsetCollapsable::write(buf, os);
 }
 
 
+void InsetIndex::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+	Font tmpfont = mi.base.font;
+	getDrawFont(mi.base.font);
+	mi.base.font.realize(tmpfont);
+	InsetCollapsable::metrics(mi, dim);
+	mi.base.font = tmpfont;
+}
 
+
+void InsetIndex::draw(PainterInfo & pi, int x, int y) const
+{
+	Font tmpfont = pi.base.font;
+	getDrawFont(pi.base.font);
+	pi.base.font.realize(tmpfont);
+	InsetCollapsable::draw(pi, x, y);
+	pi.base.font = tmpfont;
+}
+
+
+void InsetIndex::getDrawFont(Font & font) const
+{
+	font = Font(Font::ALL_INHERIT);
+	font.realize(layout_.font);
+}
+
+
+
 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
 	: InsetCommand(p, string())
 {}
Index: src/insets/InsetIndex.h
===================================================================
--- src/insets/InsetIndex.h	(revision 20607)
+++ src/insets/InsetIndex.h	(working copy)
@@ -13,6 +13,7 @@
 #define INSET_INDEX_H
 
 
+#include "InsetCollapsable.h"
 #include "InsetCommand.h"
 
 
@@ -22,23 +23,35 @@
 
 /** Used to insert index labels
   */
-class InsetIndex : public InsetCommand {
+class InsetIndex : public InsetCollapsable {
 public:
 	///
-	InsetIndex(InsetCommandParams const &);
+	InsetIndex(BufferParams const &);
 	///
-	docstring const getScreenLabel(Buffer const &) const;
+	InsetIndex(InsetIndex const &);
 	///
 	EDITABLE editable() const { return IS_EDITABLE; }
 	///
 	Inset::Code lyxCode() const;
 	///
+	///
+	void metrics(MetricsInfo &, Dimension &) const;
+	///
+	void draw(PainterInfo &, int, int) const;
+	///
+	docstring name() const { return from_ascii("Index"); }
+	///
+	void getDrawFont(Font &) const;
+	///
+	void write(Buffer const & buf, std::ostream & os) const;
+	///
 	int docbook(Buffer const &, odocstream &,
 		    OutputParams const &) const;
+	/// should paragraph indendation be omitted in any case?
+	bool neverIndent(Buffer const &) const { return true; }
+
 private:
-	virtual Inset * clone() const {
-		return new InsetIndex(params());
-	}
+	virtual Inset * clone() const;
 };
 
 
Index: src/factory.cpp
===================================================================
--- src/factory.cpp	(revision 20607)
+++ src/factory.cpp	(working copy)
@@ -177,14 +177,8 @@
 			return 0;
 		}
 
-		case LFUN_INDEX_INSERT: {
-			// Try and generate a valid index entry.
-			InsetCommandParams icp("index");
-			icp["name"] = cmd.argument().empty() ?
-				bv->cursor().innerText()->getStringToIndex(bv->cursor()) :
-				cmd.argument();
-			return new InsetIndex(icp);
-		}
+		case LFUN_INDEX_INSERT:
+			return new InsetIndex(params);
 
 		case LFUN_NOMENCL_INSERT: {
 			InsetCommandParams icp("nomenclature");
@@ -290,10 +284,7 @@
 				return new InsetInclude(iip);
 
 			} else if (name == "index") {
-				InsetCommandParams icp(name);
-				InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
-					icp);
-				return new InsetIndex(icp);
+				return new InsetIndex(params);
 
 			} else if (name == "nomenclature") {
 				InsetCommandParams icp(name);
@@ -412,7 +403,7 @@
 		} else if (insetType == "bibtex") {
 			inset.reset(new InsetBibtex(inscmd));
 		} else if (insetType == "index") {
-			inset.reset(new InsetIndex(inscmd));
+			inset.reset(new InsetIndex(buf.params()));
 		} else if (insetType == "nomenclature") {
 			inset.reset(new InsetNomencl(inscmd));
 		} else if (insetType == "include") {
@@ -502,6 +493,8 @@
 #endif
 		} else if (tmptok == "Caption") {
 			inset.reset(new InsetCaption(buf.params()));
+		} else if (tmptok == "Index") {
+			inset.reset(new InsetIndex(buf.params()));
 		} else if (tmptok == "FloatList") {
 			inset.reset(new InsetFloatList);
 		} else {
Index: src/Text3.cpp
===================================================================
--- src/Text3.cpp	(revision 20607)
+++ src/Text3.cpp	(working copy)
@@ -50,6 +50,7 @@
 #include "frontends/Clipboard.h"
 #include "frontends/Selection.h"
 
+#include "insets/InsetCollapsable.h"
 #include "insets/InsetCommand.h"
 #include "insets/InsetFloatList.h"
 #include "insets/InsetNewline.h"
@@ -192,23 +193,32 @@
 		return false;
 
 	recordUndo(cur);
-	bool gotsel = false;
-	if (cur.selection()) {
-		lyx::dispatch(FuncRequest(LFUN_CUT));
-		gotsel = true;
-	}
-	text->insertInset(cur, inset);
+	if (cmd.action == LFUN_INDEX_INSERT) {
+		docstring const ds = text->getStringToIndex(cur);
+		text->insertInset(cur, inset);
+		if (edit)
+			inset->edit(cur, true);
+		// Now put this into inset
+		static_cast<InsetCollapsable *>(inset)->text_.insertStringAsParagraphs(cur, ds);
+	} else {
+		bool gotsel = false;
+		if (cur.selection()) {
+			lyx::dispatch(FuncRequest(LFUN_CUT));
+			gotsel = true;
+		}
+		text->insertInset(cur, inset);
 
-	if (edit)
-		inset->edit(cur, true);
+		if (edit)
+			inset->edit(cur, true);
 
-	if (gotsel && pastesel) {
-		lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
-		// reset first par to default
-		if (cur.lastpit() != 0 || cur.lastpos() != 0) {
-			LayoutPtr const layout =
-				cur.buffer().params().getTextClass().defaultLayout();
-			cur.text()->paragraphs().begin()->layout(layout);
+		if (gotsel && pastesel) {
+			lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
+			// reset first par to default
+			if (cur.lastpit() != 0 || cur.lastpos() != 0) {
+				LayoutPtr const layout =
+					cur.buffer().params().getTextClass().defaultLayout();
+				cur.text()->paragraphs().begin()->layout(layout);
+			}
 		}
 	}
 	return true;
@@ -1177,6 +1187,9 @@
 	}
 
 	case LFUN_INDEX_INSERT:
+		doInsertInset(cur, this, cmd, true, true);
+		cur.posRight();
+		break;
 	case LFUN_NOMENCL_INSERT: {
 		Inset * inset = createInset(&cur.bv(), cmd);
 		if (!inset)
Index: src/Buffer.cpp
===================================================================
--- src/Buffer.cpp	(revision 20607)
+++ src/Buffer.cpp	(working copy)
@@ -144,7 +144,7 @@
 
 namespace {
 
-int const LYX_FORMAT = 288; //RGH, command insets
+int const LYX_FORMAT = 289; //MV, index collapsable
 
 } // namespace anon
 
Index: lib/lyx2lyx/LyX.py
===================================================================
--- lib/lyx2lyx/LyX.py	(revision 20665)
+++ lib/lyx2lyx/LyX.py	(working copy)
@@ -80,7 +80,7 @@
                    ("1_3",     [221], minor_versions("1.3" , 7)),
                    ("1_4", range(222,246), minor_versions("1.4" , 5)),
                    ("1_5", range(246,277), minor_versions("1.5" , 1)),
-                   ("1_6", range(277,289), minor_versions("1.6" , 0))] #RGH, command insets
+                   ("1_6", range(277,290), minor_versions("1.6" , 0))] #MV, index collapsable
 
 
 def formats_list():
Index: lib/lyx2lyx/lyx_1_6.py
===================================================================
--- lib/lyx2lyx/lyx_1_6.py	(revision 20665)
+++ lib/lyx2lyx/lyx_1_6.py	(working copy)
@@ -353,13 +353,49 @@
         i = i + 1
 
 
+def convert_latexcommand_index(document):
+    "Convert from LatexCommand form to collapsable form."
+    i = 0 
+    while True:
+        i = find_token(document.body, "\\begin_inset CommandInset index", i)
+        if i == -1:
+            return
+        if document.body[i + 1] != "LatexCommand index": # Might also be index_print
+            return
+        fullcommand = document.body[i + 2]
+        document.body[i] = "\\begin_inset Index"
+        document.body[i + 1] = "status collapsed"
+        document.body[i + 2] = "\\begin_layout standard"
+        document.body.insert(i + 3, fullcommand[6:].strip('"'))
+        document.body.insert(i + 4, "\\end_layout")
+        i = i + 5
+
+
+def revert_latexcommand_index(document):
+    "Revert from collapsable form toLatexCommand form."
+    i = 0
+    while True:
+        i = find_token(document.body, "\\begin_inset Index", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(document.body, i)
+        del document.body[j - 1]
+        del document.body[j - 2] # \end_layout
+        document.body[i] =  "\\begin_inset CommandInset index"
+        document.body[i + 1] =  "LatexCommand index"
+        document.body[i + 3] = "name " + '"' + document.body[i + 3] + '"'
+        document.body.insert(i + 4, "")
+        del document.body[i + 2] # \begin_layout standard
+        i = i + 5
+
+
+
 ##
 # Conversion hub
 #
 
 supported_versions = ["1.6.0","1.6"]
-convert = [
-           [277, [fix_wrong_tables]],
+convert = [[277, [fix_wrong_tables]],
            [278, [close_begin_deeper]],
            [279, [long_charstyle_names]],
            [280, [axe_show_label]],
@@ -370,10 +406,12 @@
            [285, []], # an empty manifest is automatically added
            [286, []],
            [287, [convert_wrapfig_options]],
-           [288, [convert_inset_command]]
+           [288, [convert_inset_command]],
+           [289, [convert_latexcommand_index]]
           ]
 
 revert =  [
+           [288, [revert_latexcommand_index]],
            [287, [revert_inset_command]],
            [286, [revert_wrapfig_options]],
            [285, [revert_pdf_options]],
Index: lib/layouts/stdinsets.inc
===================================================================
--- lib/layouts/stdinsets.inc	(revision 20607)
+++ lib/layouts/stdinsets.inc	(working copy)
@@ -101,4 +101,18 @@
 	EndFont
 End
 
+InsetLayout Index
+	LabelString           Idx
+	LatexType             command
+	LatexName             index
+	Decoration            minimalistic
+	Font
+	  Color               Green
+	  Size                Small
+	EndFont
+	LabelFont
+	  Color               Green
+	  Size                Small
+	EndFont
+End
 

Reply via email to