Subject: 24.3.50; `:link' in `defgroup' does not respect `mouse-1-click-follows-link'

From: Mauro Aranda <[email-masked]>
Date: Fri, 23 Aug 2019 17:41:45 -0300

I took a look at this bug and it turned out to be really tricky.  So I did
all the research I could, to try to find a good fix.

Because the widget-button-click code was written before the addition of
mouse-1-click-follows-link, there is some work to be done to adapt it.  Even
though there were some attempts [1], they weren't enough.

The attached patch tries to fix this bug, while trying to not change too
much
of the code of the widget library.  It introduces a keymap for the link
widgets,
and only binds mouse-2 events.  Since widget-button-click isn't changed, it
has to bind down-mouse-2 too.  In this fix, I also have been careful to not
reintroduce bugs that have come up by changing the keybindings in the
widget-keymap, such as here:
https://debbugs.gnu.org/cgi/bugreport.cgi?bug=23571

I investigated the emacs sources, and have noticed that this patch could
cause
problems with three pieces of code:

1) recentf.el: Because of Bug#22434, puts a nil :follow-link property to the
link widget.  https://debbugs.gnu.org/cgi/bugreport.cgi?bug=22434
The patch would make that mouse-1 isn't translated to mouse-2, but since
this
was a workaround, changing this code would be OK, I think.  Without
overriding :follow-link, I have checked that recentf.el works like it
should,
with this patch applied.

2) epa.el: Typing M-x epa-encrypt-file brings up a *Keys* buffer.  This
buffer
has clickable text: [Cancel] and [OK].  The epa code create this as a link
widget, and when mouse-1-click-follows-link is nil, this text becomes not
responsive with mouse-1.  I'd say this are more buttons than links,
so a fix for it (if it is considered it needs fixing) would be to turn them
into
push-button widgets.

3) mh-mime.el: I have never used MH-E.  But there is some code in mh-mime.el
that creates a link widget, with an action mh-widget-press-button, and it
looks like it has a keymap of its own.  I don't know if the behavior could
change here.

I haven't found any other code in the Emacs sources that could have
problems.
And fixing this would make code that depends on the link widgets to behave
as
expected, when mouse-1-click-follows-link is customized to a nil value.  So
I
think it would be good to fix this.

[1] https://lists.gnu.org/archive/html/emacs-devel/2008-03/msg01312.html
From d40f8a1fa30fcc67444b6afffd8149d9e9b9af1d Mon Sep 17 00:00:00 2001
From: Mauro Aranda <[email-masked]>
Date: Tue, 20 Aug 2019 20:32:41 -0300
Subject: [PATCH] Make link widgets obey mouse-1-click-follows-link

* lisp/wid-edit.el (widget-link-keymap): New variable, a keymap to use
inside a link widget.
('link widget): Restore the :follow-link property and add widget-link-keymap
as the :keymap property.  (Bug#15682)
---
 lisp/wid-edit.el | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/lisp/wid-edit.el b/lisp/wid-edit.el
index dd03a24..1ddc461 100644
--- a/lisp/wid-edit.el
+++ b/lisp/wid-edit.el
@@ -1790,17 +1790,22 @@ widget-link-suffix
   :type 'string
   :group 'widget-button)
 
+(defvar widget-link-keymap
+  (let ((map (copy-keymap widget-keymap)))
+    ;; Only bind mouse-2, since mouse-1 will be translated accordingly to
+    ;; the customization of `mouse-1-click-follows-link'.
+    (define-key map [down-mouse-1] (lookup-key widget-global-map [down-mouse-1]))
+    (define-key map [down-mouse-2] 'widget-button-click)
+    (define-key map [mouse-2] 'widget-button-click)
+    map)
+  "Keymap used inside a link widget.")
+
 (define-widget 'link 'item
   "An embedded link."
   :button-prefix 'widget-link-prefix
   :button-suffix 'widget-link-suffix
-  ;; The `follow-link' property should only be used in those contexts where the
-  ;; mouse-1 event normally doesn't follow the link, yet the `link' widget
-  ;; seems to almost always be used in contexts where (down-)mouse-1 is bound
-  ;; to `widget-button-click' and hence the "mouse-1 to mouse-2" remapping is
-  ;; not necessary (and can even be harmful).  So let's not add a :follow-link
-  ;; by default.  See (bug#22434).
-  ;; :follow-link 'mouse-face
+  :follow-link 'mouse-face
+  :keymap widget-link-keymap
   :help-echo "Follow the link."
   :format "%[%t%]")
 
-- 
2.7.4