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;
42 string const getNatbibLabel(Buffer const & buffer,
43 string const & citeType, string const & keyList,
44 string const & before, string const & after,
47 // Only start the process off after the buffer is loaded from file.
48 if (!buffer.fully_loaded())
51 typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
52 static CachedMap cached_keys;
55 typedef vector<std::pair<string, string> > InfoType;
57 buffer.fillWithBibKeys(bibkeys);
59 InfoType::const_iterator bit = bibkeys.begin();
60 InfoType::const_iterator bend = bibkeys.end();
62 biblio::InfoMap infomap;
63 for (; bit != bend; ++bit) {
64 infomap[bit->first] = bit->second;
69 cached_keys[&buffer] = infomap;
71 // the natbib citation-styles
72 // CITET: author (year)
73 // CITEP: (author,year)
74 // CITEALT: author year
75 // CITEALP: author, year
78 // CITEYEARPAR: (year)
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 + ' ';
104 if (!after.empty()) {
105 // The "after" key is appended only to the end of the whole.
106 after_str = ", " + after;
109 // One day, these might be tunable (as they are in BibTeX).
110 char const op = '('; // opening parenthesis.
111 char const cp = ')'; // closing parenthesis.
112 // puctuation mark separating citation entries.
113 char const * const sep = ";";
115 string const op_str(' ' + string(1, op));
116 string const cp_str(string(1, cp) + ' ');
117 string const sep_str(string(sep) + ' ');
120 vector<string> keys = getVectorFromString(keyList);
121 vector<string>::const_iterator it = keys.begin();
122 vector<string>::const_iterator end = keys.end();
123 for (; it != end; ++it) {
124 // get the bibdata corresponding to the key
125 string const author(biblio::getAbbreviatedAuthor(infomap, *it));
126 string const year(biblio::getYear(infomap, *it));
128 // Something isn't right. Fail safely.
129 if (author.empty() || year.empty())
132 // (authors1 (<before> year); ... ;
133 // authors_last (<before> year, <after>)
134 if (cite_type == "citet") {
135 string const tmp = numerical ? '#' + *it : year;
136 label += author + op_str + before_str + tmp +
139 // author, year; author, year; ...
140 } else if (cite_type == "citep" ||
141 cite_type == "citealp") {
143 label += *it + sep_str;
145 label += author + ", " + year + sep_str;
148 // (authors1 <before> year;
149 // authors_last <before> year, <after>)
150 } else if (cite_type == "citealt") {
151 string const tmp = numerical ? '#' + *it : year;
152 label += author + ' ' + before_str + tmp + sep_str;
154 // author; author; ...
155 } else if (cite_type == "citeauthor") {
156 label += author + sep_str;
159 } else if (cite_type == "citeyear" ||
160 cite_type == "citeyearpar") {
161 label += year + sep_str;
164 label = rtrim(rtrim(label), sep);
166 if (!after_str.empty()) {
167 if (cite_type == "citet") {
168 // insert "after" before last ')'
169 label.insert(label.size() - 1, after_str);
171 bool const add = !(numerical &&
172 (cite_type == "citeauthor" ||
173 cite_type == "citeyear"));
179 if (!before_str.empty() && (cite_type == "citep" ||
180 cite_type == "citealp" ||
181 cite_type == "citeyearpar")) {
182 label = before_str + label;
185 if (cite_type == "citep" || cite_type == "citeyearpar")
186 label = string(1, op) + label + string(1, cp);
192 string const getBasicLabel(string const & keyList, string const & after)
194 string keys(keyList);
197 if (contains(keys, ",")) {
198 // Final comma allows while loop to cover all keys
199 keys = ltrim(split(keys, label, ',')) + ',';
200 while (contains(keys, ",")) {
202 keys = ltrim(split(keys, key, ','));
209 label += ", " + after;
211 return '[' + label + ']';
217 InsetCitation::InsetCitation(InsetCommandParams const & p)
222 // InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
223 // : InsetCommand(p, false)
227 InsetCitation::~InsetCitation()
229 InsetCommandMailer mailer("citation", *this);
234 string const InsetCitation::generateLabel(Buffer const & buffer) const
236 string const before = string();
237 string const after = getOptions();
240 if (buffer.params().use_natbib) {
241 string cmd = getCmdName();
243 // We may be "upgrading" from an older LyX version.
244 // If, however, we use "cite" because the necessary
245 // author/year info is not present in the biblio
246 // database, then getNatbibLabel will exit gracefully
247 // and we'll call getBasicLabel.
248 if (buffer.params().use_numerical_citations)
253 label = getNatbibLabel(buffer, cmd, getContents(),
255 buffer.params().use_numerical_citations);
258 // Fallback to fail-safe
260 label = getBasicLabel(getContents(), after);
267 InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const & buffer) const
269 Cache::Style style = Cache::BASIC;
271 if (buffer.params().use_natbib) {
272 if (buffer.params().use_numerical_citations) {
273 style = Cache::NATBIB_NUM;
275 style = Cache::NATBIB_AY;
283 string const InsetCitation::getScreenLabel(Buffer const & buffer) const
285 Cache::Style const style = getStyle(buffer);
286 if (cache.params == params() && cache.style == style)
287 return cache.screen_label;
289 // The label has changed, so we have to re-create it.
290 string const before = string();
291 string const after = getOptions();
293 string const glabel = generateLabel(buffer);
295 unsigned int const maxLabelChars = 45;
297 string label = glabel;
298 if (label.size() > maxLabelChars) {
299 label.erase(maxLabelChars-3);
304 cache.params = params();
305 cache.generated_label = glabel;
306 cache.screen_label = label;
313 InsetCitation::priv_dispatch(FuncRequest const & cmd,
314 idx_type & idx, pos_type & pos)
316 switch (cmd.action) {
317 case LFUN_INSET_EDIT:
318 InsetCommandMailer("citation", *this).showDialog(cmd.view());
319 return DispatchResult(DISPATCHED);
322 return InsetCommand::priv_dispatch(cmd, idx, pos);
327 int InsetCitation::ascii(Buffer const & buffer, ostream & os, int) const
329 if (cache.params == params() && cache.style == getStyle(buffer))
330 os << cache.generated_label;
332 os << generateLabel(buffer);
337 // Have to overwrite the default InsetCommand method in order to check that
338 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
339 // citations and then changes his mind, turning natbib support off. The output
340 // should revert to \cite[]{}
341 int InsetCitation::latex(Buffer const & buffer, ostream & os,
342 LatexRunParams const &) const
345 if (buffer.params().use_natbib)
350 #warning What is this code supposed to do? (Lgb)
351 // my guess is that this is just waiting for when we support before,
352 // so it's a oneliner. But this is very silly ! - jbl
355 // The current strange code
357 string const before = string();
358 string const after = getOptions();
359 if (!before.empty() && buffer.params().use_natbib)
360 os << '[' << before << "][" << after << ']';
361 else if (!after.empty())
362 os << '[' << after << ']';
364 // and the cleaned up equvalent, should it just be changed? (Lgb)
365 string const after = getOptions();
367 os << '[' << after << ']';
369 string::const_iterator it = getContents().begin();
370 string::const_iterator end = getContents().end();
371 // Paranoia check: make sure that there is no whitespace in here
374 for (; it != end; ++it) {
377 if (*it != ' ' || last != ',')
381 os << '{' << content << '}';
387 void InsetCitation::validate(LaTeXFeatures & features) const
389 if (features.bufferParams().use_natbib)
390 features.require("natbib");