]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetIndex.cpp
Inset::addToToc(): change signature. Use DocIterator instead of ParConstIterator...
[lyx.git] / src / insets / InsetIndex.cpp
index d92d3852710a19783367273d1d9a87066ec7ebf4..9733467679e0d8c2ba355f15abc659ca76f6e8f9 100644 (file)
 #include "sgml.h"
 #include "TocBackend.h"
 
+#include "support/docstream.h"
 #include "support/gettext.h"
+#include "support/lstrings.h"
 
 #include <ostream>
 
 using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
@@ -45,11 +48,30 @@ int InsetIndex::latex(odocstream & os,
 {
        os << "\\index";
        os << '{';
-       if (hasFontChanges()) {
-               InsetText::plaintext(os, runparams);
-               os << '@';
+       odocstringstream ods;
+       int i = InsetText::latex(ods, runparams);
+       bool sorted = false;
+       // correctly sort macros and formatted strings
+       // if we do find a command, prepend a plain text
+       // version of the content to get sorting right,
+       // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}
+       // Don't do that if the user entered '@' himself, though.
+       if (contains(ods.str(), '\\') && !contains(ods.str(), '@')) {
+               odocstringstream odss;
+               if (InsetText::plaintext(odss, runparams) > 0) {
+                       // remove remaining \'s for the sorting part
+                       os << subst(odss.str(), from_ascii("\\"), docstring());
+                       os << '@';
+                       sorted = true;
+               }
        }
-       int i = InsetText::latex(os, runparams);
+       // if a hierarchy tag '!' is used, ommit this in the post-@ part.
+       if (sorted && contains(ods.str(), '!')) {
+               string dummy;
+               // FIXME unicode
+               os << from_utf8(rsplit(to_utf8(ods.str()), dummy, '!'));
+       } else
+               i = InsetText::latex(os, runparams);
        os << '}';
        return i;
 }
@@ -71,10 +93,10 @@ void InsetIndex::write(ostream & os) const
 }
 
 
-void InsetIndex::addToToc(ParConstIterator const & cpit) const
+void InsetIndex::addToToc(DocIterator const & cpit)
 {
-       ParConstIterator pit = cpit;
-       pit.push_back(*this);
+       DocIterator pit = cpit;
+       pit.push_back(CursorSlice(*this));
 
        Toc & toc = buffer().tocBackend().toc("index");
        docstring str;
@@ -83,16 +105,6 @@ void InsetIndex::addToToc(ParConstIterator const & cpit) const
 }
 
 
-bool InsetIndex::hasFontChanges() const
-{
-       // we only have one par
-       Paragraph par = paragraphs().back();
-       FontSpan const font_span = par.fontSpan(0);
-       Font firstfont = par.getFirstFontSettings(buffer().params());
-       return (firstfont.fontInfo() != inherit_font || par.size() > font_span.last + 1);
-}
-
-
 /////////////////////////////////////////////////////////////////////
 //
 // InsetPrintIndex