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