3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Martin Vermeer
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "BranchList.h"
18 #include "frontends/Application.h"
20 #include "support/lstrings.h"
30 : selected_(false), filenameSuffix_(false)
32 color_ = "background";
36 docstring const & Branch::branch() const
42 void Branch::setBranch(docstring const & s)
48 bool Branch::isSelected() const
54 bool Branch::setSelected(bool b)
63 bool Branch::hasFileNameSuffix() const
65 return filenameSuffix_;
69 void Branch::setFileNameSuffix(bool b)
75 string const & Branch::color() const
81 void Branch::setColor(string const & str)
85 // Update the Color table
87 bool darkmode = theApp() ? theApp()->isInDarkMode() : false;
89 color = lcolor.getX11HexName(Color_background, darkmode);
90 else if (color.size() != 7 || color[0] != '#')
91 color = lcolor.getX11HexName(lcolor.getFromLyXName(color), darkmode);
93 lcolor.setColor(to_utf8(branch_), color);
99 std::function<bool (Branch const &)> BranchNameIs(docstring const & d)
101 return [d](Branch const & b){ return b.branch() == d; };
107 Branch * BranchList::find(docstring const & name)
110 find_if(list_.begin(), list_.end(), BranchNameIs(name));
111 return it == list_.end() ? nullptr : &*it;
115 Branch const * BranchList::find(docstring const & name) const
117 List::const_iterator it =
118 find_if(list_.begin(), list_.end(), BranchNameIs(name));
119 return it == list_.end() ? nullptr : &*it;
123 bool BranchList::add(docstring const & s)
128 size_t const j = s.find_first_of(separator_, i);
130 if (j == docstring::npos)
133 name = s.substr(i, j - i);
134 // Is this name already in the list?
135 bool const already = find(name);
140 br.setSelected(false);
141 br.setFileNameSuffix(false);
144 if (j == docstring::npos)
152 bool BranchList::remove(docstring const & s)
154 size_t const size = list_.size();
155 list_.remove_if(BranchNameIs(s));
156 return size != list_.size();
160 bool BranchList::rename(docstring const & oldname,
161 docstring const & newname, bool const merge)
166 // new name already taken
168 return remove(oldname);
172 Branch * branch = find(oldname);
175 branch->setBranch(newname);
180 docstring BranchList::getFileNameSuffix() const
183 for (auto const & br : list_) {
184 if (br.isSelected() && br.hasFileNameSuffix())
185 result += "-" + br.branch();
187 return support::subst(result, from_ascii("/"), from_ascii("_"));