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
15 #pragma implementation
18 #include "insetcite.h"
20 #include "BufferView.h"
21 #include "LaTeXFeatures.h"
22 #include "frontends/LyXView.h"
26 #include "frontends/controllers/biblio.h"
27 #include "frontends/Dialogs.h"
29 #include "support/filetools.h"
30 #include "support/lstrings.h"
31 #include "support/path.h"
32 #include "support/os.h"
33 #include "support/lstrings.h"
34 #include "support/LAssert.h"
44 // An optimisation. We assume that until the first InsetCitation::edit is
45 // called, we're loading the buffer and that, therefore, we don't need to
46 // reload the bibkey list
47 std::map<Buffer const *, bool> loading_buffer;
49 string const getNatbibLabel(Buffer const * buffer,
50 string const & citeType, string const & keyList,
51 string const & before, string const & after,
54 typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
55 static CachedMap cached_keys;
57 // Only load the bibkeys once if we're loading up the buffer,
58 // else load them afresh each time.
59 map<Buffer const *, bool>::iterator lit = loading_buffer.find(buffer);
60 if (lit == loading_buffer.end())
61 loading_buffer[buffer] = true;
63 bool loadkeys = !loading_buffer[buffer];
65 CachedMap::iterator kit = cached_keys.find(buffer);
66 loadkeys = kit == cached_keys.end();
71 typedef vector<std::pair<string, string> > InfoType;
72 InfoType bibkeys = buffer->getBibkeyList();
74 InfoType::const_iterator bit = bibkeys.begin();
75 InfoType::const_iterator bend = bibkeys.end();
77 biblio::InfoMap infomap;
78 for (; bit != bend; ++bit) {
79 infomap[bit->first] = bit->second;
84 cached_keys[buffer] = infomap;
87 biblio::InfoMap infomap = cached_keys[buffer];
89 // the natbib citation-styles
90 // CITET: author (year)
91 // CITEP: (author,year)
92 // CITEALT: author year
93 // CITEALP: author, year
96 // CITEYEARPAR: (year)
98 // We don't currently use the full or forceUCase fields.
99 // bool const forceUCase = citeType[0] == 'C';
100 bool const full = citeType[citeType.size() - 1] == '*';
102 string const cite_type = full ?
103 ascii_lowercase(citeType.substr(0, citeType.size() - 1)) :
104 ascii_lowercase(citeType);
107 if (!before.empty()) {
108 // In CITET and CITEALT mode, the "before" string is
109 // attached to the label associated with each and every key.
110 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
111 // to the front of the whole only.
112 // In other modes, it is not used at all.
113 if (cite_type == "citet" ||
114 cite_type == "citealt" ||
115 cite_type == "citep" ||
116 cite_type == "citealp" ||
117 cite_type == "citeyearpar")
118 before_str = before + ' ';
122 if (!after.empty()) {
123 // The "after" key is appended only to the end of the whole.
124 after_str = ", " + after;
127 // One day, these might be tunable (as they are in BibTeX).
128 char const op = '('; // opening parenthesis.
129 char const cp = ')'; // closing parenthesis.
130 // puctuation mark separating citation entries.
131 char const * const sep = ";";
133 string const op_str(' ' + string(1, op));
134 string const cp_str(string(1, cp) + ' ');
135 string const sep_str(string(sep) + ' ');
138 vector<string> keys = getVectorFromString(keyList);
139 vector<string>::const_iterator it = keys.begin();
140 vector<string>::const_iterator end = keys.end();
141 for (; it != end; ++it) {
142 // get the bibdata corresponding to the key
143 string const author(biblio::getAbbreviatedAuthor(infomap, *it));
144 string const year(biblio::getYear(infomap, *it));
146 // Something isn't right. Fail safely.
147 if (author.empty() || year.empty())
150 // (authors1 (<before> year); ... ;
151 // authors_last (<before> year, <after>)
152 if (cite_type == "citet") {
153 string const tmp = numerical ? '#' + *it : year;
154 label += author + op_str + before_str + tmp +
157 // author, year; author, year; ...
158 } else if (cite_type == "citep" ||
159 cite_type == "citealp") {
161 label += *it + sep_str;
163 label += author + ", " + year + sep_str;
166 // (authors1 <before> year;
167 // authors_last <before> year, <after>)
168 } else if (cite_type == "citealt") {
169 string const tmp = numerical ? '#' + *it : year;
170 label += author + ' ' + before_str + tmp + sep_str;
172 // author; author; ...
173 } else if (cite_type == "citeauthor") {
174 label += author + sep_str;
177 } else if (cite_type == "citeyear" ||
178 cite_type == "citeyearpar") {
179 label += year + sep_str;
182 label = rtrim(rtrim(label), sep);
184 if (!after_str.empty()) {
185 if (cite_type == "citet") {
186 // insert "after" before last ')'
187 label.insert(label.size() - 1, after_str);
189 bool const add = !(numerical &&
190 (cite_type == "citeauthor" ||
191 cite_type == "citeyear"));
197 if (!before_str.empty() && (cite_type == "citep" ||
198 cite_type == "citealp" ||
199 cite_type == "citeyearpar")) {
200 label = before_str + label;
203 if (cite_type == "citep" || cite_type == "citeyearpar")
204 label = string(1, op) + label + string(1, cp);
210 string const getBasicLabel(string const & keyList, string const & after)
212 string keys(keyList);
215 if (contains(keys, ",")) {
216 // Final comma allows while loop to cover all keys
217 keys = ltrim(split(keys, label, ',')) + ',';
218 while (contains(keys, ",")) {
220 keys = ltrim(split(keys, key, ','));
227 label += ", " + after;
229 return '[' + label + ']';
235 InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
240 string const InsetCitation::generateLabel(Buffer const * buffer) const
242 string const before = string();
243 string const after = getOptions();
246 if (buffer->params.use_natbib) {
247 string cmd = getCmdName();
249 // We may be "upgrading" from an older LyX version.
250 // If, however, we use "cite" because the necessary
251 // author/year info is not present in the biblio
252 // database, then getNatbibLabel will exit gracefully
253 // and we'll call getBasicLabel.
254 if (buffer->params.use_numerical_citations)
259 label = getNatbibLabel(buffer, cmd, getContents(),
261 buffer->params.use_numerical_citations);
264 // Fallback to fail-safe
266 label = getBasicLabel(getContents(), after);
273 InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const * buffer) const
275 Cache::Style style = Cache::BASIC;
277 if (buffer->params.use_natbib) {
278 if (buffer->params.use_numerical_citations) {
279 style = Cache::NATBIB_NUM;
281 style = Cache::NATBIB_AY;
289 string const InsetCitation::getScreenLabel(Buffer const * buffer) const
291 Cache::Style const style = getStyle(buffer);
292 if (cache.params == params() && cache.style == style)
293 return cache.screen_label;
295 // The label has changed, so we have to re-create it.
296 string const before = string();
297 string const after = getOptions();
299 string const glabel = generateLabel(buffer);
301 unsigned int const maxLabelChars = 45;
303 string label = glabel;
304 if (label.size() > maxLabelChars) {
305 label.erase(maxLabelChars-3);
310 cache.params = params();
311 cache.generated_label = glabel;
312 cache.screen_label = label;
318 void InsetCitation::setLoadingBuffer(Buffer const * buffer, bool state) const
320 // Doesn't matter if there is no bv->buffer() entry in the map.
321 loading_buffer[buffer] = state;
325 void InsetCitation::edit(BufferView * bv, int, int, mouse_button::state)
327 // A call to edit() indicates that we're no longer loading the
328 // buffer but doing some real work.
329 setLoadingBuffer(bv->buffer(), false);
331 bv->owner()->getDialogs().showCitation(this);
335 void InsetCitation::edit(BufferView * bv, bool)
337 edit(bv, 0, 0, mouse_button::none);
341 int InsetCitation::ascii(Buffer const * buffer, ostream & os, int) const
345 if (cache.params == params() && cache.style == getStyle(buffer))
346 label = cache.generated_label;
348 label = generateLabel(buffer);
355 // Have to overwrite the default InsetCommand method in order to check that
356 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
357 // citations and then changes his mind, turning natbib support off. The output
358 // should revert to \cite[]{}
359 int InsetCitation::latex(Buffer const * buffer, ostream & os,
360 bool /*fragile*/, bool/*fs*/) const
363 if (buffer->params.use_natbib)
368 #warning What is this code supposed to do? (Lgb)
369 // my guess is that this is just waiting for when we support before,
370 // so it's a oneliner. But this is very silly ! - jbl
373 // The current strange code
375 string const before = string();
376 string const after = getOptions();
377 if (!before.empty() && buffer->params.use_natbib)
378 os << '[' << before << "][" << after << ']';
379 else if (!after.empty())
380 os << '[' << after << ']';
382 // and the cleaned up equvalent, should it just be changed? (Lgb)
383 string const after = getOptions();
385 os << '[' << after << ']';
387 string::const_iterator it = getContents().begin();
388 string::const_iterator end = getContents().end();
389 // Paranoia check: make sure that there is no whitespace in here
392 for (; it != end; ++it) {
395 if (*it != ' ' || last != ',')
399 os << '{' << content << '}';
405 void InsetCitation::validate(LaTeXFeatures & features) const
407 if (features.bufferParams().use_natbib)
408 features.require("natbib");