]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetRef.cpp
Improve handling of top and bottom margin
[lyx.git] / src / insets / InsetRef.cpp
index e8eecedde90d8dc9bf6295e0dd1e358b223ff53e..d41f0a1165a72ffbcec34e2c40c1f7a69716b048 100644 (file)
@@ -22,8 +22,9 @@
 #include "LyX.h"
 #include "OutputParams.h"
 #include "output_xhtml.h"
+#include "Paragraph.h"
 #include "ParIterator.h"
-#include "sgml.h"
+#include "xml.h"
 #include "texstream.h"
 #include "TocBackend.h"
 
@@ -40,12 +41,12 @@ namespace lyx {
 
 
 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
-       : InsetCommand(buf, p), broken_(false)
+       : InsetCommand(buf, p), broken_(false), active_(true)
 {}
 
 
 InsetRef::InsetRef(InsetRef const & ir)
-       : InsetCommand(ir), broken_(false)
+       : InsetCommand(ir), broken_(false), active_(true)
 {}
 
 
@@ -79,6 +80,28 @@ ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
 }
 
 
+docstring InsetRef::layoutName() const
+{
+       return from_ascii("Ref");
+}
+
+
+void InsetRef::changeTarget(docstring const & new_label)
+{
+       // With change tracking, we insert a new ref
+       // and delete the old one
+       if (buffer().masterParams().track_changes) {
+               InsetCommandParams icp(REF_CODE, "ref");
+               icp["reference"] = new_label;
+               string const data = InsetCommand::params2string(icp);
+               lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
+               lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
+       } else
+               setParam("reference", new_label);
+}
+
+
+
 void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        string const inset = cmd.getArg(0);
@@ -91,6 +114,15 @@ void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
                        pstring = "caps";
                else if (arg == "toggle-noprefix")
                        pstring = "noprefix";
+               else if (arg == "changetarget") {
+                       string const oldtarget = cmd.getArg(2);
+                       string const newtarget = cmd.getArg(3);
+                       if (!oldtarget.empty() && !newtarget.empty()
+                           && getParam("reference") == from_utf8(oldtarget))
+                               changeTarget(from_utf8(newtarget));
+                       cur.forceBufferUpdate();
+                       return;
+               }
        }
        // otherwise not for us
        if (pstring.empty())
@@ -111,6 +143,8 @@ bool InsetRef::getStatus(Cursor & cur, FuncRequest const & cmd,
 
        string const arg = cmd.getArg(1);
        string pstring;
+       if (arg == "changetarget")
+               return true;
        if (arg == "toggle-plural")
                pstring = "plural";
        else if (arg == "toggle-caps")
@@ -253,8 +287,6 @@ void InsetRef::latex(otexstream & os, OutputParams const & rp) const
                }
        }
        else {
-               // 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, cmd);
                docstring const ref = getParam("reference");
                p["reference"] = ref;
@@ -275,32 +307,63 @@ int InsetRef::plaintext(odocstringstream & os,
 }
 
 
-int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
+void InsetRef::docbook(XMLStream & xs, OutputParams const &) const
 {
+       docstring const & ref = getParam("reference");
+       InsetLabel const * il = buffer().insetLabel(ref, true);
+       string const & cmd = params().getCmdName();
+       docstring linkend = xml::cleanID(ref);
+
+       // A name is provided, LyX will provide it. This is supposed to be a very rare case.
+       // Link with linkend, as is it within the document (not outside, in which case xlink:href is better suited).
        docstring const & name = getParam("name");
-       if (name.empty()) {
-               if (runparams.flavor == OutputParams::XML) {
-                       os << "<xref linkend=\""
-                          << sgml::cleanID(buffer(), runparams, getParam("reference"))
-                          << "\" />";
-               } else {
-                       os << "<xref linkend=\""
-                          << sgml::cleanID(buffer(), runparams, getParam("reference"))
-                          << "\">";
+       if (!name.empty()) {
+               docstring attr = from_utf8("linkend=\"") + linkend + from_utf8("\"");
+
+               xs << xml::StartTag("link", to_utf8(attr));
+               xs << name;
+               xs << xml::EndTag("link");
+               return;
+       }
+
+       // The DocBook processor will generate the name when required.
+       docstring display_before;
+       docstring display_after;
+       docstring role;
+
+       if (il && !il->counterValue().empty()) {
+               // Try to construct a label from the InsetLabel we reference.
+               if (cmd == "vref" || cmd == "pageref" || cmd == "vpageref" || cmd == "nameref" || cmd == "formatted") {
+                       // "ref on page #", "on page #", etc. The DocBook processor deals with generating the right text,
+                       // including in the right language.
+                       role = from_ascii(cmd);
+
+                       if (cmd == "formatted") {
+                               // A formatted reference may have many parameters. Generate all of them as roles, the only
+                               // way arbitrary parameters can be passed into DocBook.
+                               if (buffer().params().use_refstyle && getParam("caps") == "true")
+                                       role += " refstyle-caps";
+                               if (buffer().params().use_refstyle && getParam("plural") == "true")
+                                       role += " refstyle-plural";
+                       }
+               } else if (cmd == "eqref") {
+                       display_before = from_ascii("(");
+                       display_after = from_ascii(")");
                }
-       } else {
-               os << "<link linkend=\""
-                  << sgml::cleanID(buffer(), runparams, getParam("reference"))
-                  << "\">"
-                  << getParam("name")
-                  << "</link>";
+               // TODO: what about labelonly? I don't get how this is supposed to work...
        }
 
-       return 0;
+       // No name, ask DocBook to generate one.
+       docstring attr = from_utf8("linkend=\"") + ref + from_utf8("\"");
+       if (!role.empty())
+               attr += " role=\"" + role + "\"";
+       xs << display_before;
+       xs << xml::CompTag("xref", to_utf8(attr));
+       xs << display_after;
 }
 
 
-docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const & op) const
+docstring InsetRef::xhtml(XMLStream & xs, OutputParams const & op) const
 {
        docstring const & ref = getParam("reference");
        InsetLabel const * il = buffer().insetLabel(ref, true);
@@ -341,10 +404,10 @@ docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const & op) const
        // FIXME What we'd really like to do is to be able to output some
        // appropriate sort of text here. But to do that, we need to associate
        // some sort of counter with the label, and we don't have that yet.
-       docstring const attr = "href=\"#" + html::cleanAttr(ref) + '"';
-       xs << html::StartTag("a", to_utf8(attr));
+       docstring const attr = "href=\"#" + xml::cleanAttr(ref) + '"';
+       xs << xml::StartTag("a", to_utf8(attr));
        xs << display_string;
-       xs << html::EndTag("a");
+       xs << xml::EndTag("a");
        return docstring();
 }
 
@@ -365,9 +428,26 @@ void InsetRef::forOutliner(docstring & os, size_t const, bool const) const
 }
 
 
-void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
+void InsetRef::updateBuffer(ParIterator const & it, UpdateType, bool const /*deleted*/)
 {
        docstring const & ref = getParam("reference");
+
+       // Check if this one is active (i.e., neither deleted with change-tracking
+       // nor in an inset that does not produce output, such as notes or inactive branches)
+       Paragraph const & para = it.paragraph();
+       active_ = !para.isDeleted(it.pos()) && para.inInset().producesOutput();
+       // If not, check whether we are in a deleted/non-outputting inset
+       if (active_) {
+               for (size_type sl = 0 ; sl < it.depth() ; ++sl) {
+                       Paragraph const & outer_par = it[sl].paragraph();
+                       if (outer_par.isDeleted(it[sl].pos())
+                           || !outer_par.inInset().producesOutput()) {
+                               active_ = false;
+                               break;
+                       }
+               }
+       }
+
        // register this inset into the buffer reference cache.
        buffer().addReference(ref, this, it);
 
@@ -410,6 +490,7 @@ void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
 
        screen_label_ = label;
        broken_ = false;
+       setBroken(broken_);
 }
 
 
@@ -420,20 +501,28 @@ docstring InsetRef::screenLabel() const
 
 
 void InsetRef::addToToc(DocIterator const & cpit, bool output_active,
-                                               UpdateType, TocBackend & backend) const
+                       UpdateType, TocBackend & backend) const
 {
+       active_ = output_active;
        docstring const & label = getParam("reference");
        if (buffer().insetLabel(label)) {
                broken_ = !buffer().activeLabel(label);
+               setBroken(broken_);
+               if (broken_ && output_active) {
+                       shared_ptr<Toc> toc2 = backend.toc("brokenrefs");
+                       toc2->push_back(TocItem(cpit, 0, screenLabel(), output_active));
+               }
                // This InsetRef has already been taken care of in InsetLabel::addToToc().
                return;
        }
 
        // It seems that this reference does not point to any valid label.
-
        broken_ = true;
+       setBroken(broken_);
        shared_ptr<Toc> toc = backend.toc("label");
        toc->push_back(TocItem(cpit, 0, screenLabel(), output_active));
+       shared_ptr<Toc> toc2 = backend.toc("brokenrefs");
+       toc2->push_back(TocItem(cpit, 0, screenLabel(), output_active));
 }
 
 
@@ -471,11 +560,16 @@ void InsetRef::validate(LaTeXFeatures & features) const
                features.require("nameref");
 }
 
-bool InsetRef::forceLTR() const
+bool InsetRef::forceLTR(OutputParams const & rp) const
 {
-       // We force LTR for references. Namerefs are output in the scripts direction
-       // at least with fontspec/bidi, though (see #11518).
+       // We force LTR for references. However,
+       // * Namerefs are output in the scripts direction
+       //   at least with fontspec/bidi and luabidi, though (see #11518).
+       // * Parentheses are automatically swapped with XeTeX/bidi 
+       //   [not with LuaTeX/luabidi] (see #11626).
        // FIXME: Re-Audit all other RTL cases.
+       if (rp.useBidiPackage())
+               return false;
        return (getCmdName() != "nameref" || !buffer().masterParams().useNonTeXFonts);
 }
 
@@ -495,6 +589,11 @@ InsetRef::type_info const InsetRef::types[] = {
 
 docstring InsetRef::getTOCString() const
 {
+       docstring const & label = getParam("reference");
+       if (buffer().insetLabel(label))
+               broken_ = !buffer().activeLabel(label) && active_;
+       else 
+               broken_ = active_;
        return tooltip_.empty() ? screenLabel() : tooltip_;
 }