]> 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 b2ba741c2bf37f50e6abf368c28c41ecb7e2f51c..9733467679e0d8c2ba355f15abc659ca76f6e8f9 100644 (file)
 
 #include "InsetIndex.h"
 
+#include "Buffer.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
-#include "gettext.h"
+#include "FuncStatus.h"
 #include "LaTeXFeatures.h"
 #include "MetricsInfo.h"
 #include "sgml.h"
+#include "TocBackend.h"
 
-#include "support/std_ostream.h"
+#include "support/docstream.h"
+#include "support/gettext.h"
+#include "support/lstrings.h"
 
+#include <ostream>
+
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
-using std::string;
-using std::ostream;
+/////////////////////////////////////////////////////////////////////
+//
+// InsetIndex
+//
+///////////////////////////////////////////////////////////////////////
 
 
-InsetIndex::InsetIndex(InsetCommandParams const & p)
-       : InsetCommand(p, "index")
+InsetIndex::InsetIndex(Buffer const & buf)
+       : InsetCollapsable(buf)
 {}
 
 
-docstring const InsetIndex::getScreenLabel(Buffer const &) const
+int InsetIndex::latex(odocstream & os,
+                         OutputParams const & runparams) const
 {
-       size_t const maxLabelChars = 25;
-
-       docstring label = "Idx:" + getParam("name");
-       if (label.size() > maxLabelChars) {
-               label.erase(maxLabelChars - 3);
-               label += "...";
+       os << "\\index";
+       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;
+               }
        }
-       return label;
+       // 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;
+}
+
+
+int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
+{
+       os << "<indexterm><primary>";
+       int const i = InsetText::docbook(os, runparams);
+       os << "</primary></indexterm>";
+       return i;
 }
 
 
-int InsetIndex::docbook(Buffer const &, odocstream & os,
-                       OutputParams const &) const
+void InsetIndex::write(ostream & os) const
 {
-       os << "<indexterm><primary>"
-          << sgml::escapeString(getParam("name"))
-          << "</primary></indexterm>";
-       return 0;
+       os << to_utf8(name()) << "\n";
+       InsetCollapsable::write(os);
 }
 
 
-Inset::Code InsetIndex::lyxCode() const
+void InsetIndex::addToToc(DocIterator const & cpit)
 {
-       return Inset::INDEX_CODE;
+       DocIterator pit = cpit;
+       pit.push_back(CursorSlice(*this));
+
+       Toc & toc = buffer().tocBackend().toc("index");
+       docstring str;
+       str = getNewLabel(str);
+       toc.push_back(TocItem(pit, 0, str));
 }
 
 
+/////////////////////////////////////////////////////////////////////
+//
+// InsetPrintIndex
+//
+///////////////////////////////////////////////////////////////////////
 
 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
        : InsetCommand(p, string())
 {}
 
 
-docstring const InsetPrintIndex::getScreenLabel(Buffer const &) const
+ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
+{
+       static ParamInfo param_info_;
+       if (param_info_.empty())
+               param_info_.add("name", ParamInfo::LATEX_REQUIRED);
+       return param_info_;
+}
+
+
+docstring InsetPrintIndex::screenLabel() const
 {
        return _("Index");
 }
@@ -79,10 +137,9 @@ void InsetPrintIndex::validate(LaTeXFeatures & features) const
 }
 
 
-Inset::Code InsetPrintIndex::lyxCode() const
+InsetCode InsetPrintIndex::lyxCode() const
 {
-       return Inset::INDEX_PRINT_CODE;
+       return INDEX_PRINT_CODE;
 }
 
-
 } // namespace lyx