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"
16 #include "BiblioInfo.h"
18 #include "buffer_funcs.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "LaTeXFeatures.h"
25 #include "output_xhtml.h"
26 #include "ParIterator.h"
27 #include "TocBackend.h"
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/FileNameList.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
38 using namespace lyx::support;
42 ParamInfo InsetCitation::param_info_;
45 InsetCitation::InsetCitation(Buffer * buf, InsetCommandParams const & p)
46 : InsetCommand(buf, p)
50 ParamInfo const & InsetCitation::findInfo(string const & /* cmdName */)
52 // standard cite does only take one argument if jurabib is
53 // not used, but jurabib extends this to two arguments, so
54 // we have to allow both here. InsetCitation takes care that
55 // LaTeX output is nevertheless correct.
56 if (param_info_.empty()) {
57 param_info_.add("after", ParamInfo::LATEX_OPTIONAL);
58 param_info_.add("before", ParamInfo::LATEX_OPTIONAL);
59 param_info_.add("key", ParamInfo::LATEX_REQUIRED);
67 vector<string> const init_possible_cite_commands()
69 char const * const possible[] = {
70 "cite", "nocite", "citet", "citep", "citealt", "citealp",
71 "citeauthor", "citeyear", "citeyearpar",
72 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
73 "Citet", "Citep", "Citealt", "Citealp", "Citeauthor",
74 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
76 "footcite", "footcitet", "footcitep", "footcitealt",
77 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
78 "citefield", "citetitle", "cite*"
80 size_t const size_possible = sizeof(possible) / sizeof(possible[0]);
82 return vector<string>(possible, possible + size_possible);
86 vector<string> const & possibleCiteCommands()
88 static vector<string> const possible = init_possible_cite_commands();
96 bool InsetCitation::isCompatibleCommand(string const & cmd)
98 vector<string> const & possibles = possibleCiteCommands();
99 vector<string>::const_iterator const end = possibles.end();
100 return find(possibles.begin(), end, cmd) != end;
104 void InsetCitation::doDispatch(Cursor & cur, FuncRequest & cmd)
106 if (cmd.action() == LFUN_INSET_MODIFY)
107 cache.recalculate = true;
108 InsetCommand::doDispatch(cur, cmd);
112 docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
114 Buffer const & buf = bv.buffer();
115 // Only after the buffer is loaded from file...
116 if (!buf.isFullyLoaded())
119 BiblioInfo const & bi = buf.masterBibInfo();
121 return _("No bibliography defined!");
123 docstring const & key = getParam("key");
125 return _("No citations selected!");
127 vector<docstring> keys = getVectorFromString(key);
128 vector<docstring>::const_iterator it = keys.begin();
129 vector<docstring>::const_iterator en = keys.end();
131 for (; it != en; ++it) {
132 docstring const key_info = bi.getInfo(*it, buffer());
133 if (key_info.empty())
137 tip += wrap(key_info, -4);
145 // FIXME See the header for the issue.
146 string defaultCiteCommand(CiteEngine engine)
153 case ENGINE_NATBIB_AUTHORYEAR:
156 case ENGINE_NATBIB_NUMERICAL:
167 string asValidLatexCommand(string const & input, CiteEngine const engine)
169 string const default_str = defaultCiteCommand(engine);
170 if (!InsetCitation::isCompatibleCommand(input))
176 if (input == "nocite")
179 output = default_str;
182 case ENGINE_NATBIB_AUTHORYEAR:
183 case ENGINE_NATBIB_NUMERICAL:
184 if (input == "cite" || input == "citefield"
185 || input == "citetitle" || input == "cite*")
186 output = default_str;
187 else if (prefixIs(input, "foot"))
188 output = input.substr(4);
193 case ENGINE_JURABIB: {
194 // Jurabib does not support the 'uppercase' natbib style.
196 output = string(1, 'c') + input.substr(1);
200 // Jurabib does not support the 'full' natbib style.
201 string::size_type const n = output.size() - 1;
202 if (output != "cite*" && output[n] == '*')
203 output = output.substr(0, n);
213 inline docstring wrapCitation(docstring const & key,
214 docstring const & content, bool for_xhtml)
218 // we have to do the escaping here, because we will ultimately
219 // write this as a raw string, so as not to escape the tags.
220 return "<a href='#" + key + "'>" +
221 html::htmlize(content, XHTMLStream::ESCAPE_ALL) + "</a>";
224 } // anonymous namespace
226 docstring InsetCitation::generateLabel(bool for_xhtml) const
229 label = complexLabel(for_xhtml);
231 // Fallback to fail-safe
233 label = basicLabel(for_xhtml);
239 docstring InsetCitation::complexLabel(bool for_xhtml) const
241 Buffer const & buf = buffer();
242 // Only start the process off after the buffer is loaded from file.
243 if (!buf.isFullyLoaded())
246 BiblioInfo const & biblist = buf.masterBibInfo();
250 // the natbib citation-styles
251 // CITET: author (year)
252 // CITEP: (author,year)
253 // CITEALT: author year
254 // CITEALP: author, year
255 // CITEAUTHOR: author
257 // CITEYEARPAR: (year)
258 // jurabib supports these plus
259 // CITE: author/<before field>
261 CiteEngine const engine = buffer().params().citeEngine();
262 // We don't currently use the full or forceUCase fields.
263 string cite_type = asValidLatexCommand(getCmdName(), engine);
264 if (cite_type[0] == 'C')
265 // If we were going to use them, this would mean ForceUCase
266 cite_type = string(1, 'c') + cite_type.substr(1);
267 if (cite_type[cite_type.size() - 1] == '*')
268 // and this would mean FULL
269 cite_type = cite_type.substr(0, cite_type.size() - 1);
271 docstring const & before = getParam("before");
272 docstring before_str;
273 if (!before.empty()) {
274 // In CITET and CITEALT mode, the "before" string is
275 // attached to the label associated with each and every key.
276 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
277 // to the front of the whole only.
278 // In other modes, it is not used at all.
279 if (cite_type == "citet" ||
280 cite_type == "citealt" ||
281 cite_type == "citep" ||
282 cite_type == "citealp" ||
283 cite_type == "citeyearpar")
284 before_str = before + ' ';
285 // In CITE (jurabib), the "before" string is used to attach
286 // the annotator (of legal texts) to the author(s) of the
288 else if (cite_type == "cite")
289 before_str = '/' + before;
292 docstring const & after = getParam("after");
294 // The "after" key is appended only to the end of the whole.
295 if (cite_type == "nocite")
296 after_str = " (" + _("not cited") + ')';
297 else if (!after.empty()) {
298 after_str = ", " + after;
301 // One day, these might be tunable (as they are in BibTeX).
302 char op, cp; // opening and closing parenthesis.
303 const char * sep; // punctuation mark separating citation entries.
304 if (engine == ENGINE_BASIC) {
314 docstring const op_str = ' ' + docstring(1, op);
315 docstring const cp_str = docstring(1, cp) + ' ';
316 docstring const sep_str = from_ascii(sep) + ' ';
319 vector<docstring> keys = getVectorFromString(getParam("key"));
320 vector<docstring>::const_iterator it = keys.begin();
321 vector<docstring>::const_iterator end = keys.end();
322 for (; it != end; ++it) {
323 // get the bibdata corresponding to the key
324 docstring const author = biblist.getAbbreviatedAuthor(*it);
325 docstring const year = biblist.getYear(*it, for_xhtml);
326 docstring const citenum = for_xhtml ? biblist.getCiteNumber(*it) : *it;
328 if (author.empty() || year.empty())
329 // We can't construct a "complex" label without that info.
333 // authors1/<before>; ... ;
334 // authors_last, <after>
335 if (cite_type == "cite") {
336 if (engine == ENGINE_BASIC) {
337 label += wrapCitation(*it, citenum, for_xhtml) + sep_str;
338 } else if (engine == ENGINE_JURABIB) {
339 if (it == keys.begin())
340 label += wrapCitation(*it, author, for_xhtml) + before_str + sep_str;
342 label += wrapCitation(*it, author, for_xhtml) + sep_str;
346 else if (cite_type == "nocite") {
347 label += *it + sep_str;
349 // (authors1 (<before> year); ... ;
350 // authors_last (<before> year, <after>)
351 else if (cite_type == "citet") {
353 case ENGINE_NATBIB_AUTHORYEAR:
354 label += author + op_str + before_str +
355 wrapCitation(*it, year, for_xhtml) + cp + sep_str;
357 case ENGINE_NATBIB_NUMERICAL:
358 label += author + op_str + before_str +
359 wrapCitation(*it, citenum, for_xhtml) + cp + sep_str;
362 label += before_str + author + op_str +
363 wrapCitation(*it, year, for_xhtml) + cp + sep_str;
369 // author, year; author, year; ...
370 else if (cite_type == "citep" ||
371 cite_type == "citealp") {
372 if (engine == ENGINE_NATBIB_NUMERICAL) {
373 label += wrapCitation(*it, citenum, for_xhtml) + sep_str;
375 label += wrapCitation(*it, author + ", " + year, for_xhtml) + sep_str;
379 // (authors1 <before> year;
380 // authors_last <before> year, <after>)
381 else if (cite_type == "citealt") {
383 case ENGINE_NATBIB_AUTHORYEAR:
384 label += author + ' ' + before_str +
385 wrapCitation(*it, year, for_xhtml) + sep_str;
387 case ENGINE_NATBIB_NUMERICAL:
388 label += author + ' ' + before_str + '#' +
389 wrapCitation(*it, citenum, for_xhtml) + sep_str;
392 label += before_str +
393 wrapCitation(*it, author + ' ' + year, for_xhtml) + sep_str;
401 // author; author; ...
402 else if (cite_type == "citeauthor") {
403 label += wrapCitation(*it, author, for_xhtml) + sep_str;
406 else if (cite_type == "citeyear" ||
407 cite_type == "citeyearpar") {
408 label += wrapCitation(*it, year, for_xhtml) + sep_str;
411 label = rtrim(rtrim(label), sep);
413 if (!after_str.empty()) {
414 if (cite_type == "citet") {
415 // insert "after" before last ')'
416 label.insert(label.size() - 1, after_str);
419 !(engine == ENGINE_NATBIB_NUMERICAL &&
420 (cite_type == "citeauthor" ||
421 cite_type == "citeyear"));
427 if (!before_str.empty() && (cite_type == "citep" ||
428 cite_type == "citealp" ||
429 cite_type == "citeyearpar")) {
430 label = before_str + label;
433 if (cite_type == "citep" || cite_type == "citeyearpar" ||
434 (cite_type == "cite" && engine == ENGINE_BASIC) )
435 label = op + label + cp;
441 docstring InsetCitation::basicLabel(bool for_xhtml) const
443 docstring keys = getParam("key");
448 // if there is no comma, then everything goes into key
449 // and keys will be empty.
450 keys = trim(split(keys, key, ','));
454 label += wrapCitation(key, key, for_xhtml);
455 } while (!keys.empty());
457 docstring const & after = getParam("after");
459 label += ", " + after;
461 return '[' + label + ']';
464 docstring InsetCitation::screenLabel() const
466 return cache.screen_label;
470 void InsetCitation::updateBuffer(ParIterator const &, UpdateType)
472 if (!cache.recalculate && buffer().citeLabelsValid())
475 // The label may have changed, so we have to re-create it.
476 docstring const glabel = generateLabel();
478 unsigned int const maxLabelChars = 45;
480 docstring label = glabel;
481 if (label.size() > maxLabelChars) {
482 label.erase(maxLabelChars - 3);
486 cache.recalculate = false;
487 cache.generated_label = glabel;
488 cache.screen_label = label;
492 void InsetCitation::addToToc(DocIterator const & cpit) const
495 // XHTML output uses the TOC to collect the citations
496 // from the document. So if this gets changed, then we
497 // will need to change how the citations are collected.
498 docstring const tocitem = getParam("key");
499 Toc & toc = buffer().tocBackend().toc("citation");
500 toc.push_back(TocItem(cpit, 0, tocitem));
504 int InsetCitation::plaintext(odocstream & os, OutputParams const &) const
506 os << cache.generated_label;
507 return cache.generated_label.size();
511 static docstring const cleanupWhitespace(docstring const & citelist)
513 docstring::const_iterator it = citelist.begin();
514 docstring::const_iterator end = citelist.end();
515 // Paranoia check: make sure that there is no whitespace in here
516 // -- at least not behind commas or at the beginning
518 char_type last = ',';
519 for (; it != end; ++it) {
522 if (*it != ' ' || last != ',')
529 int InsetCitation::docbook(odocstream & os, OutputParams const &) const
531 os << from_ascii("<citation>")
532 << cleanupWhitespace(getParam("key"))
533 << from_ascii("</citation>");
538 docstring InsetCitation::xhtml(XHTMLStream & xs, OutputParams const &) const
540 string const & cmd = getCmdName();
544 // have to output this raw, because generateLabel() will include tags
545 xs << XHTMLStream::ESCAPE_NONE << generateLabel(true);
551 void InsetCitation::toString(odocstream & os) const
553 plaintext(os, OutputParams(0));
557 void InsetCitation::forToc(docstring & os, size_t) const
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 void InsetCitation::latex(otexstream & os, OutputParams const & runparams) const
569 CiteEngine cite_engine = buffer().params().citeEngine();
570 BiblioInfo const & bi = buffer().masterBibInfo();
572 docstring const cite_str = from_utf8(
573 asValidLatexCommand(getCmdName(), cite_engine));
575 if (runparams.inulemcmd)
578 os << "\\" << cite_str;
580 docstring const & before = getParam("before");
581 docstring const & after = getParam("after");
582 if (!before.empty() && cite_engine != ENGINE_BASIC)
583 os << '[' << before << "][" << after << ']';
584 else if (!after.empty())
585 os << '[' << after << ']';
587 if (!bi.isBibtex(getParam("key")))
588 // escape chars with bibitems
589 os << '{' << escape(cleanupWhitespace(getParam("key"))) << '}';
591 os << '{' << cleanupWhitespace(getParam("key")) << '}';
593 if (runparams.inulemcmd)
598 void InsetCitation::validate(LaTeXFeatures & features) const
600 switch (features.bufferParams().citeEngine()) {
603 case ENGINE_NATBIB_AUTHORYEAR:
604 case ENGINE_NATBIB_NUMERICAL:
605 features.require("natbib");
608 features.require("jurabib");
614 string InsetCitation::contextMenuName() const
616 return "context-citation";