3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming, a.leeming@ic.ac.uk
7 * \author Herbert Voss, voss@lyx.org 2002-03-17
13 #pragma implementation
16 #include "insetcite.h"
18 #include "BufferView.h"
19 #include "LaTeXFeatures.h"
24 #include "frontends/controllers/biblio.h"
25 #include "frontends/Dialogs.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
29 #include "support/path.h"
30 #include "support/os.h"
31 #include "support/lstrings.h"
32 #include "support/LAssert.h"
42 // An optimisation. We assume that until the first InsetCitation::edit is
43 // called, we're loding the buffer and that, therefore, we don't need to
44 // reload the bibkey list
45 std::map<Buffer const *, bool> loading_buffer;
47 string const getNatbibLabel(Buffer const * buffer,
48 string const & citeType, string const & keyList,
49 string const & before, string const & after,
52 // Only reload the bibkeys if we have to...
53 map<Buffer const *, bool>::iterator lit = loading_buffer.find(buffer);
54 if (lit != loading_buffer.end())
55 loading_buffer[buffer] = true;
57 typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
58 static CachedMap cached_keys;
60 CachedMap::iterator kit = cached_keys.find(buffer);
62 if (!loading_buffer[buffer] || kit == cached_keys.end()) {
64 typedef vector<std::pair<string, string> > InfoType;
65 InfoType bibkeys = buffer->getBibkeyList();
67 InfoType::const_iterator bit = bibkeys.begin();
68 InfoType::const_iterator bend = bibkeys.end();
70 biblio::InfoMap infomap;
71 for (; bit != bend; ++bit) {
72 infomap[bit->first] = bit->second;
77 cached_keys[buffer] = infomap;
80 biblio::InfoMap infomap = cached_keys[buffer];
82 // the natbib citation-styles
83 // CITET: author (year)
84 // CITEP: (author,year)
85 // CITEALT: author year
86 // CITEALP: author, year
89 // CITEYEARPAR: (year)
91 // We don't currently use the full or forceUCase fields.
92 // bool const forceUCase = citeType[0] == 'C';
93 bool const full = citeType[citeType.size()-1] == '*';
95 string const cite_type = full ?
96 lowercase(citeType.substr(0,citeType.size()-1)) :
100 if (!before.empty()) {
101 // In CITET and CITEALT mode, the "before" string is
102 // attached to the label associated with each and every key.
103 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
104 // to the front of the whole only.
105 // In other modes, it is not used at all.
106 if (cite_type == "citet" ||
107 cite_type == "citealt" ||
108 cite_type == "citep" ||
109 cite_type == "citealp" ||
110 cite_type == "citeyearpar")
111 before_str = before + ' ';
115 if (!after.empty()) {
116 // The "after" key is appended only to the end of the whole.
117 after_str = ", " + after;
120 // One day, these might be tunable (as they are in BibTeX).
121 char const op = '('; // opening parenthesis.
122 char const cp = ')'; // closing parenthesis.
123 char const sep = ';'; // puctuation mark separating citation entries.
125 string const op_str(string(1, ' ') + string(1, op));
126 string const cp_str(string(1, cp) + string(1, ' '));
127 string const sep_str(string(1, sep) + string(1, ' '));
130 vector<string> keys = getVectorFromString(keyList);
131 vector<string>::const_iterator it = keys.begin();
132 vector<string>::const_iterator end = keys.end();
133 for (; it != end; ++it) {
134 // get the bibdata corresponding to the key
135 string const author(biblio::getAbbreviatedAuthor(infomap, *it));
136 string const year(biblio::getYear(infomap, *it));
138 // Something isn't right. Fail safely.
139 if (author.empty() || year.empty())
142 // (authors1 (<before> year); ... ;
143 // authors_last (<before> year, <after>)
144 if (cite_type == "citet") {
145 string const tmp = numerical ? '#' + *it : year;
146 label += author + op_str + before_str + tmp +
149 // author, year; author, year; ...
150 } else if (cite_type == "citep" ||
151 cite_type == "citealp") {
153 label += *it + sep_str;
155 label += author + ", " + year + sep_str;
158 // (authors1 <before> year;
159 // authors_last <before> year, <after>)
160 } else if (cite_type == "citealt") {
161 string const tmp = numerical ? '#' + *it : year;
162 label += author + ' ' + before_str + tmp + sep_str;
164 // author; author; ...
165 } else if (cite_type == "citeauthor") {
166 label += author + sep_str;
169 } else if (cite_type == "citeyear" ||
170 cite_type == "citeyearpar") {
171 label += year + sep_str;
174 label = strip(strip(label), sep);
176 if (!after_str.empty()) {
177 if (cite_type == "citet") {
178 // insert "after" before last ')'
179 label.insert(label.size()-1, after_str);
181 bool const add = !(numerical &&
182 (cite_type == "citeauthor" ||
183 cite_type == "citeyear"));
189 if (!before_str.empty() && (cite_type == "citep" ||
190 cite_type == "citealp" ||
191 cite_type == "citeyearpar")) {
192 label = before_str + label;
195 if (cite_type == "citep" || cite_type == "citeyearpar")
196 label = string(1, op) + label + string(1, cp);
202 string const getBasicLabel(string const & keyList, string const & after)
204 string keys(keyList);
207 if (contains(keys, ",")) {
208 // Final comma allows while loop to cover all keys
209 keys = frontStrip(split(keys, label, ',')) + ",";
210 while (contains(keys, ",")) {
212 keys = frontStrip(split(keys, key, ','));
219 label += ", " + after;
221 return "[" + label + "]";
227 InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
232 string const InsetCitation::generateLabel(Buffer const * buffer) const
234 string const before = string();
235 string const after = getOptions();
238 if (buffer->params.use_natbib) {
239 string cmd = getCmdName();
241 // We may be "upgrading" from an older LyX version.
242 // If, however, we use "cite" because the necessary
243 // author/year info is not present in the biblio
244 // database, then getNatbibLabel will exit gracefully
245 // and we'll call getBasicLabel.
246 if (buffer->params.use_numerical_citations)
251 label = getNatbibLabel(buffer, cmd, getContents(),
253 buffer->params.use_numerical_citations);
256 // Fallback to fail-safe
258 label = getBasicLabel(getContents(), after);
265 InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const * buffer) const
267 Cache::Style style = Cache::BASIC;
269 if (buffer->params.use_natbib) {
270 if (buffer->params.use_numerical_citations) {
271 style = Cache::NATBIB_NUM;
273 style = Cache::NATBIB_AY;
281 string const InsetCitation::getScreenLabel(Buffer const * buffer) const
283 Cache::Style const style = getStyle(buffer);
284 if (cache.params == params() && cache.style == style)
285 return cache.screen_label;
287 // The label has changed, so we have to re-create it.
288 string const before = string();
289 string const after = getOptions();
291 string const glabel = generateLabel(buffer);
293 unsigned int const maxLabelChars = 45;
295 string label = glabel;
296 if (label.size() > maxLabelChars) {
297 label.erase(maxLabelChars-3);
302 cache.params = params();
303 cache.generated_label = glabel;
304 cache.screen_label = label;
310 void InsetCitation::edit(BufferView * bv, int, int, unsigned int)
312 // A call to edit() indicates that we're no longer loading the
313 // buffer but doing some real work.
314 // Doesn't matter if there is no bv->buffer() entry in the map.
315 loading_buffer[bv->buffer()] = false;
317 bv->owner()->getDialogs()->showCitation(this);
321 void InsetCitation::edit(BufferView * bv, bool)
327 int InsetCitation::ascii(Buffer const * buffer, ostream & os, int) const
331 if (cache.params == params() && cache.style == getStyle(buffer))
332 label = cache.generated_label;
334 label = generateLabel(buffer);
341 // Have to overwrite the default InsetCommand method in order to check that
342 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
343 // citations and then changes his mind, turning natbib support off. The output
344 // should revert to \cite[]{}
345 int InsetCitation::latex(Buffer const * buffer, ostream & os,
346 bool /*fragile*/, bool/*fs*/) const
349 if (buffer->params.use_natbib)
354 string const before = string();
355 string const after = getOptions();
356 if (!before.empty() && buffer->params.use_natbib)
357 os << "[" << before << "][" << after << "]";
358 else if (!after.empty())
359 os << "[" << after << "]";
361 string::const_iterator it = getContents().begin();
362 string::const_iterator end = getContents().end();
363 // Paranoia check: make sure that there is no whitespace in here
365 for (; it != end; ++it) {
366 if (*it != ' ') content += *it;
369 os << "{" << content << "}";
375 void InsetCitation::validate(LaTeXFeatures & features) const
377 if (features.bufferParams().use_natbib)
378 features.require("natbib");