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 "frontends/controllers/frontend_helpers.h"
25 #include "support/fs_extras.h"
26 #include "support/lstrings.h"
30 #include <boost/filesystem/operations.hpp>
31 #include <boost/filesystem/exception.hpp>
36 using support::FileName;
37 using support::getStringFromVector;
38 using support::getVectorFromString;
40 using support::prefixIs;
43 using support::tokenPos;
49 namespace fs = boost::filesystem;
54 vector<string> const init_possible_cite_commands()
56 char const * const pos[] = {
57 "cite", "citet", "citep", "citealt", "citealp",
58 "citeauthor", "citeyear", "citeyearpar",
59 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
60 "Citet", "Citep", "Citealt", "Citealp", "Citeauthor",
61 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
63 "footcite", "footcitet", "footcitep", "footcitealt",
64 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
65 "citefield", "citetitle", "cite*"
67 size_t const size_pos = sizeof(pos) / sizeof(pos[0]);
69 return vector<string>(pos, pos + size_pos);
73 vector<string> const & possible_cite_commands()
75 static vector<string> const pos = init_possible_cite_commands();
80 bool is_possible_cite_command(string const & input)
82 vector<string> const & possibles = possible_cite_commands();
83 vector<string>::const_iterator const end = possibles.end();
84 return std::find(possibles.begin(), end, input) != end;
88 string const default_cite_command(biblio::CiteEngine engine)
92 case biblio::ENGINE_BASIC:
95 case biblio::ENGINE_NATBIB_AUTHORYEAR:
98 case biblio::ENGINE_NATBIB_NUMERICAL:
101 case biblio::ENGINE_JURABIB:
110 asValidLatexCommand(string const & input, biblio::CiteEngine const engine)
112 string const default_str = default_cite_command(engine);
113 if (!is_possible_cite_command(input))
118 case biblio::ENGINE_BASIC:
119 output = default_str;
122 case biblio::ENGINE_NATBIB_AUTHORYEAR:
123 case biblio::ENGINE_NATBIB_NUMERICAL:
124 if (input == "cite" || input == "citefield" ||
125 input == "citetitle" || input == "cite*")
126 output = default_str;
127 else if (prefixIs(input, "foot"))
128 output = input.substr(4);
133 case biblio::ENGINE_JURABIB: {
134 // Jurabib does not support the 'uppercase' natbib style.
136 output = string(1, 'c') + input.substr(1);
140 // Jurabib does not support the 'full' natbib style.
141 string::size_type const n = output.size() - 1;
142 if (output != "cite*" && output[n] == '*')
143 output = output.substr(0, n);
153 docstring const getNatbibLabel(Buffer const & buffer,
154 string const & citeType, docstring const & keyList,
155 docstring const & before, docstring const & after,
156 biblio::CiteEngine engine)
158 // Only start the process off after the buffer is loaded from file.
159 if (!buffer.fully_loaded())
163 typedef std::map<Buffer const *, BiblioInfo> CachedMap;
164 static CachedMap cached_keys;
166 // and cache the timestamp of the bibliography files.
167 static std::map<FileName, time_t> bibfileStatus;
171 vector<FileName> const & bibfilesCache = buffer.getBibfilesCache();
172 // compare the cached timestamps with the actual ones.
173 bool changed = false;
174 for (vector<FileName>::const_iterator it = bibfilesCache.begin();
175 it != bibfilesCache.end(); ++ it) {
176 FileName const f = *it;
178 std::time_t lastw = fs::last_write_time(f.toFilesystemEncoding());
179 if (lastw != bibfileStatus[f]) {
181 bibfileStatus[f] = lastw;
184 catch (fs::filesystem_error & fserr) {
186 lyxerr << "Couldn't find or read bibtex file "
188 LYXERR(Debug::DEBUG) << "Fs error: "
189 << fserr.what() << endl;
193 // build the list only if the bibfiles have been changed
194 if (cached_keys[&buffer].empty() || bibfileStatus.empty() || changed) {
195 biblist.fillWithBibKeys(&buffer);
196 cached_keys[&buffer] = biblist;
198 // use the cached keys
199 biblist = cached_keys[&buffer];
204 // the natbib citation-styles
205 // CITET: author (year)
206 // CITEP: (author,year)
207 // CITEALT: author year
208 // CITEALP: author, year
209 // CITEAUTHOR: author
211 // CITEYEARPAR: (year)
212 // jurabib supports these plus
213 // CITE: author/<before field>
215 // We don't currently use the full or forceUCase fields.
216 string cite_type = asValidLatexCommand(citeType, engine);
217 if (cite_type[0] == 'C')
218 //If we were going to use them, this would mean ForceUCase
219 cite_type = string(1, 'c') + cite_type.substr(1);
220 if (cite_type[cite_type.size() - 1] == '*')
221 //and this would mean FULL
222 cite_type = cite_type.substr(0, cite_type.size() - 1);
224 docstring before_str;
225 if (!before.empty()) {
226 // In CITET and CITEALT mode, the "before" string is
227 // attached to the label associated with each and every key.
228 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
229 // to the front of the whole only.
230 // In other modes, it is not used at all.
231 if (cite_type == "citet" ||
232 cite_type == "citealt" ||
233 cite_type == "citep" ||
234 cite_type == "citealp" ||
235 cite_type == "citeyearpar")
236 before_str = before + ' ';
237 // In CITE (jurabib), the "before" string is used to attach
238 // the annotator (of legal texts) to the author(s) of the
240 else if (cite_type == "cite")
241 before_str = '/' + before;
245 if (!after.empty()) {
246 // The "after" key is appended only to the end of the whole.
247 after_str = ", " + after;
250 // One day, these might be tunable (as they are in BibTeX).
251 char const op = '('; // opening parenthesis.
252 char const cp = ')'; // closing parenthesis.
253 // puctuation mark separating citation entries.
254 char const * const sep = ";";
256 docstring const op_str(' ' + docstring(1, op));
257 docstring const cp_str(docstring(1, cp) + ' ');
258 docstring const sep_str(from_ascii(sep) + ' ');
261 vector<docstring> keys = getVectorFromString(keyList);
262 vector<docstring>::const_iterator it = keys.begin();
263 vector<docstring>::const_iterator end = keys.end();
264 for (; it != end; ++it) {
265 // get the bibdata corresponding to the key
266 docstring const author(biblist.getAbbreviatedAuthor(*it));
267 docstring const year(biblist.getYear(*it));
269 // Something isn't right. Fail safely.
270 if (author.empty() || year.empty())
273 // authors1/<before>; ... ;
274 // authors_last, <after>
275 if (cite_type == "cite" && engine == biblio::ENGINE_JURABIB) {
276 if (it == keys.begin())
277 label += author + before_str + sep_str;
279 label += author + sep_str;
281 // (authors1 (<before> year); ... ;
282 // authors_last (<before> year, <after>)
283 } else if (cite_type == "citet") {
285 case biblio::ENGINE_NATBIB_AUTHORYEAR:
286 label += author + op_str + before_str +
289 case biblio::ENGINE_NATBIB_NUMERICAL:
290 label += author + op_str + before_str + '#' + *it + cp + sep_str;
292 case biblio::ENGINE_JURABIB:
293 label += before_str + author + op_str +
296 case biblio::ENGINE_BASIC:
300 // author, year; author, year; ...
301 } else if (cite_type == "citep" ||
302 cite_type == "citealp") {
303 if (engine == biblio::ENGINE_NATBIB_NUMERICAL) {
304 label += *it + sep_str;
306 label += author + ", " + year + sep_str;
309 // (authors1 <before> year;
310 // authors_last <before> year, <after>)
311 } else if (cite_type == "citealt") {
313 case biblio::ENGINE_NATBIB_AUTHORYEAR:
314 label += author + ' ' + before_str +
317 case biblio::ENGINE_NATBIB_NUMERICAL:
318 label += author + ' ' + before_str + '#' + *it + sep_str;
320 case biblio::ENGINE_JURABIB:
321 label += before_str + author + ' ' +
324 case biblio::ENGINE_BASIC:
328 // author; author; ...
329 } else if (cite_type == "citeauthor") {
330 label += author + sep_str;
333 } else if (cite_type == "citeyear" ||
334 cite_type == "citeyearpar") {
335 label += year + sep_str;
338 label = rtrim(rtrim(label), sep);
340 if (!after_str.empty()) {
341 if (cite_type == "citet") {
342 // insert "after" before last ')'
343 label.insert(label.size() - 1, after_str);
346 !(engine == biblio::ENGINE_NATBIB_NUMERICAL &&
347 (cite_type == "citeauthor" ||
348 cite_type == "citeyear"));
354 if (!before_str.empty() && (cite_type == "citep" ||
355 cite_type == "citealp" ||
356 cite_type == "citeyearpar")) {
357 label = before_str + label;
360 if (cite_type == "citep" || cite_type == "citeyearpar")
361 label = op + label + cp;
367 docstring const getBasicLabel(docstring const & keyList, docstring const & after)
369 using support::contains;
371 docstring keys(keyList);
374 if (contains(keys, ',')) {
375 // Final comma allows while loop to cover all keys
376 keys = ltrim(split(keys, label, ',')) + ',';
377 while (contains(keys, ',')) {
379 keys = ltrim(split(keys, key, ','));
386 label += ", " + after;
388 return '[' + label + ']';
394 InsetCitation::InsetCitation(InsetCommandParams const & p)
395 : InsetCommand(p, "citation")
399 docstring const InsetCitation::generateLabel(Buffer const & buffer) const
401 docstring const before = getParam("before");
402 docstring const after = getParam("after");
405 biblio::CiteEngine const engine = buffer.params().getEngine();
406 if (engine != biblio::ENGINE_BASIC) {
407 label = getNatbibLabel(buffer, getCmdName(), getParam("key"),
408 before, after, engine);
411 // Fallback to fail-safe
413 label = getBasicLabel(getParam("key"), after);
420 docstring const InsetCitation::getScreenLabel(Buffer const & buffer) const
422 biblio::CiteEngine const engine = buffer.params().getEngine();
423 if (cache.params == params() && cache.engine == engine)
424 return cache.screen_label;
426 // The label has changed, so we have to re-create it.
427 docstring const glabel = generateLabel(buffer);
429 unsigned int const maxLabelChars = 45;
431 docstring label = glabel;
432 if (label.size() > maxLabelChars) {
433 label.erase(maxLabelChars-3);
437 cache.engine = engine;
438 cache.params = params();
439 cache.generated_label = glabel;
440 cache.screen_label = label;
446 int InsetCitation::plaintext(Buffer const & buffer, odocstream & os,
447 OutputParams const &) const
451 if (cache.params == params() &&
452 cache.engine == buffer.params().getEngine())
453 str = cache.generated_label;
455 str = generateLabel(buffer);
464 docstring const cleanupWhitespace(docstring const & citelist)
466 docstring::const_iterator it = citelist.begin();
467 docstring::const_iterator end = citelist.end();
468 // Paranoia check: make sure that there is no whitespace in here
469 // -- at least not behind commas or at the beginning
471 char_type last = ',';
472 for (; it != end; ++it) {
475 if (*it != ' ' || last != ',')
481 // end anon namyspace
484 int InsetCitation::docbook(Buffer const &, odocstream & os,
485 OutputParams const &) const
488 << cleanupWhitespace(getParam("key"))
494 int InsetCitation::textString(Buffer const & buf, odocstream & os,
495 OutputParams const & op) const
497 return plaintext(buf, os, op);
501 // Have to overwrite the default InsetCommand method in order to check that
502 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
503 // citations and then changes his mind, turning natbib support off. The output
504 // should revert to \cite[]{}
505 int InsetCitation::latex(Buffer const & buffer, odocstream & os,
506 OutputParams const &) const
508 biblio::CiteEngine cite_engine = buffer.params().getEngine();
510 docstring const cite_str = from_utf8(
511 asValidLatexCommand(getCmdName(), cite_engine));
513 os << "\\" << cite_str;
515 docstring const & before = getParam("before");
516 docstring const & after = getParam("after");
517 if (!before.empty() && cite_engine != biblio::ENGINE_BASIC)
518 os << '[' << before << "][" << after << ']';
519 else if (!after.empty())
520 os << '[' << after << ']';
522 os << '{' << cleanupWhitespace(getParam("key")) << '}';
528 void InsetCitation::validate(LaTeXFeatures & features) const
530 switch (features.bufferParams().getEngine()) {
531 case biblio::ENGINE_BASIC:
533 case biblio::ENGINE_NATBIB_AUTHORYEAR:
534 case biblio::ENGINE_NATBIB_NUMERICAL:
535 features.require("natbib");
537 case biblio::ENGINE_JURABIB:
538 features.require("jurabib");
544 void InsetCitation::replaceContents(string const & from, string const & to)
546 if (tokenPos(getContents(), ',', from) != -1) {
547 vector<string> items = getVectorFromString(getContents());
548 std::replace(items.begin(), items.end(), from, to);
549 setContents(getStringFromVector(items));