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 bibkeysInfo_.fillWithBibKeys(&(kernel().buffer()));
53 if (citeStyles_.empty())
54 citeStyles_ = biblio::getCiteStyles(engine);
56 if ((use_styles && citeStyles_.size() == 1) ||
57 (!use_styles && citeStyles_.size() != 1))
58 citeStyles_ = biblio::getCiteStyles(engine);
66 void ControlCitation::clearParams()
68 ControlCommand::clearParams();
73 vector<docstring> const ControlCitation::availableKeys() const
75 return bibkeysInfo_.getKeys();
79 vector<docstring> const ControlCitation::availableFields() const
81 return bibkeysInfo_.getFields();
85 vector<docstring> const ControlCitation::availableEntries() const
87 return bibkeysInfo_.getEntries();
91 void ControlCitation::filterByEntryType(
92 vector<docstring> & keyVector, docstring entryType)
94 if (entryType.empty())
97 vector<docstring>::iterator it = keyVector.begin();
98 vector<docstring>::iterator end = keyVector.end();
100 vector<docstring> result;
101 for (; it != end; ++it) {
102 docstring const key = *it;
103 BiblioInfo::const_iterator cit = bibkeysInfo_.find(key);
104 if (cit == bibkeysInfo_.end())
106 if (cit->second.entryType == entryType)
107 result.push_back(key);
113 biblio::CiteEngine const ControlCitation::getEngine() const
115 return kernel().buffer().params().getEngine();
119 docstring const ControlCitation::getInfo(docstring const & key) const
121 if (bibkeysInfo_.empty())
124 return bibkeysInfo_.getInfo(key);
130 // Escape special chars.
131 // All characters are literals except: '.|*?+(){}[]^$\'
132 // These characters are literals when preceded by a "\", which is done here
133 // @todo: This function should be moved to support, and then the test in tests
134 // should be moved there as well.
135 docstring const escape_special_chars(docstring const & expr)
137 // Search for all chars '.|*?+(){}[^$]\'
138 // Note that '[' and '\' must be escaped.
139 // This is a limitation of boost::regex, but all other chars in BREs
140 // are assumed literal.
141 boost::regex reg("[].|*?+(){}^$\\[\\\\]");
143 // $& is a perl-like expression that expands to all
144 // of the current match
145 // The '$' must be prefixed with the escape character '\' for
146 // boost to treat it as a literal.
147 // Thus, to prefix a matched expression with '\', we use:
149 return from_utf8(boost::regex_replace(to_utf8(expr), reg, "\\\\$&"));
154 vector<docstring> ControlCitation::searchKeys(
155 vector<docstring> const & keys_to_search, bool only_keys,
156 docstring const & search_expression, docstring field,
157 bool case_sensitive, bool regex)
159 vector<docstring> foundKeys;
161 docstring expr = support::trim(search_expression);
166 // We must escape special chars in the search_expr so that
167 // it is treated as a simple string by boost::regex.
168 expr = escape_special_chars(expr);
170 boost::regex reg_exp(to_utf8(expr), case_sensitive ?
171 boost::regex_constants::normal : boost::regex_constants::icase);
173 vector<docstring>::const_iterator it = keys_to_search.begin();
174 vector<docstring>::const_iterator end = keys_to_search.end();
175 for (; it != end; ++it ) {
176 BiblioInfo::const_iterator info = bibkeysInfo_.find(*it);
177 if (info == bibkeysInfo_.end())
180 BibTeXInfo const & kvm = info->second;
184 else if (field.empty())
185 data = to_utf8(*it) + ' ' + to_utf8(kvm.allData);
186 else if (kvm.hasField(field))
187 data = to_utf8(kvm.getValueForField(field));
193 if (boost::regex_search(data, reg_exp))
194 foundKeys.push_back(*it);
196 catch (boost::regex_error &) {
197 return vector<docstring>();
204 vector<docstring> const ControlCitation::getCiteStrings(docstring const & key) const
206 return bibkeysInfo_.getCiteStrings(key, kernel().buffer());
209 } // namespace frontend