]> git.lyx.org Git - lyx.git/blob - src/IndicesList.cpp
Correction: The inset name is citation, not cite
[lyx.git] / src / IndicesList.cpp
1 /**
2  * \file IndicesList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Spitzmüller
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "IndicesList.h"
14 #include "Color.h"
15
16 #include "frontends/Application.h"
17
18 #include "support/convert.h"
19 #include "support/lstrings.h"
20
21 #include <algorithm>
22
23 using namespace std;
24 using namespace lyx::support;
25
26 namespace lyx {
27
28 namespace {
29
30 class IndexNamesEqual : public std::unary_function<Index, bool>
31 {
32 public:
33         IndexNamesEqual(docstring const & name) : name_(name) {}
34
35         bool operator()(Index const & index) const
36         {
37                 return index.index() == name_;
38         }
39 private:
40         docstring name_;
41 };
42
43
44 class IndexHasShortcut : public std::unary_function<Index, bool>
45 {
46 public:
47         IndexHasShortcut(docstring const & shortcut) : shortc_(shortcut) {}
48
49         bool operator()(Index const & index) const
50         {
51                 return index.shortcut() == shortc_;
52         }
53 private:
54         docstring shortc_;
55 };
56
57 }
58
59
60 /////////////////////////////////////////////////////////////////////
61 //
62 // Index
63 //
64 /////////////////////////////////////////////////////////////////////
65
66
67 Index::Index()
68 {
69         // no theApp() with command line export
70         if (theApp())
71                 theApp()->getRgbColor(Color_indexlabel, color_);
72         else
73                 frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
74 }
75
76
77 docstring const & Index::index() const
78 {
79         return index_;
80 }
81
82
83 void Index::setIndex(docstring const & s)
84 {
85         index_ = s;
86 }
87
88
89 docstring const & Index::shortcut() const
90 {
91         return shortcut_;
92 }
93
94
95 void Index::setShortcut(docstring const & s)
96 {
97         shortcut_ = s;
98 }
99
100
101 RGBColor const & Index::color() const
102 {
103         return color_;
104 }
105
106
107 void Index::setColor(RGBColor const & c)
108 {
109         color_ = c;
110 }
111
112
113 void Index::setColor(string const & str)
114 {
115         if (str.size() == 7 && str[0] == '#')
116                 color_ = rgbFromHexName(str);
117         else {
118                 // no color set or invalid color -- use predefined color
119                 // no theApp() with command line export
120                 if (theApp())
121                         theApp()->getRgbColor(Color_indexlabel, color_);
122                 else
123                         frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
124         }
125 }
126
127
128 /////////////////////////////////////////////////////////////////////
129 //
130 // IndicesList
131 //
132 /////////////////////////////////////////////////////////////////////
133
134
135 Index * IndicesList::find(docstring const & name)
136 {
137         List::iterator it =
138                 find_if(list.begin(), list.end(), IndexNamesEqual(name));
139         return it == list.end() ? 0 : &*it;
140 }
141
142
143 Index const * IndicesList::find(docstring const & name) const
144 {
145         List::const_iterator it =
146                 find_if(list.begin(), list.end(), IndexNamesEqual(name));
147         return it == list.end() ? 0 : &*it;
148 }
149
150
151 Index * IndicesList::findShortcut(docstring const & shortcut)
152 {
153         List::iterator it =
154                 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
155         return it == list.end() ? 0 : &*it;
156 }
157
158
159 Index const * IndicesList::findShortcut(docstring const & shortcut) const
160 {
161         List::const_iterator it =
162                 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
163         return it == list.end() ? 0 : &*it;
164 }
165
166
167 bool IndicesList::add(docstring const & n, docstring const & s)
168 {
169         bool added = false;
170         size_t i = 0;
171         while (true) {
172                 size_t const j = n.find_first_of(separator_, i);
173                 docstring name;
174                 if (j == docstring::npos)
175                         name = n.substr(i);
176                 else
177                         name = n.substr(i, j - i);
178                 // Is this name already in the list?
179                 bool const already =
180                         find_if(list.begin(), list.end(),
181                                      IndexNamesEqual(name)) != list.end();
182                 if (!already) {
183                         added = true;
184                         Index in;
185                         in.setIndex(name);
186                         docstring sc = s.empty() ?
187                                 trim(lowercase(name.substr(0, 3))) : s;
188                         if (findShortcut(sc) != 0) {
189                                 int i = 1;
190                                 docstring scn = sc + convert<docstring>(i);
191                                 while (findShortcut(scn) != 0) {
192                                         ++i;
193                                         scn = sc + convert<docstring>(i);
194                                 }
195                                 in.setShortcut(scn);
196                         } else
197                                 in.setShortcut(sc);
198                         list.push_back(in);
199                 }
200                 if (j == docstring::npos)
201                         break;
202                 i = j + 1;
203         }
204         return added;
205 }
206
207
208 bool IndicesList::addDefault(docstring const & n)
209 {
210         if (findShortcut(from_ascii("idx")) != 0)
211                 // we already have a default
212                 return false;
213         return add(n, from_ascii("idx"));
214 }
215
216 bool IndicesList::remove(docstring const & s)
217 {
218         size_t const size = list.size();
219         list.remove_if(IndexNamesEqual(s));
220         return size != list.size();
221 }
222
223
224 bool IndicesList::rename(docstring const & oldname,
225         docstring const & newname)
226 {
227         if (newname.empty())
228                 return false;
229         if (find_if(list.begin(), list.end(),
230                     IndexNamesEqual(newname)) != list.end())
231                 // new name already taken
232                 return false;
233
234         Index * index = find(oldname);
235         if (!index)
236                 return false;
237         index->setIndex(newname);
238         return true;
239 }
240
241
242 } // namespace lyx