Subject: bug#20152: 24.4: bookmarks display wrong annotation (FIX INCLUDED)

From: Boruch Baum <[email-masked]>
Date: Fri, 20 Mar 2015 12:29:17 -0400

On 03/20/2015 10:31 AM, Stefan Monnier wrote:
> Same here, please resend your fix as a patch ("diff -u" or "diff -c").
I've attached TWO versions of the patch, in order to give you the option
to easily choose whether you want the second additional feature
mentioned below.

>> "Boruch" == Boruch Baum <[email-masked]> writes:
> 
>> In the bookmark list buffer, after viewing an annotation for a
>> bookmark, navigating to another bookmark entry does not kill the
>> annotation buffer display, leading to confusion about which entry
>> the still-visible annotation refers.
> 
>> The atttached bugfix kills the annotation buffer on navigation,
>> and remaps navigation keys accordingly.
> 
>> The bugfix also introduces an option to automatically display
>> annotations as one navigates within the bookmark list buffer.
This is reflected in file `emacs_bug_20152-a.patch'

>> The bugfix also introduces an option to toggle auto-display of
>> annotations.
This is reflected in file `emacs_bug_20152-b.patch'

-- 
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0

--- bookmark.el	2015-03-20 12:11:20.889491239 -0400
+++ bookmark-new.el	2015-03-20 12:14:45.825484595 -0400
@@ -1487,9 +1487,11 @@
     (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
     (define-key map "x" 'bookmark-bmenu-execute-deletions)
     (define-key map "d" 'bookmark-bmenu-delete)
-    (define-key map " " 'next-line)
-    (define-key map "n" 'next-line)
-    (define-key map "p" 'previous-line)
+    (define-key map " " 'bookmark-bmenu-next-line)
+    (define-key map "n" 'bookmark-bmenu-next-line)
+    (define-key map [remap next-line] 'bookmark-bmenu-next-line)
+    (define-key map "p" 'bookmark-bmenu-previous-line)
+    (define-key map [remap previous-line] 'bookmark-bmenu-previous-line)
     (define-key map "\177" 'bookmark-bmenu-backup-unmark)
     (define-key map "u" 'bookmark-bmenu-unmark)
     (define-key map "m" 'bookmark-bmenu-mark)
@@ -1501,6 +1503,8 @@
     (define-key map "A" 'bookmark-bmenu-show-all-annotations)
     (define-key map "e" 'bookmark-bmenu-edit-annotation)
     (define-key map "/" 'bookmark-bmenu-search)
+    (define-key map [remap backward-char] 'bookmark-bmenu-backward-char)
+    (define-key map [remap forward-char]  'bookmark-bmenu-forward-char)
     (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
     map))
 
@@ -2068,6 +2072,54 @@
     (forward-line 1))
   (forward-line 0))
 
+(defvar bookmark-bmenu-auto-display-annotations nil
+"Whether to automatically display a bookmark's annotation as one
+navigates through the bookmark list. `t' for yes. Default is
+`nil'.")
+
+(defun bookmark-bmenu-next-line (&optional ARG TRY-VSCROLL)
+"Move cursor vertically down ARG lines within the bookmark list.
+Refer to function `next-line' for details."
+  (interactive "^p\np")
+  (let
+    ((annotation-buffer
+       (get-buffer "*Bookmark Annotation*")))
+    (when annotation-buffer
+      (kill-buffer annotation-buffer)))
+  (next-line ARG TRY-VSCROLL)
+  (when bookmark-bmenu-auto-display-annotations
+     (bookmark-bmenu-show-annotation)))
+
+(defun bookmark-bmenu-previous-line (&optional ARG TRY-VSCROLL)
+"Move cursor vertically up ARG lines within the bookmark list.
+Refer to function `previous-line' for details."
+  (interactive "^p\np")
+  (let
+    ((annotation-buffer
+       (get-buffer "*Bookmark Annotation*")))
+    (when annotation-buffer
+      (kill-buffer annotation-buffer)))
+  (previous-line ARG TRY-VSCROLL)
+  (when bookmark-bmenu-auto-display-annotations
+     (bookmark-bmenu-show-annotation)))
+
+(defun bookmark-bmenu-forward-char (&optional N)
+  (interactive "p")
+  (bookmark-bmenu-backward-forward-char 'forward-char N))
+
+(defun bookmark-bmenu-backward-char (&optional N)
+  (interactive "p")
+  (bookmark-bmenu-backward-forward-char 'backward-char N))
+
+(defun bookmark-bmenu-backward-forward-char (direction-function N)
+  (let (annotation-buffer
+        (initial-line (line-number-at-pos)))
+    (funcall direction-function N)
+    (when (/= initial-line (line-number-at-pos))
+      (when (setq annotation-buffer (get-buffer "*Bookmark Annotation*"))
+        (kill-buffer annotation-buffer))
+      (when bookmark-bmenu-auto-display-annotations
+        (bookmark-bmenu-show-annotation)))))
 
 
 ;;; Menu bar stuff.  Prefix is "bookmark-menu".
--- bookmark.el	2015-03-20 12:16:19.485481558 -0400
+++ bookmark-new.el	2015-03-20 12:17:41.241478908 -0400
@@ -1487,9 +1487,11 @@
     (define-key map "\C-d" 'bookmark-bmenu-delete-backwards)
     (define-key map "x" 'bookmark-bmenu-execute-deletions)
     (define-key map "d" 'bookmark-bmenu-delete)
-    (define-key map " " 'next-line)
-    (define-key map "n" 'next-line)
-    (define-key map "p" 'previous-line)
+    (define-key map " " 'bookmark-bmenu-next-line)
+    (define-key map "n" 'bookmark-bmenu-next-line)
+    (define-key map [remap next-line] 'bookmark-bmenu-next-line)
+    (define-key map "p" 'bookmark-bmenu-previous-line)
+    (define-key map [remap previous-line] 'bookmark-bmenu-previous-line)
     (define-key map "\177" 'bookmark-bmenu-backup-unmark)
     (define-key map "u" 'bookmark-bmenu-unmark)
     (define-key map "m" 'bookmark-bmenu-mark)
@@ -1501,6 +1503,8 @@
     (define-key map "A" 'bookmark-bmenu-show-all-annotations)
     (define-key map "e" 'bookmark-bmenu-edit-annotation)
     (define-key map "/" 'bookmark-bmenu-search)
+    (define-key map [remap backward-char] 'bookmark-bmenu-backward-char)
+    (define-key map [remap forward-char]  'bookmark-bmenu-forward-char)
     (define-key map [mouse-2] 'bookmark-bmenu-other-window-with-mouse)
     map))
 
@@ -1901,9 +1905,20 @@
       (bookmark-bmenu-other-window))))

+(defvar bookmark-bmenu-toggle-auto-display-annotations nil
+"When not `nil', function `bookmark-bmenu-show-annotation' (by
+default, bound to `a`), toggles whether to automatically display
+a bookmark's annotation as one navigates through the bookmark
+list. Default is `nil'.")
+
 (defun bookmark-bmenu-show-annotation ()
   "Show the annotation for the current bookmark in another window."
   (interactive)
+  (when (and (called-interactively-p "any")
+             bookmark-bmenu-toggle-auto-display-annotations)
+    (if bookmark-bmenu-auto-display-annotations
+      (setq bookmark-bmenu-auto-display-annotations nil)
+     (setq bookmark-bmenu-auto-display-annotations t)))
   (let ((bookmark (bookmark-bmenu-bookmark)))
     (bookmark-show-annotation bookmark)))
 
@@ -2068,6 +2083,54 @@
     (forward-line 1))
   (forward-line 0))
 
+(defvar bookmark-bmenu-auto-display-annotations nil
+"Whether to automatically display a bookmark's annotation as one
+navigates through the bookmark list. `t' for yes. Default is
+`nil'.")
+
+(defun bookmark-bmenu-next-line (&optional ARG TRY-VSCROLL)
+"Move cursor vertically down ARG lines within the bookmark list.
+Refer to function `next-line' for details."
+  (interactive "^p\np")
+  (let
+    ((annotation-buffer
+       (get-buffer "*Bookmark Annotation*")))
+    (when annotation-buffer
+      (kill-buffer annotation-buffer)))
+  (next-line ARG TRY-VSCROLL)
+  (when bookmark-bmenu-auto-display-annotations
+     (bookmark-bmenu-show-annotation)))
+
+(defun bookmark-bmenu-previous-line (&optional ARG TRY-VSCROLL)
+"Move cursor vertically up ARG lines within the bookmark list.
+Refer to function `previous-line' for details."
+  (interactive "^p\np")
+  (let
+    ((annotation-buffer
+       (get-buffer "*Bookmark Annotation*")))
+    (when annotation-buffer
+      (kill-buffer annotation-buffer)))
+  (previous-line ARG TRY-VSCROLL)
+  (when bookmark-bmenu-auto-display-annotations
+     (bookmark-bmenu-show-annotation)))
+
+(defun bookmark-bmenu-forward-char (&optional N)
+  (interactive "p")
+  (bookmark-bmenu-backward-forward-char 'forward-char N))
+
+(defun bookmark-bmenu-backward-char (&optional N)
+  (interactive "p")
+  (bookmark-bmenu-backward-forward-char 'backward-char N))
+
+(defun bookmark-bmenu-backward-forward-char (direction-function N)
+  (let (annotation-buffer
+        (initial-line (line-number-at-pos)))
+    (funcall direction-function N)
+    (when (/= initial-line (line-number-at-pos))
+      (when (setq annotation-buffer (get-buffer "*Bookmark Annotation*"))
+        (kill-buffer annotation-buffer))
+      (when bookmark-bmenu-auto-display-annotations
+        (bookmark-bmenu-show-annotation)))))
 
 
 ;;; Menu bar stuff.  Prefix is "bookmark-menu".