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.
13 #include "IndicesList.h"
16 #include "frontends/Application.h"
18 #include "support/convert.h"
19 #include "support/lstrings.h"
24 using namespace lyx::support;
28 /////////////////////////////////////////////////////////////////////
32 /////////////////////////////////////////////////////////////////////
37 // no theApp() with command line export
39 theApp()->getRgbColor(Color_indexlabel, color_);
41 frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
45 docstring const & Index::index() const
51 void Index::setIndex(docstring const & s)
57 docstring const & Index::shortcut() const
63 void Index::setShortcut(docstring const & s)
69 RGBColor const & Index::color() const
75 void Index::setColor(RGBColor const & c)
81 void Index::setColor(string const & str)
83 if (str.size() == 7 && str[0] == '#')
84 color_ = rgbFromHexName(str);
86 // no color set or invalid color -- use predefined color
87 // no theApp() with command line export
89 theApp()->getRgbColor(Color_indexlabel, color_);
91 frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
96 /////////////////////////////////////////////////////////////////////
100 /////////////////////////////////////////////////////////////////////
105 std::function<bool (Index const &)> IndexNameIs(docstring const & name)
107 return [name](Index const & idx){ return idx.index() == name; };
113 Index * IndicesList::find(docstring const & name)
116 find_if(list.begin(), list.end(), IndexNameIs(name));
117 return it == list.end() ? nullptr : &*it;
121 Index const * IndicesList::find(docstring const & name) const
123 List::const_iterator it =
124 find_if(list.begin(), list.end(), IndexNameIs(name));
125 return it == list.end() ? nullptr : &*it;
131 std::function<bool (Index const &)> IndexShortcutIs(docstring const & sc)
133 return [sc](Index const & idx){ return idx.shortcut() == sc; };
139 Index * IndicesList::findShortcut(docstring const & shortcut)
142 find_if(list.begin(), list.end(), IndexShortcutIs(shortcut));
143 return it == list.end() ? nullptr : &*it;
147 Index const * IndicesList::findShortcut(docstring const & shortcut) const
149 List::const_iterator it =
150 find_if(list.begin(), list.end(), IndexShortcutIs(shortcut));
151 return it == list.end() ? nullptr : &*it;
155 bool IndicesList::add(docstring const & n, docstring const & s)
160 size_t const j = n.find_first_of(separator_, i);
162 if (j == docstring::npos)
165 name = n.substr(i, j - i);
166 // Is this name already in the list?
167 bool const already = find(name);
172 docstring sc = s.empty() ?
173 trim(lowercase(name.substr(0, 3))) : s;
174 if (findShortcut(sc) != nullptr) {
176 docstring scn = sc + convert<docstring>(k);
177 while (findShortcut(scn) != nullptr) {
179 scn = sc + convert<docstring>(k);
186 if (j == docstring::npos)
194 bool IndicesList::addDefault(docstring const & n)
196 if (findShortcut(from_ascii("idx")) != nullptr)
197 // we already have a default
199 return add(n, from_ascii("idx"));
202 bool IndicesList::remove(docstring const & s)
204 size_t const size = list.size();
205 list.remove_if(IndexNameIs(s));
206 return size != list.size();
210 bool IndicesList::rename(docstring const & oldname,
211 docstring const & newname)
216 // new name already taken
219 Index * index = find(oldname);
222 index->setIndex(newname);