2 * \file InsetCitation.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
14 #include "InsetCitation.h"
16 #include "BiblioInfo.h"
18 #include "buffer_funcs.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "LaTeXFeatures.h"
25 #include "output_xhtml.h"
26 #include "ParIterator.h"
27 #include "TocBackend.h"
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"
38 using namespace lyx::support;
42 ParamInfo InsetCitation::param_info_;
45 InsetCitation::InsetCitation(Buffer * buf, InsetCommandParams const & p)
46 : InsetCommand(buf, p)
50 ParamInfo const & InsetCitation::findInfo(string const & /* cmdName */)
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);
67 vector<string> const init_possible_cite_commands()
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*",
76 "footcite", "footcitet", "footcitep", "footcitealt",
77 "footcitealp", "footciteauthor", "footciteyear", "footciteyearpar",
78 "citefield", "citetitle", "cite*"
80 size_t const size_possible = sizeof(possible) / sizeof(possible[0]);
82 return vector<string>(possible, possible + size_possible);
86 vector<string> const & possibleCiteCommands()
88 static vector<string> const possible = init_possible_cite_commands();
96 // FIXME: use the citeCommands provided by the TextClass
97 // instead of possibleCiteCommands defined in this file.
98 bool InsetCitation::isCompatibleCommand(string const & cmd)
100 vector<string> const & possibles = possibleCiteCommands();
101 vector<string>::const_iterator const end = possibles.end();
102 return find(possibles.begin(), end, cmd) != end;
106 void InsetCitation::doDispatch(Cursor & cur, FuncRequest & cmd)
108 if (cmd.action() == LFUN_INSET_MODIFY)
109 cache.recalculate = true;
110 InsetCommand::doDispatch(cur, cmd);
114 docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
116 Buffer const & buf = bv.buffer();
117 // Only after the buffer is loaded from file...
118 if (!buf.isFullyLoaded())
121 BiblioInfo const & bi = buf.masterBibInfo();
123 return _("No bibliography defined!");
125 docstring const & key = getParam("key");
127 return _("No citations selected!");
129 vector<docstring> keys = getVectorFromString(key);
130 vector<docstring>::const_iterator it = keys.begin();
131 vector<docstring>::const_iterator en = keys.end();
133 for (; it != en; ++it) {
134 docstring const key_info = bi.getInfo(*it, buffer());
135 if (key_info.empty())
139 tip += wrap(key_info, -4);
148 CitationStyle asValidLatexCommand(string const & input, vector<CitationStyle> const valid_styles)
150 CitationStyle cs = valid_styles[0];
151 cs.forceUpperCase = false;
152 cs.fullAuthorList = false;
153 if (!InsetCitation::isCompatibleCommand(input))
156 string normalized_input = input;
157 string::size_type const n = input.size() - 1;
159 normalized_input[0] = 'c';
161 normalized_input = normalized_input.substr(0, n);
163 vector<CitationStyle>::const_iterator it = valid_styles.begin();
164 vector<CitationStyle>::const_iterator end = valid_styles.end();
165 for (; it != end; ++it) {
166 CitationStyle this_cs = *it;
167 if (this_cs.cmd == normalized_input) {
173 cs.forceUpperCase &= input[0] == 'C';
174 cs.fullAuthorList &= input[n] == '*';
180 inline docstring wrapCitation(docstring const & key,
181 docstring const & content, bool for_xhtml)
185 // we have to do the escaping here, because we will ultimately
186 // write this as a raw string, so as not to escape the tags.
187 return "<a href='#" + key + "'>" +
188 html::htmlize(content, XHTMLStream::ESCAPE_ALL) + "</a>";
191 } // anonymous namespace
193 docstring InsetCitation::generateLabel(bool for_xhtml) const
196 label = complexLabel(for_xhtml);
198 // Fallback to fail-safe
200 label = basicLabel(for_xhtml);
206 docstring InsetCitation::complexLabel(bool for_xhtml) const
208 Buffer const & buf = buffer();
209 // Only start the process off after the buffer is loaded from file.
210 if (!buf.isFullyLoaded())
213 BiblioInfo const & biblist = buf.masterBibInfo();
217 docstring const & key = getParam("key");
219 return _("No citations selected!");
221 // We don't currently use the full or forceUCase fields.
222 string cite_type = getCmdName();
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);
230 docstring const & before = getParam("before");
231 docstring const & after = getParam("after");
233 // FIXME: allow to add cite macros
235 buffer().params().documentClass().addCiteMacro("!textbefore", to_utf8(before));
236 buffer().params().documentClass().addCiteMacro("!textafter", to_utf8(after));
239 vector<docstring> const keys = getVectorFromString(key);
240 label = biblist.getLabel(keys, buffer(), cite_type, for_xhtml, before, after);
245 docstring InsetCitation::basicLabel(bool for_xhtml) const
247 docstring keys = getParam("key");
252 // if there is no comma, then everything goes into key
253 // and keys will be empty.
254 keys = trim(split(keys, key, ','));
258 label += wrapCitation(key, key, for_xhtml);
259 } while (!keys.empty());
261 docstring const & after = getParam("after");
263 label += ", " + after;
265 return '[' + label + ']';
268 docstring InsetCitation::screenLabel() const
270 return cache.screen_label;
274 void InsetCitation::updateBuffer(ParIterator const &, UpdateType)
276 if (!cache.recalculate && buffer().citeLabelsValid())
279 // The label may have changed, so we have to re-create it.
280 docstring const glabel = generateLabel();
282 unsigned int const maxLabelChars = 45;
284 docstring label = glabel;
285 if (label.size() > maxLabelChars) {
286 label.erase(maxLabelChars - 3);
290 cache.recalculate = false;
291 cache.generated_label = glabel;
292 cache.screen_label = label;
296 void InsetCitation::addToToc(DocIterator const & cpit) const
299 // XHTML output uses the TOC to collect the citations
300 // from the document. So if this gets changed, then we
301 // will need to change how the citations are collected.
302 docstring const tocitem = getParam("key");
303 Toc & toc = buffer().tocBackend().toc("citation");
304 toc.push_back(TocItem(cpit, 0, tocitem));
308 int InsetCitation::plaintext(odocstream & os, OutputParams const &) const
310 os << cache.generated_label;
311 return cache.generated_label.size();
315 static docstring const cleanupWhitespace(docstring const & citelist)
317 docstring::const_iterator it = citelist.begin();
318 docstring::const_iterator end = citelist.end();
319 // Paranoia check: make sure that there is no whitespace in here
320 // -- at least not behind commas or at the beginning
322 char_type last = ',';
323 for (; it != end; ++it) {
326 if (*it != ' ' || last != ',')
333 int InsetCitation::docbook(odocstream & os, OutputParams const &) const
335 os << from_ascii("<citation>")
336 << cleanupWhitespace(getParam("key"))
337 << from_ascii("</citation>");
342 docstring InsetCitation::xhtml(XHTMLStream & xs, OutputParams const &) const
344 string const & cmd = getCmdName();
348 // have to output this raw, because generateLabel() will include tags
349 xs << XHTMLStream::ESCAPE_NONE << generateLabel(true);
355 void InsetCitation::toString(odocstream & os) const
357 plaintext(os, OutputParams(0));
361 void InsetCitation::forToc(docstring & os, size_t) const
367 // Have to overwrite the default InsetCommand method in order to check that
368 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
369 // citations and then changes his mind, turning natbib support off. The output
370 // should revert to the default citation command as provided by the citation
371 // engine, e.g. \cite[]{} for the basic engine.
372 void InsetCitation::latex(otexstream & os, OutputParams const & runparams) const
374 vector<CitationStyle> citation_styles = buffer().params().citeStyles();
375 CitationStyle cs = asValidLatexCommand(getCmdName(), citation_styles);
376 BiblioInfo const & bi = buffer().masterBibInfo();
378 docstring const cite_str = from_utf8(citationStyleToString(cs));
380 if (runparams.inulemcmd)
383 os << "\\" << cite_str;
385 docstring const & before = getParam("before");
386 docstring const & after = getParam("after");
387 if (!before.empty() && cs.textBefore)
388 os << '[' << before << "][" << after << ']';
389 else if (!after.empty() && cs.textAfter)
390 os << '[' << after << ']';
392 if (!bi.isBibtex(getParam("key")))
393 // escape chars with bibitems
394 os << '{' << escape(cleanupWhitespace(getParam("key"))) << '}';
396 os << '{' << cleanupWhitespace(getParam("key")) << '}';
398 if (runparams.inulemcmd)
403 string InsetCitation::contextMenuName() const
405 return "context-citation";