2 * \file ControlCitation.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include "ControlCitation.h"
17 #include "BufferParams.h"
18 #include "debug.h" // temporary
20 #include "support/lstrings.h"
22 #include <boost/regex.hpp>
33 vector<biblio::CiteStyle> ControlCitation::citeStyles_;
36 ControlCitation::ControlCitation(Dialog & d)
37 : ControlCommand(d, "cite", "citation")
41 bool ControlCitation::initialiseParams(string const & data)
43 if (!ControlCommand::initialiseParams(data))
46 biblio::CiteEngine const engine =
47 kernel().buffer().params().getEngine();
49 bool use_styles = engine != biblio::ENGINE_BASIC;
51 vector<pair<string, docstring> > blist;
52 kernel().buffer().fillWithBibKeys(blist);
54 for (size_t i = 0; i < blist.size(); ++i)
55 bibkeysInfo_[blist[i].first] = blist[i].second;
57 if (citeStyles_.empty())
58 citeStyles_ = biblio::getCiteStyles(engine);
60 if ((use_styles && citeStyles_.size() == 1) ||
61 (!use_styles && citeStyles_.size() != 1))
62 citeStyles_ = biblio::getCiteStyles(engine);
70 void ControlCitation::clearParams()
72 ControlCommand::clearParams();
77 vector<string> const ControlCitation::availableKeys() const
79 return biblio::getKeys(bibkeysInfo_);
83 biblio::CiteEngine const ControlCitation::getEngine() const
85 return kernel().buffer().params().getEngine();
89 docstring const ControlCitation::getInfo(std::string const & key) const
91 if (bibkeysInfo_.empty())
94 return biblio::getInfo(bibkeysInfo_, key);
100 // Escape special chars.
101 // All characters are literals except: '.|*?+(){}[]^$\'
102 // These characters are literals when preceded by a "\", which is done here
103 // @todo: This function should be moved to support, and then the test in tests
104 // should be moved there as well.
105 docstring const escape_special_chars(docstring const & expr)
107 // Search for all chars '.|*?+(){}[^$]\'
108 // Note that '[' and '\' must be escaped.
109 // This is a limitation of boost::regex, but all other chars in BREs
110 // are assumed literal.
111 boost::regex reg("[].|*?+(){}^$\\[\\\\]");
113 // $& is a perl-like expression that expands to all
114 // of the current match
115 // The '$' must be prefixed with the escape character '\' for
116 // boost to treat it as a literal.
117 // Thus, to prefix a matched expression with '\', we use:
119 return from_utf8(boost::regex_replace(to_utf8(expr), reg, "\\\\$&"));
124 vector<string> ControlCitation::searchKeys(
125 vector<string> const & keys_to_search,
126 docstring const & search_expression,
127 bool case_sensitive, bool regex)
129 vector<string> foundKeys;
131 docstring expr = support::trim(search_expression);
136 // We must escape special chars in the search_expr so that
137 // it is treated as a simple string by boost::regex.
138 expr = escape_special_chars(expr);
140 boost::regex reg_exp(to_utf8(expr), case_sensitive?
141 boost::regex_constants::normal : boost::regex_constants::icase);
143 vector<string>::const_iterator it = keys_to_search.begin();
144 vector<string>::const_iterator end = keys_to_search.end();
145 for (; it != end; ++it ) {
146 biblio::InfoMap::const_iterator info = bibkeysInfo_.find(*it);
147 if (info == bibkeysInfo_.end())
152 data += ' ' + to_utf8(info->second);
155 // Attempts to find a match for the current RE
156 // somewhere in data.
157 if (boost::regex_search(data, reg_exp))
158 foundKeys.push_back(*it);
160 catch (boost::regex_error &) {
161 return vector<string>();
168 vector<docstring> const ControlCitation::getCiteStrings(string const & key) const
170 biblio::CiteEngine const engine = kernel().buffer().params().getEngine();
171 vector<biblio::CiteStyle> const cs = biblio::getCiteStyles(engine);
173 if (engine == biblio::ENGINE_NATBIB_NUMERICAL)
174 return biblio::getNumericalStrings(key, bibkeysInfo_, cs);
176 return biblio::getAuthorYearStrings(key, bibkeysInfo_, cs);
179 } // namespace frontend