]> git.lyx.org Git - lyx.git/blob - src/IndicesList.cpp
Avoid full metrics computation with Update:FitCursor
[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 /////////////////////////////////////////////////////////////////////
29 //
30 // Index
31 //
32 /////////////////////////////////////////////////////////////////////
33
34
35 Index::Index()
36 {
37         // no theApp() with command line export
38         if (theApp())
39                 theApp()->getRgbColor(Color_indexlabel, color_);
40         else
41                 frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
42 }
43
44
45 docstring const & Index::index() const
46 {
47         return index_;
48 }
49
50
51 void Index::setIndex(docstring const & s)
52 {
53         index_ = s;
54 }
55
56
57 docstring const & Index::shortcut() const
58 {
59         return shortcut_;
60 }
61
62
63 void Index::setShortcut(docstring const & s)
64 {
65         shortcut_ = s;
66 }
67
68
69 RGBColor const & Index::color() const
70 {
71         return color_;
72 }
73
74
75 void Index::setColor(RGBColor const & c)
76 {
77         color_ = c;
78 }
79
80
81 void Index::setColor(string const & str)
82 {
83         if (str.size() == 7 && str[0] == '#')
84                 color_ = rgbFromHexName(str);
85         else {
86                 // no color set or invalid color -- use predefined color
87                 // no theApp() with command line export
88                 if (theApp())
89                         theApp()->getRgbColor(Color_indexlabel, color_);
90                 else
91                         frontend::Application::getRgbColorUncached(Color_indexlabel, color_);
92         }
93 }
94
95
96 /////////////////////////////////////////////////////////////////////
97 //
98 // IndicesList
99 //
100 /////////////////////////////////////////////////////////////////////
101
102
103 namespace{
104
105 std::function<bool (Index const &)> IndexNameIs(docstring const & name)
106 {
107         return [name](Index const & idx){ return idx.index() == name; };
108 }
109
110 }
111
112
113 Index * IndicesList::find(docstring const & name)
114 {
115         List::iterator it =
116                 find_if(list.begin(), list.end(), IndexNameIs(name));
117         return it == list.end() ? nullptr : &*it;
118 }
119
120
121 Index const * IndicesList::find(docstring const & name) const
122 {
123         List::const_iterator it =
124                 find_if(list.begin(), list.end(), IndexNameIs(name));
125         return it == list.end() ? nullptr : &*it;
126 }
127
128
129 namespace {
130
131 std::function<bool (Index const &)> IndexShortcutIs(docstring const & sc)
132 {
133         return [sc](Index const & idx){ return idx.shortcut() == sc; };
134 }
135
136 } // namespace
137
138
139 Index * IndicesList::findShortcut(docstring const & shortcut)
140 {
141         List::iterator it =
142                 find_if(list.begin(), list.end(), IndexShortcutIs(shortcut));
143         return it == list.end() ? nullptr : &*it;
144 }
145
146
147 Index const * IndicesList::findShortcut(docstring const & shortcut) const
148 {
149         List::const_iterator it =
150                 find_if(list.begin(), list.end(), IndexShortcutIs(shortcut));
151         return it == list.end() ? nullptr : &*it;
152 }
153
154
155 bool IndicesList::add(docstring const & n, docstring const & s)
156 {
157         bool added = false;
158         size_t i = 0;
159         while (true) {
160                 size_t const j = n.find_first_of(separator_, i);
161                 docstring name;
162                 if (j == docstring::npos)
163                         name = n.substr(i);
164                 else
165                         name = n.substr(i, j - i);
166                 // Is this name already in the list?
167                 bool const already = find(name);
168                 if (!already) {
169                         added = true;
170                         Index in;
171                         in.setIndex(name);
172                         docstring sc = s.empty() ?
173                                 trim(lowercase(name.substr(0, 3))) : s;
174                         if (findShortcut(sc) != nullptr) {
175                                 int k = 1;
176                                 docstring scn = sc + convert<docstring>(k);
177                                 while (findShortcut(scn) != nullptr) {
178                                         ++k;
179                                         scn = sc + convert<docstring>(k);
180                                 }
181                                 in.setShortcut(scn);
182                         } else
183                                 in.setShortcut(sc);
184                         list.push_back(in);
185                 }
186                 if (j == docstring::npos)
187                         break;
188                 i = j + 1;
189         }
190         return added;
191 }
192
193
194 bool IndicesList::addDefault(docstring const & n)
195 {
196         if (findShortcut(from_ascii("idx")) != nullptr)
197                 // we already have a default
198                 return false;
199         return add(n, from_ascii("idx"));
200 }
201
202 bool IndicesList::remove(docstring const & s)
203 {
204         size_t const size = list.size();
205         list.remove_if(IndexNameIs(s));
206         return size != list.size();
207 }
208
209
210 bool IndicesList::rename(docstring const & oldname,
211         docstring const & newname)
212 {
213         if (newname.empty())
214                 return false;
215         if (find(newname))
216                 // new name already taken
217                 return false;
218
219         Index * index = find(oldname);
220         if (!index)
221                 return false;
222         index->setIndex(newname);
223         return true;
224 }
225
226
227 } // namespace lyx