]> git.lyx.org Git - features.git/blob - src/insets/InsetIndex.cpp
* src/insets/InsetIndex.cpp (output_latex):
[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         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("!"));
58         std::vector<docstring> const levels_plain =
59                 getVectorFromString(ods2.str(), from_ascii("!"));
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                         // remove remaining \'s for the sorting part
75                         docstring const ppart =
76                                 subst(*it2, from_ascii("\\"), docstring());
77                         os << ppart;
78                         os << '@';
79                         i += ppart.size() + 1;
80                 }
81                 docstring const tpart = *it;
82                 os << tpart;
83                 i += tpart.size();
84                 if (it2 < levels_plain.end())
85                         ++it2;
86         }
87         os << '}';
88         i += 1;
89         return i;
90 }
91
92
93 int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
94 {
95         os << "<indexterm><primary>";
96         int const i = InsetText::docbook(os, runparams);
97         os << "</primary></indexterm>";
98         return i;
99 }
100
101
102 void InsetIndex::write(ostream & os) const
103 {
104         os << to_utf8(name()) << "\n";
105         InsetCollapsable::write(os);
106 }
107
108
109 void InsetIndex::addToToc(DocIterator const & cpit)
110 {
111         DocIterator pit = cpit;
112         pit.push_back(CursorSlice(*this));
113
114         Toc & toc = buffer().tocBackend().toc("index");
115         docstring str;
116         str = getNewLabel(str);
117         toc.push_back(TocItem(pit, 0, str));
118         // Proceed with the rest of the inset.
119         InsetCollapsable::addToToc(cpit);
120 }
121
122
123 /////////////////////////////////////////////////////////////////////
124 //
125 // InsetPrintIndex
126 //
127 ///////////////////////////////////////////////////////////////////////
128
129 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
130         : InsetCommand(p, string())
131 {}
132
133
134 ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
135 {
136         static ParamInfo param_info_;
137         if (param_info_.empty())
138                 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
139         return param_info_;
140 }
141
142
143 docstring InsetPrintIndex::screenLabel() const
144 {
145         return _("Index");
146 }
147
148
149 void InsetPrintIndex::validate(LaTeXFeatures & features) const
150 {
151         features.require("makeidx");
152 }
153
154
155 InsetCode InsetPrintIndex::lyxCode() const
156 {
157         return INDEX_PRINT_CODE;
158 }
159
160 } // namespace lyx