3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
14 #include "insetcite.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "LaTeXFeatures.h"
23 #include "frontends/controllers/biblio.h"
25 #include "support/lstrings.h"
27 using lyx::support::ascii_lowercase;
28 using lyx::support::contains;
29 using lyx::support::getVectorFromString;
30 using lyx::support::ltrim;
31 using lyx::support::rtrim;
32 using lyx::support::split;
42 string const getNatbibLabel(Buffer const & buffer,
43 string const & citeType, string const & keyList,
44 string const & before, string const & after,
45 bool numerical, bool jura)
47 // Only start the process off after the buffer is loaded from file.
48 if (!buffer.fully_loaded())
51 typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
52 static CachedMap cached_keys;
55 typedef vector<std::pair<string, string> > InfoType;
57 buffer.fillWithBibKeys(bibkeys);
59 InfoType::const_iterator bit = bibkeys.begin();
60 InfoType::const_iterator bend = bibkeys.end();
62 biblio::InfoMap infomap;
63 for (; bit != bend; ++bit) {
64 infomap[bit->first] = bit->second;
69 cached_keys[&buffer] = infomap;
71 // the natbib citation-styles
72 // CITET: author (year)
73 // CITEP: (author,year)
74 // CITEALT: author year
75 // CITEALP: author, year
78 // CITEYEARPAR: (year)
79 // jurabib supports these plus
80 // CITE: author/<before field>
82 // We don't currently use the full or forceUCase fields.
83 // bool const forceUCase = citeType[0] == 'C';
84 bool const full = citeType[citeType.size() - 1] == '*';
86 string const cite_type = full ?
87 ascii_lowercase(citeType.substr(0, citeType.size() - 1)) :
88 ascii_lowercase(citeType);
91 if (!before.empty()) {
92 // In CITET and CITEALT mode, the "before" string is
93 // attached to the label associated with each and every key.
94 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
95 // to the front of the whole only.
96 // In other modes, it is not used at all.
97 if (cite_type == "citet" ||
98 cite_type == "citealt" ||
99 cite_type == "citep" ||
100 cite_type == "citealp" ||
101 cite_type == "citeyearpar")
102 before_str = before + ' ';
103 // In CITE (jurabib), the "before" string is used to attach
104 // the annotator (of legal texts) to the author(s) of the
106 else if (cite_type == "cite")
107 before_str = '/' + before;
111 if (!after.empty()) {
112 // The "after" key is appended only to the end of the whole.
113 after_str = ", " + after;
116 // One day, these might be tunable (as they are in BibTeX).
117 char const op = '('; // opening parenthesis.
118 char const cp = ')'; // closing parenthesis.
119 // puctuation mark separating citation entries.
120 char const * const sep = ";";
122 string const op_str(' ' + string(1, op));
123 string const cp_str(string(1, cp) + ' ');
124 string const sep_str(string(sep) + ' ');
127 vector<string> keys = getVectorFromString(keyList);
128 vector<string>::const_iterator it = keys.begin();
129 vector<string>::const_iterator end = keys.end();
130 for (; it != end; ++it) {
131 // get the bibdata corresponding to the key
132 string const author(biblio::getAbbreviatedAuthor(infomap, *it));
133 string const year(biblio::getYear(infomap, *it));
135 // Something isn't right. Fail safely.
136 if (author.empty() || year.empty())
139 // authors1/<before>; ... ;
140 // authors_last, <after>
141 if (cite_type == "cite" && jura) {
142 if (it == keys.begin())
143 label += author + before_str + sep_str;
145 label += author + sep_str;
147 // (authors1 (<before> year); ... ;
148 // authors_last (<before> year, <after>)
149 } else if (cite_type == "citet") {
150 string const tmp = numerical ? '#' + *it : year;
152 label += author + op_str + before_str + tmp +
155 label += before_str + author + op_str + tmp +
158 // author, year; author, year; ...
159 } else if (cite_type == "citep" ||
160 cite_type == "citealp") {
162 label += *it + sep_str;
164 label += author + ", " + year + sep_str;
167 // (authors1 <before> year;
168 // authors_last <before> year, <after>)
169 } else if (cite_type == "citealt") {
170 string const tmp = numerical ? '#' + *it : year;
172 label += author + ' ' + before_str + tmp + sep_str;
174 label += before_str + author + ' ' + tmp + sep_str;
176 // author; author; ...
177 } else if (cite_type == "citeauthor") {
178 label += author + sep_str;
181 } else if (cite_type == "citeyear" ||
182 cite_type == "citeyearpar") {
183 label += year + sep_str;
186 label = rtrim(rtrim(label), sep);
188 if (!after_str.empty()) {
189 if (cite_type == "citet") {
190 // insert "after" before last ')'
191 label.insert(label.size() - 1, after_str);
193 bool const add = !(numerical &&
194 (cite_type == "citeauthor" ||
195 cite_type == "citeyear"));
201 if (!before_str.empty() && (cite_type == "citep" ||
202 cite_type == "citealp" ||
203 cite_type == "citeyearpar")) {
204 label = before_str + label;
207 if (cite_type == "citep" || cite_type == "citeyearpar")
208 label = string(1, op) + label + string(1, cp);
214 string const getBasicLabel(string const & keyList, string const & after)
216 string keys(keyList);
219 if (contains(keys, ',')) {
220 // Final comma allows while loop to cover all keys
221 keys = ltrim(split(keys, label, ',')) + ',';
222 while (contains(keys, ',')) {
224 keys = ltrim(split(keys, key, ','));
231 label += ", " + after;
233 return '[' + label + ']';
239 InsetCitation::InsetCitation(InsetCommandParams const & p)
240 : InsetCommand(p, "citation")
244 string const InsetCitation::generateLabel(Buffer const & buffer) const
246 string const before = getSecOptions();
247 string const after = getOptions();
250 if (buffer.params().use_natbib || buffer.params().use_jurabib) {
251 string cmd = getCmdName();
252 if (buffer.params().use_natbib && cmd == "cite") {
253 // We may be "upgrading" from an older LyX version.
254 // If, however, we use "cite" because the necessary
255 // author/year info is not present in the biblio
256 // database, then getNatbibLabel will exit gracefully
257 // and we'll call getBasicLabel.
258 if (buffer.params().use_numerical_citations)
263 label = getNatbibLabel(buffer, cmd, getContents(),
265 buffer.params().use_numerical_citations,
266 buffer.params().use_jurabib);
269 // Fallback to fail-safe
271 label = getBasicLabel(getContents(), after);
278 InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const & buffer) const
280 Cache::Style style = Cache::BASIC;
282 if (buffer.params().use_natbib) {
283 if (buffer.params().use_numerical_citations) {
284 style = Cache::NATBIB_NUM;
286 style = Cache::NATBIB_AY;
290 if (buffer.params().use_jurabib)
291 style = Cache::JURABIB;
297 string const InsetCitation::getScreenLabel(Buffer const & buffer) const
299 Cache::Style const style = getStyle(buffer);
300 if (cache.params == params() && cache.style == style)
301 return cache.screen_label;
303 // The label has changed, so we have to re-create it.
304 string const before = getSecOptions();
305 string const after = getOptions();
307 string const glabel = generateLabel(buffer);
309 unsigned int const maxLabelChars = 45;
311 string label = glabel;
312 if (label.size() > maxLabelChars) {
313 label.erase(maxLabelChars-3);
318 cache.params = params();
319 cache.generated_label = glabel;
320 cache.screen_label = label;
326 int InsetCitation::plaintext(Buffer const & buffer, ostream & os, int) const
328 if (cache.params == params() && cache.style == getStyle(buffer))
329 os << cache.generated_label;
331 os << generateLabel(buffer);
336 // Have to overwrite the default InsetCommand method in order to check that
337 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
338 // citations and then changes his mind, turning natbib support off. The output
339 // should revert to \cite[]{}
340 int InsetCitation::latex(Buffer const & buffer, ostream & os,
341 OutputParams const &) const
344 if (buffer.params().use_natbib)
346 else if (buffer.params().use_jurabib) {
347 // jurabib does not (yet) support "force upper case"
348 // and "full author name". Fallback.
349 string cmd = getCmdName();
352 size_t n = cmd.size() - 1;
354 cmd = cmd.substr(0,n);
359 string const before = getSecOptions();
360 string const after = getOptions();
362 && (buffer.params().use_natbib || buffer.params().use_jurabib))
363 os << '[' << before << "][" << after << ']';
364 else if (!after.empty())
365 os << '[' << after << ']';
367 string::const_iterator it = getContents().begin();
368 string::const_iterator end = getContents().end();
369 // Paranoia check: make sure that there is no whitespace in here
372 for (; it != end; ++it) {
375 if (*it != ' ' || last != ',')
379 os << '{' << content << '}';
385 void InsetCitation::validate(LaTeXFeatures & features) const
387 if (features.bufferParams().use_natbib)
388 features.require("natbib");
389 else if (features.bufferParams().use_jurabib)
390 features.require("jurabib");