From bea16dfe69fba7d3bbb7a67bcf60b3c4739aad7c Mon Sep 17 00:00:00 2001 From: Spencer Baugh Date: Fri, 26 Sep 2025 18:13:30 -0400 Subject: Respect keymaps in buffer text for clicks on displayed strings When clicking on a string displayed by a display property, also look at the text properties of the underlying buffer text for keymaps, not just the displayed string. The displayed string takes precedence over the buffer text, but it doesn't replace it. Also, we should use the buffer's local map even for clicks on the mode line. (Otherwise, what's the point of the event?) * src/keymap.c (Fcurrent_active_maps): Consider displayed string, then buffer text, then fall back to local map. (Bug#79505) * test/src/keymap-tests.el (keymap-test-keymaps-for-non-buffer-positions): Add more tests. --- test/src/keymap-tests.el | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'test/src') diff --git a/test/src/keymap-tests.el b/test/src/keymap-tests.el index 950c741a6dd..0d203f6fcb0 100644 --- a/test/src/keymap-tests.el +++ b/test/src/keymap-tests.el @@ -509,25 +509,36 @@ g .. h foo ;; From the parent this time/ (should (equal (keymap-lookup map "u") #'undo)))) +(defun keymap-test--get-maps (posn) + (let ((maps (current-active-maps nil posn))) + ;; The local map should always be present. + (should (memq (current-local-map) maps)) + maps)) + (defun keymap-test--maps-for-posn (area string) - (current-active-maps - nil + (keymap-test--get-maps ;; FIXME: This test would be better if this was a real position ;; created by a real click. - `(,(selected-window) ,area (1 . 1) 0 (,string . 0) nil (1 . 1) nil (1 . 1) (1 . 1)))) + (let (;; If AREA is passed, the click isn't in the buffer; otherwise + ;; it's at position 1. + (pos (if area nil 1))) + `(,(selected-window) ,area (1 . 1) 0 (,string . 0) + ,pos (1 . 1) nil (1 . 1) (1 . 1))))) (ert-deftest keymap-test-keymaps-for-non-buffer-positions () "`current-active-maps' with non-buffer positions. (bug#76620)" (with-temp-buffer (pop-to-buffer (current-buffer)) (let ((keymap (make-sparse-keymap "keymap-at-point"))) + (use-local-map (make-sparse-keymap "buffer-local-map")) (insert (propertize "string" 'keymap keymap)) (goto-char (point-min)) - (should (memq keymap (current-active-maps))) + (should (memq keymap (keymap-test--get-maps nil))) (should-not (memq keymap (keymap-test--maps-for-posn 'mode-line nil))) (should-not (memq keymap (keymap-test--maps-for-posn 'mode-line "s"))) - (should-not (memq keymap (keymap-test--maps-for-posn nil "s"))) + (should (memq keymap (keymap-test--maps-for-posn nil "s"))) (should (memq keymap (keymap-test--maps-for-posn nil nil))) + (should (memq keymap (keymap-test--get-maps "a"))) (let* ((mode-line-keymap (make-sparse-keymap "keymap-in-mode-line")) (s (propertize "string" 'keymap mode-line-keymap))) ;; Respect `keymap' in the string clicked on. -- cgit v1.2.1