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"
22 #include "DispatchResult.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
29 #include "output_xhtml.h"
30 #include "output_docbook.h"
31 #include "ParIterator.h"
32 #include "texstream.h"
33 #include "TocBackend.h"
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/FileNameList.h"
38 #include "support/filetools.h"
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
46 using namespace lyx::support;
50 InsetCitation::InsetCitation(Buffer * buf, InsetCommandParams const & p)
51 : InsetCommand(buf, p)
53 buffer().scheduleBiblioTempRemoval();
58 InsetCitation::~InsetCitation()
61 /* We do not use buffer() because Coverity believes that this
62 * may throw an exception. Actually this code path is not
63 * taken when buffer_ == 0 */
64 buffer_->scheduleBiblioTempRemoval();
68 // May well be over-ridden when session settings are loaded
69 // in GuiCitation. Unfortunately, that will not happen until
70 // such a dialog is created.
71 bool InsetCitation::last_literal = true;
74 ParamInfo const & InsetCitation::findInfo(string const & /* cmdName */)
76 static ParamInfo param_info_;
78 // standard cite does only take one argument, but biblatex, jurabib
79 // and natbib extend this to two arguments, so
80 // we have to allow both here. InsetCitation takes care that
81 // LaTeX output is nevertheless correct.
82 if (param_info_.empty()) {
83 param_info_.add("after", ParamInfo::LATEX_OPTIONAL,
84 ParamInfo::HANDLING_LATEXIFY);
85 param_info_.add("before", ParamInfo::LATEX_OPTIONAL,
86 ParamInfo::HANDLING_LATEXIFY);
87 param_info_.add("key", ParamInfo::LATEX_REQUIRED);
88 param_info_.add("pretextlist", ParamInfo::LATEX_OPTIONAL,
89 ParamInfo::HANDLING_LATEXIFY);
90 param_info_.add("posttextlist", ParamInfo::LATEX_OPTIONAL,
91 ParamInfo::HANDLING_LATEXIFY);
92 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
98 // We allow any command here, since we fall back to cite
99 // anyway if a command is not allowed by a style
100 bool InsetCitation::isCompatibleCommand(string const &)
106 CitationStyle InsetCitation::getCitationStyle(BufferParams const & bp, string const & input,
107 vector<CitationStyle> const & valid_styles) const
109 CitationStyle cs = valid_styles[0];
110 cs.forceUpperCase = false;
111 cs.hasStarredVersion = false;
113 string normalized_input = input;
114 string::size_type const n = input.size() - 1;
115 if (isUpperCase(input[0]))
116 normalized_input[0] = lowercase(input[0]);
118 normalized_input = normalized_input.substr(0, n);
120 string const alias = bp.getCiteAlias(normalized_input);
122 normalized_input = alias;
124 vector<CitationStyle>::const_iterator it = valid_styles.begin();
125 vector<CitationStyle>::const_iterator end = valid_styles.end();
126 for (; it != end; ++it) {
127 CitationStyle this_cs = *it;
128 if (this_cs.name == normalized_input) {
138 void InsetCitation::doDispatch(Cursor & cur, FuncRequest & cmd)
140 switch (cmd.action()) {
141 case LFUN_INSET_EDIT:
144 case LFUN_INSET_MODIFY: {
145 buffer().scheduleBiblioTempRemoval();
146 cache.recalculate = true;
147 if (cmd.getArg(0) == "toggleparam") {
148 string cmdname = getCmdName();
150 buffer().masterParams().getCiteAlias(cmdname);
153 string const par = cmd.getArg(1);
154 string newcmdname = cmdname;
156 if (suffixIs(cmdname, "*"))
157 newcmdname = rtrim(cmdname, "*");
159 newcmdname = cmdname + "*";
160 } else if (par == "casing") {
161 if (isUpperCase(cmdname[0]))
162 newcmdname[0] = lowercase(cmdname[0]);
164 newcmdname[0] = uppercase(newcmdname[0]);
166 cmd = FuncRequest(LFUN_INSET_MODIFY, "changetype " + newcmdname);
171 InsetCommand::doDispatch(cur, cmd);
172 if (cmd.action() == LFUN_INSET_MODIFY)
177 bool InsetCitation::openCitationPossible() const
179 Buffer const & buf = *buffer_;
180 // only after the buffer is loaded from file...
181 if (!buf.isFullyLoaded())
184 BiblioInfo const & bi = buf.masterBibInfo();
188 docstring const & key = getParam("key");
192 // does bibtex item contains some locator?
193 vector<docstring> keys = getVectorFromString(key);
194 docstring doi, url, file;
195 for (docstring const & kvar : keys) {
196 bi.getLocators(kvar, doi, url, file);
197 if (!file.empty() || !doi.empty() || !url.empty())
201 // last resort: is external script activated?
202 return lyxrc.citation_search;
205 void InsetCitation::openCitation()
207 Buffer const & buf = *buffer_;
208 BiblioInfo const & bi = buf.masterBibInfo();
209 docstring const & key = getParam("key");
211 vector<docstring> keys = getVectorFromString(key);
212 docstring titledata, doi, url, file;
213 for (docstring const & kvar : keys) {
215 titledata = bi.getInfo(kvar, buffer(), ci,
216 from_ascii(lyxrc.citation_search_pattern));
217 // some cleanup: commas, " and " and " et al.", as used in name lists,
218 // are not expected in file names
219 titledata = subst(titledata, from_ascii(","), docstring());
220 titledata = subst(titledata, from_ascii(" and "), from_ascii(" "));
221 titledata = subst(titledata, from_ascii(" et al."), docstring());
222 bi.getLocators(kvar, doi, url, file);
223 LYXERR(Debug::INSETS, "Locators: doi:" << doi << " url:"
224 << url << " file:" << file << " title data:" << titledata
225 << " citation search: " << lyxrc.citation_search
226 << " citation search pattern: " << lyxrc.citation_search_pattern);
229 locator = provideScheme(file, from_ascii("file"));
230 } else if (!doi.empty()) {
231 locator = provideScheme(doi, from_ascii("doi"));
232 } else if (!url.empty()) {
235 locator = "EXTERNAL " + titledata;
237 LYXERR(Debug::INSETS, "Resolved locator: " << locator);
238 FuncRequest cmd = FuncRequest(LFUN_CITATION_OPEN, locator);
244 bool InsetCitation::getStatus(Cursor & cur, FuncRequest const & cmd,
245 FuncStatus & status) const
247 switch (cmd.action()) {
248 // Handle the alias case
249 case LFUN_INSET_MODIFY:
250 if (cmd.getArg(0) == "changetype") {
251 string cmdname = getCmdName();
253 buffer().masterParams().getCiteAlias(cmdname);
256 if (suffixIs(cmdname, "*"))
257 cmdname = rtrim(cmdname, "*");
258 string const newtype = cmd.getArg(1);
259 status.setEnabled(isCompatibleCommand(newtype));
260 status.setOnOff(newtype == cmdname);
262 if (cmd.getArg(0) == "toggleparam") {
263 string cmdname = getCmdName();
265 buffer().masterParams().getCiteAlias(cmdname);
268 vector<CitationStyle> citation_styles =
269 buffer().masterParams().citeStyles();
270 CitationStyle cs = getCitationStyle(buffer().masterParams(),
271 cmdname, citation_styles);
272 if (cmd.getArg(1) == "star") {
273 status.setEnabled(cs.hasStarredVersion);
274 status.setOnOff(suffixIs(cmdname, "*"));
276 else if (cmd.getArg(1) == "casing") {
277 status.setEnabled(cs.forceUpperCase);
278 status.setOnOff(isUpperCase(cmdname[0]));
282 case LFUN_INSET_EDIT:
283 return openCitationPossible();
285 return InsetCommand::getStatus(cur, cmd, status);
290 bool InsetCitation::addKey(string const & key)
292 docstring const ukey = from_utf8(trim(key));
293 docstring const & curkeys = getParam("key");
294 if (curkeys.empty()) {
295 setParam("key", ukey);
296 cache.recalculate = true;
300 vector<docstring> keys = getVectorFromString(curkeys);
301 for (auto const & k : keys) {
303 LYXERR0("Key " << key << " already present.");
307 keys.push_back(ukey);
308 setParam("key", getStringFromVector(keys));
309 cache.recalculate = true;
314 docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
316 Buffer const & buf = bv.buffer();
317 // Only after the buffer is loaded from file...
318 if (!buf.isFullyLoaded())
321 BiblioInfo const & bi = buf.masterBibInfo();
323 return _("No bibliography defined!");
325 docstring const & key = getParam("key");
327 return _("No citations selected!");
331 vector<docstring> keys = getVectorFromString(key);
332 if (keys.size() == 1)
333 return bi.getInfo(keys[0], buffer(), ci);
338 for (docstring const & kvar : keys) {
339 docstring const key_info = bi.getInfo(kvar, buffer(), ci);
340 // limit to reasonable size.
341 if (count > 9 && keys.size() > 11) {
342 tip.push_back(0x2026);// HORIZONTAL ELLIPSIS
344 + bformat(_("+ %1$d more entries."), int(keys.size() - count))
348 if (key_info.empty())
350 tip += "<li>" + key_info + "</li>";
360 CitationStyle asValidLatexCommand(BufferParams const & bp, string const & input,
361 vector<CitationStyle> const & valid_styles)
363 CitationStyle cs = valid_styles[0];
364 cs.forceUpperCase = false;
365 cs.hasStarredVersion = false;
367 string normalized_input = input;
368 string::size_type const n = input.size() - 1;
369 if (isUpperCase(input[0]))
370 normalized_input[0] = lowercase(input[0]);
372 normalized_input = normalized_input.substr(0, n);
374 string const alias = bp.getCiteAlias(normalized_input);
376 normalized_input = alias;
378 vector<CitationStyle>::const_iterator it = valid_styles.begin();
379 vector<CitationStyle>::const_iterator end = valid_styles.end();
380 for (; it != end; ++it) {
381 CitationStyle this_cs = *it;
382 if (this_cs.name == normalized_input) {
388 cs.forceUpperCase &= input[0] == uppercase(input[0]);
389 cs.hasStarredVersion &= input[n] == '*';
395 inline docstring wrapCitation(docstring const & key,
396 docstring const & content, bool for_xhtml)
400 // we have to do the escaping here, because we will ultimately
401 // write this as a raw string, so as not to escape the tags.
402 return "<a href='#LyXCite-" + xml::cleanAttr(key) + "'>" +
403 xml::escapeString(content, XMLStream::ESCAPE_ALL) + "</a>";
406 } // anonymous namespace
409 vector<pair<docstring, docstring>> InsetCitation::getQualifiedLists(docstring const & p) const
411 vector<docstring> ps =
412 getVectorFromString(p, from_ascii("\t"));
414 for (docstring const & s: ps) {
417 if (contains(s, ' '))
418 val = split(s, key, ' ');
419 res.push_back(make_pair(key, val));
424 docstring InsetCitation::generateLabel(bool for_xhtml) const
427 label = complexLabel(for_xhtml);
429 // Fallback to fail-safe
431 label = basicLabel(for_xhtml);
437 docstring InsetCitation::complexLabel(bool for_xhtml) const
439 Buffer const & buf = buffer();
440 // Only start the process off after the buffer is loaded from file.
441 if (!buf.isFullyLoaded())
444 docstring const & key = getParam("key");
446 BiblioInfo const & biblist = buf.masterBibInfo();
448 // mark broken citations
451 if (biblist.empty()) {
457 return _("No citations selected!");
459 // check all citations
460 // we only really want the last 'false', to suppress trimming, but
461 // we need to give the other defaults, too, to set it.
462 vector<docstring> keys =
463 getVectorFromString(key, from_ascii(","), false, false);
464 for (auto const & k : keys) {
465 if (biblist.find(k) == biblist.end()) {
471 string cite_type = getCmdName();
472 bool const uppercase = isUpperCase(cite_type[0]);
474 cite_type[0] = lowercase(cite_type[0]);
475 bool const starred = (cite_type[cite_type.size() - 1] == '*');
477 cite_type = cite_type.substr(0, cite_type.size() - 1);
480 string const alias = buf.masterParams().getCiteAlias(cite_type);
484 // FIXME: allow to add cite macros
486 buffer().params().documentClass().addCiteMacro("!textbefore", to_utf8(before));
487 buffer().params().documentClass().addCiteMacro("!textafter", to_utf8(after));
490 CitationStyle cs = getCitationStyle(buffer().masterParams(),
491 cite_type, buffer().masterParams().citeStyles());
492 bool const qualified = cs.hasQualifiedList
494 || !getParam("pretextlist").empty()
495 || !getParam("posttextlist").empty());
496 QualifiedList pres = getQualifiedLists(getParam("pretextlist"));
497 QualifiedList posts = getQualifiedLists(getParam("posttextlist"));
500 ci.textBefore = getParam("before");
501 ci.textAfter = getParam("after");
502 ci.forceUpperCase = uppercase;
503 ci.Starred = starred;
504 ci.max_size = UINT_MAX;
505 ci.isQualified = qualified;
507 ci.posttexts = posts;
509 ci.max_key_size = UINT_MAX;
510 ci.context = CiteItem::Export;
512 ci.richtext = for_xhtml;
513 label = biblist.getLabel(keys, buffer(), cite_type, ci);
518 docstring InsetCitation::basicLabel(bool for_xhtml) const
520 docstring keys = getParam("key");
525 // if there is no comma, then everything goes into key
526 // and keys will be empty.
527 keys = split(keys, key, ',');
530 label += wrapCitation(key, key, for_xhtml);
531 } while (!keys.empty());
533 docstring const & after = getParam("after");
535 label += ", " + after;
537 return '[' + label + ']';
541 bool InsetCitation::forceLTR(OutputParams const & rp) const
543 // We have to force LTR for numeric references
544 // [= bibliography, plain BibTeX, numeric natbib
545 // and biblatex]. Except for XeTeX/bidi. See #3005.
546 if (buffer().masterParams().useBidiPackage(rp))
548 return (buffer().masterParams().citeEngine() == "basic"
549 || buffer().masterParams().citeEngineType() == ENGINE_TYPE_NUMERICAL);
552 docstring InsetCitation::screenLabel() const
554 return cache.screen_label;
558 void InsetCitation::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
560 if (!cache.recalculate && buffer().citeLabelsValid())
562 // The label may have changed, so we have to re-create it.
563 docstring const glabel = generateLabel();
564 cache.recalculate = false;
565 cache.generated_label = glabel;
566 unsigned int const maxLabelChars = 45;
567 cache.screen_label = glabel;
568 support::truncateWithEllipsis(cache.screen_label, maxLabelChars, true);
572 void InsetCitation::addToToc(DocIterator const & cpit, bool output_active,
573 UpdateType, TocBackend & backend) const
576 // BiblioInfo::collectCitedEntries() uses the TOC to collect the citations
577 // from the document. It is used indirectly, via BiblioInfo::makeCitationLables,
578 // by both XHTML and plaintext output. So, if we change what goes into the TOC,
579 // then we will also need to change that routine.
582 tocitem = _("BROKEN: ");
583 tocitem += getParam("key");
584 TocBuilder & b = backend.builder("citation");
585 b.pushItem(cpit, tocitem, output_active);
588 shared_ptr<Toc> toc2 = backend.toc("brokenrefs");
589 toc2->push_back(TocItem(cpit, 0, tocitem, output_active));
594 int InsetCitation::plaintext(odocstringstream & os,
595 OutputParams const &, size_t) const
597 string const & cmd = getCmdName();
601 docstring const label = generateLabel();
607 static docstring const cleanupWhitespace(docstring const & citelist)
609 // Paranoia check: make sure that there is no whitespace in here
610 // -- at least not behind commas or at the beginning
612 char_type last = ',';
613 for (char_type c : citelist) {
616 if (c != ' ' || last != ',')
623 void InsetCitation::cleanKeys() {
624 docstring cleankeys = cleanupWhitespace(getParam("key"));
625 setParam("key", cleankeys);
628 void InsetCitation::docbook(XMLStream & xs, OutputParams const &) const
630 if (getCmdName() == "nocite")
633 // Split the different citations (on ","), so that one tag can be output for each of them.
634 // DocBook does not support having multiple citations in one tag, so that we have to deal with formatting here.
635 docstring citations = getParam("key");
636 if (citations.find(',') == string::npos) {
637 xs << xml::CompTag("biblioref", "linkend=\"" + to_utf8(xml::cleanID(citations)) + "\"");
640 while (pos != string::npos) {
641 pos = citations.find(',');
642 xs << xml::CompTag("biblioref", "linkend=\"" + to_utf8(xml::cleanID(citations.substr(0, pos))) + "\"");
643 citations.erase(0, pos + 1);
645 if (pos != string::npos) {
653 docstring InsetCitation::xhtml(XMLStream & xs, OutputParams const &) const
655 if (getCmdName() == "nocite")
658 // have to output this raw, because generateLabel() will include tags
659 xs << XMLStream::ESCAPE_NONE << generateLabel(true);
665 void InsetCitation::toString(odocstream & os) const
667 odocstringstream ods;
668 plaintext(ods, OutputParams(nullptr));
673 void InsetCitation::forOutliner(docstring & os, size_t const, bool const) const
679 // Have to overwrite the default InsetCommand method in order to check that
680 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
681 // citations and then changes his mind, turning natbib support off. The output
682 // should revert to the default citation command as provided by the citation
683 // engine, e.g. \cite[]{} for the basic engine.
684 void InsetCitation::latex(otexstream & os, OutputParams const & runparams) const
686 // When this is a child compiled on its own, we use the children
687 // own bibinfo, else the master's
688 BiblioInfo const & bi = runparams.is_child
689 ? buffer().masterBibInfo() : buffer().bibInfo();
690 docstring const key = getParam("key");
691 // "keyonly" command: output the plain key and stop.
692 if (getCmdName() == "keyonly") {
693 // Special command to only return the key
694 if (!bi.isBibtex(getParam("key")))
695 // escape chars with bibitems
696 os << escape(cleanupWhitespace(key));
698 os << cleanupWhitespace(key);
701 vector<CitationStyle> citation_styles = buffer().masterParams().citeStyles();
702 CitationStyle cs = asValidLatexCommand(buffer().masterParams(),
703 getCmdName(), citation_styles);
705 docstring const cite_str = from_utf8(citationStyleToString(cs, true));
707 // check if we have to do a qualified list
708 vector<docstring> keys = getVectorFromString(cleanupWhitespace(key));
709 bool const qualified = cs.hasQualifiedList
710 && (!getParam("pretextlist").empty()
711 || !getParam("posttextlist").empty());
713 if (runparams.inulemcmd > 0)
716 os << "\\" << cite_str;
721 ParamInfo const & pinfo = findInfo(string());
722 docstring before = params().prepareCommand(runparams, getParam("before"),
723 pinfo["before"].handling());
724 docstring after = params().prepareCommand(runparams, getParam("after"),
725 pinfo["after"].handling());
726 if (!before.empty() && cs.textBefore) {
728 os << '(' << protectArgument(before, '(', ')')
729 << ")(" << protectArgument(after, '(', ')') << ')';
731 os << '[' << protectArgument(before) << "]["
732 << protectArgument(after) << ']';
733 } else if (!after.empty() && cs.textAfter) {
735 os << '(' << protectArgument(after, '(', ')') << ')';
737 os << '[' << protectArgument(after) << ']';
740 if (!bi.isBibtex(key))
741 // escape chars with bibitems
742 os << '{' << escape(cleanupWhitespace(key)) << '}';
745 QualifiedList pres = getQualifiedLists(getParam("pretextlist"));
746 QualifiedList posts = getQualifiedLists(getParam("posttextlist"));
747 for (docstring const & k : keys) {
749 QualifiedList::iterator it = pres.begin();
750 for (; it != pres.end() ; ++it) {
751 if ((*it).first == k) {
752 prenote = (*it).second;
757 docstring bef = params().prepareCommand(runparams, prenote,
758 pinfo["pretextlist"].handling());
760 QualifiedList::iterator pit = posts.begin();
761 for (; pit != posts.end() ; ++pit) {
762 if ((*pit).first == k) {
763 postnote = (*pit).second;
768 docstring aft = params().prepareCommand(runparams, postnote,
769 pinfo["posttextlist"].handling());
771 os << '[' << protectArgument(bef)
772 << "][" << protectArgument(aft) << ']';
773 else if (!aft.empty())
774 os << '[' << protectArgument(aft) << ']';
775 os << '{' << k << '}';
778 os << '{' << cleanupWhitespace(key) << '}';
781 if (runparams.inulemcmd)
786 pair<int, int> InsetCitation::isWords() const
788 docstring const label = generateLabel(false);
789 return pair<int, int>(label.size(), wordCount(label));
793 string InsetCitation::contextMenuName() const
795 return "context-citation";