]> git.lyx.org Git - features.git/blobdiff - src/insets/InsetRef.cpp
Introduce a labels&references cache at buffer level. This cache uses the already...
[features.git] / src / insets / InsetRef.cpp
index 075344047a87175ced757d52d0348951dd6c92f9..88c76fa5530b5e8a2fe9f48216d884a2f4025a8e 100644 (file)
 #include "Cursor.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
-#include "gettext.h"
 #include "LaTeXFeatures.h"
 #include "LyXFunc.h"
 #include "OutputParams.h"
+#include "ParIterator.h"
 #include "sgml.h"
+#include "TocBackend.h"
 
+#include "support/docstream.h"
+#include "support/gettext.h"
 #include "support/lstrings.h"
 
+using namespace lyx::support;
+using namespace std;
 
 namespace lyx {
 
-using support::escape;
-
-using std::string;
-using std::ostream;
-
 
 InsetRef::InsetRef(InsetCommandParams const & p, Buffer const & buf)
        : InsetCommand(p, "ref"), isLatex(buf.isLatex())
@@ -42,15 +42,38 @@ InsetRef::InsetRef(InsetRef const & ir)
 {}
 
 
+bool InsetRef::isCompatibleCommand(string const & s) {
+       //FIXME This is likely not the best way to handle this.
+       //But this stuff is hardcoded elsewhere already.
+       return s == "ref" 
+               || s == "pageref"
+               || s == "vref" 
+               || s == "vpageref"
+               || s == "prettyref"
+               || s == "eqref";
+}
+
+
+ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
+{
+       static ParamInfo param_info_;
+       if (param_info_.empty()) {
+               param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
+               param_info_.add("reference", ParamInfo::LATEX_REQUIRED);
+       }
+       return param_info_;
+}
+
+
 void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
        case LFUN_MOUSE_RELEASE:
                // Eventually trigger dialog with button 3 not 1
                if (cmd.button() == mouse_button::button3)
-                       lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO, 
+                       lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO,
                                                  getParam("reference")));
-               else 
+               else
                        InsetCommand::doDispatch(cur, cmd);
                break;
 
@@ -60,7 +83,7 @@ void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
 }
 
 
-docstring const InsetRef::getScreenLabel(Buffer const &) const
+docstring InsetRef::screenLabel() const
 {
        docstring temp;
        for (int i = 0; !types[i].latex_name.empty(); ++i) {
@@ -79,19 +102,18 @@ docstring const InsetRef::getScreenLabel(Buffer const &) const
 }
 
 
-int InsetRef::latex(Buffer const &, odocstream & os,
-                    OutputParams const &) const
+int InsetRef::latex(odocstream & os, OutputParams const &) const
 {
-       // Don't output p_["name"], this is only used in docbook
-       InsetCommandParams p(getCmdName());
+       // We don't want to output p_["name"], since that is only used 
+       // in docbook. So we construct new params, without it, and use that.
+       InsetCommandParams p(REF_CODE, getCmdName());
        p["reference"] = getParam("reference");
        os << escape(p.getCommand());
        return 0;
 }
 
 
-int InsetRef::plaintext(Buffer const &, odocstream & os,
-                        OutputParams const &) const
+int InsetRef::plaintext(odocstream & os, OutputParams const &) const
 {
        docstring const str = getParam("reference");
        os << '[' << str << ']';
@@ -99,24 +121,23 @@ int InsetRef::plaintext(Buffer const &, odocstream & os,
 }
 
 
-int InsetRef::docbook(Buffer const & buf, odocstream & os,
-                      OutputParams const & runparams) const
+int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
 {
        docstring const & name = getParam("name");
        if (name.empty()) {
                if (runparams.flavor == OutputParams::XML) {
-                       os << "<xref linkend=\"" 
-                          << sgml::cleanID(buf, runparams, getParam("reference")) 
+                       os << "<xref linkend=\""
+                          << sgml::cleanID(buffer(), runparams, getParam("reference"))
                           << "\" />";
                } else {
-                       os << "<xref linkend=\"" 
-                          << sgml::cleanID(buf, runparams, getParam("reference")) 
+                       os << "<xref linkend=\""
+                          << sgml::cleanID(buffer(), runparams, getParam("reference"))
                           << "\">";
                }
        } else {
-               os << "<link linkend=\"" 
-                  << sgml::cleanID(buf, runparams, getParam("reference"))
-                  << "\">" 
+               os << "<link linkend=\""
+                  << sgml::cleanID(buffer(), runparams, getParam("reference"))
+                  << "\">"
                   << getParam("name")
                   << "</link>";
        }
@@ -125,10 +146,29 @@ int InsetRef::docbook(Buffer const & buf, odocstream & os,
 }
 
 
-int InsetRef::textString(Buffer const & buf, odocstream & os,
-                      OutputParams const & op) const
+void InsetRef::textString(odocstream & os) const
+{
+       plaintext(os, OutputParams(0));
+}
+
+
+void InsetRef::updateLabels(ParIterator const & it)
+{
+       docstring const & label = getParam("reference");
+       buffer().references(label).push_back(make_pair(this, it));
+}
+
+
+void InsetRef::addToToc(ParConstIterator const & cpit) const
 {
-       return plaintext(buf, os, op);
+       docstring const & label = getParam("reference");
+       if (buffer().insetLabel(label))
+               // This InsetRef has already been taken care of in InsetLabel::addToToc().
+               return;
+
+       Toc & toc = buffer().tocBackend().toc("label");
+       docstring const reflabel = _("BROKEN: ") + screenLabel();
+       toc.push_back(TocItem(cpit, 0, reflabel));
 }