Not so soon, I guess, since that FIXME was from r6305.
[lyx.git] / src / IndicesList.cpp
bloba73dbb90b042be053f81db83c8e918fc46d7b223
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.
6 * \author Jürgen Spitzmüller
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "IndicesList.h"
14 #include "Color.h"
16 #include "frontends/Application.h"
18 #include "support/convert.h"
19 #include "support/lstrings.h"
21 #include <algorithm>
23 using namespace std;
24 using namespace lyx::support;
26 namespace lyx {
28 namespace {
30 class IndexNamesEqual : public std::unary_function<Index, bool>
32 public:
33 IndexNamesEqual(docstring const & name) : name_(name) {}
35 bool operator()(Index const & index) const
37 return index.index() == name_;
39 private:
40 docstring name_;
44 class IndexHasShortcut : public std::unary_function<Index, bool>
46 public:
47 IndexHasShortcut(docstring const & shortcut) : shortc_(shortcut) {}
49 bool operator()(Index const & index) const
51 return index.shortcut() == shortc_;
53 private:
54 docstring shortc_;
60 /////////////////////////////////////////////////////////////////////
62 // Index
64 /////////////////////////////////////////////////////////////////////
67 Index::Index()
69 // no theApp() with command line export
70 if (theApp())
71 theApp()->getRgbColor(Color_indexlabel, color_);
75 docstring const & Index::index() const
77 return index_;
81 void Index::setIndex(docstring const & s)
83 index_ = s;
87 docstring const & Index::shortcut() const
89 return shortcut_;
93 void Index::setShortcut(docstring const & s)
95 shortcut_ = s;
99 RGBColor const & Index::color() const
101 return color_;
105 void Index::setColor(RGBColor const & c)
107 color_ = c;
111 void Index::setColor(string const & str)
113 if (str.size() == 7 && str[0] == '#')
114 color_ = rgbFromHexName(str);
115 else
116 // no color set or invalid color -- use predefined color
117 theApp()->getRgbColor(Color_indexlabel, color_);
121 /////////////////////////////////////////////////////////////////////
123 // IndicesList
125 /////////////////////////////////////////////////////////////////////
128 Index * IndicesList::find(docstring const & name)
130 List::iterator it =
131 find_if(list.begin(), list.end(), IndexNamesEqual(name));
132 return it == list.end() ? 0 : &*it;
136 Index const * IndicesList::find(docstring const & name) const
138 List::const_iterator it =
139 find_if(list.begin(), list.end(), IndexNamesEqual(name));
140 return it == list.end() ? 0 : &*it;
144 Index * IndicesList::findShortcut(docstring const & shortcut)
146 List::iterator it =
147 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
148 return it == list.end() ? 0 : &*it;
152 Index const * IndicesList::findShortcut(docstring const & shortcut) const
154 List::const_iterator it =
155 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
156 return it == list.end() ? 0 : &*it;
160 bool IndicesList::add(docstring const & n, docstring const & s)
162 bool added = false;
163 size_t i = 0;
164 while (true) {
165 size_t const j = n.find_first_of(separator_, i);
166 docstring name;
167 if (j == docstring::npos)
168 name = n.substr(i);
169 else
170 name = n.substr(i, j - i);
171 // Is this name already in the list?
172 bool const already =
173 find_if(list.begin(), list.end(),
174 IndexNamesEqual(name)) != list.end();
175 if (!already) {
176 added = true;
177 Index in;
178 in.setIndex(name);
179 docstring sc = s.empty() ?
180 trim(lowercase(name.substr(0, 3))) : s;
181 if (findShortcut(sc) != 0) {
182 int i = 1;
183 docstring scn = sc + convert<docstring>(i);
184 while (findShortcut(scn) != 0) {
185 ++i;
186 scn = sc + convert<docstring>(i);
188 in.setShortcut(scn);
189 } else
190 in.setShortcut(sc);
191 list.push_back(in);
193 if (j == docstring::npos)
194 break;
195 i = j + 1;
197 return added;
201 bool IndicesList::addDefault(docstring const & n)
203 if (findShortcut(from_ascii("idx")) != 0)
204 // we already have a default
205 return false;
206 return add(n, from_ascii("idx"));
209 bool IndicesList::remove(docstring const & s)
211 size_t const size = list.size();
212 list.remove_if(IndexNamesEqual(s));
213 return size != list.size();
217 bool IndicesList::rename(docstring const & oldname,
218 docstring const & newname)
220 if (newname.empty())
221 return false;
222 if (find_if(list.begin(), list.end(),
223 IndexNamesEqual(newname)) != list.end())
224 // new name already taken
225 return false;
227 Index * index = find(oldname);
228 if (!index)
229 return false;
230 index->setIndex(newname);
231 return true;
235 } // namespace lyx