]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCitation.cpp
Cocoa based Qt-4.6 needs to paint every character separately to match metrics computa...
[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 "BiblioInfo.h"
17 #include "Buffer.h"
18 #include "buffer_funcs.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "DispatchResult.h"
22 #include "FuncCode.h"
23 #include "FuncRequest.h"
24 #include "LaTeXFeatures.h"
25 #include "output_xhtml.h"
26 #include "ParIterator.h"
27 #include "TocBackend.h"
28
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/FileNameList.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34
35 #include <algorithm>
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41
42 ParamInfo InsetCitation::param_info_;
43
44
45 InsetCitation::InsetCitation(Buffer * buf, InsetCommandParams const & p)
46         : InsetCommand(buf, p)
47 {}
48
49
50 ParamInfo const & InsetCitation::findInfo(string const & /* cmdName */)
51 {
52         // standard cite does only take one argument if jurabib is
53         // not used, but jurabib extends this to two arguments, so
54         // we have to allow both here. InsetCitation takes care that
55         // LaTeX output is nevertheless correct.
56         if (param_info_.empty()) {
57                 param_info_.add("after", ParamInfo::LATEX_OPTIONAL);
58                 param_info_.add("before", ParamInfo::LATEX_OPTIONAL);
59                 param_info_.add("key", ParamInfo::LATEX_REQUIRED);
60         }
61         return param_info_;
62 }
63
64
65 namespace {
66
67 vector<string> const init_possible_cite_commands()
68 {
69         char const * const possible[] = {
70                 "cite", "nocite", "citet", "citep", "citealt", "citealp",
71                 "citeauthor", "citeyear", "citeyearpar",
72                 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
73                 "Citet",  "Citep",  "Citealt",  "Citealp",  "Citeauthor",
74                 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
75                 "fullcite",
76                 "footcite", "footcitet", "footcitep", "footcitealt",
77                 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
78                 "citefield", "citetitle", "cite*"
79         };
80         size_t const size_possible = sizeof(possible) / sizeof(possible[0]);
81
82         return vector<string>(possible, possible + size_possible);
83 }
84
85
86 vector<string> const & possibleCiteCommands()
87 {
88         static vector<string> const possible = init_possible_cite_commands();
89         return possible;
90 }
91
92
93 } // anon namespace
94
95
96 bool InsetCitation::isCompatibleCommand(string const & cmd)
97 {
98         vector<string> const & possibles = possibleCiteCommands();
99         vector<string>::const_iterator const end = possibles.end();
100         return find(possibles.begin(), end, cmd) != end;
101 }
102
103
104 void InsetCitation::doDispatch(Cursor & cur, FuncRequest & cmd)
105 {
106         if (cmd.action() == LFUN_INSET_MODIFY)
107                 cache.recalculate = true;
108         InsetCommand::doDispatch(cur, cmd);
109 }
110
111
112 docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
113 {
114         Buffer const & buf = bv.buffer();
115         // Only after the buffer is loaded from file...
116         if (!buf.isFullyLoaded())
117                 return docstring();
118
119         BiblioInfo const & bi = buf.masterBibInfo();
120         if (bi.empty())
121                 return _("No bibliography defined!");
122
123         docstring const & key = getParam("key");
124         if (key.empty())
125                 return _("No citations selected!");
126
127         vector<docstring> keys = getVectorFromString(key);
128         vector<docstring>::const_iterator it = keys.begin();
129         vector<docstring>::const_iterator en = keys.end();
130         docstring tip;
131         for (; it != en; ++it) {
132                 docstring const key_info = bi.getInfo(*it, buffer());
133                 if (key_info.empty())
134                         continue;
135                 if (!tip.empty())
136                         tip += "\n";
137                 tip += wrap(key_info, -4);
138         }
139         return tip;
140 }
141
142
143 namespace {
144         
145 // FIXME See the header for the issue.
146 string defaultCiteCommand(CiteEngine engine)
147 {
148         string str;
149         switch (engine) {
150                 case ENGINE_BASIC:
151                         str = "cite";
152                         break;
153                 case ENGINE_NATBIB_AUTHORYEAR:
154                         str = "citet";
155                         break;
156                 case ENGINE_NATBIB_NUMERICAL:
157                         str = "citep";
158                         break;
159                 case ENGINE_JURABIB:
160                         str = "cite";
161                         break;
162         }
163         return str;
164 }
165
166         
167 string asValidLatexCommand(string const & input, CiteEngine const engine)
168 {
169         string const default_str = defaultCiteCommand(engine);
170         if (!InsetCitation::isCompatibleCommand(input))
171                 return default_str;
172
173         string output;
174         switch (engine) {
175                 case ENGINE_BASIC:
176                         if (input == "nocite")
177                                 output = input;
178                         else
179                                 output = default_str;
180                         break;
181
182                 case ENGINE_NATBIB_AUTHORYEAR:
183                 case ENGINE_NATBIB_NUMERICAL:
184                         if (input == "cite" || input == "citefield"
185                             || input == "citetitle" || input == "cite*")
186                                 output = default_str;
187                         else if (prefixIs(input, "foot"))
188                                 output = input.substr(4);
189                         else
190                                 output = input;
191                         break;
192
193                 case ENGINE_JURABIB: {
194                         // Jurabib does not support the 'uppercase' natbib style.
195                         if (input[0] == 'C')
196                                 output = string(1, 'c') + input.substr(1);
197                         else
198                                 output = input;
199
200                         // Jurabib does not support the 'full' natbib style.
201                         string::size_type const n = output.size() - 1;
202                         if (output != "cite*" && output[n] == '*')
203                                 output = output.substr(0, n);
204
205                         break;
206                 }
207         }
208
209         return output;
210 }
211
212
213 inline docstring wrapCitation(docstring const & key, 
214                 docstring const & content, bool for_xhtml)
215 {
216         if (!for_xhtml)
217                 return content;
218         // we have to do the escaping here, because we will ultimately
219         // write this as a raw string, so as not to escape the tags.
220         return "<a href='#" + key + "'>" +
221                         html::htmlize(content, XHTMLStream::ESCAPE_ALL) + "</a>";
222 }
223
224 } // anonymous namespace
225
226 docstring InsetCitation::generateLabel(bool for_xhtml) const
227 {
228         docstring label;
229         label = complexLabel(for_xhtml);
230
231         // Fallback to fail-safe
232         if (label.empty())
233                 label = basicLabel(for_xhtml);
234
235         return label;
236 }
237
238
239 docstring InsetCitation::complexLabel(bool for_xhtml) const
240 {
241         Buffer const & buf = buffer();
242         // Only start the process off after the buffer is loaded from file.
243         if (!buf.isFullyLoaded())
244                 return docstring();
245
246         BiblioInfo const & biblist = buf.masterBibInfo();
247         if (biblist.empty())
248                 return docstring();
249
250         // the natbib citation-styles
251         // CITET:       author (year)
252         // CITEP:       (author,year)
253         // CITEALT:     author year
254         // CITEALP:     author, year
255         // CITEAUTHOR:  author
256         // CITEYEAR:    year
257         // CITEYEARPAR: (year)
258         // jurabib supports these plus
259         // CITE:        author/<before field>
260
261         CiteEngine const engine = buffer().params().citeEngine();
262         // We don't currently use the full or forceUCase fields.
263         string cite_type = asValidLatexCommand(getCmdName(), engine);
264         if (cite_type[0] == 'C')
265                 // If we were going to use them, this would mean ForceUCase
266                 cite_type = string(1, 'c') + cite_type.substr(1);
267         if (cite_type[cite_type.size() - 1] == '*')
268                 // and this would mean FULL
269                 cite_type = cite_type.substr(0, cite_type.size() - 1);
270
271         docstring const & before = getParam("before");
272         docstring before_str;
273         if (!before.empty()) {
274                 // In CITET and CITEALT mode, the "before" string is
275                 // attached to the label associated with each and every key.
276                 // In CITEP, CITEALP and CITEYEARPAR mode, it is attached
277                 // to the front of the whole only.
278                 // In other modes, it is not used at all.
279                 if (cite_type == "citet" ||
280                     cite_type == "citealt" ||
281                     cite_type == "citep" ||
282                     cite_type == "citealp" ||
283                     cite_type == "citeyearpar")
284                         before_str = before + ' ';
285                 // In CITE (jurabib), the "before" string is used to attach
286                 // the annotator (of legal texts) to the author(s) of the
287                 // first reference.
288                 else if (cite_type == "cite")
289                         before_str = '/' + before;
290         }
291
292         docstring const & after = getParam("after");
293         docstring after_str;
294         // The "after" key is appended only to the end of the whole.
295         if (cite_type == "nocite")
296                 after_str =  " (" + _("not cited") + ')';
297         else if (!after.empty()) {
298                 after_str = ", " + after;
299         }
300
301         // One day, these might be tunable (as they are in BibTeX).
302         char op, cp;    // opening and closing parenthesis.
303         const char * sep;       // punctuation mark separating citation entries.
304         if (engine == ENGINE_BASIC) {
305                 op  = '[';
306                 cp  = ']';
307                 sep = ",";
308         } else {
309                 op  = '(';
310                 cp  = ')';
311                 sep = ";";
312         }
313
314         docstring const op_str = ' ' + docstring(1, op);
315         docstring const cp_str = docstring(1, cp) + ' ';
316         docstring const sep_str = from_ascii(sep) + ' ';
317
318         docstring label;
319         vector<docstring> keys = getVectorFromString(getParam("key"));
320         vector<docstring>::const_iterator it  = keys.begin();
321         vector<docstring>::const_iterator end = keys.end();
322         for (; it != end; ++it) {
323                 // get the bibdata corresponding to the key
324                 docstring const author = biblist.getAbbreviatedAuthor(*it);
325                 docstring const year = biblist.getYear(*it, for_xhtml);
326                 docstring const citenum = for_xhtml ? biblist.getCiteNumber(*it) : *it;
327
328                 if (author.empty() || year.empty())
329                         // We can't construct a "complex" label without that info.
330                         // So fail safely.
331                         return docstring();
332
333                 // authors1/<before>;  ... ;
334                 //  authors_last, <after>
335                 if (cite_type == "cite") {
336                         if (engine == ENGINE_BASIC) {
337                                 label += wrapCitation(*it, citenum, for_xhtml) + sep_str;
338                         } else if (engine == ENGINE_JURABIB) {
339                                 if (it == keys.begin())
340                                         label += wrapCitation(*it, author, for_xhtml) + before_str + sep_str;
341                                 else
342                                         label += wrapCitation(*it, author, for_xhtml) + sep_str;
343                         }
344                 } 
345                 // nocite
346                 else if (cite_type == "nocite") {
347                         label += *it + sep_str;
348                 } 
349                 // (authors1 (<before> year);  ... ;
350                 //  authors_last (<before> year, <after>)
351                 else if (cite_type == "citet") {
352                         switch (engine) {
353                         case ENGINE_NATBIB_AUTHORYEAR:
354                                 label += author + op_str + before_str +
355                                         wrapCitation(*it, year, for_xhtml) + cp + sep_str;
356                                 break;
357                         case ENGINE_NATBIB_NUMERICAL:
358                                 label += author + op_str + before_str + 
359                                         wrapCitation(*it, citenum, for_xhtml) + cp + sep_str;
360                                 break;
361                         case ENGINE_JURABIB:
362                                 label += before_str + author + op_str +
363                                         wrapCitation(*it, year, for_xhtml) + cp + sep_str;
364                                 break;
365                         case ENGINE_BASIC:
366                                 break;
367                         }
368                 } 
369                 // author, year; author, year; ...      
370                 else if (cite_type == "citep" ||
371                            cite_type == "citealp") {
372                         if (engine == ENGINE_NATBIB_NUMERICAL) {
373                                 label += wrapCitation(*it, citenum, for_xhtml) + sep_str;
374                         } else {
375                                 label += wrapCitation(*it, author + ", " + year, for_xhtml) + sep_str;
376                         }
377
378                 } 
379                 // (authors1 <before> year;
380                 //  authors_last <before> year, <after>)
381                 else if (cite_type == "citealt") {
382                         switch (engine) {
383                         case ENGINE_NATBIB_AUTHORYEAR:
384                                 label += author + ' ' + before_str +
385                                         wrapCitation(*it, year, for_xhtml) + sep_str;
386                                 break;
387                         case ENGINE_NATBIB_NUMERICAL:
388                                 label += author + ' ' + before_str + '#' + 
389                                         wrapCitation(*it, citenum, for_xhtml) + sep_str;
390                                 break;
391                         case ENGINE_JURABIB:
392                                 label += before_str + 
393                                         wrapCitation(*it, author + ' ' + year, for_xhtml) + sep_str;
394                                 break;
395                         case ENGINE_BASIC:
396                                 break;
397                         }
398
399                 
400                 } 
401                 // author; author; ...
402                 else if (cite_type == "citeauthor") {
403                         label += wrapCitation(*it, author, for_xhtml) + sep_str;
404                 }
405                 // year; year; ...
406                 else if (cite_type == "citeyear" ||
407                            cite_type == "citeyearpar") {
408                         label += wrapCitation(*it, year, for_xhtml) + sep_str;
409                 }
410         }
411         label = rtrim(rtrim(label), sep);
412
413         if (!after_str.empty()) {
414                 if (cite_type == "citet") {
415                         // insert "after" before last ')'
416                         label.insert(label.size() - 1, after_str);
417                 } else {
418                         bool const add =
419                                 !(engine == ENGINE_NATBIB_NUMERICAL &&
420                                   (cite_type == "citeauthor" ||
421                                    cite_type == "citeyear"));
422                         if (add)
423                                 label += after_str;
424                 }
425         }
426
427         if (!before_str.empty() && (cite_type == "citep" ||
428                                     cite_type == "citealp" ||
429                                     cite_type == "citeyearpar")) {
430                 label = before_str + label;
431         }
432
433         if (cite_type == "citep" || cite_type == "citeyearpar" || 
434             (cite_type == "cite" && engine == ENGINE_BASIC) )
435                 label = op + label + cp;
436
437         return label;
438 }
439
440
441 docstring InsetCitation::basicLabel(bool for_xhtml) const
442 {
443         docstring keys = getParam("key");
444         docstring label;
445
446         docstring key;
447         do {
448                 // if there is no comma, then everything goes into key
449                 // and keys will be empty.
450                 keys = trim(split(keys, key, ','));
451                 key = trim(key);
452                 if (!label.empty())
453                         label += ", ";
454                 label += wrapCitation(key, key, for_xhtml);
455         } while (!keys.empty());
456
457         docstring const & after = getParam("after");
458         if (!after.empty())
459                 label += ", " + after;
460
461         return '[' + label + ']';
462 }
463
464 docstring InsetCitation::screenLabel() const
465 {
466         return cache.screen_label;
467 }
468
469
470 void InsetCitation::updateBuffer(ParIterator const &, UpdateType)
471 {
472         if (!cache.recalculate && buffer().citeLabelsValid())
473                 return;
474
475         // The label may have changed, so we have to re-create it.
476         docstring const glabel = generateLabel();
477
478         unsigned int const maxLabelChars = 45;
479
480         docstring label = glabel;
481         if (label.size() > maxLabelChars) {
482                 label.erase(maxLabelChars - 3);
483                 label += "...";
484         }
485
486         cache.recalculate = false;
487         cache.generated_label = glabel;
488         cache.screen_label = label;
489 }
490
491
492 void InsetCitation::addToToc(DocIterator const & cpit) const
493 {
494         // NOTE
495         // XHTML output uses the TOC to collect the citations
496         // from the document. So if this gets changed, then we
497         // will need to change how the citations are collected.
498         docstring const tocitem = getParam("key");
499         Toc & toc = buffer().tocBackend().toc("citation");
500         toc.push_back(TocItem(cpit, 0, tocitem));
501 }
502
503
504 int InsetCitation::plaintext(odocstream & os, OutputParams const &) const
505 {
506         os << cache.generated_label;
507         return cache.generated_label.size();
508 }
509
510
511 static docstring const cleanupWhitespace(docstring const & citelist)
512 {
513         docstring::const_iterator it  = citelist.begin();
514         docstring::const_iterator end = citelist.end();
515         // Paranoia check: make sure that there is no whitespace in here
516         // -- at least not behind commas or at the beginning
517         docstring result;
518         char_type last = ',';
519         for (; it != end; ++it) {
520                 if (*it != ' ')
521                         last = *it;
522                 if (*it != ' ' || last != ',')
523                         result += *it;
524         }
525         return result;
526 }
527
528
529 int InsetCitation::docbook(odocstream & os, OutputParams const &) const
530 {
531         os << from_ascii("<citation>")
532            << cleanupWhitespace(getParam("key"))
533            << from_ascii("</citation>");
534         return 0;
535 }
536
537
538 docstring InsetCitation::xhtml(XHTMLStream & xs, OutputParams const &) const
539 {
540         string const & cmd = getCmdName();
541         if (cmd == "nocite")
542                 return docstring();
543
544         // have to output this raw, because generateLabel() will include tags
545         xs << XHTMLStream::ESCAPE_NONE << generateLabel(true);
546
547         return docstring();
548 }
549
550
551 void InsetCitation::toString(odocstream & os) const
552 {
553         plaintext(os, OutputParams(0));
554 }
555
556
557 void InsetCitation::forToc(docstring & os, size_t) const
558 {
559         os += screenLabel();
560 }
561
562
563 // Have to overwrite the default InsetCommand method in order to check that
564 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
565 // citations and then changes his mind, turning natbib support off. The output
566 // should revert to \cite[]{}
567 void InsetCitation::latex(otexstream & os, OutputParams const & runparams) const
568 {
569         CiteEngine cite_engine = buffer().params().citeEngine();
570         BiblioInfo const & bi = buffer().masterBibInfo();
571         // FIXME UNICODE
572         docstring const cite_str = from_utf8(
573                 asValidLatexCommand(getCmdName(), cite_engine));
574
575         if (runparams.inulemcmd)
576                 os << "\\mbox{";
577
578         os << "\\" << cite_str;
579
580         docstring const & before = getParam("before");
581         docstring const & after  = getParam("after");
582         if (!before.empty() && cite_engine != ENGINE_BASIC)
583                 os << '[' << before << "][" << after << ']';
584         else if (!after.empty())
585                 os << '[' << after << ']';
586
587         if (!bi.isBibtex(getParam("key")))
588                 // escape chars with bibitems
589                 os << '{' << escape(cleanupWhitespace(getParam("key"))) << '}';
590         else
591                 os << '{' << cleanupWhitespace(getParam("key")) << '}';
592
593         if (runparams.inulemcmd)
594                 os << "}";
595 }
596
597
598 void InsetCitation::validate(LaTeXFeatures & features) const
599 {
600         switch (features.bufferParams().citeEngine()) {
601         case ENGINE_BASIC:
602                 break;
603         case ENGINE_NATBIB_AUTHORYEAR:
604         case ENGINE_NATBIB_NUMERICAL:
605                 features.require("natbib");
606                 break;
607         case ENGINE_JURABIB:
608                 features.require("jurabib");
609                 break;
610         }
611 }
612
613
614 docstring InsetCitation::contextMenuName() const
615 {
616         return from_ascii("context-citation");
617 }
618
619
620 } // namespace lyx