]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetCitation.cpp
Make sure we center displayed equations.
[lyx.git] / src / insets / InsetCitation.cpp
index d5c43535b101a79a706b559e066b25b8de12f90e..4bf6333f2791bd4dd56d2b7a1d0efd8a9e64440d 100644 (file)
@@ -33,6 +33,7 @@
 #include "support/lstrings.h"
 
 #include <algorithm>
+#include <climits>
 
 using namespace std;
 using namespace lyx::support;
@@ -44,7 +45,16 @@ ParamInfo InsetCitation::param_info_;
 
 InsetCitation::InsetCitation(Buffer * buf, InsetCommandParams const & p)
        : InsetCommand(buf, p)
-{}
+{
+       buffer().removeBiblioTempFiles();
+}
+
+
+InsetCitation::~InsetCitation()
+{
+       if (isBufferLoaded())
+               buffer().removeBiblioTempFiles();
+}
 
 
 ParamInfo const & InsetCitation::findInfo(string const & /* cmdName */)
@@ -105,12 +115,40 @@ bool InsetCitation::isCompatibleCommand(string const & cmd)
 
 void InsetCitation::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
-       if (cmd.action() == LFUN_INSET_MODIFY)
+       if (cmd.action() == LFUN_INSET_MODIFY) {
+               buffer().removeBiblioTempFiles();
                cache.recalculate = true;
+       }
        InsetCommand::doDispatch(cur, cmd);
 }
 
 
+bool InsetCitation::addKey(string const & key)
+{
+       docstring const ukey = from_utf8(key);
+       docstring const & curkeys = getParam("key");
+       if (curkeys.empty()) {
+               setParam("key", ukey);
+               cache.recalculate = true;
+               return true;
+       }
+
+       vector<docstring> keys = getVectorFromString(curkeys);
+       vector<docstring>::const_iterator it = keys.begin();
+       vector<docstring>::const_iterator en = keys.end();
+       for (; it != en; ++it) {
+               if (*it == ukey) {
+                       LYXERR0("Key " << key << " already present.");
+                       return false;
+               }
+       }
+       keys.push_back(ukey);
+       setParam("key", getStringFromVector(keys));
+       cache.recalculate = true;
+       return true;
+}
+
+
 docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
 {
        Buffer const & buf = bv.buffer();
@@ -145,7 +183,7 @@ docstring InsetCitation::toolTip(BufferView const & bv, int, int) const
 namespace {
 
 
-CitationStyle asValidLatexCommand(string const & input, vector<CitationStyle> const valid_styles)
+CitationStyle asValidLatexCommand(string const & input, vector<CitationStyle> const valid_styles)
 {
        CitationStyle cs = valid_styles[0];
        cs.forceUpperCase = false;
@@ -184,7 +222,7 @@ inline docstring wrapCitation(docstring const & key,
                return content;
        // we have to do the escaping here, because we will ultimately
        // write this as a raw string, so as not to escape the tags.
-       return "<a href='#LyXCite-" + key + "'>" +
+       return "<a href='#LyXCite-" + html::cleanAttr(key) + "'>" +
                        html::htmlize(content, XHTMLStream::ESCAPE_ALL) + "</a>";
 }
 
@@ -236,8 +274,8 @@ docstring InsetCitation::complexLabel(bool for_xhtml) const
        buffer().params().documentClass().addCiteMacro("!textafter", to_utf8(after));
        */
        docstring label;
-       vector<docstring> const keys = getVectorFromString(key);
-       label = biblist.getLabel(keys, buffer(), cite_type, for_xhtml, before, after);
+       vector<docstring> keys = getVectorFromString(key);
+       label = biblist.getLabel(keys, buffer(), cite_type, for_xhtml, UINT_MAX, before, after);
        return label;
 }
 
@@ -275,40 +313,40 @@ void InsetCitation::updateBuffer(ParIterator const &, UpdateType)
 {
        if (!cache.recalculate && buffer().citeLabelsValid())
                return;
-
        // The label may have changed, so we have to re-create it.
        docstring const glabel = generateLabel();
-
-       unsigned int const maxLabelChars = 45;
-
-       docstring label = glabel;
-       if (label.size() > maxLabelChars) {
-               label.erase(maxLabelChars - 3);
-               label += "...";
-       }
-
        cache.recalculate = false;
        cache.generated_label = glabel;
-       cache.screen_label = label;
+       unsigned int const maxLabelChars = 45;
+       cache.screen_label = glabel.substr(0, maxLabelChars + 1);
+       support::truncateWithEllipsis(cache.screen_label, maxLabelChars);
 }
 
 
-void InsetCitation::addToToc(DocIterator const & cpit) const
+void InsetCitation::addToToc(DocIterator const & cpit, bool output_active,
+                                                        UpdateType) const
 {
        // NOTE
-       // XHTML output uses the TOC to collect the citations
-       // from the document. So if this gets changed, then we
-       // will need to change how the citations are collected.
+       // BiblioInfo::collectCitedEntries() uses the TOC to collect the citations 
+       // from the document. It is used indirectly, via BiblioInfo::makeCitationLables,
+       // by both XHTML and plaintext output. So, if we change what goes into the TOC,
+       // then we will also need to change that routine.
        docstring const tocitem = getParam("key");
-       Toc & toc = buffer().tocBackend().toc("citation");
-       toc.push_back(TocItem(cpit, 0, tocitem));
+       shared_ptr<Toc> toc = buffer().tocBackend().toc("citation");
+       toc->push_back(TocItem(cpit, 0, tocitem, output_active));
 }
 
 
-int InsetCitation::plaintext(odocstream & os, OutputParams const &) const
+int InsetCitation::plaintext(odocstringstream & os,
+       OutputParams const &, size_t) const
 {
-       os << cache.generated_label;
-       return cache.generated_label.size();
+       string const & cmd = getCmdName();
+       if (cmd == "nocite")
+               return 0;
+
+       docstring const label = generateLabel(false);
+       os << label;
+       return label.size();
 }
 
 
@@ -354,11 +392,13 @@ docstring InsetCitation::xhtml(XHTMLStream & xs, OutputParams const &) const
 
 void InsetCitation::toString(odocstream & os) const
 {
-       plaintext(os, OutputParams(0));
+       odocstringstream ods;
+       plaintext(ods, OutputParams(0));
+       os << ods.str();
 }
 
 
-void InsetCitation::forToc(docstring & os, size_t) const
+void InsetCitation::forOutliner(docstring & os, size_t const, bool const) const
 {
        os += screenLabel();
 }
@@ -377,7 +417,7 @@ void InsetCitation::latex(otexstream & os, OutputParams const & runparams) const
        // FIXME UNICODE
        docstring const cite_str = from_utf8(citationStyleToString(cs));
 
-       if (runparams.inulemcmd)
+       if (runparams.inulemcmd > 0)
                os << "\\mbox{";
 
        os << "\\" << cite_str;