]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathRef.cpp
typo
[lyx.git] / src / mathed / InsetMathRef.cpp
index 8ba745fdc4dc1b6062a93d4c282951fa281e7300..b70b7eae5e7b6c17c64ec2bc4bfc2ca834dbd004 100644 (file)
@@ -3,7 +3,7 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author André Pönitz
+ * \author André Pönitz
  *
  * Full author contact details are available in file CREDITS.
  */
 
 #include "InsetMathRef.h"
 
+#include "BufferParams.h"
 #include "BufferView.h"
-#include "LaTeXFeatures.h"
 #include "Buffer.h"
 #include "Cursor.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
+#include "LaTeXFeatures.h"
+#include "LyX.h"
 #include "MathData.h"
 #include "MathFactory.h"
+#include "MathStream.h"
 #include "MathSupport.h"
-#include "OutputParams.h"
-#include "sgml.h"
+#include "ParIterator.h"
+#include "xml.h"
 
 #include "insets/InsetCommand.h"
+#include "insets/InsetRef.h"
 
 #include "support/debug.h"
 #include "support/gettext.h"
+#include "support/lstrings.h"
+#include "support/textutils.h"
 
 #include <ostream>
 
@@ -35,13 +41,13 @@ using namespace std;
 
 namespace lyx {
 
-InsetMathRef::InsetMathRef()
-       : CommandInset(from_ascii("ref"), false)
+InsetMathRef::InsetMathRef(Buffer * buf)
+       : InsetMathCommand(buf, from_ascii("ref"), false)
 {}
 
 
-InsetMathRef::InsetMathRef(docstring const & data)
-       : CommandInset(data, false)
+InsetMathRef::InsetMathRef(Buffer * buf, docstring const & data)
+       : InsetMathCommand(buf, data, false)
 {}
 
 
@@ -59,46 +65,94 @@ void InsetMathRef::infoize(odocstream & os) const
 
 void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
-       switch (cmd.action) {
-       case LFUN_INSET_MODIFY:
-               if (cmd.getArg(0) == "ref") {
+       // Ctrl + click: go to label
+       if (cmd.action() == LFUN_MOUSE_RELEASE && cmd.modifier() == ControlModifier) {
+               LYXERR0("trying to goto ref '" << to_utf8(asString(cell(0))) << "'");
+               //FIXME: use DispatchResult argument
+               lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO, asString(cell(0))));
+               return;
+       }
+
+       switch (cmd.action()) {
+       case LFUN_INSET_MODIFY: {
+               string const arg0 = cmd.getArg(0);
+               string const arg1   = cmd.getArg(1);
+               if (arg0 == "ref") {
+                       if (arg1 == "changetarget") {
+                               string const oldtarget = cmd.getArg(2);
+                               string const newtarget = cmd.getArg(3);
+                               if (!oldtarget.empty() && !newtarget.empty()
+                                   && asString(cell(0)) == from_utf8(oldtarget))
+                                       changeTarget(from_utf8(newtarget));
+                               cur.forceBufferUpdate();
+                               break;
+                       }
                        MathData ar;
                        if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
+                               cur.recordUndo();
+                               Buffer & buf = buffer();
                                *this = *ar[0].nucleus()->asRefInset();
+                               setBuffer(buf);
+                               break;
+                       }
+               } else if (arg0 == "changetype") {
+                       docstring const data = from_ascii(createDialogStr(arg1));
+                       MathData ar;
+                       if (createInsetMath_fromDialogStr(data, ar)) {
+                               cur.recordUndo();
+                               Buffer & buf = buffer();
+                               *this = *ar[0].nucleus()->asRefInset();
+                               setBuffer(buf);
                                break;
                        }
                }
                cur.undispatched();
                break;
+       }
 
        case LFUN_INSET_DIALOG_UPDATE: {
-               string const data = createDialogStr("ref");
+               string const data = createDialogStr();
                cur.bv().updateDialog("ref", data);
                break;
        }
 
+       case LFUN_INSET_SETTINGS: {
+               string const data = createDialogStr();
+               cur.bv().showDialog("ref", data, this);
+               cur.dispatched();
+               break;
+       }
+
        case LFUN_MOUSE_RELEASE:
-               if (cmd.button() == mouse_button::button3) {
-                       LYXERR0("trying to goto ref '" << to_utf8(asString(cell(0))) << "'");
-                       cur.bv().dispatch(FuncRequest(LFUN_LABEL_GOTO, asString(cell(0))));
+               if (cur.selection()) {
+                       cur.undispatched();
                        break;
                }
                if (cmd.button() == mouse_button::button1) {
-                       // Eventually trigger dialog with button 3, not 1
-                       string const data = createDialogStr("ref");
+                       string const data = createDialogStr();
                        cur.bv().showDialog("ref", data, this);
                        break;
                }
                cur.undispatched();
                break;
 
-       case LFUN_MOUSE_PRESS:
-       case LFUN_MOUSE_MOTION:
+       case LFUN_MOUSE_PRESS: {
+               bool do_selection = cmd.button() == mouse_button::button1
+                       && cmd.modifier() == ShiftModifier;
+               // For some reason the cursor points inside the first cell, which is not
+               // active.
+               cur.leaveInset(*this);
+               cur.bv().mouseSetCursor(cur, do_selection);
+               break;
+       }
+
+       case LFUN_MOUSE_DOUBLE:
+       case LFUN_MOUSE_TRIPLE:
                // eat other mouse commands
                break;
 
        default:
-               CommandInset::doDispatch(cur, cmd);
+               InsetMathCommand::doDispatch(cur, cmd);
                break;
        }
 }
@@ -107,17 +161,19 @@ void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd)
 bool InsetMathRef::getStatus(Cursor & cur, FuncRequest const & cmd,
                         FuncStatus & status) const
 {
-       switch (cmd.action) {
+       switch (cmd.action()) {
        // we handle these
        case LFUN_INSET_MODIFY:
        case LFUN_INSET_DIALOG_UPDATE:
+       case LFUN_INSET_SETTINGS:
        case LFUN_MOUSE_RELEASE:
        case LFUN_MOUSE_PRESS:
-       case LFUN_MOUSE_MOTION:
+       case LFUN_MOUSE_DOUBLE:
+       case LFUN_MOUSE_TRIPLE:
                status.setEnabled(true);
                return true;
        default:
-               return CommandInset::getStatus(cur, cmd, status);
+               return InsetMathCommand::getStatus(cur, cmd, status);
        }
 }
 
@@ -143,43 +199,171 @@ docstring const InsetMathRef::screenLabel() const
 
 void InsetMathRef::validate(LaTeXFeatures & features) const
 {
+       // This really should not happen here but does.
+       if (!buffer_) {
+               LYXERR0("Unassigned buffer_ in InsetMathRef::write!");
+               LYXERR0("LaTeX output may be wrong!");
+       }
+       bool const use_refstyle =
+               buffer_ && buffer().params().use_refstyle;
+
        if (commandname() == "vref" || commandname() == "vpageref")
                features.require("varioref");
-       else if (commandname() == "prettyref")
-               features.require("prettyref");
-       else if (commandname() == "eqref")
+       else if (commandname() == "formatted") {
+               if (use_refstyle)
+                       features.require("refstyle");
+               else
+                       features.require("prettyref");
+       }
+       // if eqref is used with refstyle, we do our own output
+       else if (commandname() == "eqref" && use_refstyle)
                features.require("amsmath");
+       else if (commandname() == "nameref")
+               features.require("nameref");
 }
 
 
-int InsetMathRef::docbook(odocstream & os, OutputParams const & runparams) const
+void InsetMathRef::docbook(XMLStream & xs, OutputParams const &) const
 {
        if (cell(1).empty()) {
-               os << "<xref linkend=\""
-                  << sgml::cleanID(buffer(), runparams, asString(cell(0)));
-               if (runparams.flavor == OutputParams::XML)
-                       os << "\"/>";
-               else
-                       os << "\">";
+               docstring attr = from_utf8("linkend=\"") + xml::cleanID(asString(cell(0))) + from_utf8("\"");
+               xs << xml::CompTag("xref", to_utf8(attr));
        } else {
-               os << "<link linkend=\""
-                  << sgml::cleanID(buffer(), runparams, asString(cell(0)))
-                  << "\">"
+               // Link with linkend, as is it within the document (not outside, in which case xlink:href is better suited).
+               docstring attr = from_utf8("linkend=\"") + xml::cleanID(asString(cell(0))) + from_utf8("\"");
+               xs << xml::StartTag("link", to_utf8(attr))
                   << asString(cell(1))
-                  << "</link>";
+                  << xml::EndTag("link");
+       }
+}
+
+
+void InsetMathRef::updateBuffer(ParIterator const & it, UpdateType /*utype*/, bool const /*deleted*/)
+{
+       if (!buffer_) {
+               LYXERR0("InsetMathRef::updateBuffer: no buffer_!");
+               return;
        }
+       // register this inset into the buffer reference cache.
+       buffer().addReference(getTarget(), this, it);
+}
 
-       return 0;
+
+string const InsetMathRef::createDialogStr(string const & type) const
+{
+       InsetCommandParams icp(REF_CODE, (type.empty()
+                       ?  to_ascii(commandname()) : type));
+       icp["reference"] = asString(cell(0));
+       if (!cell(1).empty())
+               icp["name"] = asString(cell(1));
+       return InsetCommand::params2string(icp);
+}
+
+
+docstring const InsetMathRef::getTarget() const
+{
+       return asString(cell(0));
 }
 
 
-string const InsetMathRef::createDialogStr(string const & name) const
+void InsetMathRef::changeTarget(docstring const & target)
 {
        InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
-       icp["reference"] = asString(cell(0));
+       icp["reference"] = target;
        if (!cell(1).empty())
                icp["name"] = asString(cell(1));
-       return InsetCommand::params2string(name, icp);
+       MathData ar;
+       Buffer & buf = buffer();
+       if (createInsetMath_fromDialogStr(
+           from_utf8(InsetCommand::params2string(icp)), ar)) {
+               *this = *ar[0].nucleus()->asRefInset();
+               // FIXME audit setBuffer calls
+               setBuffer(buf);
+       }
+}
+
+
+void InsetMathRef::write(TeXMathStream & os) const
+{
+       docstring const & cmd = commandname();
+       // This should not happen, but of course it does
+       if (!buffer_) {
+               LYXERR0("Unassigned buffer_ in InsetMathRef::write!");
+               LYXERR0("LaTeX output may be wrong!");
+       }
+       bool const use_refstyle =
+               buffer_ && buffer().params().use_refstyle;
+       bool special_case =  cmd == "formatted" ||
+                       cmd == "labelonly" ||
+                       (cmd == "eqref" && use_refstyle);
+       // are we writing to the LyX file or not in a special case?
+       if (!os.latex() || !special_case) {
+               // if so, then this is easy
+               InsetMathCommand::write(os);
+               return;
+       }
+       // we need to translate 'formatted' to prettyref or refstyle-type
+       // commands and just output the label with labelonly
+       // most of this is borrowed from InsetRef and should be kept in 
+       // sync with that.
+       ModeSpecifier specifier(os, currentMode(), lockedMode(), asciiOnly());
+       MathEnsurer ensurer(os, false);
+
+       if (use_refstyle && cmd == "eqref") {
+               // we advertise this as printing "(n)", so we'll do that, at least
+               // for refstyle, since refstlye's own \eqref prints, by default,
+               // "equation n". if one wants \eqref, one can get it by using a
+               // formatted label in this case.
+               os << '(' << from_ascii("\\ref{") << cell(0) << from_ascii("})");
+       }
+       else if (cmd == "formatted") {
+               if (!use_refstyle)
+                       os << "\\prettyref{" << cell(0) << "}";
+               else {
+                        odocstringstream ods;
+                       // get the label we are referencing
+                       for (auto const & d : cell(0)) {
+                               ods << d;
+                       }
+                       docstring const ref = ods.str();
+
+                       /*
+                       At the moment, the 'plural' and 'caps' options will
+                       not work here. The reason is that we handle these as
+                       'internal' LyX argumemts, but those are not handled by
+                       InsetCommandParams::getCommand, which is what is used
+                       in createInsetMath_fromDialogStr to interpret the data
+                       coming from the dialog.
+                       If this is fixed, then what follows will get the info
+                       we need.
+                       Fixing it, I think, would mean sub-classing
+                       InsetCommandParams to InsetRefParams, and the overriding
+                       getCommand.
+                       *******************************************************
+                       // reset
+                       ods.str(docstring());
+                       ods.clear();
+                       // get the options from the optional argument
+                       for (auto const & d : cell(1))
+                               ods << d;
+                       docstring const options = ods.str();
+                       bool const caps   = support::contains(options, 'C');
+                       bool const plural = support::contains(options, 's');
+                       */
+                       docstring label;
+                       docstring prefix;
+                       docstring const fcmd =
+                               InsetRef::getFormattedCmd(ref, label, prefix, true);
+                       os << fcmd;
+                       //if (plural)
+                       //      os << "[s]";
+                       os << '{' << label << '}';
+               }
+       }
+       else if (cmd == "labelonly") {
+               // noprefix does not work here, for reasons given above.
+               os << cell(0);
+       }
 }
 
 
@@ -189,7 +373,9 @@ InsetMathRef::ref_type_info InsetMathRef::types[] = {
        { from_ascii("pageref"),   from_ascii(N_("Page Number")),           from_ascii(N_("Page: "))},
        { from_ascii("vpageref"),  from_ascii(N_("Textual Page Number")),   from_ascii(N_("TextPage: "))},
        { from_ascii("vref"),      from_ascii(N_("Standard+Textual Page")), from_ascii(N_("Ref+Text: "))},
-       { from_ascii("prettyref"), from_ascii(N_("PrettyRef")),             from_ascii(N_("FormatRef: "))},
+       { from_ascii("formatted"), from_ascii(N_("PrettyRef")),             from_ascii(N_("FormatRef: "))},
+       { from_ascii("nameref"),   from_ascii(N_("Reference to Name")),     from_ascii(N_("NameRef: "))},
+       { from_ascii("labelonly"), from_ascii(N_("Label Only")),             from_ascii(N_("Label Only: "))},
        { from_ascii(""), from_ascii(""), from_ascii("") }
 };