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)
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 (citeType == "citet" ||
99 citeType == "citealt" ||
100 citeType == "citep" ||
101 citeType == "citealp" ||
102 citeType == "citeyearpar")
103 before_str = before + ' ';
107 if (!after.empty()) {
108 // The "after" key is appended only to the end of the whole.
109 after_str = ", " + after;
112 // One day, these might be tunable (as they are in BibTeX).
113 char const op = '('; // opening parenthesis.
114 char const cp = ')'; // closing parenthesis.
115 char const sep = ';'; // puctuation mark separating citation entries.
117 string const op_str(string(1, ' ') + string(1, op));
118 string const cp_str(string(1, cp) + string(1, ' '));
119 string const sep_str(string(1, sep) + string(1, ' '));
122 vector<string> keys = getVectorFromString(keyList);
123 vector<string>::const_iterator it = keys.begin();
124 vector<string>::const_iterator end = keys.end();
125 for (; it != end; ++it) {
126 // get the bibdata corresponding to the key
127 string const author(biblio::getAbbreviatedAuthor(infomap, *it));
128 string const year(biblio::getYear(infomap, *it));
130 // Something isn't right. Fail safely.
131 if (author.empty() || year.empty())
134 // (authors1 (<before> year); ... ;
135 // authors_last (<before> year, <after>)
136 if (citeType == "citet") {
137 string const tmp = numerical ? '#' + *it : year;
138 label += author + op_str + before_str + tmp +
141 // author, year; author, year; ...
142 } else if (citeType == "citep" ||
143 citeType == "citealp") {
145 label += *it + sep_str;
147 label += author + ", " + year + sep_str;
150 // (authors1 <before> year;
151 // authors_last <before> year, <after>)
152 } else if (citeType == "citealt") {
153 string const tmp = numerical ? '#' + *it : year;
154 label += author + ' ' + before_str + tmp + sep_str;
156 // author; author; ...
157 } else if (citeType == "citeauthor") {
158 label += author + sep_str;
161 } else if (citeType == "citeyear" ||
162 citeType == "citeyearpar") {
163 label += year + sep_str;
166 label = strip(strip(label), sep);
168 if (!after_str.empty()) {
169 if (citeType == "citet") {
170 // insert "after" before last ')'
171 label.insert(label.size()-1, after_str);
173 bool const add = !(numerical &&
174 (citeType == "citeauthor" ||
175 citeType == "citeyear"));
181 if (!before_str.empty() && (citeType == "citep" ||
182 citeType == "citealp" ||
183 citeType == "citeyearpar")) {
184 label = before_str + label;
187 if (citeType == "citep" || citeType == "citeyearpar")
188 label = string(1, op) + label + string(1, cp);
194 string const getBasicLabel(string const & keyList, string const & after)
196 string keys(keyList);
199 if (contains(keys, ",")) {
200 // Final comma allows while loop to cover all keys
201 keys = frontStrip(split(keys, label, ',')) + ",";
202 while (contains(keys, ",")) {
204 keys = frontStrip(split(keys, key, ','));
211 label += ", " + after;
213 return "[" + label + "]";
219 InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
224 string const InsetCitation::generateLabel(Buffer const * buffer) const
226 string const before = string();
227 string const after = getOptions();
230 if (buffer->params.use_natbib) {
231 string cmd = getCmdName();
233 // We may be "upgrading" from an older LyX version.
234 // If, however, we use "cite" because the necessary
235 // author/year info is not present in the biblio
236 // database, then getNatbibLabel will exit gracefully
237 // and we'll call getBasicLabel.
238 if (buffer->params.use_numerical_citations)
243 label = getNatbibLabel(buffer, cmd, getContents(),
245 buffer->params.use_numerical_citations);
248 // Fallback to fail-safe
250 label = getBasicLabel(getContents(), after);
257 InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const * buffer) const
259 Cache::Style style = Cache::BASIC;
261 if (buffer->params.use_natbib) {
262 if (buffer->params.use_numerical_citations) {
263 style = Cache::NATBIB_NUM;
265 style = Cache::NATBIB_AY;
273 string const InsetCitation::getScreenLabel(Buffer const * buffer) const
275 Cache::Style const style = getStyle(buffer);
276 if (cache.params == params() && cache.style == style)
277 return cache.screen_label;
279 // The label has changed, so we have to re-create it.
280 string const before = string();
281 string const after = getOptions();
283 string const glabel = generateLabel(buffer);
285 unsigned int const maxLabelChars = 45;
287 string label = glabel;
288 if (label.size() > maxLabelChars) {
289 label.erase(maxLabelChars-3);
294 cache.params = params();
295 cache.generated_label = glabel;
296 cache.screen_label = label;
302 void InsetCitation::edit(BufferView * bv, int, int, unsigned int)
304 // A call to edit() indicates that we're no longer loading the
305 // buffer but doing some real work.
306 // Doesn't matter if there is no bv->buffer() entry in the map.
307 loading_buffer[bv->buffer()] = false;
309 bv->owner()->getDialogs()->showCitation(this);
313 void InsetCitation::edit(BufferView * bv, bool)
319 int InsetCitation::ascii(Buffer const * buffer, ostream & os, int) const
323 if (cache.params == params() && cache.style == getStyle(buffer))
324 label = cache.generated_label;
326 label = generateLabel(buffer);
333 // Have to overwrite the default InsetCommand method in order to check that
334 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
335 // citations and then changes his mind, turning natbib support off. The output
336 // should revert to \cite[]{}
337 int InsetCitation::latex(Buffer const * buffer, ostream & os,
338 bool /*fragile*/, bool/*fs*/) const
341 if (buffer->params.use_natbib)
346 string const before = string();
347 string const after = getOptions();
348 if (!before.empty() && buffer->params.use_natbib)
349 os << "[" << before << "][" << after << "]";
350 else if (!after.empty())
351 os << "[" << after << "]";
353 string::const_iterator it = getContents().begin();
354 string::const_iterator end = getContents().end();
355 // Paranoia check: make sure that there is no whitespace in here
357 for (; it != end; ++it) {
358 if (*it != ' ') content += *it;
361 os << "{" << content << "}";
367 void InsetCitation::validate(LaTeXFeatures & features) const
369 if (features.bufferParams().use_natbib)
370 features.require("natbib");