]> git.lyx.org Git - lyx.git/blob - src/IndicesList.cpp
InsetCommand ctor: Pass 'Buffer *'
[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 }
73
74
75 docstring const & Index::index() const
76 {
77         return index_;
78 }
79
80
81 void Index::setIndex(docstring const & s)
82 {
83         index_ = s;
84 }
85
86
87 docstring const & Index::shortcut() const
88 {
89         return shortcut_;
90 }
91
92
93 void Index::setShortcut(docstring const & s)
94 {
95         shortcut_ = s;
96 }
97
98
99 RGBColor const & Index::color() const
100 {
101         return color_;
102 }
103
104
105 void Index::setColor(RGBColor const & c)
106 {
107         color_ = c;
108 }
109
110
111 void Index::setColor(string const & str)
112 {
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_);
118 }
119
120
121 /////////////////////////////////////////////////////////////////////
122 //
123 // IndicesList
124 //
125 /////////////////////////////////////////////////////////////////////
126
127
128 Index * IndicesList::find(docstring const & name)
129 {
130         List::iterator it =
131                 find_if(list.begin(), list.end(), IndexNamesEqual(name));
132         return it == list.end() ? 0 : &*it;
133 }
134
135
136 Index const * IndicesList::find(docstring const & name) const
137 {
138         List::const_iterator it =
139                 find_if(list.begin(), list.end(), IndexNamesEqual(name));
140         return it == list.end() ? 0 : &*it;
141 }
142
143
144 Index * IndicesList::findShortcut(docstring const & shortcut)
145 {
146         List::iterator it =
147                 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
148         return it == list.end() ? 0 : &*it;
149 }
150
151
152 Index const * IndicesList::findShortcut(docstring const & shortcut) const
153 {
154         List::const_iterator it =
155                 find_if(list.begin(), list.end(), IndexHasShortcut(shortcut));
156         return it == list.end() ? 0 : &*it;
157 }
158
159
160 bool IndicesList::add(docstring const & n, docstring const & s)
161 {
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);
187                                 }
188                                 in.setShortcut(scn);
189                         } else
190                                 in.setShortcut(sc);
191                         list.push_back(in);
192                 }
193                 if (j == docstring::npos)
194                         break;
195                 i = j + 1;
196         }
197         return added;
198 }
199
200
201 bool IndicesList::addDefault(docstring const & n)
202 {
203         if (findShortcut(from_ascii("idx")) != 0)
204                 // we already have a default
205                 return false;
206         return add(n, from_ascii("idx"));
207 }
208
209 bool IndicesList::remove(docstring const & s)
210 {
211         size_t const size = list.size();
212         list.remove_if(IndexNamesEqual(s));
213         return size != list.size();
214 }
215
216
217 bool IndicesList::rename(docstring const & oldname,
218         docstring const & newname)
219 {
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;
226
227         Index * index = find(oldname);
228         if (!index)
229                 return false;
230         index->setIndex(newname);
231         return true;
232 }
233
234
235 } // namespace lyx