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 //FIXME See the header for the issue.
79 string const default_cite_command(biblio::CiteEngine engine)
83 case biblio::ENGINE_BASIC:
86 case biblio::ENGINE_NATBIB_AUTHORYEAR:
89 case biblio::ENGINE_NATBIB_NUMERICAL:
92 case biblio::ENGINE_JURABIB:
101 asValidLatexCommand(string const & input, biblio::CiteEngine const engine)
103 string const default_str = default_cite_command(engine);
104 if (!InsetCitation::isCompatibleCommand(input))
109 case biblio::ENGINE_BASIC:
110 output = default_str;
113 case biblio::ENGINE_NATBIB_AUTHORYEAR:
114 case biblio::ENGINE_NATBIB_NUMERICAL:
115 if (input == "cite" || input == "citefield" ||
116 input == "citetitle" || input == "cite*")
117 output = default_str;
118 else if (prefixIs(input, "foot"))
119 output = input.substr(4);
124 case biblio::ENGINE_JURABIB: {
125 // Jurabib does not support the 'uppercase' natbib style.
127 output = string(1, 'c') + input.substr(1);
131 // Jurabib does not support the 'full' natbib style.
132 string::size_type const n = output.size() - 1;
133 if (output != "cite*" && output[n] == '*')
134 output = output.substr(0, n);
144 docstring const getNatbibLabel(Buffer const & buffer,
145 string const & citeType, docstring const & keyList,
146 docstring const & before, docstring const & after,
147 biblio::CiteEngine engine)
149 // Only start the process off after the buffer is loaded from file.
150 if (!buffer.isFullyLoaded())
154 typedef std::map<Buffer const *, BiblioInfo> CachedMap;
155 static CachedMap cached_keys;
157 // and cache the timestamp of the bibliography files.
158 static std::map<FileName, time_t> bibfileStatus;
162 vector<FileName> const & bibfilesCache = buffer.getBibfilesCache();
163 // compare the cached timestamps with the actual ones.
164 bool changed = false;
165 for (vector<FileName>::const_iterator it = bibfilesCache.begin();
166 it != bibfilesCache.end(); ++ it) {
167 FileName const f = *it;
169 std::time_t lastw = f.lastModified();
170 if (lastw != bibfileStatus[f]) {
172 bibfileStatus[f] = lastw;
175 catch (fs::filesystem_error & fserr) {
177 lyxerr << "Couldn't find or read bibtex file "
179 LYXERR(Debug::DEBUG) << "Fs error: "
180 << fserr.what() << endl;
184 // build the list only if the bibfiles have been changed
185 if (cached_keys[&buffer].empty() || bibfileStatus.empty() || changed) {
186 biblist.fillWithBibKeys(&buffer);
187 cached_keys[&buffer] = biblist;
189 // use the cached keys
190 biblist = cached_keys[&buffer];
196 // the natbib citation-styles
197 // CITET: author (year)
198 // CITEP: (author,year)
199 // CITEALT: author year
200 // CITEALP: author, year
201 // CITEAUTHOR: author
203 // CITEYEARPAR: (year)
204 // jurabib supports these plus
205 // CITE: author/<before field>
207 // We don't currently use the full or forceUCase fields.
208 string cite_type = asValidLatexCommand(citeType, engine);
209 if (cite_type[0] == 'C')
210 //If we were going to use them, this would mean ForceUCase
211 cite_type = string(1, 'c') + cite_type.substr(1);
212 if (cite_type[cite_type.size() - 1] == '*')
213 //and this would mean FULL
214 cite_type = cite_type.substr(0, cite_type.size() - 1);
216 docstring before_str;
217 if (!before.empty()) {
218 // In CITET and CITEALT mode, the "before" string is
219 // attached to the label associated with each and every key.
220 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
221 // to the front of the whole only.
222 // In other modes, it is not used at all.
223 if (cite_type == "citet" ||
224 cite_type == "citealt" ||
225 cite_type == "citep" ||
226 cite_type == "citealp" ||
227 cite_type == "citeyearpar")
228 before_str = before + ' ';
229 // In CITE (jurabib), the "before" string is used to attach
230 // the annotator (of legal texts) to the author(s) of the
232 else if (cite_type == "cite")
233 before_str = '/' + before;
237 if (!after.empty()) {
238 // The "after" key is appended only to the end of the whole.
239 after_str = ", " + after;
242 // One day, these might be tunable (as they are in BibTeX).
243 char const op = '('; // opening parenthesis.
244 char const cp = ')'; // closing parenthesis.
245 // puctuation mark separating citation entries.
246 char const * const sep = ";";
248 docstring const op_str = ' ' + docstring(1, op);
249 docstring const cp_str = docstring(1, cp) + ' ';
250 docstring const sep_str = from_ascii(sep) + ' ';
253 vector<docstring> keys = getVectorFromString(keyList);
254 vector<docstring>::const_iterator it = keys.begin();
255 vector<docstring>::const_iterator end = keys.end();
256 for (; it != end; ++it) {
257 // get the bibdata corresponding to the key
258 docstring const author(biblist.getAbbreviatedAuthor(*it));
259 docstring const year(biblist.getYear(*it));
261 // Something isn't right. Fail safely.
262 if (author.empty() || year.empty())
265 // authors1/<before>; ... ;
266 // authors_last, <after>
267 if (cite_type == "cite" && engine == biblio::ENGINE_JURABIB) {
268 if (it == keys.begin())
269 label += author + before_str + sep_str;
271 label += author + sep_str;
273 // (authors1 (<before> year); ... ;
274 // authors_last (<before> year, <after>)
275 } else if (cite_type == "citet") {
277 case biblio::ENGINE_NATBIB_AUTHORYEAR:
278 label += author + op_str + before_str +
281 case biblio::ENGINE_NATBIB_NUMERICAL:
282 label += author + op_str + before_str + '#' + *it + cp + sep_str;
284 case biblio::ENGINE_JURABIB:
285 label += before_str + author + op_str +
288 case biblio::ENGINE_BASIC:
292 // author, year; author, year; ...
293 } else if (cite_type == "citep" ||
294 cite_type == "citealp") {
295 if (engine == biblio::ENGINE_NATBIB_NUMERICAL) {
296 label += *it + sep_str;
298 label += author + ", " + year + sep_str;
301 // (authors1 <before> year;
302 // authors_last <before> year, <after>)
303 } else if (cite_type == "citealt") {
305 case biblio::ENGINE_NATBIB_AUTHORYEAR:
306 label += author + ' ' + before_str +
309 case biblio::ENGINE_NATBIB_NUMERICAL:
310 label += author + ' ' + before_str + '#' + *it + sep_str;
312 case biblio::ENGINE_JURABIB:
313 label += before_str + author + ' ' +
316 case biblio::ENGINE_BASIC:
320 // author; author; ...
321 } else if (cite_type == "citeauthor") {
322 label += author + sep_str;
325 } else if (cite_type == "citeyear" ||
326 cite_type == "citeyearpar") {
327 label += year + sep_str;
330 label = rtrim(rtrim(label), sep);
332 if (!after_str.empty()) {
333 if (cite_type == "citet") {
334 // insert "after" before last ')'
335 label.insert(label.size() - 1, after_str);
338 !(engine == biblio::ENGINE_NATBIB_NUMERICAL &&
339 (cite_type == "citeauthor" ||
340 cite_type == "citeyear"));
346 if (!before_str.empty() && (cite_type == "citep" ||
347 cite_type == "citealp" ||
348 cite_type == "citeyearpar")) {
349 label = before_str + label;
352 if (cite_type == "citep" || cite_type == "citeyearpar")
353 label = op + label + cp;
359 docstring const getBasicLabel(docstring const & keyList, docstring const & after)
361 using support::contains;
363 docstring keys = keyList;
366 if (contains(keys, ',')) {
367 // Final comma allows while loop to cover all keys
368 keys = ltrim(split(keys, label, ',')) + ',';
369 while (contains(keys, ',')) {
371 keys = ltrim(split(keys, key, ','));
378 label += ", " + after;
380 return '[' + label + ']';
386 InsetCitation::InsetCitation(InsetCommandParams const & p)
387 : InsetCommand(p, "citation")
391 CommandInfo const * InsetCitation::findInfo(std::string const & /* cmdName */)
393 // standard cite does only take one argument if jurabib is
394 // not used, but jurabib extends this to two arguments, so
395 // we have to allow both here. InsetCitation takes care that
396 // LaTeX output is nevertheless correct.
397 static const char * const paramnames[] =
398 {"after", "before", "key", ""};
399 static const bool isoptional[] = {true, true, false};
400 static const CommandInfo info = {3, paramnames, isoptional};
405 bool InsetCitation::isCompatibleCommand(std::string const & cmd)
407 vector<string> const & possibles = possible_cite_commands();
408 vector<string>::const_iterator const end = possibles.end();
409 return std::find(possibles.begin(), end, cmd) != end;
413 docstring const InsetCitation::generateLabel(Buffer const & buffer) const
415 docstring const before = getParam("before");
416 docstring const after = getParam("after");
419 biblio::CiteEngine const engine = buffer.params().getEngine();
420 if (engine != biblio::ENGINE_BASIC) {
421 label = getNatbibLabel(buffer, getCmdName(), getParam("key"),
422 before, after, engine);
425 // Fallback to fail-safe
427 label = getBasicLabel(getParam("key"), after);
434 docstring const InsetCitation::getScreenLabel(Buffer const & buffer) const
436 biblio::CiteEngine const engine = buffer.params().getEngine();
437 if (cache.params == params() && cache.engine == engine)
438 return cache.screen_label;
440 // The label has changed, so we have to re-create it.
441 docstring const glabel = generateLabel(buffer);
443 unsigned int const maxLabelChars = 45;
445 docstring label = glabel;
446 if (label.size() > maxLabelChars) {
447 label.erase(maxLabelChars-3);
451 cache.engine = engine;
452 cache.params = params();
453 cache.generated_label = glabel;
454 cache.screen_label = label;
460 int InsetCitation::plaintext(Buffer const & buffer, odocstream & os,
461 OutputParams const &) const
465 if (cache.params == params() &&
466 cache.engine == buffer.params().getEngine())
467 str = cache.generated_label;
469 str = generateLabel(buffer);
476 static docstring const cleanupWhitespace(docstring const & citelist)
478 docstring::const_iterator it = citelist.begin();
479 docstring::const_iterator end = citelist.end();
480 // Paranoia check: make sure that there is no whitespace in here
481 // -- at least not behind commas or at the beginning
483 char_type last = ',';
484 for (; it != end; ++it) {
487 if (*it != ' ' || last != ',')
494 int InsetCitation::docbook(Buffer const &, odocstream & os,
495 OutputParams const &) const
498 << cleanupWhitespace(getParam("key"))
504 int InsetCitation::textString(Buffer const & buf, odocstream & os,
505 OutputParams const & op) const
507 return plaintext(buf, os, op);
511 // Have to overwrite the default InsetCommand method in order to check that
512 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
513 // citations and then changes his mind, turning natbib support off. The output
514 // should revert to \cite[]{}
515 int InsetCitation::latex(Buffer const & buffer, odocstream & os,
516 OutputParams const &) const
518 biblio::CiteEngine cite_engine = buffer.params().getEngine();
520 docstring const cite_str = from_utf8(
521 asValidLatexCommand(getCmdName(), cite_engine));
523 os << "\\" << cite_str;
525 docstring const & before = getParam("before");
526 docstring const & after = getParam("after");
527 if (!before.empty() && cite_engine != biblio::ENGINE_BASIC)
528 os << '[' << before << "][" << after << ']';
529 else if (!after.empty())
530 os << '[' << after << ']';
532 os << '{' << cleanupWhitespace(getParam("key")) << '}';
538 void InsetCitation::validate(LaTeXFeatures & features) const
540 switch (features.bufferParams().getEngine()) {
541 case biblio::ENGINE_BASIC:
543 case biblio::ENGINE_NATBIB_AUTHORYEAR:
544 case biblio::ENGINE_NATBIB_NUMERICAL:
545 features.require("natbib");
547 case biblio::ENGINE_JURABIB:
548 features.require("jurabib");