]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCitation.cpp
5a007cf46f666a36ca1c8d6052ddb8798cefce0e
[lyx.git] / src / insets / InsetCitation.cpp
1 /**
2  * \file InsetCitation.cpp
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 Voß
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetCitation.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "debug.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "LaTeXFeatures.h"
22
23 #include "frontends/controllers/frontend_helpers.h"
24
25 #include "support/fs_extras.h"
26 #include "support/lstrings.h"
27
28 #include <algorithm>
29
30 #include <boost/filesystem/operations.hpp>
31 #include <boost/filesystem/exception.hpp>
32
33
34 namespace lyx {
35
36 using support::ascii_lowercase;
37 using support::contains;
38 using support::FileName;
39 using support::getStringFromVector;
40 using support::getVectorFromString;
41 using support::ltrim;
42 using support::prefixIs;
43 using support::rtrim;
44 using support::split;
45 using support::tokenPos;
46
47 using std::endl;
48 using std::replace;
49 using std::string;
50 using std::ostream;
51 using std::vector;
52 using std::map;
53
54 namespace fs = boost::filesystem;
55
56
57 namespace {
58
59 vector<string> const init_possible_cite_commands()
60 {
61         char const * const pos[] = {
62                 "cite", "citet", "citep", "citealt", "citealp",
63                 "citeauthor", "citeyear", "citeyearpar",
64                 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
65                 "Citet",  "Citep",  "Citealt",  "Citealp",  "Citeauthor",
66                 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
67                 "fullcite",
68                 "footcite", "footcitet", "footcitep", "footcitealt",
69                 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
70                 "citefield", "citetitle", "cite*"
71         };
72         size_t const size_pos = sizeof(pos) / sizeof(pos[0]);
73
74         return vector<string>(pos, pos + size_pos);
75 }
76
77
78 vector<string> const & possible_cite_commands()
79 {
80         static vector<string> const pos = init_possible_cite_commands();
81         return pos;
82 }
83
84
85 bool is_possible_cite_command(string const & input)
86 {
87         vector<string> const & possibles = possible_cite_commands();
88         vector<string>::const_iterator const end = possibles.end();
89         return std::find(possibles.begin(), end, input) != end;
90 }
91
92
93 string const default_cite_command(biblio::CiteEngine engine)
94 {
95         string str;
96         switch (engine) {
97                 case biblio::ENGINE_BASIC:
98                         str = "cite";
99                         break;
100                 case biblio::ENGINE_NATBIB_AUTHORYEAR:
101                         str = "citet";
102                         break;
103                 case biblio::ENGINE_NATBIB_NUMERICAL:
104                         str = "citep";
105                         break;
106                 case biblio::ENGINE_JURABIB:
107                         str = "cite";
108                         break;
109         }
110         return str;
111 }
112
113                 
114 string const 
115                 asValidLatexCommand(string const & input, biblio::CiteEngine const engine)
116 {
117         string const default_str = default_cite_command(engine);
118         if (!is_possible_cite_command(input))
119                 return default_str;
120
121         string output;
122         switch (engine) {
123                 case biblio::ENGINE_BASIC:
124                         output = default_str;
125                         break;
126
127                 case biblio::ENGINE_NATBIB_AUTHORYEAR:
128                 case biblio::ENGINE_NATBIB_NUMERICAL:
129                         if (input == "cite" || input == "citefield" ||
130                                                         input == "citetitle" || input == "cite*")
131                                 output = default_str;
132                         else if (prefixIs(input, "foot"))
133                                 output = input.substr(4);
134                         else
135                                 output = input;
136                         break;
137
138                         case biblio::ENGINE_JURABIB: {
139                 // Jurabib does not support the 'uppercase' natbib style.
140                                 if (input[0] == 'C')
141                                         output = string(1, 'c') + input.substr(1);
142                                 else
143                                         output = input;
144
145                 // Jurabib does not support the 'full' natbib style.
146                                 string::size_type const n = output.size() - 1;
147                                 if (output != "cite*" && output[n] == '*')
148                                         output = output.substr(0, n);
149
150                                 break;
151                         }
152         }
153
154         return output;
155 }
156
157
158 docstring const getNatbibLabel(Buffer const & buffer,
159                             string const & citeType, docstring const & keyList,
160                             docstring const & before, docstring const & after,
161                             biblio::CiteEngine engine)
162 {
163         // Only start the process off after the buffer is loaded from file.
164         if (!buffer.fully_loaded())
165                 return docstring();
166
167         // Cache the labels
168         typedef std::map<Buffer const *, BiblioInfo> CachedMap;
169         static CachedMap cached_keys;
170
171         // and cache the timestamp of the bibliography files.
172         static std::map<FileName, time_t> bibfileStatus;
173
174         BiblioInfo biblist;
175
176         vector<FileName> const & bibfilesCache = buffer.getBibfilesCache();
177         // compare the cached timestamps with the actual ones.
178         bool changed = false;
179         for (vector<FileName>::const_iterator it = bibfilesCache.begin();
180                         it != bibfilesCache.end(); ++ it) {
181                 FileName const f = *it;
182                 try {
183                         std::time_t lastw = fs::last_write_time(f.toFilesystemEncoding());
184                         if (lastw != bibfileStatus[f]) {
185                                 changed = true;
186                                 bibfileStatus[f] = lastw;
187                         }
188                 }
189                 catch (fs::filesystem_error & fserr) {
190                         changed = true;
191                         lyxerr << "Couldn't find or read bibtex file "
192                                << f << endl;
193                         LYXERR(Debug::DEBUG) << "Fs error: "
194                                              << fserr.what() << endl;
195                 }
196         }
197
198         // build the list only if the bibfiles have been changed
199         if (cached_keys[&buffer].empty() || bibfileStatus.empty() || changed) {
200                 biblist.fillWithBibKeys(&buffer);
201                 cached_keys[&buffer] = biblist;
202         } else
203                 // use the cached keys
204                 biblist = cached_keys[&buffer];
205
206         if (biblist.empty())
207                 return docstring();
208
209         // the natbib citation-styles
210         // CITET:       author (year)
211         // CITEP:       (author,year)
212         // CITEALT:     author year
213         // CITEALP:     author, year
214         // CITEAUTHOR:  author
215         // CITEYEAR:    year
216         // CITEYEARPAR: (year)
217         // jurabib supports these plus
218         // CITE:        author/<before field>
219
220         // We don't currently use the full or forceUCase fields.
221         string cite_type = asValidLatexCommand(citeType, engine);
222         if (cite_type[0] == 'C')
223                 //If we were going to use them, this would mean ForceUCase
224                 cite_type = string(1, 'c') + cite_type.substr(1);
225         if (cite_type[cite_type.size() - 1] == '*')
226                 //and this would mean FULL
227                 cite_type = cite_type.substr(0, cite_type.size() - 1);
228
229         docstring before_str;
230         if (!before.empty()) {
231                 // In CITET and CITEALT mode, the "before" string is
232                 // attached to the label associated with each and every key.
233                 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
234                 // to the front of the whole only.
235                 // In other modes, it is not used at all.
236                 if (cite_type == "citet" ||
237                     cite_type == "citealt" ||
238                     cite_type == "citep" ||
239                     cite_type == "citealp" ||
240                     cite_type == "citeyearpar")
241                         before_str = before + ' ';
242                 // In CITE (jurabib), the "before" string is used to attach
243                 // the annotator (of legal texts) to the author(s) of the
244                 // first reference.
245                 else if (cite_type == "cite")
246                         before_str = '/' + before;
247         }
248
249         docstring after_str;
250         if (!after.empty()) {
251                 // The "after" key is appended only to the end of the whole.
252                 after_str = ", " + after;
253         }
254
255         // One day, these might be tunable (as they are in BibTeX).
256         char const op  = '('; // opening parenthesis.
257         char const cp  = ')'; // closing parenthesis.
258         // puctuation mark separating citation entries.
259         char const * const sep = ";";
260
261         docstring const op_str(' ' + docstring(1, op));
262         docstring const cp_str(docstring(1, cp) + ' ');
263         docstring const sep_str(from_ascii(sep) + ' ');
264
265         docstring label;
266         vector<docstring> keys = getVectorFromString(keyList);
267         vector<docstring>::const_iterator it  = keys.begin();
268         vector<docstring>::const_iterator end = keys.end();
269         for (; it != end; ++it) {
270                 // get the bibdata corresponding to the key
271                 docstring const author(biblist.getAbbreviatedAuthor(*it));
272                 docstring const year(biblist.getYear(*it));
273
274                 // Something isn't right. Fail safely.
275                 if (author.empty() || year.empty())
276                         return docstring();
277
278                 // authors1/<before>;  ... ;
279                 //  authors_last, <after>
280                 if (cite_type == "cite" && engine == biblio::ENGINE_JURABIB) {
281                         if (it == keys.begin())
282                                 label += author + before_str + sep_str;
283                         else
284                                 label += author + sep_str;
285
286                 // (authors1 (<before> year);  ... ;
287                 //  authors_last (<before> year, <after>)
288                 } else if (cite_type == "citet") {
289                         switch (engine) {
290                         case biblio::ENGINE_NATBIB_AUTHORYEAR:
291                                 label += author + op_str + before_str +
292                                         year + cp + sep_str;
293                                 break;
294                         case biblio::ENGINE_NATBIB_NUMERICAL:
295                                 label += author + op_str + before_str + '#' + *it + cp + sep_str;
296                                 break;
297                         case biblio::ENGINE_JURABIB:
298                                 label += before_str + author + op_str +
299                                         year + cp + sep_str;
300                                 break;
301                         case biblio::ENGINE_BASIC:
302                                 break;
303                         }
304
305                 // author, year; author, year; ...
306                 } else if (cite_type == "citep" ||
307                            cite_type == "citealp") {
308                         if (engine == biblio::ENGINE_NATBIB_NUMERICAL) {
309                                 label += *it + sep_str;
310                         } else {
311                                 label += author + ", " + year + sep_str;
312                         }
313
314                 // (authors1 <before> year;
315                 //  authors_last <before> year, <after>)
316                 } else if (cite_type == "citealt") {
317                         switch (engine) {
318                         case biblio::ENGINE_NATBIB_AUTHORYEAR:
319                                 label += author + ' ' + before_str +
320                                         year + sep_str;
321                                 break;
322                         case biblio::ENGINE_NATBIB_NUMERICAL:
323                                 label += author + ' ' + before_str + '#' + *it + sep_str;
324                                 break;
325                         case biblio::ENGINE_JURABIB:
326                                 label += before_str + author + ' ' +
327                                         year + sep_str;
328                                 break;
329                         case biblio::ENGINE_BASIC:
330                                 break;
331                         }
332
333                 // author; author; ...
334                 } else if (cite_type == "citeauthor") {
335                         label += author + sep_str;
336
337                 // year; year; ...
338                 } else if (cite_type == "citeyear" ||
339                            cite_type == "citeyearpar") {
340                         label += year + sep_str;
341                 }
342         }
343         label = rtrim(rtrim(label), sep);
344
345         if (!after_str.empty()) {
346                 if (cite_type == "citet") {
347                         // insert "after" before last ')'
348                         label.insert(label.size() - 1, after_str);
349                 } else {
350                         bool const add =
351                                 !(engine == biblio::ENGINE_NATBIB_NUMERICAL &&
352                                   (cite_type == "citeauthor" ||
353                                    cite_type == "citeyear"));
354                         if (add)
355                                 label += after_str;
356                 }
357         }
358
359         if (!before_str.empty() && (cite_type == "citep" ||
360                                     cite_type == "citealp" ||
361                                     cite_type == "citeyearpar")) {
362                 label = before_str + label;
363         }
364
365         if (cite_type == "citep" || cite_type == "citeyearpar")
366                 label = op + label + cp;
367
368         return label;
369 }
370
371
372 docstring const getBasicLabel(docstring const & keyList, docstring const & after)
373 {
374         docstring keys(keyList);
375         docstring label;
376
377         if (contains(keys, ',')) {
378                 // Final comma allows while loop to cover all keys
379                 keys = ltrim(split(keys, label, ',')) + ',';
380                 while (contains(keys, ',')) {
381                         docstring key;
382                         keys = ltrim(split(keys, key, ','));
383                         label += ", " + key;
384                 }
385         } else
386                 label = keys;
387
388         if (!after.empty())
389                 label += ", " + after;
390
391         return '[' + label + ']';
392 }
393
394 } // anon namespace
395
396
397 InsetCitation::InsetCitation(InsetCommandParams const & p)
398         : InsetCommand(p, "citation")
399 {}
400
401
402 docstring const InsetCitation::generateLabel(Buffer const & buffer) const
403 {
404         docstring const before = getParam("before");
405         docstring const after  = getParam("after");
406
407         docstring label;
408         biblio::CiteEngine const engine = buffer.params().getEngine();
409         if (engine != biblio::ENGINE_BASIC) {
410                 label = getNatbibLabel(buffer, getCmdName(), getParam("key"),
411                                        before, after, engine);
412         }
413
414         // Fallback to fail-safe
415         if (label.empty()) {
416                 label = getBasicLabel(getParam("key"), after);
417         }
418
419         return label;
420 }
421
422
423 docstring const InsetCitation::getScreenLabel(Buffer const & buffer) const
424 {
425         biblio::CiteEngine const engine = buffer.params().getEngine();
426         if (cache.params == params() && cache.engine == engine)
427                 return cache.screen_label;
428
429         // The label has changed, so we have to re-create it.
430         docstring const glabel = generateLabel(buffer);
431
432         unsigned int const maxLabelChars = 45;
433
434         docstring label = glabel;
435         if (label.size() > maxLabelChars) {
436                 label.erase(maxLabelChars-3);
437                 label += "...";
438         }
439
440         cache.engine  = engine;
441         cache.params = params();
442         cache.generated_label = glabel;
443         cache.screen_label = label;
444
445         return label;
446 }
447
448
449 int InsetCitation::plaintext(Buffer const & buffer, odocstream & os,
450                              OutputParams const &) const
451 {
452         docstring str;
453
454         if (cache.params == params() &&
455             cache.engine == buffer.params().getEngine())
456                 str = cache.generated_label;
457         else
458                 str = generateLabel(buffer);
459
460         os << str;
461         return str.size();
462 }
463
464
465 namespace {
466
467 docstring const cleanupWhitespace(docstring const & citelist)
468 {
469         docstring::const_iterator it  = citelist.begin();
470         docstring::const_iterator end = citelist.end();
471         // Paranoia check: make sure that there is no whitespace in here
472         // -- at least not behind commas or at the beginning
473         docstring result;
474         char_type last = ',';
475         for (; it != end; ++it) {
476                 if (*it != ' ')
477                         last = *it;
478                 if (*it != ' ' || last != ',')
479                         result += *it;
480         }
481         return result;
482 }
483
484 // end anon namyspace
485 }
486
487 int InsetCitation::docbook(Buffer const &, odocstream & os,
488                            OutputParams const &) const
489 {
490         os << "<citation>"
491            << cleanupWhitespace(getParam("key"))
492            << "</citation>";
493         return 0;
494 }
495
496
497 int InsetCitation::textString(Buffer const & buf, odocstream & os,
498                        OutputParams const & op) const
499 {
500         return plaintext(buf, os, op);
501 }
502
503
504 // Have to overwrite the default InsetCommand method in order to check that
505 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
506 // citations and then changes his mind, turning natbib support off. The output
507 // should revert to \cite[]{}
508 int InsetCitation::latex(Buffer const & buffer, odocstream & os,
509                          OutputParams const &) const
510 {
511         biblio::CiteEngine cite_engine = buffer.params().getEngine();
512         // FIXME UNICODE
513         docstring const cite_str = from_utf8(
514                 asValidLatexCommand(getCmdName(), cite_engine));
515
516         os << "\\" << cite_str;
517
518         docstring const & before = getParam("before");
519         docstring const & after  = getParam("after");
520         if (!before.empty() && cite_engine != biblio::ENGINE_BASIC)
521                 os << '[' << before << "][" << after << ']';
522         else if (!after.empty())
523                 os << '[' << after << ']';
524
525         os << '{' << cleanupWhitespace(getParam("key")) << '}';
526
527         return 0;
528 }
529
530
531 void InsetCitation::validate(LaTeXFeatures & features) const
532 {
533         switch (features.bufferParams().getEngine()) {
534         case biblio::ENGINE_BASIC:
535                 break;
536         case biblio::ENGINE_NATBIB_AUTHORYEAR:
537         case biblio::ENGINE_NATBIB_NUMERICAL:
538                 features.require("natbib");
539                 break;
540         case biblio::ENGINE_JURABIB:
541                 features.require("jurabib");
542                 break;
543         }
544 }
545
546
547 void InsetCitation::replaceContents(string const & from, string const & to)
548 {
549         if (tokenPos(getContents(), ',', from) != -1) {
550                 vector<string> items = getVectorFromString(getContents());
551                 replace(items.begin(), items.end(), from, to);
552                 setContents(getStringFromVector(items));
553         }
554 }
555
556
557 } // namespace lyx