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"
18 #include "support/debug.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "LaTeXFeatures.h"
23 #include "support/lstrings.h"
24 #include "support/docstream.h"
25 #include "support/FileNameList.h"
33 using support::FileName;
34 using support::FileNameList;
35 using support::getStringFromVector;
36 using support::getVectorFromString;
38 using support::prefixIs;
41 using support::tokenPos;
45 vector<string> const init_possible_cite_commands()
47 char const * const possible[] = {
48 "cite", "citet", "citep", "citealt", "citealp",
49 "citeauthor", "citeyear", "citeyearpar",
50 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
51 "Citet", "Citep", "Citealt", "Citealp", "Citeauthor",
52 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
54 "footcite", "footcitet", "footcitep", "footcitealt",
55 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
56 "citefield", "citetitle", "cite*"
58 size_t const size_possible = sizeof(possible) / sizeof(possible[0]);
60 return vector<string>(possible, possible + size_possible);
64 vector<string> const & possible_cite_commands()
66 static vector<string> const possible = init_possible_cite_commands();
71 //FIXME See the header for the issue.
72 string const default_cite_command(biblio::CiteEngine engine)
76 case biblio::ENGINE_BASIC:
79 case biblio::ENGINE_NATBIB_AUTHORYEAR:
82 case biblio::ENGINE_NATBIB_NUMERICAL:
85 case biblio::ENGINE_JURABIB:
94 asValidLatexCommand(string const & input, biblio::CiteEngine const engine)
96 string const default_str = default_cite_command(engine);
97 if (!InsetCitation::isCompatibleCommand(input))
102 case biblio::ENGINE_BASIC:
103 output = default_str;
106 case biblio::ENGINE_NATBIB_AUTHORYEAR:
107 case biblio::ENGINE_NATBIB_NUMERICAL:
108 if (input == "cite" || input == "citefield" ||
109 input == "citetitle" || input == "cite*")
110 output = default_str;
111 else if (prefixIs(input, "foot"))
112 output = input.substr(4);
117 case biblio::ENGINE_JURABIB: {
118 // Jurabib does not support the 'uppercase' natbib style.
120 output = string(1, 'c') + input.substr(1);
124 // Jurabib does not support the 'full' natbib style.
125 string::size_type const n = output.size() - 1;
126 if (output != "cite*" && output[n] == '*')
127 output = output.substr(0, n);
137 docstring const getNatbibLabel(Buffer const & buffer,
138 string const & citeType, docstring const & keyList,
139 docstring const & before, docstring const & after,
140 biblio::CiteEngine engine)
142 // Only start the process off after the buffer is loaded from file.
143 if (!buffer.isFullyLoaded())
147 typedef std::map<Buffer const *, BiblioInfo> CachedMap;
148 static CachedMap cached_keys;
150 // and cache the timestamp of the bibliography files.
151 static std::map<FileName, time_t> bibfileStatus;
155 FileNameList const & bibfilesCache = buffer.getBibfilesCache();
156 // compare the cached timestamps with the actual ones.
157 bool changed = false;
158 for (FileNameList::const_iterator it = bibfilesCache.begin();
159 it != bibfilesCache.end(); ++ it) {
160 FileName const f = *it;
161 std::time_t lastw = f.lastModified();
162 if (lastw != bibfileStatus[f]) {
164 bibfileStatus[f] = lastw;
168 // build the list only if the bibfiles have been changed
169 if (cached_keys[&buffer].empty() || bibfileStatus.empty() || changed) {
170 biblist.fillWithBibKeys(&buffer);
171 cached_keys[&buffer] = biblist;
173 // use the cached keys
174 biblist = cached_keys[&buffer];
180 // the natbib citation-styles
181 // CITET: author (year)
182 // CITEP: (author,year)
183 // CITEALT: author year
184 // CITEALP: author, year
185 // CITEAUTHOR: author
187 // CITEYEARPAR: (year)
188 // jurabib supports these plus
189 // CITE: author/<before field>
191 // We don't currently use the full or forceUCase fields.
192 string cite_type = asValidLatexCommand(citeType, engine);
193 if (cite_type[0] == 'C')
194 //If we were going to use them, this would mean ForceUCase
195 cite_type = string(1, 'c') + cite_type.substr(1);
196 if (cite_type[cite_type.size() - 1] == '*')
197 //and this would mean FULL
198 cite_type = cite_type.substr(0, cite_type.size() - 1);
200 docstring before_str;
201 if (!before.empty()) {
202 // In CITET and CITEALT mode, the "before" string is
203 // attached to the label associated with each and every key.
204 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
205 // to the front of the whole only.
206 // In other modes, it is not used at all.
207 if (cite_type == "citet" ||
208 cite_type == "citealt" ||
209 cite_type == "citep" ||
210 cite_type == "citealp" ||
211 cite_type == "citeyearpar")
212 before_str = before + ' ';
213 // In CITE (jurabib), the "before" string is used to attach
214 // the annotator (of legal texts) to the author(s) of the
216 else if (cite_type == "cite")
217 before_str = '/' + before;
221 if (!after.empty()) {
222 // The "after" key is appended only to the end of the whole.
223 after_str = ", " + after;
226 // One day, these might be tunable (as they are in BibTeX).
227 char const op = '('; // opening parenthesis.
228 char const cp = ')'; // closing parenthesis.
229 // puctuation mark separating citation entries.
230 char const * const sep = ";";
232 docstring const op_str = ' ' + docstring(1, op);
233 docstring const cp_str = docstring(1, cp) + ' ';
234 docstring const sep_str = from_ascii(sep) + ' ';
237 vector<docstring> keys = getVectorFromString(keyList);
238 vector<docstring>::const_iterator it = keys.begin();
239 vector<docstring>::const_iterator end = keys.end();
240 for (; it != end; ++it) {
241 // get the bibdata corresponding to the key
242 docstring const author(biblist.getAbbreviatedAuthor(*it));
243 docstring const year(biblist.getYear(*it));
245 // Something isn't right. Fail safely.
246 if (author.empty() || year.empty())
249 // authors1/<before>; ... ;
250 // authors_last, <after>
251 if (cite_type == "cite" && engine == biblio::ENGINE_JURABIB) {
252 if (it == keys.begin())
253 label += author + before_str + sep_str;
255 label += author + sep_str;
257 // (authors1 (<before> year); ... ;
258 // authors_last (<before> year, <after>)
259 } else if (cite_type == "citet") {
261 case biblio::ENGINE_NATBIB_AUTHORYEAR:
262 label += author + op_str + before_str +
265 case biblio::ENGINE_NATBIB_NUMERICAL:
266 label += author + op_str + before_str + '#' + *it + cp + sep_str;
268 case biblio::ENGINE_JURABIB:
269 label += before_str + author + op_str +
272 case biblio::ENGINE_BASIC:
276 // author, year; author, year; ...
277 } else if (cite_type == "citep" ||
278 cite_type == "citealp") {
279 if (engine == biblio::ENGINE_NATBIB_NUMERICAL) {
280 label += *it + sep_str;
282 label += author + ", " + year + sep_str;
285 // (authors1 <before> year;
286 // authors_last <before> year, <after>)
287 } else if (cite_type == "citealt") {
289 case biblio::ENGINE_NATBIB_AUTHORYEAR:
290 label += author + ' ' + before_str +
293 case biblio::ENGINE_NATBIB_NUMERICAL:
294 label += author + ' ' + before_str + '#' + *it + sep_str;
296 case biblio::ENGINE_JURABIB:
297 label += before_str + author + ' ' +
300 case biblio::ENGINE_BASIC:
304 // author; author; ...
305 } else if (cite_type == "citeauthor") {
306 label += author + sep_str;
309 } else if (cite_type == "citeyear" ||
310 cite_type == "citeyearpar") {
311 label += year + sep_str;
314 label = rtrim(rtrim(label), sep);
316 if (!after_str.empty()) {
317 if (cite_type == "citet") {
318 // insert "after" before last ')'
319 label.insert(label.size() - 1, after_str);
322 !(engine == biblio::ENGINE_NATBIB_NUMERICAL &&
323 (cite_type == "citeauthor" ||
324 cite_type == "citeyear"));
330 if (!before_str.empty() && (cite_type == "citep" ||
331 cite_type == "citealp" ||
332 cite_type == "citeyearpar")) {
333 label = before_str + label;
336 if (cite_type == "citep" || cite_type == "citeyearpar")
337 label = op + label + cp;
343 docstring const getBasicLabel(docstring const & keyList, docstring const & after)
345 using support::contains;
347 docstring keys = keyList;
350 if (contains(keys, ',')) {
351 // Final comma allows while loop to cover all keys
352 keys = ltrim(split(keys, label, ',')) + ',';
353 while (contains(keys, ',')) {
355 keys = ltrim(split(keys, key, ','));
362 label += ", " + after;
364 return '[' + label + ']';
370 InsetCitation::InsetCitation(InsetCommandParams const & p)
371 : InsetCommand(p, "citation")
375 CommandInfo const * InsetCitation::findInfo(std::string const & /* cmdName */)
377 // standard cite does only take one argument if jurabib is
378 // not used, but jurabib extends this to two arguments, so
379 // we have to allow both here. InsetCitation takes care that
380 // LaTeX output is nevertheless correct.
381 static const char * const paramnames[] =
382 {"after", "before", "key", ""};
383 static const bool isoptional[] = {true, true, false};
384 static const CommandInfo info = {3, paramnames, isoptional};
389 bool InsetCitation::isCompatibleCommand(std::string const & cmd)
391 vector<string> const & possibles = possible_cite_commands();
392 vector<string>::const_iterator const end = possibles.end();
393 return std::find(possibles.begin(), end, cmd) != end;
397 docstring const InsetCitation::generateLabel(Buffer const & buffer) const
399 docstring const before = getParam("before");
400 docstring const after = getParam("after");
403 biblio::CiteEngine const engine = buffer.params().getEngine();
404 if (engine != biblio::ENGINE_BASIC) {
405 label = getNatbibLabel(buffer, getCmdName(), getParam("key"),
406 before, after, engine);
409 // Fallback to fail-safe
411 label = getBasicLabel(getParam("key"), after);
417 docstring const InsetCitation::getScreenLabel(Buffer const & buffer) const
419 biblio::CiteEngine const engine = buffer.params().getEngine();
420 if (cache.params == params() && cache.engine == engine)
421 return cache.screen_label;
423 // The label has changed, so we have to re-create it.
424 docstring const glabel = generateLabel(buffer);
426 unsigned int const maxLabelChars = 45;
428 docstring label = glabel;
429 if (label.size() > maxLabelChars) {
430 label.erase(maxLabelChars-3);
434 cache.engine = engine;
435 cache.params = params();
436 cache.generated_label = glabel;
437 cache.screen_label = label;
443 int InsetCitation::plaintext(Buffer const & buffer, odocstream & os,
444 OutputParams const &) const
448 if (cache.params == params() &&
449 cache.engine == buffer.params().getEngine())
450 str = cache.generated_label;
452 str = generateLabel(buffer);
459 static docstring const cleanupWhitespace(docstring const & citelist)
461 docstring::const_iterator it = citelist.begin();
462 docstring::const_iterator end = citelist.end();
463 // Paranoia check: make sure that there is no whitespace in here
464 // -- at least not behind commas or at the beginning
466 char_type last = ',';
467 for (; it != end; ++it) {
470 if (*it != ' ' || last != ',')
477 int InsetCitation::docbook(Buffer const &, odocstream & os,
478 OutputParams const &) const
480 os << from_ascii("<citation>")
481 << cleanupWhitespace(getParam("key"))
482 << from_ascii("</citation>");
487 int InsetCitation::textString(Buffer const & buf, odocstream & os,
488 OutputParams const & op) const
490 return plaintext(buf, os, op);
494 // Have to overwrite the default InsetCommand method in order to check that
495 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
496 // citations and then changes his mind, turning natbib support off. The output
497 // should revert to \cite[]{}
498 int InsetCitation::latex(Buffer const & buffer, odocstream & os,
499 OutputParams const &) const
501 biblio::CiteEngine cite_engine = buffer.params().getEngine();
503 docstring const cite_str = from_utf8(
504 asValidLatexCommand(getCmdName(), cite_engine));
506 os << "\\" << cite_str;
508 docstring const & before = getParam("before");
509 docstring const & after = getParam("after");
510 if (!before.empty() && cite_engine != biblio::ENGINE_BASIC)
511 os << '[' << before << "][" << after << ']';
512 else if (!after.empty())
513 os << '[' << after << ']';
515 os << '{' << cleanupWhitespace(getParam("key")) << '}';
521 void InsetCitation::validate(LaTeXFeatures & features) const
523 switch (features.bufferParams().getEngine()) {
524 case biblio::ENGINE_BASIC:
526 case biblio::ENGINE_NATBIB_AUTHORYEAR:
527 case biblio::ENGINE_NATBIB_NUMERICAL:
528 features.require("natbib");
530 case biblio::ENGINE_JURABIB:
531 features.require("jurabib");