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 "frontends/controllers/biblio.h"
25 #include "support/lstrings.h"
27 using lyx::support::ascii_lowercase;
28 using lyx::support::contains;
29 using lyx::support::getVectorFromString;
30 using lyx::support::ltrim;
31 using lyx::support::rtrim;
32 using lyx::support::split;
39 namespace biblio = lyx::biblio;
44 string const getNatbibLabel(Buffer const & buffer,
45 string const & citeType, string const & keyList,
46 string const & before, string const & after,
47 biblio::CiteEngine engine)
49 // Only start the process off after the buffer is loaded from file.
50 if (!buffer.fully_loaded())
53 typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
54 static CachedMap cached_keys;
57 typedef vector<std::pair<string, string> > InfoType;
59 buffer.fillWithBibKeys(bibkeys);
61 InfoType::const_iterator bit = bibkeys.begin();
62 InfoType::const_iterator bend = bibkeys.end();
64 biblio::InfoMap infomap;
65 for (; bit != bend; ++bit) {
66 infomap[bit->first] = bit->second;
71 cached_keys[&buffer] = infomap;
73 // the natbib citation-styles
74 // CITET: author (year)
75 // CITEP: (author,year)
76 // CITEALT: author year
77 // CITEALP: author, year
80 // CITEYEARPAR: (year)
81 // jurabib supports these plus
82 // CITE: author/<before field>
84 // We don't currently use the full or forceUCase fields.
85 string cite_type = biblio::asValidLatexCommand(citeType, engine);
86 if (cite_type[0] == 'C')
87 cite_type = string(1, 'c') + cite_type.substr(1);
88 if (cite_type[cite_type.size() - 1] == '*')
89 cite_type = cite_type.substr(0, cite_type.size() - 1);
92 if (!before.empty()) {
93 // In CITET and CITEALT mode, the "before" string is
94 // attached to the label associated with each and every key.
95 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
96 // to the front of the whole only.
97 // In other modes, it is not used at all.
98 if (cite_type == "citet" ||
99 cite_type == "citealt" ||
100 cite_type == "citep" ||
101 cite_type == "citealp" ||
102 cite_type == "citeyearpar")
103 before_str = before + ' ';
104 // In CITE (jurabib), the "before" string is used to attach
105 // the annotator (of legal texts) to the author(s) of the
107 else if (cite_type == "cite")
108 before_str = '/' + before;
112 if (!after.empty()) {
113 // The "after" key is appended only to the end of the whole.
114 after_str = ", " + after;
117 // One day, these might be tunable (as they are in BibTeX).
118 char const op = '('; // opening parenthesis.
119 char const cp = ')'; // closing parenthesis.
120 // puctuation mark separating citation entries.
121 char const * const sep = ";";
123 string const op_str(' ' + string(1, op));
124 string const cp_str(string(1, cp) + ' ');
125 string const sep_str(string(sep) + ' ');
128 vector<string> keys = getVectorFromString(keyList);
129 vector<string>::const_iterator it = keys.begin();
130 vector<string>::const_iterator end = keys.end();
131 for (; it != end; ++it) {
132 // get the bibdata corresponding to the key
133 string const author(biblio::getAbbreviatedAuthor(infomap, *it));
134 string const year(biblio::getYear(infomap, *it));
136 // Something isn't right. Fail safely.
137 if (author.empty() || year.empty())
140 // authors1/<before>; ... ;
141 // authors_last, <after>
142 if (cite_type == "cite" && engine == biblio::ENGINE_JURABIB) {
143 if (it == keys.begin())
144 label += author + before_str + sep_str;
146 label += author + sep_str;
148 // (authors1 (<before> year); ... ;
149 // authors_last (<before> year, <after>)
150 } else if (cite_type == "citet") {
152 case biblio::ENGINE_NATBIB_AUTHORYEAR:
153 label += author + op_str + before_str +
156 case biblio::ENGINE_NATBIB_NUMERICAL:
157 label += author + op_str + before_str +
158 '#' + *it + cp + sep_str;
160 case biblio::ENGINE_JURABIB:
161 label += before_str + author + op_str +
164 case biblio::ENGINE_BASIC:
168 // author, year; author, year; ...
169 } else if (cite_type == "citep" ||
170 cite_type == "citealp") {
171 if (engine == biblio::ENGINE_NATBIB_NUMERICAL) {
172 label += *it + sep_str;
174 label += author + ", " + year + sep_str;
177 // (authors1 <before> year;
178 // authors_last <before> year, <after>)
179 } else if (cite_type == "citealt") {
181 case biblio::ENGINE_NATBIB_AUTHORYEAR:
182 label += author + ' ' + before_str +
185 case biblio::ENGINE_NATBIB_NUMERICAL:
186 label += author + ' ' + before_str +
189 case biblio::ENGINE_JURABIB:
190 label += before_str + author + ' ' +
193 case biblio::ENGINE_BASIC:
197 // author; author; ...
198 } else if (cite_type == "citeauthor") {
199 label += author + sep_str;
202 } else if (cite_type == "citeyear" ||
203 cite_type == "citeyearpar") {
204 label += year + sep_str;
207 label = rtrim(rtrim(label), sep);
209 if (!after_str.empty()) {
210 if (cite_type == "citet") {
211 // insert "after" before last ')'
212 label.insert(label.size() - 1, after_str);
215 !(engine == biblio::ENGINE_NATBIB_NUMERICAL &&
216 (cite_type == "citeauthor" ||
217 cite_type == "citeyear"));
223 if (!before_str.empty() && (cite_type == "citep" ||
224 cite_type == "citealp" ||
225 cite_type == "citeyearpar")) {
226 label = before_str + label;
229 if (cite_type == "citep" || cite_type == "citeyearpar")
230 label = string(1, op) + label + string(1, cp);
236 string const getBasicLabel(string const & keyList, string const & after)
238 string keys(keyList);
241 if (contains(keys, ',')) {
242 // Final comma allows while loop to cover all keys
243 keys = ltrim(split(keys, label, ',')) + ',';
244 while (contains(keys, ',')) {
246 keys = ltrim(split(keys, key, ','));
253 label += ", " + after;
255 return '[' + label + ']';
261 InsetCitation::InsetCitation(InsetCommandParams const & p)
262 : InsetCommand(p, "citation")
266 string const InsetCitation::generateLabel(Buffer const & buffer) const
268 string const before = getSecOptions();
269 string const after = getOptions();
272 biblio::CiteEngine const engine = buffer.params().cite_engine;
273 if (engine != biblio::ENGINE_BASIC) {
274 label = getNatbibLabel(buffer, getCmdName(), getContents(),
275 before, after, engine);
278 // Fallback to fail-safe
280 label = getBasicLabel(getContents(), after);
287 string const InsetCitation::getScreenLabel(Buffer const & buffer) const
289 biblio::CiteEngine const engine = biblio::getEngine(buffer);
290 if (cache.params == params() && cache.engine == engine)
291 return cache.screen_label;
293 // The label has changed, so we have to re-create it.
294 string const before = getSecOptions();
295 string const after = getOptions();
297 string const glabel = generateLabel(buffer);
299 unsigned int const maxLabelChars = 45;
301 string label = glabel;
302 if (label.size() > maxLabelChars) {
303 label.erase(maxLabelChars-3);
307 cache.engine = engine;
308 cache.params = params();
309 cache.generated_label = glabel;
310 cache.screen_label = label;
316 int InsetCitation::plaintext(Buffer const & buffer, ostream & os, OutputParams const &) const
318 if (cache.params == params() &&
319 cache.engine == biblio::getEngine(buffer))
320 os << cache.generated_label;
322 os << generateLabel(buffer);
329 string const cleanupWhitespace(string const & citelist)
331 string::const_iterator it = citelist.begin();
332 string::const_iterator end = citelist.end();
333 // Paranoia check: make sure that there is no whitespace in here
334 // -- at least not behind commas or at the beginning
337 for (; it != end; ++it) {
340 if (*it != ' ' || last != ',')
346 // end anon namyspace
349 int InsetCitation::docbook(Buffer const &, ostream & os, OutputParams const &) const
351 os << "<citation>" << cleanupWhitespace(getContents()) << "</citation>";
356 int InsetCitation::textString(Buffer const & buf, ostream & os,
357 OutputParams const & op) const
359 return plaintext(buf, os, op);
363 // Have to overwrite the default InsetCommand method in order to check that
364 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
365 // citations and then changes his mind, turning natbib support off. The output
366 // should revert to \cite[]{}
367 int InsetCitation::latex(Buffer const & buffer, ostream & os,
368 OutputParams const &) const
370 biblio::CiteEngine const cite_engine = buffer.params().cite_engine;
371 string const cite_str =
372 biblio::asValidLatexCommand(getCmdName(), cite_engine);
374 os << "\\" << cite_str;
376 string const before = getSecOptions();
377 string const after = getOptions();
378 if (!before.empty() && cite_engine != biblio::ENGINE_BASIC)
379 os << '[' << before << "][" << after << ']';
380 else if (!after.empty())
381 os << '[' << after << ']';
383 os << '{' << cleanupWhitespace(getContents()) << '}';
389 void InsetCitation::validate(LaTeXFeatures & features) const
391 switch (features.bufferParams().cite_engine) {
392 case biblio::ENGINE_BASIC:
394 case biblio::ENGINE_NATBIB_AUTHORYEAR:
395 case biblio::ENGINE_NATBIB_NUMERICAL:
396 features.require("natbib");
398 case biblio::ENGINE_JURABIB:
399 features.require("jurabib");