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 "funcrequest.h"
20 #include "LaTeXFeatures.h"
22 #include "frontends/controllers/biblio.h"
24 #include "support/lstrings.h"
26 using lyx::support::ascii_lowercase;
27 using lyx::support::contains;
28 using lyx::support::getVectorFromString;
29 using lyx::support::ltrim;
30 using lyx::support::rtrim;
31 using lyx::support::split;
41 string const getNatbibLabel(Buffer const & buffer,
42 string const & citeType, string const & keyList,
43 string const & before, string const & after,
46 // Only start the process off after the buffer is loaded from file.
47 if (!buffer.fully_loaded())
50 typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
51 static CachedMap cached_keys;
54 typedef vector<std::pair<string, string> > InfoType;
56 buffer.fillWithBibKeys(bibkeys);
58 InfoType::const_iterator bit = bibkeys.begin();
59 InfoType::const_iterator bend = bibkeys.end();
61 biblio::InfoMap infomap;
62 for (; bit != bend; ++bit) {
63 infomap[bit->first] = bit->second;
68 cached_keys[&buffer] = infomap;
70 // the natbib citation-styles
71 // CITET: author (year)
72 // CITEP: (author,year)
73 // CITEALT: author year
74 // CITEALP: author, year
77 // CITEYEARPAR: (year)
79 // We don't currently use the full or forceUCase fields.
80 // bool const forceUCase = citeType[0] == 'C';
81 bool const full = citeType[citeType.size() - 1] == '*';
83 string const cite_type = full ?
84 ascii_lowercase(citeType.substr(0, citeType.size() - 1)) :
85 ascii_lowercase(citeType);
88 if (!before.empty()) {
89 // In CITET and CITEALT mode, the "before" string is
90 // attached to the label associated with each and every key.
91 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
92 // to the front of the whole only.
93 // In other modes, it is not used at all.
94 if (cite_type == "citet" ||
95 cite_type == "citealt" ||
96 cite_type == "citep" ||
97 cite_type == "citealp" ||
98 cite_type == "citeyearpar")
99 before_str = before + ' ';
103 if (!after.empty()) {
104 // The "after" key is appended only to the end of the whole.
105 after_str = ", " + after;
108 // One day, these might be tunable (as they are in BibTeX).
109 char const op = '('; // opening parenthesis.
110 char const cp = ')'; // closing parenthesis.
111 // puctuation mark separating citation entries.
112 char const * const sep = ";";
114 string const op_str(' ' + string(1, op));
115 string const cp_str(string(1, cp) + ' ');
116 string const sep_str(string(sep) + ' ');
119 vector<string> keys = getVectorFromString(keyList);
120 vector<string>::const_iterator it = keys.begin();
121 vector<string>::const_iterator end = keys.end();
122 for (; it != end; ++it) {
123 // get the bibdata corresponding to the key
124 string const author(biblio::getAbbreviatedAuthor(infomap, *it));
125 string const year(biblio::getYear(infomap, *it));
127 // Something isn't right. Fail safely.
128 if (author.empty() || year.empty())
131 // (authors1 (<before> year); ... ;
132 // authors_last (<before> year, <after>)
133 if (cite_type == "citet") {
134 string const tmp = numerical ? '#' + *it : year;
135 label += author + op_str + before_str + tmp +
138 // author, year; author, year; ...
139 } else if (cite_type == "citep" ||
140 cite_type == "citealp") {
142 label += *it + sep_str;
144 label += author + ", " + year + sep_str;
147 // (authors1 <before> year;
148 // authors_last <before> year, <after>)
149 } else if (cite_type == "citealt") {
150 string const tmp = numerical ? '#' + *it : year;
151 label += author + ' ' + before_str + tmp + sep_str;
153 // author; author; ...
154 } else if (cite_type == "citeauthor") {
155 label += author + sep_str;
158 } else if (cite_type == "citeyear" ||
159 cite_type == "citeyearpar") {
160 label += year + sep_str;
163 label = rtrim(rtrim(label), sep);
165 if (!after_str.empty()) {
166 if (cite_type == "citet") {
167 // insert "after" before last ')'
168 label.insert(label.size() - 1, after_str);
170 bool const add = !(numerical &&
171 (cite_type == "citeauthor" ||
172 cite_type == "citeyear"));
178 if (!before_str.empty() && (cite_type == "citep" ||
179 cite_type == "citealp" ||
180 cite_type == "citeyearpar")) {
181 label = before_str + label;
184 if (cite_type == "citep" || cite_type == "citeyearpar")
185 label = string(1, op) + label + string(1, cp);
191 string const getBasicLabel(string const & keyList, string const & after)
193 string keys(keyList);
196 if (contains(keys, ",")) {
197 // Final comma allows while loop to cover all keys
198 keys = ltrim(split(keys, label, ',')) + ',';
199 while (contains(keys, ",")) {
201 keys = ltrim(split(keys, key, ','));
208 label += ", " + after;
210 return '[' + label + ']';
216 InsetCitation::InsetCitation(InsetCommandParams const & p)
221 // InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
222 // : InsetCommand(p, false)
226 InsetCitation::~InsetCitation()
228 InsetCommandMailer mailer("citation", *this);
233 string const InsetCitation::generateLabel(Buffer const & buffer) const
235 string const before = string();
236 string const after = getOptions();
239 if (buffer.params().use_natbib) {
240 string cmd = getCmdName();
242 // We may be "upgrading" from an older LyX version.
243 // If, however, we use "cite" because the necessary
244 // author/year info is not present in the biblio
245 // database, then getNatbibLabel will exit gracefully
246 // and we'll call getBasicLabel.
247 if (buffer.params().use_numerical_citations)
252 label = getNatbibLabel(buffer, cmd, getContents(),
254 buffer.params().use_numerical_citations);
257 // Fallback to fail-safe
259 label = getBasicLabel(getContents(), after);
266 InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const & buffer) const
268 Cache::Style style = Cache::BASIC;
270 if (buffer.params().use_natbib) {
271 if (buffer.params().use_numerical_citations) {
272 style = Cache::NATBIB_NUM;
274 style = Cache::NATBIB_AY;
282 string const InsetCitation::getScreenLabel(Buffer const & buffer) const
284 Cache::Style const style = getStyle(buffer);
285 if (cache.params == params() && cache.style == style)
286 return cache.screen_label;
288 // The label has changed, so we have to re-create it.
289 string const before = string();
290 string const after = getOptions();
292 string const glabel = generateLabel(buffer);
294 unsigned int const maxLabelChars = 45;
296 string label = glabel;
297 if (label.size() > maxLabelChars) {
298 label.erase(maxLabelChars-3);
303 cache.params = params();
304 cache.generated_label = glabel;
305 cache.screen_label = label;
312 InsetCitation::priv_dispatch(FuncRequest const & cmd,
313 idx_type & idx, pos_type & pos)
315 switch (cmd.action) {
316 case LFUN_INSET_EDIT:
317 InsetCommandMailer("citation", *this).showDialog(cmd.view());
321 return InsetCommand::priv_dispatch(cmd, idx, pos);
326 int InsetCitation::ascii(Buffer const & buffer, ostream & os, int) const
328 if (cache.params == params() && cache.style == getStyle(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 LatexRunParams const &) const
344 if (buffer.params().use_natbib)
349 #warning What is this code supposed to do? (Lgb)
350 // my guess is that this is just waiting for when we support before,
351 // so it's a oneliner. But this is very silly ! - jbl
354 // The current strange code
356 string const before = string();
357 string const after = getOptions();
358 if (!before.empty() && buffer.params().use_natbib)
359 os << '[' << before << "][" << after << ']';
360 else if (!after.empty())
361 os << '[' << after << ']';
363 // and the cleaned up equvalent, should it just be changed? (Lgb)
364 string const after = getOptions();
366 os << '[' << after << ']';
368 string::const_iterator it = getContents().begin();
369 string::const_iterator end = getContents().end();
370 // Paranoia check: make sure that there is no whitespace in here
373 for (; it != end; ++it) {
376 if (*it != ' ' || last != ',')
380 os << '{' << content << '}';
386 void InsetCitation::validate(LaTeXFeatures & features) const
388 if (features.bufferParams().use_natbib)
389 features.require("natbib");