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 "insetcite.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "dispatchresult.h"
20 #include "funcrequest.h"
21 #include "LaTeXFeatures.h"
23 #include "support/lstrings.h"
25 using lyx::support::ascii_lowercase;
26 using lyx::support::contains;
27 using lyx::support::getVectorFromString;
28 using lyx::support::ltrim;
29 using lyx::support::rtrim;
30 using lyx::support::split;
40 string const getNatbibLabel(Buffer const & buffer,
41 string const & citeType, string const & keyList,
42 string const & before, string const & after,
43 bool numerical, bool jura)
45 // Only start the process off after the buffer is loaded from file.
46 if (!buffer.fully_loaded())
49 typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
50 static CachedMap cached_keys;
53 typedef vector<std::pair<string, string> > InfoType;
55 buffer.fillWithBibKeys(bibkeys);
57 InfoType::const_iterator bit = bibkeys.begin();
58 InfoType::const_iterator bend = bibkeys.end();
60 biblio::InfoMap infomap;
61 for (; bit != bend; ++bit) {
62 infomap[bit->first] = bit->second;
67 cached_keys[&buffer] = infomap;
69 // the natbib citation-styles
70 // CITET: author (year)
71 // CITEP: (author,year)
72 // CITEALT: author year
73 // CITEALP: author, year
76 // CITEYEARPAR: (year)
77 // jurabib supports these plus
78 // CITE: author/<before field>
80 // We don't currently use the full or forceUCase fields.
81 // bool const forceUCase = citeType[0] == 'C';
82 bool const full = citeType[citeType.size() - 1] == '*';
84 string const cite_type = full ?
85 ascii_lowercase(citeType.substr(0, citeType.size() - 1)) :
86 ascii_lowercase(citeType);
89 if (!before.empty()) {
90 // In CITET and CITEALT mode, the "before" string is
91 // attached to the label associated with each and every key.
92 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
93 // to the front of the whole only.
94 // In other modes, it is not used at all.
95 if (cite_type == "citet" ||
96 cite_type == "citealt" ||
97 cite_type == "citep" ||
98 cite_type == "citealp" ||
99 cite_type == "citeyearpar")
100 before_str = before + ' ';
101 // In CITE (jurabib), the "before" string is used to attach
102 // the annotator (of legal texts) to the author(s) of the
104 else if (cite_type == "cite")
105 before_str = '/' + before;
109 if (!after.empty()) {
110 // The "after" key is appended only to the end of the whole.
111 after_str = ", " + after;
114 // One day, these might be tunable (as they are in BibTeX).
115 char const op = '('; // opening parenthesis.
116 char const cp = ')'; // closing parenthesis.
117 // puctuation mark separating citation entries.
118 char const * const sep = ";";
120 string const op_str(' ' + string(1, op));
121 string const cp_str(string(1, cp) + ' ');
122 string const sep_str(string(sep) + ' ');
125 vector<string> keys = getVectorFromString(keyList);
126 vector<string>::const_iterator it = keys.begin();
127 vector<string>::const_iterator end = keys.end();
128 for (; it != end; ++it) {
129 // get the bibdata corresponding to the key
130 string const author(biblio::getAbbreviatedAuthor(infomap, *it));
131 string const year(biblio::getYear(infomap, *it));
133 // Something isn't right. Fail safely.
134 if (author.empty() || year.empty())
137 // authors1/<before>; ... ;
138 // authors_last, <after>
139 if (cite_type == "cite" && jura) {
140 if (it == keys.begin())
141 label += author + before_str + sep_str;
143 label += author + sep_str;
145 // (authors1 (<before> year); ... ;
146 // authors_last (<before> year, <after>)
147 } else if (cite_type == "citet") {
148 string const tmp = numerical ? '#' + *it : year;
150 label += author + op_str + before_str + tmp +
153 label += before_str + author + op_str + tmp +
156 // author, year; author, year; ...
157 } else if (cite_type == "citep" ||
158 cite_type == "citealp") {
160 label += *it + sep_str;
162 label += author + ", " + year + sep_str;
165 // (authors1 <before> year;
166 // authors_last <before> year, <after>)
167 } else if (cite_type == "citealt") {
168 string const tmp = numerical ? '#' + *it : year;
170 label += author + ' ' + before_str + tmp + sep_str;
172 label += before_str + author + ' ' + tmp + sep_str;
174 // author; author; ...
175 } else if (cite_type == "citeauthor") {
176 label += author + sep_str;
179 } else if (cite_type == "citeyear" ||
180 cite_type == "citeyearpar") {
181 label += year + sep_str;
184 label = rtrim(rtrim(label), sep);
186 if (!after_str.empty()) {
187 if (cite_type == "citet") {
188 // insert "after" before last ')'
189 label.insert(label.size() - 1, after_str);
191 bool const add = !(numerical &&
192 (cite_type == "citeauthor" ||
193 cite_type == "citeyear"));
199 if (!before_str.empty() && (cite_type == "citep" ||
200 cite_type == "citealp" ||
201 cite_type == "citeyearpar")) {
202 label = before_str + label;
205 if (cite_type == "citep" || cite_type == "citeyearpar")
206 label = string(1, op) + label + string(1, cp);
212 string const getBasicLabel(string const & keyList, string const & after)
214 string keys(keyList);
217 if (contains(keys, ',')) {
218 // Final comma allows while loop to cover all keys
219 keys = ltrim(split(keys, label, ',')) + ',';
220 while (contains(keys, ',')) {
222 keys = ltrim(split(keys, key, ','));
229 label += ", " + after;
231 return '[' + label + ']';
237 InsetCitation::InsetCitation(InsetCommandParams const & p)
238 : InsetCommand(p, "citation")
242 string const InsetCitation::generateLabel(Buffer const & buffer) const
244 string const before = getSecOptions();
245 string const after = getOptions();
248 if (buffer.params().use_natbib || buffer.params().use_jurabib) {
249 string cmd = getCmdName();
250 if (buffer.params().use_natbib && cmd == "cite") {
251 // We may be "upgrading" from an older LyX version.
252 // If, however, we use "cite" because the necessary
253 // author/year info is not present in the biblio
254 // database, then getNatbibLabel will exit gracefully
255 // and we'll call getBasicLabel.
256 if (buffer.params().use_numerical_citations)
261 label = getNatbibLabel(buffer, cmd, getContents(),
263 buffer.params().use_numerical_citations,
264 buffer.params().use_jurabib);
267 // Fallback to fail-safe
269 label = getBasicLabel(getContents(), after);
276 string const InsetCitation::getScreenLabel(Buffer const & buffer) const
278 biblio::CiteEngine const engine = biblio::getEngine(buffer);
279 if (cache.params == params() && cache.engine == engine)
280 return cache.screen_label;
282 // The label has changed, so we have to re-create it.
283 string const before = getSecOptions();
284 string const after = getOptions();
286 string const glabel = generateLabel(buffer);
288 unsigned int const maxLabelChars = 45;
290 string label = glabel;
291 if (label.size() > maxLabelChars) {
292 label.erase(maxLabelChars-3);
296 cache.engine = engine;
297 cache.params = params();
298 cache.generated_label = glabel;
299 cache.screen_label = label;
305 int InsetCitation::plaintext(Buffer const & buffer, ostream & os, int) const
307 if (cache.params == params() &&
308 cache.engine == biblio::getEngine(buffer))
309 os << cache.generated_label;
311 os << generateLabel(buffer);
316 // Have to overwrite the default InsetCommand method in order to check that
317 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
318 // citations and then changes his mind, turning natbib support off. The output
319 // should revert to \cite[]{}
320 int InsetCitation::latex(Buffer const & buffer, ostream & os,
321 OutputParams const &) const
324 if (buffer.params().use_natbib)
326 else if (buffer.params().use_jurabib) {
327 // jurabib does not (yet) support "force upper case"
328 // and "full author name". Fallback.
329 string cmd = getCmdName();
332 size_t n = cmd.size() - 1;
334 cmd = cmd.substr(0,n);
339 string const before = getSecOptions();
340 string const after = getOptions();
342 && (buffer.params().use_natbib || buffer.params().use_jurabib))
343 os << '[' << before << "][" << after << ']';
344 else if (!after.empty())
345 os << '[' << after << ']';
347 string::const_iterator it = getContents().begin();
348 string::const_iterator end = getContents().end();
349 // Paranoia check: make sure that there is no whitespace in here
352 for (; it != end; ++it) {
355 if (*it != ' ' || last != ',')
359 os << '{' << content << '}';
365 void InsetCitation::validate(LaTeXFeatures & features) const
367 if (features.bufferParams().use_natbib)
368 features.require("natbib");
369 else if (features.bufferParams().use_jurabib)
370 features.require("jurabib");