]> git.lyx.org Git - features.git/blob - src/insets/InsetIndex.cpp
fix bug 4837.
[features.git] / src / insets / InsetIndex.cpp
1 /**
2  * \file InsetIndex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 #include <config.h>
11
12 #include "InsetIndex.h"
13
14 #include "Buffer.h"
15 #include "DispatchResult.h"
16 #include "FuncRequest.h"
17 #include "FuncStatus.h"
18 #include "LaTeXFeatures.h"
19 #include "MetricsInfo.h"
20 #include "sgml.h"
21 #include "TocBackend.h"
22
23 #include "support/docstream.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
26
27 #include <ostream>
28
29 using namespace std;
30 using namespace lyx::support;
31
32 namespace lyx {
33
34 /////////////////////////////////////////////////////////////////////
35 //
36 // InsetIndex
37 //
38 ///////////////////////////////////////////////////////////////////////
39
40
41 InsetIndex::InsetIndex(Buffer const & buf)
42         : InsetCollapsable(buf)
43 {}
44
45
46 int InsetIndex::latex(odocstream & os,
47                           OutputParams const & runparams) const
48 {
49         os << "\\index";
50         os << '{';
51         odocstringstream ods;
52         int i = InsetText::latex(ods, runparams);
53         bool sorted = false;
54         // correctly sort macros and formatted strings
55         // if we do find a command, prepend a plain text
56         // version of the content to get sorting right,
57         // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}
58         // Don't do that if the user entered '@' himself, though.
59         if (contains(ods.str(), '\\') && !contains(ods.str(), '@')) {
60                 if (InsetText::plaintext(os, runparams) > 0) {
61                         os << '@';
62                         sorted = true;
63                 }
64         }
65         // if a hierarchy tag '!' is used, ommit this in the post-@ part.
66         if (sorted && contains(ods.str(), '!')) {
67                 string dummy;
68                 // FIXME unicode
69                 os << from_utf8(rsplit(to_utf8(ods.str()), dummy, '!'));
70         } else
71                 i = InsetText::latex(os, runparams);
72         os << '}';
73         return i;
74 }
75
76
77 int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
78 {
79         os << "<indexterm><primary>";
80         int const i = InsetText::docbook(os, runparams);
81         os << "</primary></indexterm>";
82         return i;
83 }
84
85
86 void InsetIndex::write(ostream & os) const
87 {
88         os << to_utf8(name()) << "\n";
89         InsetCollapsable::write(os);
90 }
91
92
93 void InsetIndex::addToToc(ParConstIterator const & cpit) const
94 {
95         ParConstIterator pit = cpit;
96         pit.push_back(*this);
97
98         Toc & toc = buffer().tocBackend().toc("index");
99         docstring str;
100         str = getNewLabel(str);
101         toc.push_back(TocItem(pit, 0, str));
102 }
103
104
105 /////////////////////////////////////////////////////////////////////
106 //
107 // InsetPrintIndex
108 //
109 ///////////////////////////////////////////////////////////////////////
110
111 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
112         : InsetCommand(p, string())
113 {}
114
115
116 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
117 {
118         static ParamInfo param_info_;
119         if (param_info_.empty())
120                 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
121         return param_info_;
122 }
123
124
125 docstring InsetPrintIndex::screenLabel() const
126 {
127         return _("Index");
128 }
129
130
131 void InsetPrintIndex::validate(LaTeXFeatures & features) const
132 {
133         features.require("makeidx");
134 }
135
136
137 InsetCode InsetPrintIndex::lyxCode() const
138 {
139         return INDEX_PRINT_CODE;
140 }
141
142 } // namespace lyx