]> git.lyx.org Git - lyx.git/blob - src/insets/InsetIndex.cpp
Replace home made temp file creation with safer (and cleaner) Qt' solution. Should...
[lyx.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         int i = 7;
52         odocstringstream ods;
53         InsetText::latex(ods, runparams);
54         odocstringstream ods2;
55         InsetText::plaintext(ods2, runparams);
56         std::vector<docstring> const levels =
57                 getVectorFromString(ods.str(), from_ascii("!"), true);
58         std::vector<docstring> const levels_plain =
59                 getVectorFromString(ods2.str(), from_ascii("!"), true);
60         vector<docstring>::const_iterator it = levels.begin();
61         vector<docstring>::const_iterator end = levels.end();
62         vector<docstring>::const_iterator it2 = levels_plain.begin();
63         for (; it != end; ++it) {
64                 if (it > levels.begin()) {
65                         os << '!';
66                         i += 1;
67                 }
68                 // correctly sort macros and formatted strings
69                 // if we do find a command, prepend a plain text
70                 // version of the content to get sorting right,
71                 // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}
72                 // Don't do that if the user entered '@' himself, though.
73                 if (contains(*it, '\\') && !contains(*it, '@')) {
74                         // Plaintext might return nothing (e.g. for ERTs)
75                         docstring spart =
76                                 (it2 < levels_plain.end()
77                                  && !(*it2).empty()) ? *it2 : *it;
78                         // remove remaining \'s for the sorting part
79                         docstring const ppart =
80                                 subst(spart, from_ascii("\\"), docstring());
81                         os << ppart;
82                         os << '@';
83                         i += ppart.size() + 1;
84                 }
85                 docstring const tpart = *it;
86                 os << tpart;
87                 i += tpart.size();
88                 if (it2 < levels_plain.end())
89                         ++it2;
90         }
91         os << '}';
92         i += 1;
93         return i;
94 }
95
96
97 int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
98 {
99         os << "<indexterm><primary>";
100         int const i = InsetText::docbook(os, runparams);
101         os << "</primary></indexterm>";
102         return i;
103 }
104
105
106 void InsetIndex::write(ostream & os) const
107 {
108         os << to_utf8(name()) << "\n";
109         InsetCollapsable::write(os);
110 }
111
112
113 void InsetIndex::addToToc(DocIterator const & cpit)
114 {
115         DocIterator pit = cpit;
116         pit.push_back(CursorSlice(*this));
117
118         Toc & toc = buffer().tocBackend().toc("index");
119         docstring str;
120         str = getNewLabel(str);
121         toc.push_back(TocItem(pit, 0, str));
122         // Proceed with the rest of the inset.
123         InsetCollapsable::addToToc(cpit);
124 }
125
126
127 /////////////////////////////////////////////////////////////////////
128 //
129 // InsetPrintIndex
130 //
131 ///////////////////////////////////////////////////////////////////////
132
133 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
134         : InsetCommand(p, string())
135 {}
136
137
138 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
139 {
140         static ParamInfo param_info_;
141         if (param_info_.empty())
142                 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
143         return param_info_;
144 }
145
146
147 docstring InsetPrintIndex::screenLabel() const
148 {
149         return _("Index");
150 }
151
152
153 void InsetPrintIndex::validate(LaTeXFeatures & features) const
154 {
155         features.require("makeidx");
156 }
157
158
159 InsetCode InsetPrintIndex::lyxCode() const
160 {
161         return INDEX_PRINT_CODE;
162 }
163
164 } // namespace lyx