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 biblio::CiteEngine engine)
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" && engine == biblio::ENGINE_JURABIB) {
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") {
151 case biblio::ENGINE_NATBIB_AUTHORYEAR:
152 label += author + op_str + before_str +
155 case biblio::ENGINE_NATBIB_NUMERICAL:
156 label += author + op_str + before_str +
157 '#' + *it + cp + sep_str;
159 case biblio::ENGINE_JURABIB:
160 label += before_str + author + op_str +
163 case biblio::ENGINE_BASIC:
167 // author, year; author, year; ...
168 } else if (cite_type == "citep" ||
169 cite_type == "citealp") {
170 if (engine == biblio::ENGINE_NATBIB_NUMERICAL) {
171 label += *it + sep_str;
173 label += author + ", " + year + sep_str;
176 // (authors1 <before> year;
177 // authors_last <before> year, <after>)
178 } else if (cite_type == "citealt") {
180 case biblio::ENGINE_NATBIB_AUTHORYEAR:
181 label += author + ' ' + before_str +
184 case biblio::ENGINE_NATBIB_NUMERICAL:
185 label += author + ' ' + before_str +
188 case biblio::ENGINE_JURABIB:
189 label += before_str + author + ' ' +
192 case biblio::ENGINE_BASIC:
196 // author; author; ...
197 } else if (cite_type == "citeauthor") {
198 label += author + sep_str;
201 } else if (cite_type == "citeyear" ||
202 cite_type == "citeyearpar") {
203 label += year + sep_str;
206 label = rtrim(rtrim(label), sep);
208 if (!after_str.empty()) {
209 if (cite_type == "citet") {
210 // insert "after" before last ')'
211 label.insert(label.size() - 1, after_str);
214 !(engine == biblio::ENGINE_NATBIB_NUMERICAL &&
215 (cite_type == "citeauthor" ||
216 cite_type == "citeyear"));
222 if (!before_str.empty() && (cite_type == "citep" ||
223 cite_type == "citealp" ||
224 cite_type == "citeyearpar")) {
225 label = before_str + label;
228 if (cite_type == "citep" || cite_type == "citeyearpar")
229 label = string(1, op) + label + string(1, cp);
235 string const getBasicLabel(string const & keyList, string const & after)
237 string keys(keyList);
240 if (contains(keys, ',')) {
241 // Final comma allows while loop to cover all keys
242 keys = ltrim(split(keys, label, ',')) + ',';
243 while (contains(keys, ',')) {
245 keys = ltrim(split(keys, key, ','));
252 label += ", " + after;
254 return '[' + label + ']';
260 InsetCitation::InsetCitation(InsetCommandParams const & p)
261 : InsetCommand(p, "citation")
265 string const InsetCitation::generateLabel(Buffer const & buffer) const
267 string const before = getSecOptions();
268 string const after = getOptions();
271 biblio::CiteEngine const engine = buffer.params().cite_engine;
272 if (engine != biblio::ENGINE_BASIC) {
273 string cmd = getCmdName();
275 // We may be "upgrading" from an older LyX version.
276 // If, however, we use "cite" because the necessary
277 // author/year info is not present in the biblio
278 // database, then getNatbibLabel will exit gracefully
279 // and we'll call getBasicLabel.
280 if (engine == biblio::ENGINE_NATBIB_NUMERICAL)
282 else if (engine == biblio::ENGINE_NATBIB_AUTHORYEAR)
285 label = getNatbibLabel(buffer, cmd, getContents(),
286 before, after, engine);
289 // Fallback to fail-safe
291 label = getBasicLabel(getContents(), after);
298 string const InsetCitation::getScreenLabel(Buffer const & buffer) const
300 biblio::CiteEngine const engine = biblio::getEngine(buffer);
301 if (cache.params == params() && cache.engine == engine)
302 return cache.screen_label;
304 // The label has changed, so we have to re-create it.
305 string const before = getSecOptions();
306 string const after = getOptions();
308 string const glabel = generateLabel(buffer);
310 unsigned int const maxLabelChars = 45;
312 string label = glabel;
313 if (label.size() > maxLabelChars) {
314 label.erase(maxLabelChars-3);
318 cache.engine = engine;
319 cache.params = params();
320 cache.generated_label = glabel;
321 cache.screen_label = label;
327 int InsetCitation::plaintext(Buffer const & buffer, ostream & os, int) const
329 if (cache.params == params() &&
330 cache.engine == biblio::getEngine(buffer))
331 os << cache.generated_label;
333 os << generateLabel(buffer);
338 // Have to overwrite the default InsetCommand method in order to check that
339 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
340 // citations and then changes his mind, turning natbib support off. The output
341 // should revert to \cite[]{}
342 int InsetCitation::latex(Buffer const & buffer, ostream & os,
343 OutputParams const &) const
345 biblio::CiteEngine const cite_engine = buffer.params().cite_engine;
348 switch (cite_engine) {
349 case biblio::ENGINE_BASIC:
352 case biblio::ENGINE_NATBIB_AUTHORYEAR:
353 case biblio::ENGINE_NATBIB_NUMERICAL:
356 case biblio::ENGINE_JURABIB:
358 // jurabib does not (yet) support "force upper case"
359 // and "full author name". Fallback.
360 string cmd = getCmdName();
363 size_t n = cmd.size() - 1;
365 cmd = cmd.substr(0,n);
371 string const before = getSecOptions();
372 string const after = getOptions();
373 if (!before.empty() && cite_engine != biblio::ENGINE_BASIC)
374 os << '[' << before << "][" << after << ']';
375 else if (!after.empty())
376 os << '[' << after << ']';
378 string::const_iterator it = getContents().begin();
379 string::const_iterator end = getContents().end();
380 // Paranoia check: make sure that there is no whitespace in here
383 for (; it != end; ++it) {
386 if (*it != ' ' || last != ',')
390 os << '{' << content << '}';
396 void InsetCitation::validate(LaTeXFeatures & features) const
398 switch (features.bufferParams().cite_engine) {
399 case biblio::ENGINE_BASIC:
401 case biblio::ENGINE_NATBIB_AUTHORYEAR:
402 case biblio::ENGINE_NATBIB_NUMERICAL:
403 features.require("natbib");
405 case biblio::ENGINE_JURABIB:
406 features.require("jurabib");