On Mon, Sep 24, 2007 at 06:31:03AM +0300, Martin Vermeer wrote:
> On Sun, Sep 23, 2007 at 06:06:23AM +0300, Martin Vermeer wrote:
> > On Sat, Sep 22, 2007 at 06:49:34PM +0200, Jürgen Spitzmüller wrote:
> > > Martin Vermeer wrote:
> > > > Actually have a look at the attached. I somewhat changed my mind since
> > > > the previous reply: what is inside an index inset is real LaTeX, so
> > > > logically it should be a real LyXText. (Yes, there are some limitations
> > > > such as no linebreaks allowed; but you get the idea.)
> > > 
> > > Yes, this was the idea.
> > > 
> > > > The attached does everything the old index inset does wrt. indexing the
> > > > selection and the previous/surrounding word. And it collapses to a
> > > > really small button, and can be opened by "Open all insets". Which the
> > > > old one did not...
> > > 
> > > I like it.
> > > 
> > > Jürgen
> > 
> > This requires a lyx2lyx entry, if we change over to this.
> > 
> > The next step would be to provide a \see inset that is only
> > valid inside an index entry. It would be good to have a general
> > mechanism for this.
> > 
> > (Does this solution lose any functionality wrt. the old one?
> > What other objections are there?)
>  
> Actually I see several problems.
> 
> 1) DocBook. Both the index inset and nomencl contain special code for
> DocBook.
> 
> 2) Preamble/validate stuff. At least makeidx checks for being provided
> by the class. 
> 
> I suppose 2) could be addressed by adding a "Require" item to the
> flexinset layout parameters. 1) is trickier.
> 
> Any ideas?

The proper way for this is, it seems, to keep the hardwired InsetIndex,
but make it a collapsable. The attached patch does this. The inset is
now configurable from the layout files. It still works without feature 
loss.

This will need a lyx2lyx entry, but I don't quite know how to do it
yet. I'll come back to that.

- Martin

Index: src/insets/InsetIndex.cpp
===================================================================
--- src/insets/InsetIndex.cpp	(revision 20348)
+++ src/insets/InsetIndex.cpp	(working copy)
@@ -27,41 +27,47 @@
 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);
 }
 
 
-
 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
 	: InsetCommand(p, string())
 {}
Index: src/insets/InsetIndex.h
===================================================================
--- src/insets/InsetIndex.h	(revision 20348)
+++ src/insets/InsetIndex.h	(working copy)
@@ -13,6 +13,7 @@
 #define INSET_INDEX_H
 
 
+#include "InsetCollapsable.h"
 #include "InsetCommand.h"
 
 
@@ -22,23 +23,25 @@
 
 /** 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;
 	///
+	docstring name() const { return from_ascii("Index"); }
+	///
+	void write(Buffer const & buf, std::ostream & os) const;
+	///
 	int docbook(Buffer const &, odocstream &,
 		    OutputParams const &) const;
 private:
-	virtual Inset * clone() const {
-		return new InsetIndex(params());
-	}
+	virtual Inset * clone() const;
 };
 
 
Index: src/factory.cpp
===================================================================
--- src/factory.cpp	(revision 20348)
+++ 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");
@@ -289,11 +283,8 @@
 				InsetIncludeMailer::string2params(to_utf8(cmd.argument()), iip);
 				return new InsetInclude(iip);
 
-			} else if (name == "index") {
-				InsetCommandParams icp(name);
-				InsetCommandMailer::string2params(name, to_utf8(cmd.argument()),
-					icp);
-				return new InsetIndex(icp);
+			} else if (name == "Index") {
+				return new InsetIndex(params);
 
 			} else if (name == "nomenclature") {
 				InsetCommandParams icp(name);
@@ -414,8 +405,9 @@
 			inset.reset(new InsetBibitem(inscmd));
 		} else if (cmdName == "bibtex") {
 			inset.reset(new InsetBibtex(inscmd));
+		// (Is this stuff obsolete)?
 		} else if (cmdName == "index") {
-			inset.reset(new InsetIndex(inscmd));
+			inset.reset(new InsetIndex(buf.params()));
 		} else if (cmdName == "nomenclature") {
 			inset.reset(new InsetNomencl(inscmd));
 		} else if (cmdName == "include") {
@@ -517,6 +509,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 20348)
+++ src/Text3.cpp	(working copy)
@@ -49,6 +49,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"
@@ -211,23 +212,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;
@@ -1193,6 +1203,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: lib/layouts/stdinsets.inc
===================================================================
--- lib/layouts/stdinsets.inc	(revision 20348)
+++ lib/layouts/stdinsets.inc	(working copy)
@@ -101,4 +101,14 @@
 	EndFont
 End
 
+InsetLayout Index
+	LabelString           Idx
+	LatexType             command
+	LatexName             index
+	Decoration            minimalistic
+	LabelFont
+	  Color               Red
+	  Size                Small
+	EndFont
+End
 

Reply via email to