]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetCitation.cpp
Routines for calculating numerical labels for BibTeX citations.
[lyx.git] / src / insets / InsetCitation.cpp
index c41cd370eff2cfd9d69f2d53708e5d652bed8ef3..088b671ec3d35624c60b94fc2a4121c6d29438e7 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Angus Leeming
- * \author Herbert Voß
+ * \author Herbert Voß
  *
  * Full author contact details are available in file CREDITS.
  */
 #include "Buffer.h"
 #include "buffer_funcs.h"
 #include "BufferParams.h"
+#include "BufferView.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
 #include "LaTeXFeatures.h"
+#include "output_xhtml.h"
 #include "ParIterator.h"
 #include "TocBackend.h"
 
@@ -94,13 +96,16 @@ string asValidLatexCommand(string const & input, CiteEngine const engine)
        string output;
        switch (engine) {
                case ENGINE_BASIC:
-                       output = input;
+                       if (input == "nocite")
+                               output = input;
+                       else
+                               output = default_str;
                        break;
 
                case ENGINE_NATBIB_AUTHORYEAR:
                case ENGINE_NATBIB_NUMERICAL:
-                       if (input == "cite" || input == "citefield" ||
-                                                       input == "citetitle" || input == "cite*")
+                       if (input == "cite" || input == "citefield"
+                           || input == "citetitle" || input == "cite*")
                                output = default_str;
                        else if (prefixIs(input, "foot"))
                                output = input.substr(4);
@@ -155,10 +160,10 @@ docstring complexLabel(Buffer const & buffer,
        // We don't currently use the full or forceUCase fields.
        string cite_type = asValidLatexCommand(citeType, engine);
        if (cite_type[0] == 'C')
-               //If we were going to use them, this would mean ForceUCase
+               // If we were going to use them, this would mean ForceUCase
                cite_type = string(1, 'c') + cite_type.substr(1);
        if (cite_type[cite_type.size() - 1] == '*')
-               //and this would mean FULL
+               // and this would mean FULL
                cite_type = cite_type.substr(0, cite_type.size() - 1);
 
        docstring before_str;
@@ -191,7 +196,7 @@ docstring complexLabel(Buffer const & buffer,
 
        // One day, these might be tunable (as they are in BibTeX).
        char op, cp;    // opening and closing parenthesis.
-       char * sep;     // punctuation mark separating citation entries.
+       const char * sep;       // punctuation mark separating citation entries.
        if (engine == ENGINE_BASIC) {
                op  = '[';
                cp  = ']';
@@ -351,8 +356,8 @@ docstring basicLabel(docstring const & keyList, docstring const & after)
 ParamInfo InsetCitation::param_info_;
 
 
-InsetCitation::InsetCitation(InsetCommandParams const & p)
-       : InsetCommand(p, "citation")
+InsetCitation::InsetCitation(Buffer * buf, InsetCommandParams const & p)
+       : InsetCommand(buf, p, "citation")
 {}
 
 
@@ -379,10 +384,42 @@ bool InsetCitation::isCompatibleCommand(string const & cmd)
 }
 
 
+docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
+{
+       Buffer const & buf = bv.buffer();
+       // Only after the buffer is loaded from file...
+       if (!buf.isFullyLoaded())
+               return docstring();
+
+       BiblioInfo const & bi = buf.masterBibInfo();
+       if (bi.empty())
+               return _("No bibliography defined!");
+
+       docstring const & key = getParam("key");
+       if (key.empty())
+               return _("No citations selected!");
+
+       vector<docstring> keys = getVectorFromString(key);
+       vector<docstring>::const_iterator it = keys.begin();
+       vector<docstring>::const_iterator en = keys.end();
+       docstring tip;
+       for (; it != en; ++it) {
+               docstring const key_info = bi.getInfo(*it);
+               if (key_info.empty())
+                       continue;
+               if (!tip.empty())
+                       tip += "\n";
+               tip += wrap(key_info, -4);
+       }
+       return tip;
+}
+
+
+
 docstring InsetCitation::generateLabel() const
 {
-       docstring const before = getParam("before");
-       docstring const after  = getParam("after");
+       docstring const before = getParam("before");
+       docstring const after  = getParam("after");
 
        docstring label;
        CiteEngine const engine = buffer().params().citeEngine();
@@ -403,7 +440,7 @@ docstring InsetCitation::screenLabel() const
 }
 
 
-void InsetCitation::updateLabels(ParIterator const &)
+void InsetCitation::updateLabels(ParIterator const &, bool)
 {
        CiteEngine const engine = buffer().params().citeEngine();
        if (cache.params == params() && cache.engine == engine)
@@ -427,10 +464,10 @@ void InsetCitation::updateLabels(ParIterator const &)
 }
 
 
-void InsetCitation::addToToc(ParConstIterator const & cpit) const
+void InsetCitation::addToToc(DocIterator const & cpit)
 {
        Toc & toc = buffer().tocBackend().toc("citation");
-       toc.push_back(TocItem(cpit, 0, cache.screen_label));
+       toc.push_back(TocItem(cpit, 0, getParam("key")));
 }
 
 
@@ -468,7 +505,49 @@ int InsetCitation::docbook(odocstream & os, OutputParams const &) const
 }
 
 
-void InsetCitation::textString(odocstream & os) const
+docstring InsetCitation::xhtml(XHTMLStream & xs, OutputParams const &) const
+{
+       BiblioInfo const & bi = buffer().masterBibInfo();
+       docstring const & key_list = getParam("key");
+       if (key_list.empty())
+               return docstring();
+
+       // FIXME We should do a better job outputing different things for the
+       // different citation styles.   For now, we use square brackets for every
+       // case.
+       xs << "[";
+       docstring const & before = getParam("before");
+       if (!before.empty())
+               xs << before << " ";
+
+       vector<docstring> const keys = getVectorFromString(key_list);
+       vector<docstring>::const_iterator it = keys.begin();
+       vector<docstring>::const_iterator const en = keys.end();
+       bool first = true;
+       for (; it != en; ++it) {
+               BiblioInfo::const_iterator const bt = bi.find(*it);
+               if (bt == bi.end())
+                       continue;
+               BibTeXInfo const & bibinfo = bt->second;
+               if (!first) {
+                       xs << ", ";
+                       first = false;
+               }
+               docstring const & label = bibinfo.label();
+               docstring const & target = label.empty() ? *it : label;
+               string const attr = "href='#" + to_utf8(*it) + "'";
+               xs << StartTag("a", attr) << target << EndTag("a");
+       }
+
+       docstring const & after = getParam("after");
+       if (!after.empty())
+               xs << ", " << after;
+       xs << "]";
+       return docstring();
+}
+
+
+void InsetCitation::tocString(odocstream & os) const
 {
        plaintext(os, OutputParams(0));
 }
@@ -478,13 +557,16 @@ void InsetCitation::textString(odocstream & os) const
 // the \cite command is valid. Eg, the user has natbib enabled, inputs some
 // citations and then changes his mind, turning natbib support off. The output
 // should revert to \cite[]{}
-int InsetCitation::latex(odocstream & os, OutputParams const &) const
+int InsetCitation::latex(odocstream & os, OutputParams const & runparams) const
 {
        CiteEngine cite_engine = buffer().params().citeEngine();
        // FIXME UNICODE
        docstring const cite_str = from_utf8(
                asValidLatexCommand(getCmdName(), cite_engine));
 
+       if (runparams.inulemcmd)
+               os << "\\mbox{";
+
        os << "\\" << cite_str;
 
        docstring const & before = getParam("before");
@@ -496,6 +578,9 @@ int InsetCitation::latex(odocstream & os, OutputParams const &) const
 
        os << '{' << cleanupWhitespace(getParam("key")) << '}';
 
+       if (runparams.inulemcmd)
+               os << "}";
+
        return 0;
 }