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