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 biblio::CiteEngine engine)
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" && engine == biblio::ENGINE_JURABIB) {
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") {
149 case biblio::ENGINE_NATBIB_AUTHORYEAR:
150 label += author + op_str + before_str +
153 case biblio::ENGINE_NATBIB_NUMERICAL:
154 label += author + op_str + before_str +
155 '#' + *it + cp + sep_str;
157 case biblio::ENGINE_JURABIB:
158 label += before_str + author + op_str +
161 case biblio::ENGINE_BASIC:
165 // author, year; author, year; ...
166 } else if (cite_type == "citep" ||
167 cite_type == "citealp") {
168 if (engine == biblio::ENGINE_NATBIB_NUMERICAL) {
169 label += *it + sep_str;
171 label += author + ", " + year + sep_str;
174 // (authors1 <before> year;
175 // authors_last <before> year, <after>)
176 } else if (cite_type == "citealt") {
178 case biblio::ENGINE_NATBIB_AUTHORYEAR:
179 label += author + ' ' + before_str +
182 case biblio::ENGINE_NATBIB_NUMERICAL:
183 label += author + ' ' + before_str +
186 case biblio::ENGINE_JURABIB:
187 label += before_str + author + ' ' +
190 case biblio::ENGINE_BASIC:
194 // author; author; ...
195 } else if (cite_type == "citeauthor") {
196 label += author + sep_str;
199 } else if (cite_type == "citeyear" ||
200 cite_type == "citeyearpar") {
201 label += year + sep_str;
204 label = rtrim(rtrim(label), sep);
206 if (!after_str.empty()) {
207 if (cite_type == "citet") {
208 // insert "after" before last ')'
209 label.insert(label.size() - 1, after_str);
212 !(engine == biblio::ENGINE_NATBIB_NUMERICAL &&
213 (cite_type == "citeauthor" ||
214 cite_type == "citeyear"));
220 if (!before_str.empty() && (cite_type == "citep" ||
221 cite_type == "citealp" ||
222 cite_type == "citeyearpar")) {
223 label = before_str + label;
226 if (cite_type == "citep" || cite_type == "citeyearpar")
227 label = string(1, op) + label + string(1, cp);
233 string const getBasicLabel(string const & keyList, string const & after)
235 string keys(keyList);
238 if (contains(keys, ',')) {
239 // Final comma allows while loop to cover all keys
240 keys = ltrim(split(keys, label, ',')) + ',';
241 while (contains(keys, ',')) {
243 keys = ltrim(split(keys, key, ','));
250 label += ", " + after;
252 return '[' + label + ']';
258 InsetCitation::InsetCitation(InsetCommandParams const & p)
259 : InsetCommand(p, "citation")
263 string const InsetCitation::generateLabel(Buffer const & buffer) const
265 string const before = getSecOptions();
266 string const after = getOptions();
269 biblio::CiteEngine const engine = buffer.params().cite_engine;
270 if (engine != biblio::ENGINE_BASIC) {
271 string cmd = getCmdName();
273 // We may be "upgrading" from an older LyX version.
274 // If, however, we use "cite" because the necessary
275 // author/year info is not present in the biblio
276 // database, then getNatbibLabel will exit gracefully
277 // and we'll call getBasicLabel.
278 if (engine == biblio::ENGINE_NATBIB_NUMERICAL)
280 else if (engine == biblio::ENGINE_NATBIB_AUTHORYEAR)
283 label = getNatbibLabel(buffer, cmd, getContents(),
284 before, after, engine);
287 // Fallback to fail-safe
289 label = getBasicLabel(getContents(), after);
296 string const InsetCitation::getScreenLabel(Buffer const & buffer) const
298 biblio::CiteEngine const engine = biblio::getEngine(buffer);
299 if (cache.params == params() && cache.engine == engine)
300 return cache.screen_label;
302 // The label has changed, so we have to re-create it.
303 string const before = getSecOptions();
304 string const after = getOptions();
306 string const glabel = generateLabel(buffer);
308 unsigned int const maxLabelChars = 45;
310 string label = glabel;
311 if (label.size() > maxLabelChars) {
312 label.erase(maxLabelChars-3);
316 cache.engine = engine;
317 cache.params = params();
318 cache.generated_label = glabel;
319 cache.screen_label = label;
325 int InsetCitation::plaintext(Buffer const & buffer, ostream & os, int) const
327 if (cache.params == params() &&
328 cache.engine == biblio::getEngine(buffer))
329 os << cache.generated_label;
331 os << generateLabel(buffer);
336 // Have to overwrite the default InsetCommand method in order to check that
337 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
338 // citations and then changes his mind, turning natbib support off. The output
339 // should revert to \cite[]{}
340 int InsetCitation::latex(Buffer const & buffer, ostream & os,
341 OutputParams const &) const
343 biblio::CiteEngine const cite_engine = buffer.params().cite_engine;
346 switch (cite_engine) {
347 case biblio::ENGINE_BASIC:
350 case biblio::ENGINE_NATBIB_AUTHORYEAR:
351 case biblio::ENGINE_NATBIB_NUMERICAL:
354 case biblio::ENGINE_JURABIB:
356 // jurabib does not (yet) support "force upper case"
357 // and "full author name". Fallback.
358 string cmd = getCmdName();
361 size_t n = cmd.size() - 1;
363 cmd = cmd.substr(0,n);
369 string const before = getSecOptions();
370 string const after = getOptions();
371 if (!before.empty() && cite_engine != biblio::ENGINE_BASIC)
372 os << '[' << before << "][" << after << ']';
373 else if (!after.empty())
374 os << '[' << after << ']';
376 string::const_iterator it = getContents().begin();
377 string::const_iterator end = getContents().end();
378 // Paranoia check: make sure that there is no whitespace in here
381 for (; it != end; ++it) {
384 if (*it != ' ' || last != ',')
388 os << '{' << content << '}';
394 void InsetCitation::validate(LaTeXFeatures & features) const
396 switch (features.bufferParams().cite_engine) {
397 case biblio::ENGINE_BASIC:
399 case biblio::ENGINE_NATBIB_AUTHORYEAR:
400 case biblio::ENGINE_NATBIB_NUMERICAL:
401 features.require("natbib");
403 case biblio::ENGINE_JURABIB:
404 features.require("jurabib");