]> git.lyx.org Git - lyx.git/blob - src/insets/insetcite.C
Rename LatexRunParams::fragile as moving_arg.
[lyx.git] / src / insets / insetcite.C
1 /**
2  * \file insetcite.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Herbert Voss
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #include "insetcite.h"
15 #include "funcrequest.h"
16 #include "buffer.h"
17 #include "BufferView.h"
18 #include "LaTeXFeatures.h"
19
20 #include "frontends/controllers/biblio.h"
21
22 #include "support/lstrings.h"
23
24 #include <map>
25
26 using std::ostream;
27 using std::vector;
28 using std::map;
29
30 namespace {
31
32 // An optimisation. We assume that until the first InsetCitation::edit is
33 // called, we're loading the buffer and that, therefore, we don't need to
34 // reload the bibkey list
35 std::map<Buffer const *, bool> loading_buffer;
36
37 string const getNatbibLabel(Buffer const * buffer,
38                             string const & citeType, string const & keyList,
39                             string const & before, string const & after,
40                             bool numerical)
41 {
42         typedef std::map<Buffer const *, biblio::InfoMap> CachedMap;
43         static CachedMap cached_keys;
44
45         // Only load the bibkeys once if we're loading up the buffer,
46         // else load them afresh each time.
47         map<Buffer const *, bool>::iterator lit = loading_buffer.find(buffer);
48         if (lit == loading_buffer.end())
49                 loading_buffer[buffer] = true;
50
51         bool loadkeys = !loading_buffer[buffer];
52         if (!loadkeys) {
53                 CachedMap::iterator kit = cached_keys.find(buffer);
54                 loadkeys = kit == cached_keys.end();
55         }
56
57         if (loadkeys) {
58                 // build the keylist
59                 typedef vector<std::pair<string, string> > InfoType;
60                 InfoType bibkeys;
61                 buffer->fillWithBibKeys(bibkeys);
62
63                 InfoType::const_iterator bit  = bibkeys.begin();
64                 InfoType::const_iterator bend = bibkeys.end();
65
66                 biblio::InfoMap infomap;
67                 for (; bit != bend; ++bit) {
68                         infomap[bit->first] = bit->second;
69                 }
70                 if (infomap.empty())
71                         return string();
72
73                 cached_keys[buffer] = infomap;
74         }
75
76         biblio::InfoMap infomap = cached_keys[buffer];
77
78         // the natbib citation-styles
79         // CITET:       author (year)
80         // CITEP:       (author,year)
81         // CITEALT:     author year
82         // CITEALP:     author, year
83         // CITEAUTHOR:  author
84         // CITEYEAR:    year
85         // CITEYEARPAR: (year)
86
87         // We don't currently use the full or forceUCase fields.
88         // bool const forceUCase = citeType[0] == 'C';
89         bool const full = citeType[citeType.size() - 1] == '*';
90
91         string const cite_type = full ?
92                 ascii_lowercase(citeType.substr(0, citeType.size() - 1)) :
93                 ascii_lowercase(citeType);
94
95         string before_str;
96         if (!before.empty()) {
97                 // In CITET and CITEALT mode, the "before" string is
98                 // attached to the label associated with each and every key.
99                 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
100                 // to the front of the whole only.
101                 // In other modes, it is not used at all.
102                 if (cite_type == "citet" ||
103                     cite_type == "citealt" ||
104                     cite_type == "citep" ||
105                     cite_type == "citealp" ||
106                     cite_type == "citeyearpar")
107                         before_str = before + ' ';
108         }
109
110         string after_str;
111         if (!after.empty()) {
112                 // The "after" key is appended only to the end of the whole.
113                 after_str = ", " + after;
114         }
115
116         // One day, these might be tunable (as they are in BibTeX).
117         char const op  = '('; // opening parenthesis.
118         char const cp  = ')'; // closing parenthesis.
119         // puctuation mark separating citation entries.
120         char const * const sep = ";";
121
122         string const op_str(' ' + string(1, op));
123         string const cp_str(string(1, cp) + ' ');
124         string const sep_str(string(sep) + ' ');
125
126         string label;
127         vector<string> keys = getVectorFromString(keyList);
128         vector<string>::const_iterator it  = keys.begin();
129         vector<string>::const_iterator end = keys.end();
130         for (; it != end; ++it) {
131                 // get the bibdata corresponding to the key
132                 string const author(biblio::getAbbreviatedAuthor(infomap, *it));
133                 string const year(biblio::getYear(infomap, *it));
134
135                 // Something isn't right. Fail safely.
136                 if (author.empty() || year.empty())
137                         return string();
138
139                 // (authors1 (<before> year);  ... ;
140                 //  authors_last (<before> year, <after>)
141                 if (cite_type == "citet") {
142                         string const tmp = numerical ? '#' + *it : year;
143                         label += author + op_str + before_str + tmp +
144                                 cp + sep_str;
145
146                 // author, year; author, year; ...
147                 } else if (cite_type == "citep" ||
148                            cite_type == "citealp") {
149                         if (numerical) {
150                                 label += *it + sep_str;
151                         } else {
152                                 label += author + ", " + year + sep_str;
153                         }
154
155                 // (authors1 <before> year;
156                 //  authors_last <before> year, <after>)
157                 } else if (cite_type == "citealt") {
158                         string const tmp = numerical ? '#' + *it : year;
159                         label += author + ' ' + before_str + tmp + sep_str;
160
161                 // author; author; ...
162                 } else if (cite_type == "citeauthor") {
163                         label += author + sep_str;
164
165                 // year; year; ...
166                 } else if (cite_type == "citeyear" ||
167                            cite_type == "citeyearpar") {
168                         label += year + sep_str;
169                 }
170         }
171         label = rtrim(rtrim(label), sep);
172
173         if (!after_str.empty()) {
174                 if (cite_type == "citet") {
175                         // insert "after" before last ')'
176                         label.insert(label.size() - 1, after_str);
177                 } else {
178                         bool const add = !(numerical &&
179                                            (cite_type == "citeauthor" ||
180                                             cite_type == "citeyear"));
181                         if (add)
182                                 label += after_str;
183                 }
184         }
185
186         if (!before_str.empty() && (cite_type == "citep" ||
187                                     cite_type == "citealp" ||
188                                     cite_type == "citeyearpar")) {
189                 label = before_str + label;
190         }
191
192         if (cite_type == "citep" || cite_type == "citeyearpar")
193                 label = string(1, op) + label + string(1, cp);
194
195         return label;
196 }
197
198
199 string const getBasicLabel(string const & keyList, string const & after)
200 {
201         string keys(keyList);
202         string label;
203
204         if (contains(keys, ",")) {
205                 // Final comma allows while loop to cover all keys
206                 keys = ltrim(split(keys, label, ',')) + ',';
207                 while (contains(keys, ",")) {
208                         string key;
209                         keys = ltrim(split(keys, key, ','));
210                         label += ", " + key;
211                 }
212         } else
213                 label = keys;
214
215         if (!after.empty())
216                 label += ", " + after;
217
218         return '[' + label + ']';
219 }
220
221 } // anon namespace
222
223
224 InsetCitation::InsetCitation(InsetCommandParams const & p, bool)
225         : InsetCommand(p)
226 {}
227
228
229 InsetCitation::~InsetCitation()
230 {
231         InsetCommandMailer mailer("citation", *this);
232         mailer.hideDialog();
233 }
234
235
236 string const InsetCitation::generateLabel(Buffer const * buffer) const
237 {
238         string const before = string();
239         string const after  = getOptions();
240
241         string label;
242         if (buffer->params.use_natbib) {
243                 string cmd = getCmdName();
244                 if (cmd == "cite") {
245                         // We may be "upgrading" from an older LyX version.
246                         // If, however, we use "cite" because the necessary
247                         // author/year info is not present in the biblio
248                         // database, then getNatbibLabel will exit gracefully
249                         // and we'll call getBasicLabel.
250                         if (buffer->params.use_numerical_citations)
251                                 cmd = "citep";
252                         else
253                                 cmd = "citet";
254                 }
255                 label = getNatbibLabel(buffer, cmd, getContents(),
256                                        before, after,
257                                        buffer->params.use_numerical_citations);
258         }
259
260         // Fallback to fail-safe
261         if (label.empty()) {
262                 label = getBasicLabel(getContents(), after);
263         }
264
265         return label;
266 }
267
268
269 InsetCitation::Cache::Style InsetCitation::getStyle(Buffer const * buffer) const
270 {
271         Cache::Style style = Cache::BASIC;
272
273         if (buffer->params.use_natbib) {
274                 if (buffer->params.use_numerical_citations) {
275                         style = Cache::NATBIB_NUM;
276                 } else {
277                         style = Cache::NATBIB_AY;
278                 }
279         }
280
281         return style;
282 }
283
284
285 string const InsetCitation::getScreenLabel(Buffer const * buffer) const
286 {
287         Cache::Style const style = getStyle(buffer);
288         if (cache.params == params() && cache.style == style)
289                 return cache.screen_label;
290
291         // The label has changed, so we have to re-create it.
292         string const before = string();
293         string const after  = getOptions();
294
295         string const glabel = generateLabel(buffer);
296
297         unsigned int const maxLabelChars = 45;
298
299         string label = glabel;
300         if (label.size() > maxLabelChars) {
301                 label.erase(maxLabelChars-3);
302                 label += "...";
303         }
304
305         cache.style  = style;
306         cache.params = params();
307         cache.generated_label = glabel;
308         cache.screen_label = label;
309
310         return label;
311 }
312
313
314 void InsetCitation::setLoadingBuffer(Buffer const * buffer, bool state) const
315 {
316         // Doesn't matter if there is no bv->buffer() entry in the map.
317         loading_buffer[buffer] = state;
318 }
319
320
321 dispatch_result InsetCitation::localDispatch(FuncRequest const & cmd)
322 {
323         switch (cmd.action) {
324                 case LFUN_INSET_EDIT:
325                         // A call to edit indicates that we're no longer loading the
326                         // buffer but doing some real work.
327                         setLoadingBuffer(cmd.view()->buffer(), false);
328                         InsetCommandMailer("citation", *this).showDialog(cmd.view());
329                         break;
330
331                 default:
332                         return UNDISPATCHED;
333         }
334         return DISPATCHED;
335 }
336
337
338 int InsetCitation::ascii(Buffer const * buffer, ostream & os, int) const
339 {
340         string label;
341
342         if (cache.params == params() && cache.style == getStyle(buffer))
343                 label = cache.generated_label;
344         else
345                 label = generateLabel(buffer);
346
347         os << label;
348         return 0;
349 }
350
351
352 // Have to overwrite the default InsetCommand method in order to check that
353 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
354 // citations and then changes his mind, turning natbib support off. The output
355 // should revert to \cite[]{}
356 int InsetCitation::latex(Buffer const * buffer, ostream & os,
357                          LatexRunParams const &) const
358 {
359         os << "\\";
360         if (buffer->params.use_natbib)
361                 os << getCmdName();
362         else
363                 os << "cite";
364
365 #warning What is this code supposed to do? (Lgb)
366 // my guess is that this is just waiting for when we support before,
367 // so it's a oneliner. But this is very silly ! - jbl
368
369 #if 1
370         // The current strange code
371
372         string const before = string();
373         string const after  = getOptions();
374         if (!before.empty() && buffer->params.use_natbib)
375                 os << '[' << before << "][" << after << ']';
376         else if (!after.empty())
377                 os << '[' << after << ']';
378 #else
379         // and the cleaned up equvalent, should it just be changed? (Lgb)
380         string const after  = getOptions();
381         if (!after.empty())
382                 os << '[' << after << ']';
383 #endif
384         string::const_iterator it  = getContents().begin();
385         string::const_iterator end = getContents().end();
386         // Paranoia check: make sure that there is no whitespace in here
387         string content;
388         char last = ',';
389         for (; it != end; ++it) {
390                 if (*it != ' ')
391                         last = *it;
392                 if (*it != ' ' || last != ',')
393                         content += *it;
394         }
395
396         os << '{' << content << '}';
397
398         return 0;
399 }
400
401
402 void InsetCitation::validate(LaTeXFeatures & features) const
403 {
404         if (features.bufferParams().use_natbib)
405                 features.require("natbib");
406 }