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 "buffer_funcs.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "LaTeXFeatures.h"
23 #include "output_xhtml.h"
24 #include "ParIterator.h"
25 #include "TocBackend.h"
27 #include "support/debug.h"
28 #include "support/docstream.h"
29 #include "support/FileNameList.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
36 using namespace lyx::support;
40 ParamInfo InsetCitation::param_info_;
43 InsetCitation::InsetCitation(Buffer * buf, InsetCommandParams const & p)
44 : InsetCommand(buf, p, "citation")
48 ParamInfo const & InsetCitation::findInfo(string const & /* cmdName */)
50 // standard cite does only take one argument if jurabib is
51 // not used, but jurabib extends this to two arguments, so
52 // we have to allow both here. InsetCitation takes care that
53 // LaTeX output is nevertheless correct.
54 if (param_info_.empty()) {
55 param_info_.add("after", ParamInfo::LATEX_OPTIONAL);
56 param_info_.add("before", ParamInfo::LATEX_OPTIONAL);
57 param_info_.add("key", ParamInfo::LATEX_REQUIRED);
65 vector<string> const init_possible_cite_commands()
67 char const * const possible[] = {
68 "cite", "nocite", "citet", "citep", "citealt", "citealp",
69 "citeauthor", "citeyear", "citeyearpar",
70 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
71 "Citet", "Citep", "Citealt", "Citealp", "Citeauthor",
72 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
74 "footcite", "footcitet", "footcitep", "footcitealt",
75 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
76 "citefield", "citetitle", "cite*"
78 size_t const size_possible = sizeof(possible) / sizeof(possible[0]);
80 return vector<string>(possible, possible + size_possible);
84 vector<string> const & possibleCiteCommands()
86 static vector<string> const possible = init_possible_cite_commands();
94 bool InsetCitation::isCompatibleCommand(string const & cmd)
96 vector<string> const & possibles = possibleCiteCommands();
97 vector<string>::const_iterator const end = possibles.end();
98 return find(possibles.begin(), end, cmd) != end;
102 docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
104 Buffer const & buf = bv.buffer();
105 // Only after the buffer is loaded from file...
106 if (!buf.isFullyLoaded())
109 BiblioInfo const & bi = buf.masterBibInfo();
111 return _("No bibliography defined!");
113 docstring const & key = getParam("key");
115 return _("No citations selected!");
117 vector<docstring> keys = getVectorFromString(key);
118 vector<docstring>::const_iterator it = keys.begin();
119 vector<docstring>::const_iterator en = keys.end();
121 for (; it != en; ++it) {
122 docstring const key_info = bi.getInfo(*it);
123 if (key_info.empty())
127 tip += wrap(key_info, -4);
135 // FIXME See the header for the issue.
136 string defaultCiteCommand(CiteEngine engine)
143 case ENGINE_NATBIB_AUTHORYEAR:
146 case ENGINE_NATBIB_NUMERICAL:
157 string asValidLatexCommand(string const & input, CiteEngine const engine)
159 string const default_str = defaultCiteCommand(engine);
160 if (!InsetCitation::isCompatibleCommand(input))
166 if (input == "nocite")
169 output = default_str;
172 case ENGINE_NATBIB_AUTHORYEAR:
173 case ENGINE_NATBIB_NUMERICAL:
174 if (input == "cite" || input == "citefield"
175 || input == "citetitle" || input == "cite*")
176 output = default_str;
177 else if (prefixIs(input, "foot"))
178 output = input.substr(4);
183 case ENGINE_JURABIB: {
184 // Jurabib does not support the 'uppercase' natbib style.
186 output = string(1, 'c') + input.substr(1);
190 // Jurabib does not support the 'full' natbib style.
191 string::size_type const n = output.size() - 1;
192 if (output != "cite*" && output[n] == '*')
193 output = output.substr(0, n);
202 } // anonymous namespace
204 docstring InsetCitation::generateLabel() const
207 label = complexLabel();
209 // Fallback to fail-safe
211 label = basicLabel();
217 docstring InsetCitation::complexLabel() const
219 Buffer const & buf = buffer();
220 // Only start the process off after the buffer is loaded from file.
221 if (!buf.isFullyLoaded())
224 BiblioInfo const & biblist = buf.masterBibInfo();
228 // the natbib citation-styles
229 // CITET: author (year)
230 // CITEP: (author,year)
231 // CITEALT: author year
232 // CITEALP: author, year
233 // CITEAUTHOR: author
235 // CITEYEARPAR: (year)
236 // jurabib supports these plus
237 // CITE: author/<before field>
239 CiteEngine const engine = buffer().params().citeEngine();
240 // We don't currently use the full or forceUCase fields.
241 string cite_type = asValidLatexCommand(getCmdName(), engine);
242 if (cite_type[0] == 'C')
243 // If we were going to use them, this would mean ForceUCase
244 cite_type = string(1, 'c') + cite_type.substr(1);
245 if (cite_type[cite_type.size() - 1] == '*')
246 // and this would mean FULL
247 cite_type = cite_type.substr(0, cite_type.size() - 1);
249 docstring const & before = getParam("before");
250 docstring before_str;
251 if (!before.empty()) {
252 // In CITET and CITEALT mode, the "before" string is
253 // attached to the label associated with each and every key.
254 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
255 // to the front of the whole only.
256 // In other modes, it is not used at all.
257 if (cite_type == "citet" ||
258 cite_type == "citealt" ||
259 cite_type == "citep" ||
260 cite_type == "citealp" ||
261 cite_type == "citeyearpar")
262 before_str = before + ' ';
263 // In CITE (jurabib), the "before" string is used to attach
264 // the annotator (of legal texts) to the author(s) of the
266 else if (cite_type == "cite")
267 before_str = '/' + before;
270 docstring const & after = getParam("after");
272 // The "after" key is appended only to the end of the whole.
273 if (cite_type == "nocite")
274 after_str = " (" + _("not cited") + ')';
275 else if (!after.empty()) {
276 after_str = ", " + after;
279 // One day, these might be tunable (as they are in BibTeX).
280 char op, cp; // opening and closing parenthesis.
281 const char * sep; // punctuation mark separating citation entries.
282 if (engine == ENGINE_BASIC) {
292 docstring const op_str = ' ' + docstring(1, op);
293 docstring const cp_str = docstring(1, cp) + ' ';
294 docstring const sep_str = from_ascii(sep) + ' ';
297 vector<docstring> keys = getVectorFromString(getParam("key"));
298 vector<docstring>::const_iterator it = keys.begin();
299 vector<docstring>::const_iterator end = keys.end();
300 for (; it != end; ++it) {
301 // get the bibdata corresponding to the key
302 docstring const author = biblist.getAbbreviatedAuthor(*it);
303 docstring const year = biblist.getYear(*it);
305 // Something isn't right. Fail safely.
306 if (author.empty() || year.empty())
309 // authors1/<before>; ... ;
310 // authors_last, <after>
311 if (cite_type == "cite") {
312 if (engine == ENGINE_BASIC) {
313 label += *it + sep_str;
314 } else if (engine == ENGINE_JURABIB) {
315 if (it == keys.begin())
316 label += author + before_str + sep_str;
318 label += author + sep_str;
322 } else if (cite_type == "nocite") {
323 label += *it + sep_str;
325 // (authors1 (<before> year); ... ;
326 // authors_last (<before> year, <after>)
327 } else if (cite_type == "citet") {
329 case ENGINE_NATBIB_AUTHORYEAR:
330 label += author + op_str + before_str +
333 case ENGINE_NATBIB_NUMERICAL:
334 label += author + op_str + before_str + '#' + *it + cp + sep_str;
337 label += before_str + author + op_str +
344 // author, year; author, year; ...
345 } else if (cite_type == "citep" ||
346 cite_type == "citealp") {
347 if (engine == ENGINE_NATBIB_NUMERICAL) {
348 label += *it + sep_str;
350 label += author + ", " + year + sep_str;
353 // (authors1 <before> year;
354 // authors_last <before> year, <after>)
355 } else if (cite_type == "citealt") {
357 case ENGINE_NATBIB_AUTHORYEAR:
358 label += author + ' ' + before_str +
361 case ENGINE_NATBIB_NUMERICAL:
362 label += author + ' ' + before_str + '#' + *it + sep_str;
365 label += before_str + author + ' ' +
372 // author; author; ...
373 } else if (cite_type == "citeauthor") {
374 label += author + sep_str;
377 } else if (cite_type == "citeyear" ||
378 cite_type == "citeyearpar") {
379 label += year + sep_str;
382 label = rtrim(rtrim(label), sep);
384 if (!after_str.empty()) {
385 if (cite_type == "citet") {
386 // insert "after" before last ')'
387 label.insert(label.size() - 1, after_str);
390 !(engine == ENGINE_NATBIB_NUMERICAL &&
391 (cite_type == "citeauthor" ||
392 cite_type == "citeyear"));
398 if (!before_str.empty() && (cite_type == "citep" ||
399 cite_type == "citealp" ||
400 cite_type == "citeyearpar")) {
401 label = before_str + label;
404 if (cite_type == "citep" || cite_type == "citeyearpar" ||
405 (cite_type == "cite" && engine == ENGINE_BASIC) )
406 label = op + label + cp;
412 docstring InsetCitation::basicLabel() const
414 docstring keys = getParam("key");
417 if (contains(keys, ',')) {
418 // Final comma allows while loop to cover all keys
419 keys = ltrim(split(keys, label, ',')) + ',';
420 while (contains(keys, ',')) {
422 keys = ltrim(split(keys, key, ','));
429 docstring const & after = getParam("after");
431 label += ", " + after;
433 return '[' + label + ']';
436 docstring InsetCitation::screenLabel() const
438 return cache.screen_label;
442 void InsetCitation::updateLabels(ParIterator const &, bool)
444 CiteEngine const engine = buffer().params().citeEngine();
445 if (cache.params == params() && cache.engine == engine)
448 // The label has changed, so we have to re-create it.
449 docstring const glabel = generateLabel();
451 unsigned int const maxLabelChars = 45;
453 docstring label = glabel;
454 if (label.size() > maxLabelChars) {
455 label.erase(maxLabelChars-3);
459 cache.engine = engine;
460 cache.params = params();
461 cache.generated_label = glabel;
462 cache.screen_label = label;
466 void InsetCitation::addToToc(DocIterator const & cpit)
468 Toc & toc = buffer().tocBackend().toc("citation");
469 toc.push_back(TocItem(cpit, 0, getParam("key")));
473 int InsetCitation::plaintext(odocstream & os, OutputParams const &) const
475 os << cache.generated_label;
476 return cache.generated_label.size();
480 static docstring const cleanupWhitespace(docstring const & citelist)
482 docstring::const_iterator it = citelist.begin();
483 docstring::const_iterator end = citelist.end();
484 // Paranoia check: make sure that there is no whitespace in here
485 // -- at least not behind commas or at the beginning
487 char_type last = ',';
488 for (; it != end; ++it) {
491 if (*it != ' ' || last != ',')
498 int InsetCitation::docbook(odocstream & os, OutputParams const &) const
500 os << from_ascii("<citation>")
501 << cleanupWhitespace(getParam("key"))
502 << from_ascii("</citation>");
507 docstring InsetCitation::xhtml(XHTMLStream & xs, OutputParams const &) const
509 string const & cmd = getCmdName();
513 BiblioInfo const & bi = buffer().masterBibInfo();
514 docstring const & key_list = getParam("key");
515 if (key_list.empty())
518 // FIXME We should do a better job outputing different things for the
519 // different citation styles. For now, we use square brackets for every
522 docstring const & before = getParam("before");
526 vector<docstring> const keys = getVectorFromString(key_list);
527 vector<docstring>::const_iterator it = keys.begin();
528 vector<docstring>::const_iterator const en = keys.end();
530 for (; it != en; ++it) {
531 BiblioInfo::const_iterator const bt = bi.find(*it);
534 BibTeXInfo const & bibinfo = bt->second;
539 docstring citekey = bibinfo.citeKey();
540 if (citekey.empty()) {
541 citekey = bibinfo.label();
545 string const attr = "href='#" + to_utf8(*it) + "'";
546 xs << StartTag("a", attr) << citekey << EndTag("a");
549 docstring const & after = getParam("after");
557 void InsetCitation::tocString(odocstream & os) const
559 plaintext(os, OutputParams(0));
563 // Have to overwrite the default InsetCommand method in order to check that
564 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
565 // citations and then changes his mind, turning natbib support off. The output
566 // should revert to \cite[]{}
567 int InsetCitation::latex(odocstream & os, OutputParams const & runparams) const
569 CiteEngine cite_engine = buffer().params().citeEngine();
571 docstring const cite_str = from_utf8(
572 asValidLatexCommand(getCmdName(), cite_engine));
574 if (runparams.inulemcmd)
577 os << "\\" << cite_str;
579 docstring const & before = getParam("before");
580 docstring const & after = getParam("after");
581 if (!before.empty() && cite_engine != ENGINE_BASIC)
582 os << '[' << before << "][" << after << ']';
583 else if (!after.empty())
584 os << '[' << after << ']';
586 os << '{' << cleanupWhitespace(getParam("key")) << '}';
588 if (runparams.inulemcmd)
595 void InsetCitation::validate(LaTeXFeatures & features) const
597 switch (features.bufferParams().citeEngine()) {
600 case ENGINE_NATBIB_AUTHORYEAR:
601 case ENGINE_NATBIB_NUMERICAL:
602 features.require("natbib");
605 features.require("jurabib");
611 docstring InsetCitation::contextMenu(BufferView const &, int, int) const
613 return from_ascii("context-citation");