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 "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "LaTeXFeatures.h"
22 #include "ParIterator.h"
23 #include "TocBackend.h"
25 #include "support/debug.h"
26 #include "support/docstream.h"
27 #include "support/FileNameList.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
34 using namespace lyx::support;
40 vector<string> const init_possible_cite_commands()
42 char const * const possible[] = {
43 "cite", "nocite", "citet", "citep", "citealt", "citealp",
44 "citeauthor", "citeyear", "citeyearpar",
45 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
46 "Citet", "Citep", "Citealt", "Citealp", "Citeauthor",
47 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
49 "footcite", "footcitet", "footcitep", "footcitealt",
50 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
51 "citefield", "citetitle", "cite*"
53 size_t const size_possible = sizeof(possible) / sizeof(possible[0]);
55 return vector<string>(possible, possible + size_possible);
59 vector<string> const & possibleCiteCommands()
61 static vector<string> const possible = init_possible_cite_commands();
66 // FIXME See the header for the issue.
67 string defaultCiteCommand(CiteEngine engine)
74 case ENGINE_NATBIB_AUTHORYEAR:
77 case ENGINE_NATBIB_NUMERICAL:
88 string asValidLatexCommand(string const & input, CiteEngine const engine)
90 string const default_str = defaultCiteCommand(engine);
91 if (!InsetCitation::isCompatibleCommand(input))
100 case ENGINE_NATBIB_AUTHORYEAR:
101 case ENGINE_NATBIB_NUMERICAL:
102 if (input == "cite" || input == "citefield" ||
103 input == "citetitle" || input == "cite*")
104 output = default_str;
105 else if (prefixIs(input, "foot"))
106 output = input.substr(4);
111 case ENGINE_JURABIB: {
112 // Jurabib does not support the 'uppercase' natbib style.
114 output = string(1, 'c') + input.substr(1);
118 // Jurabib does not support the 'full' natbib style.
119 string::size_type const n = output.size() - 1;
120 if (output != "cite*" && output[n] == '*')
121 output = output.substr(0, n);
131 docstring complexLabel(Buffer const & buffer,
132 string const & citeType, docstring const & keyList,
133 docstring const & before, docstring const & after,
136 // Only start the process off after the buffer is loaded from file.
137 if (!buffer.isFullyLoaded())
140 BiblioInfo const & biblist = buffer.masterBibInfo();
144 // the natbib citation-styles
145 // CITET: author (year)
146 // CITEP: (author,year)
147 // CITEALT: author year
148 // CITEALP: author, year
149 // CITEAUTHOR: author
151 // CITEYEARPAR: (year)
152 // jurabib supports these plus
153 // CITE: author/<before field>
155 // We don't currently use the full or forceUCase fields.
156 string cite_type = asValidLatexCommand(citeType, engine);
157 if (cite_type[0] == 'C')
158 //If we were going to use them, this would mean ForceUCase
159 cite_type = string(1, 'c') + cite_type.substr(1);
160 if (cite_type[cite_type.size() - 1] == '*')
161 //and this would mean FULL
162 cite_type = cite_type.substr(0, cite_type.size() - 1);
164 docstring before_str;
165 if (!before.empty()) {
166 // In CITET and CITEALT mode, the "before" string is
167 // attached to the label associated with each and every key.
168 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
169 // to the front of the whole only.
170 // In other modes, it is not used at all.
171 if (cite_type == "citet" ||
172 cite_type == "citealt" ||
173 cite_type == "citep" ||
174 cite_type == "citealp" ||
175 cite_type == "citeyearpar")
176 before_str = before + ' ';
177 // In CITE (jurabib), the "before" string is used to attach
178 // the annotator (of legal texts) to the author(s) of the
180 else if (cite_type == "cite")
181 before_str = '/' + before;
185 // The "after" key is appended only to the end of the whole.
186 if (cite_type == "nocite")
187 after_str = " (" + _("not cited") + ')';
188 else if (!after.empty()) {
189 after_str = ", " + after;
192 // One day, these might be tunable (as they are in BibTeX).
193 char op, cp; // opening and closing parenthesis.
194 char * sep; // punctuation mark separating citation entries.
195 if (engine == ENGINE_BASIC) {
205 docstring const op_str = ' ' + docstring(1, op);
206 docstring const cp_str = docstring(1, cp) + ' ';
207 docstring const sep_str = from_ascii(sep) + ' ';
210 vector<docstring> keys = getVectorFromString(keyList);
211 vector<docstring>::const_iterator it = keys.begin();
212 vector<docstring>::const_iterator end = keys.end();
213 for (; it != end; ++it) {
214 // get the bibdata corresponding to the key
215 docstring const author(biblist.getAbbreviatedAuthor(*it));
216 docstring const year(biblist.getYear(*it));
218 // Something isn't right. Fail safely.
219 if (author.empty() || year.empty())
222 // authors1/<before>; ... ;
223 // authors_last, <after>
224 if (cite_type == "cite") {
225 if (engine == ENGINE_BASIC) {
226 label += *it + sep_str;
227 } else if (engine == ENGINE_JURABIB) {
228 if (it == keys.begin())
229 label += author + before_str + sep_str;
231 label += author + sep_str;
235 } else if (cite_type == "nocite") {
236 label += *it + sep_str;
238 // (authors1 (<before> year); ... ;
239 // authors_last (<before> year, <after>)
240 } else if (cite_type == "citet") {
242 case ENGINE_NATBIB_AUTHORYEAR:
243 label += author + op_str + before_str +
246 case ENGINE_NATBIB_NUMERICAL:
247 label += author + op_str + before_str + '#' + *it + cp + sep_str;
250 label += before_str + author + op_str +
257 // author, year; author, year; ...
258 } else if (cite_type == "citep" ||
259 cite_type == "citealp") {
260 if (engine == ENGINE_NATBIB_NUMERICAL) {
261 label += *it + sep_str;
263 label += author + ", " + year + sep_str;
266 // (authors1 <before> year;
267 // authors_last <before> year, <after>)
268 } else if (cite_type == "citealt") {
270 case ENGINE_NATBIB_AUTHORYEAR:
271 label += author + ' ' + before_str +
274 case ENGINE_NATBIB_NUMERICAL:
275 label += author + ' ' + before_str + '#' + *it + sep_str;
278 label += before_str + author + ' ' +
285 // author; author; ...
286 } else if (cite_type == "citeauthor") {
287 label += author + sep_str;
290 } else if (cite_type == "citeyear" ||
291 cite_type == "citeyearpar") {
292 label += year + sep_str;
295 label = rtrim(rtrim(label), sep);
297 if (!after_str.empty()) {
298 if (cite_type == "citet") {
299 // insert "after" before last ')'
300 label.insert(label.size() - 1, after_str);
303 !(engine == ENGINE_NATBIB_NUMERICAL &&
304 (cite_type == "citeauthor" ||
305 cite_type == "citeyear"));
311 if (!before_str.empty() && (cite_type == "citep" ||
312 cite_type == "citealp" ||
313 cite_type == "citeyearpar")) {
314 label = before_str + label;
317 if (cite_type == "citep" || cite_type == "citeyearpar" ||
318 (cite_type == "cite" && engine == ENGINE_BASIC) )
319 label = op + label + cp;
325 docstring basicLabel(docstring const & keyList, docstring const & after)
327 docstring keys = keyList;
330 if (contains(keys, ',')) {
331 // Final comma allows while loop to cover all keys
332 keys = ltrim(split(keys, label, ',')) + ',';
333 while (contains(keys, ',')) {
335 keys = ltrim(split(keys, key, ','));
343 label += ", " + after;
345 return '[' + label + ']';
351 ParamInfo InsetCitation::param_info_;
354 InsetCitation::InsetCitation(InsetCommandParams const & p)
355 : InsetCommand(p, "citation")
359 ParamInfo const & InsetCitation::findInfo(string const & /* cmdName */)
361 // standard cite does only take one argument if jurabib is
362 // not used, but jurabib extends this to two arguments, so
363 // we have to allow both here. InsetCitation takes care that
364 // LaTeX output is nevertheless correct.
365 if (param_info_.empty()) {
366 param_info_.add("after", ParamInfo::LATEX_OPTIONAL);
367 param_info_.add("before", ParamInfo::LATEX_OPTIONAL);
368 param_info_.add("key", ParamInfo::LATEX_REQUIRED);
374 bool InsetCitation::isCompatibleCommand(string const & cmd)
376 vector<string> const & possibles = possibleCiteCommands();
377 vector<string>::const_iterator const end = possibles.end();
378 return find(possibles.begin(), end, cmd) != end;
382 docstring InsetCitation::generateLabel() const
384 docstring const before = getParam("before");
385 docstring const after = getParam("after");
388 CiteEngine const engine = buffer().params().citeEngine();
389 label = complexLabel(buffer(), getCmdName(), getParam("key"),
390 before, after, engine);
392 // Fallback to fail-safe
394 label = basicLabel(getParam("key"), after);
400 docstring InsetCitation::screenLabel() const
402 return cache.screen_label;
406 void InsetCitation::updateLabels(ParIterator const &)
408 CiteEngine const engine = buffer().params().citeEngine();
409 if (cache.params == params() && cache.engine == engine)
412 // The label has changed, so we have to re-create it.
413 docstring const glabel = generateLabel();
415 unsigned int const maxLabelChars = 45;
417 docstring label = glabel;
418 if (label.size() > maxLabelChars) {
419 label.erase(maxLabelChars-3);
423 cache.engine = engine;
424 cache.params = params();
425 cache.generated_label = glabel;
426 cache.screen_label = label;
430 void InsetCitation::addToToc(ParConstIterator const & cpit) const
432 Toc & toc = buffer().tocBackend().toc("citation");
433 toc.push_back(TocItem(cpit, 0, cache.screen_label));
437 int InsetCitation::plaintext(odocstream & os, OutputParams const &) const
439 os << cache.generated_label;
440 return cache.generated_label.size();
444 static docstring const cleanupWhitespace(docstring const & citelist)
446 docstring::const_iterator it = citelist.begin();
447 docstring::const_iterator end = citelist.end();
448 // Paranoia check: make sure that there is no whitespace in here
449 // -- at least not behind commas or at the beginning
451 char_type last = ',';
452 for (; it != end; ++it) {
455 if (*it != ' ' || last != ',')
462 int InsetCitation::docbook(odocstream & os, OutputParams const &) const
464 os << from_ascii("<citation>")
465 << cleanupWhitespace(getParam("key"))
466 << from_ascii("</citation>");
471 void InsetCitation::textString(odocstream & os) const
473 plaintext(os, OutputParams(0));
477 // Have to overwrite the default InsetCommand method in order to check that
478 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
479 // citations and then changes his mind, turning natbib support off. The output
480 // should revert to \cite[]{}
481 int InsetCitation::latex(odocstream & os, OutputParams const &) const
483 CiteEngine cite_engine = buffer().params().citeEngine();
485 docstring const cite_str = from_utf8(
486 asValidLatexCommand(getCmdName(), cite_engine));
488 os << "\\" << cite_str;
490 docstring const & before = getParam("before");
491 docstring const & after = getParam("after");
492 if (!before.empty() && cite_engine != ENGINE_BASIC)
493 os << '[' << before << "][" << after << ']';
494 else if (!after.empty())
495 os << '[' << after << ']';
497 os << '{' << cleanupWhitespace(getParam("key")) << '}';
503 void InsetCitation::validate(LaTeXFeatures & features) const
505 switch (features.bufferParams().citeEngine()) {
508 case ENGINE_NATBIB_AUTHORYEAR:
509 case ENGINE_NATBIB_NUMERICAL:
510 features.require("natbib");
513 features.require("jurabib");
519 docstring InsetCitation::contextMenu(BufferView const &, int, int) const
521 return from_ascii("context-citation");