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