]> git.lyx.org Git - lyx.git/blob - src/BranchList.cpp
FindAdv: Amend ec387b6d: Handle search for '{' and '}'
[lyx.git] / src / BranchList.cpp
1 /**
2  * \file BranchList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Martin Vermeer
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "BranchList.h"
15 #include "Color.h"
16 #include "ColorSet.h"
17
18 #include "frontends/Application.h"
19
20 #include "support/lstrings.h"
21
22 #include <algorithm>
23
24 using namespace std;
25
26
27 namespace lyx {
28
29 Branch::Branch()
30         : selected_(false), filenameSuffix_(false)
31 {
32         color_ = "background";
33 }
34
35
36 docstring const & Branch::branch() const
37 {
38         return branch_;
39 }
40
41
42 void Branch::setBranch(docstring const & s)
43 {
44         branch_ = s;
45 }
46
47
48 bool Branch::isSelected() const
49 {
50         return selected_;
51 }
52
53
54 bool Branch::setSelected(bool b)
55 {
56         if (b == selected_)
57                 return false;
58         selected_ = b;
59         return true;
60 }
61
62
63 bool Branch::hasFileNameSuffix() const
64 {
65         return filenameSuffix_;
66 }
67
68
69 void Branch::setFileNameSuffix(bool b)
70 {
71         filenameSuffix_ = b;
72 }
73
74
75 string const & Branch::color() const
76 {
77         return color_;
78 }
79
80
81 void Branch::setColor(string const & str)
82 {
83         color_ = str;
84 }
85
86
87 namespace {
88
89 std::function<bool (Branch const &)> BranchNameIs(docstring const & d)
90 {
91         return [d](Branch const & b){ return b.branch() == d; };
92 }
93
94 } // namespace
95
96
97 Branch * BranchList::find(docstring const & name)
98 {
99         List::iterator it =
100                 find_if(list_.begin(), list_.end(), BranchNameIs(name));
101         return it == list_.end() ? nullptr : &*it;
102 }
103
104
105 Branch const * BranchList::find(docstring const & name) const
106 {
107         List::const_iterator it =
108                 find_if(list_.begin(), list_.end(), BranchNameIs(name));
109         return it == list_.end() ? nullptr : &*it;
110 }
111
112
113 bool BranchList::add(docstring const & s)
114 {
115         bool added = false;
116         size_t i = 0;
117         while (true) {
118                 size_t const j = s.find_first_of(separator_, i);
119                 docstring name;
120                 if (j == docstring::npos)
121                         name = s.substr(i);
122                 else
123                         name = s.substr(i, j - i);
124                 // Is this name already in the list?
125                 bool const already = find(name);
126                 if (!already) {
127                         added = true;
128                         Branch br;
129                         br.setBranch(name);
130                         br.setSelected(false);
131                         br.setFileNameSuffix(false);
132                         list_.push_back(br);
133                 }
134                 if (j == docstring::npos)
135                         break;
136                 i = j + 1;
137         }
138         return added;
139 }
140
141
142 bool BranchList::remove(docstring const & s)
143 {
144         size_t const size = list_.size();
145         list_.remove_if(BranchNameIs(s));
146         return size != list_.size();
147 }
148
149
150 bool BranchList::rename(docstring const & oldname,
151         docstring const & newname, bool const merge)
152 {
153         if (newname.empty())
154                 return false;
155         if (find(newname)) {
156                 // new name already taken
157                 if (merge)
158                       return remove(oldname);
159                 return false;
160         }
161
162         Branch * branch = find(oldname);
163         if (!branch)
164                 return false;
165         branch->setBranch(newname);
166         return true;
167 }
168
169
170 docstring BranchList::getFileNameSuffix() const
171 {
172         docstring result;
173         for (auto const & br : list_) {
174                 if (br.isSelected() && br.hasFileNameSuffix())
175                         result += "-" + br.branch();
176         }
177         return support::subst(result, from_ascii("/"), from_ascii("_"));
178 }
179
180 } // namespace lyx