2 * \file InsetCitation.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
9 * Full author contact details are available in file CREDITS.
14 #include "InsetCitation.h"
17 #include "BufferParams.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "LaTeXFeatures.h"
23 #include "support/fs_extras.h"
24 #include "support/lstrings.h"
28 #include <boost/filesystem/operations.hpp>
29 #include <boost/filesystem/exception.hpp>
34 using support::FileName;
35 using support::getStringFromVector;
36 using support::getVectorFromString;
38 using support::prefixIs;
41 using support::tokenPos;
47 namespace fs = boost::filesystem;
52 vector<string> const init_possible_cite_commands()
54 char const * const possible[] = {
55 "cite", "citet", "citep", "citealt", "citealp",
56 "citeauthor", "citeyear", "citeyearpar",
57 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
58 "Citet", "Citep", "Citealt", "Citealp", "Citeauthor",
59 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
61 "footcite", "footcitet", "footcitep", "footcitealt",
62 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
63 "citefield", "citetitle", "cite*"
65 size_t const size_possible = sizeof(possible) / sizeof(possible[0]);
67 return vector<string>(possible, possible + size_possible);
71 vector<string> const & possible_cite_commands()
73 static vector<string> const possible = init_possible_cite_commands();
78 bool is_possible_cite_command(string const & input)
80 vector<string> const & possibles = possible_cite_commands();
81 vector<string>::const_iterator const end = possibles.end();
82 return std::find(possibles.begin(), end, input) != end;
86 string const default_cite_command(biblio::CiteEngine engine)
90 case biblio::ENGINE_BASIC:
93 case biblio::ENGINE_NATBIB_AUTHORYEAR:
96 case biblio::ENGINE_NATBIB_NUMERICAL:
99 case biblio::ENGINE_JURABIB:
108 asValidLatexCommand(string const & input, biblio::CiteEngine const engine)
110 string const default_str = default_cite_command(engine);
111 if (!is_possible_cite_command(input))
116 case biblio::ENGINE_BASIC:
117 output = default_str;
120 case biblio::ENGINE_NATBIB_AUTHORYEAR:
121 case biblio::ENGINE_NATBIB_NUMERICAL:
122 if (input == "cite" || input == "citefield" ||
123 input == "citetitle" || input == "cite*")
124 output = default_str;
125 else if (prefixIs(input, "foot"))
126 output = input.substr(4);
131 case biblio::ENGINE_JURABIB: {
132 // Jurabib does not support the 'uppercase' natbib style.
134 output = string(1, 'c') + input.substr(1);
138 // Jurabib does not support the 'full' natbib style.
139 string::size_type const n = output.size() - 1;
140 if (output != "cite*" && output[n] == '*')
141 output = output.substr(0, n);
151 docstring const getNatbibLabel(Buffer const & buffer,
152 string const & citeType, docstring const & keyList,
153 docstring const & before, docstring const & after,
154 biblio::CiteEngine engine)
156 // Only start the process off after the buffer is loaded from file.
157 if (!buffer.isFullyLoaded())
161 typedef std::map<Buffer const *, BiblioInfo> CachedMap;
162 static CachedMap cached_keys;
164 // and cache the timestamp of the bibliography files.
165 static std::map<FileName, time_t> bibfileStatus;
169 vector<FileName> const & bibfilesCache = buffer.getBibfilesCache();
170 // compare the cached timestamps with the actual ones.
171 bool changed = false;
172 for (vector<FileName>::const_iterator it = bibfilesCache.begin();
173 it != bibfilesCache.end(); ++ it) {
174 FileName const f = *it;
176 std::time_t lastw = f.lastModified();
177 if (lastw != bibfileStatus[f]) {
179 bibfileStatus[f] = lastw;
182 catch (fs::filesystem_error & fserr) {
184 lyxerr << "Couldn't find or read bibtex file "
186 LYXERR(Debug::DEBUG) << "Fs error: "
187 << fserr.what() << endl;
191 // build the list only if the bibfiles have been changed
192 if (cached_keys[&buffer].empty() || bibfileStatus.empty() || changed) {
193 biblist.fillWithBibKeys(&buffer);
194 cached_keys[&buffer] = biblist;
196 // use the cached keys
197 biblist = cached_keys[&buffer];
203 // the natbib citation-styles
204 // CITET: author (year)
205 // CITEP: (author,year)
206 // CITEALT: author year
207 // CITEALP: author, year
208 // CITEAUTHOR: author
210 // CITEYEARPAR: (year)
211 // jurabib supports these plus
212 // CITE: author/<before field>
214 // We don't currently use the full or forceUCase fields.
215 string cite_type = asValidLatexCommand(citeType, engine);
216 if (cite_type[0] == 'C')
217 //If we were going to use them, this would mean ForceUCase
218 cite_type = string(1, 'c') + cite_type.substr(1);
219 if (cite_type[cite_type.size() - 1] == '*')
220 //and this would mean FULL
221 cite_type = cite_type.substr(0, cite_type.size() - 1);
223 docstring before_str;
224 if (!before.empty()) {
225 // In CITET and CITEALT mode, the "before" string is
226 // attached to the label associated with each and every key.
227 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
228 // to the front of the whole only.
229 // In other modes, it is not used at all.
230 if (cite_type == "citet" ||
231 cite_type == "citealt" ||
232 cite_type == "citep" ||
233 cite_type == "citealp" ||
234 cite_type == "citeyearpar")
235 before_str = before + ' ';
236 // In CITE (jurabib), the "before" string is used to attach
237 // the annotator (of legal texts) to the author(s) of the
239 else if (cite_type == "cite")
240 before_str = '/' + before;
244 if (!after.empty()) {
245 // The "after" key is appended only to the end of the whole.
246 after_str = ", " + after;
249 // One day, these might be tunable (as they are in BibTeX).
250 char const op = '('; // opening parenthesis.
251 char const cp = ')'; // closing parenthesis.
252 // puctuation mark separating citation entries.
253 char const * const sep = ";";
255 docstring const op_str = ' ' + docstring(1, op);
256 docstring const cp_str = docstring(1, cp) + ' ';
257 docstring const sep_str = from_ascii(sep) + ' ';
260 vector<docstring> keys = getVectorFromString(keyList);
261 vector<docstring>::const_iterator it = keys.begin();
262 vector<docstring>::const_iterator end = keys.end();
263 for (; it != end; ++it) {
264 // get the bibdata corresponding to the key
265 docstring const author(biblist.getAbbreviatedAuthor(*it));
266 docstring const year(biblist.getYear(*it));
268 // Something isn't right. Fail safely.
269 if (author.empty() || year.empty())
272 // authors1/<before>; ... ;
273 // authors_last, <after>
274 if (cite_type == "cite" && engine == biblio::ENGINE_JURABIB) {
275 if (it == keys.begin())
276 label += author + before_str + sep_str;
278 label += author + sep_str;
280 // (authors1 (<before> year); ... ;
281 // authors_last (<before> year, <after>)
282 } else if (cite_type == "citet") {
284 case biblio::ENGINE_NATBIB_AUTHORYEAR:
285 label += author + op_str + before_str +
288 case biblio::ENGINE_NATBIB_NUMERICAL:
289 label += author + op_str + before_str + '#' + *it + cp + sep_str;
291 case biblio::ENGINE_JURABIB:
292 label += before_str + author + op_str +
295 case biblio::ENGINE_BASIC:
299 // author, year; author, year; ...
300 } else if (cite_type == "citep" ||
301 cite_type == "citealp") {
302 if (engine == biblio::ENGINE_NATBIB_NUMERICAL) {
303 label += *it + sep_str;
305 label += author + ", " + year + sep_str;
308 // (authors1 <before> year;
309 // authors_last <before> year, <after>)
310 } else if (cite_type == "citealt") {
312 case biblio::ENGINE_NATBIB_AUTHORYEAR:
313 label += author + ' ' + before_str +
316 case biblio::ENGINE_NATBIB_NUMERICAL:
317 label += author + ' ' + before_str + '#' + *it + sep_str;
319 case biblio::ENGINE_JURABIB:
320 label += before_str + author + ' ' +
323 case biblio::ENGINE_BASIC:
327 // author; author; ...
328 } else if (cite_type == "citeauthor") {
329 label += author + sep_str;
332 } else if (cite_type == "citeyear" ||
333 cite_type == "citeyearpar") {
334 label += year + sep_str;
337 label = rtrim(rtrim(label), sep);
339 if (!after_str.empty()) {
340 if (cite_type == "citet") {
341 // insert "after" before last ')'
342 label.insert(label.size() - 1, after_str);
345 !(engine == biblio::ENGINE_NATBIB_NUMERICAL &&
346 (cite_type == "citeauthor" ||
347 cite_type == "citeyear"));
353 if (!before_str.empty() && (cite_type == "citep" ||
354 cite_type == "citealp" ||
355 cite_type == "citeyearpar")) {
356 label = before_str + label;
359 if (cite_type == "citep" || cite_type == "citeyearpar")
360 label = op + label + cp;
366 docstring const getBasicLabel(docstring const & keyList, docstring const & after)
368 using support::contains;
370 docstring keys = keyList;
373 if (contains(keys, ',')) {
374 // Final comma allows while loop to cover all keys
375 keys = ltrim(split(keys, label, ',')) + ',';
376 while (contains(keys, ',')) {
378 keys = ltrim(split(keys, key, ','));
385 label += ", " + after;
387 return '[' + label + ']';
393 InsetCitation::InsetCitation(InsetCommandParams const & p)
394 : InsetCommand(p, "citation")
398 docstring const InsetCitation::generateLabel(Buffer const & buffer) const
400 docstring const before = getParam("before");
401 docstring const after = getParam("after");
404 biblio::CiteEngine const engine = buffer.params().getEngine();
405 if (engine != biblio::ENGINE_BASIC) {
406 label = getNatbibLabel(buffer, getCmdName(), getParam("key"),
407 before, after, engine);
410 // Fallback to fail-safe
412 label = getBasicLabel(getParam("key"), after);
419 docstring const InsetCitation::getScreenLabel(Buffer const & buffer) const
421 biblio::CiteEngine const engine = buffer.params().getEngine();
422 if (cache.params == params() && cache.engine == engine)
423 return cache.screen_label;
425 // The label has changed, so we have to re-create it.
426 docstring const glabel = generateLabel(buffer);
428 unsigned int const maxLabelChars = 45;
430 docstring label = glabel;
431 if (label.size() > maxLabelChars) {
432 label.erase(maxLabelChars-3);
436 cache.engine = engine;
437 cache.params = params();
438 cache.generated_label = glabel;
439 cache.screen_label = label;
445 int InsetCitation::plaintext(Buffer const & buffer, odocstream & os,
446 OutputParams const &) const
450 if (cache.params == params() &&
451 cache.engine == buffer.params().getEngine())
452 str = cache.generated_label;
454 str = generateLabel(buffer);
461 static docstring const cleanupWhitespace(docstring const & citelist)
463 docstring::const_iterator it = citelist.begin();
464 docstring::const_iterator end = citelist.end();
465 // Paranoia check: make sure that there is no whitespace in here
466 // -- at least not behind commas or at the beginning
468 char_type last = ',';
469 for (; it != end; ++it) {
472 if (*it != ' ' || last != ',')
479 int InsetCitation::docbook(Buffer const &, odocstream & os,
480 OutputParams const &) const
483 << cleanupWhitespace(getParam("key"))
489 int InsetCitation::textString(Buffer const & buf, odocstream & os,
490 OutputParams const & op) const
492 return plaintext(buf, os, op);
496 // Have to overwrite the default InsetCommand method in order to check that
497 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
498 // citations and then changes his mind, turning natbib support off. The output
499 // should revert to \cite[]{}
500 int InsetCitation::latex(Buffer const & buffer, odocstream & os,
501 OutputParams const &) const
503 biblio::CiteEngine cite_engine = buffer.params().getEngine();
505 docstring const cite_str = from_utf8(
506 asValidLatexCommand(getCmdName(), cite_engine));
508 os << "\\" << cite_str;
510 docstring const & before = getParam("before");
511 docstring const & after = getParam("after");
512 if (!before.empty() && cite_engine != biblio::ENGINE_BASIC)
513 os << '[' << before << "][" << after << ']';
514 else if (!after.empty())
515 os << '[' << after << ']';
517 os << '{' << cleanupWhitespace(getParam("key")) << '}';
523 void InsetCitation::validate(LaTeXFeatures & features) const
525 switch (features.bufferParams().getEngine()) {
526 case biblio::ENGINE_BASIC:
528 case biblio::ENGINE_NATBIB_AUTHORYEAR:
529 case biblio::ENGINE_NATBIB_NUMERICAL:
530 features.require("natbib");
532 case biblio::ENGINE_JURABIB:
533 features.require("jurabib");