diff options
| author | Spencer Baugh | 2025-09-26 18:13:30 -0400 |
|---|---|---|
| committer | Eli Zaretskii | 2025-10-18 12:48:41 +0300 |
| commit | bea16dfe69fba7d3bbb7a67bcf60b3c4739aad7c (patch) | |
| tree | 3d3ae3d45ee090ecdab3167d72f0b7c9f82156a2 | |
| parent | 59da5020da703aae784ec8afeaa32348c6b6bfd5 (diff) | |
| download | emacs-bea16dfe69fba7d3bbb7a67bcf60b3c4739aad7c.tar.gz emacs-bea16dfe69fba7d3bbb7a67bcf60b3c4739aad7c.zip | |
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 <mode-line>
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.
| -rw-r--r-- | src/keymap.c | 47 | ||||
| -rw-r--r-- | test/src/keymap-tests.el | 21 |
2 files changed, 39 insertions, 29 deletions
diff --git a/src/keymap.c b/src/keymap.c index 295b209f06b..a01dee1476b 100644 --- a/src/keymap.c +++ b/src/keymap.c | |||
| @@ -1721,33 +1721,12 @@ means to return the active maps for that window's buffer. */) | |||
| 1721 | /* For a mouse click, get the local text-property keymap | 1721 | /* For a mouse click, get the local text-property keymap |
| 1722 | of the place clicked on, rather than point. */ | 1722 | of the place clicked on, rather than point. */ |
| 1723 | 1723 | ||
| 1724 | if (POSN_INBUFFER_P (position)) | 1724 | local_map = Qnil; |
| 1725 | { | 1725 | keymap = Qnil; |
| 1726 | Lisp_Object pos = POSN_BUFFER_POSN (position); | ||
| 1727 | if (FIXNUMP (pos) | ||
| 1728 | && XFIXNUM (pos) >= BEG && XFIXNUM (pos) <= Z) | ||
| 1729 | { | ||
| 1730 | local_map = get_local_map (XFIXNUM (pos), | ||
| 1731 | current_buffer, Qlocal_map); | ||
| 1732 | |||
| 1733 | keymap = get_local_map (XFIXNUM (pos), | ||
| 1734 | current_buffer, Qkeymap); | ||
| 1735 | } | ||
| 1736 | } | ||
| 1737 | |||
| 1738 | Lisp_Object pos_area = POSN_POSN (position); | ||
| 1739 | if (EQ (pos_area, Qmode_line) || EQ (pos_area, Qheader_line)) | ||
| 1740 | { | ||
| 1741 | /* For clicks on mode line or header line, ignore the maps | ||
| 1742 | we found at POSITION, because properties at point are | ||
| 1743 | not relevant in that case. */ | ||
| 1744 | local_map = Qnil; | ||
| 1745 | keymap = Qnil; | ||
| 1746 | } | ||
| 1747 | 1726 | ||
| 1748 | /* If on a mode line string with a local keymap, or for a | 1727 | /* If on a mode line string with a local keymap, or for a |
| 1749 | click on a string, i.e. overlay string or a string | 1728 | click on a string, i.e. overlay string or a string |
| 1750 | displayed via the `display' property, consider only the | 1729 | displayed via the `display' property, first consider the |
| 1751 | `local-map' and `keymap' properties of that string. */ | 1730 | `local-map' and `keymap' properties of that string. */ |
| 1752 | 1731 | ||
| 1753 | if (CONSP (string) && STRINGP (XCAR (string))) | 1732 | if (CONSP (string) && STRINGP (XCAR (string))) |
| @@ -1763,6 +1742,26 @@ means to return the active maps for that window's buffer. */) | |||
| 1763 | } | 1742 | } |
| 1764 | } | 1743 | } |
| 1765 | 1744 | ||
| 1745 | Lisp_Object buffer_posn = POSN_BUFFER_POSN (position); | ||
| 1746 | /* Then, if the click was in the buffer, get the local | ||
| 1747 | text-property keymap of the place clicked on. */ | ||
| 1748 | |||
| 1749 | if (FIXNUMP (buffer_posn) | ||
| 1750 | && XFIXNUM (buffer_posn) >= BEG && XFIXNUM (buffer_posn) <= Z) | ||
| 1751 | { | ||
| 1752 | /* The properties in POSN_STRING take precedence, if set. */ | ||
| 1753 | if (NILP (local_map)) | ||
| 1754 | local_map = get_local_map (XFIXNUM (buffer_posn), | ||
| 1755 | current_buffer, Qlocal_map); | ||
| 1756 | |||
| 1757 | if (NILP (keymap)) | ||
| 1758 | keymap = get_local_map (XFIXNUM (buffer_posn), | ||
| 1759 | current_buffer, Qkeymap); | ||
| 1760 | } | ||
| 1761 | |||
| 1762 | /* Finally, fall back on the buffer's local map. */ | ||
| 1763 | if (NILP (local_map)) | ||
| 1764 | local_map = BVAR (current_buffer, keymap); | ||
| 1766 | } | 1765 | } |
| 1767 | 1766 | ||
| 1768 | if (!NILP (local_map)) | 1767 | if (!NILP (local_map)) |
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 | |||
| 509 | ;; From the parent this time/ | 509 | ;; From the parent this time/ |
| 510 | (should (equal (keymap-lookup map "u") #'undo)))) | 510 | (should (equal (keymap-lookup map "u") #'undo)))) |
| 511 | 511 | ||
| 512 | (defun keymap-test--get-maps (posn) | ||
| 513 | (let ((maps (current-active-maps nil posn))) | ||
| 514 | ;; The local map should always be present. | ||
| 515 | (should (memq (current-local-map) maps)) | ||
| 516 | maps)) | ||
| 517 | |||
| 512 | (defun keymap-test--maps-for-posn (area string) | 518 | (defun keymap-test--maps-for-posn (area string) |
| 513 | (current-active-maps | 519 | (keymap-test--get-maps |
| 514 | nil | ||
| 515 | ;; FIXME: This test would be better if this was a real position | 520 | ;; FIXME: This test would be better if this was a real position |
| 516 | ;; created by a real click. | 521 | ;; created by a real click. |
| 517 | `(,(selected-window) ,area (1 . 1) 0 (,string . 0) nil (1 . 1) nil (1 . 1) (1 . 1)))) | 522 | (let (;; If AREA is passed, the click isn't in the buffer; otherwise |
| 523 | ;; it's at position 1. | ||
| 524 | (pos (if area nil 1))) | ||
| 525 | `(,(selected-window) ,area (1 . 1) 0 (,string . 0) | ||
| 526 | ,pos (1 . 1) nil (1 . 1) (1 . 1))))) | ||
| 518 | 527 | ||
| 519 | (ert-deftest keymap-test-keymaps-for-non-buffer-positions () | 528 | (ert-deftest keymap-test-keymaps-for-non-buffer-positions () |
| 520 | "`current-active-maps' with non-buffer positions. (bug#76620)" | 529 | "`current-active-maps' with non-buffer positions. (bug#76620)" |
| 521 | (with-temp-buffer | 530 | (with-temp-buffer |
| 522 | (pop-to-buffer (current-buffer)) | 531 | (pop-to-buffer (current-buffer)) |
| 523 | (let ((keymap (make-sparse-keymap "keymap-at-point"))) | 532 | (let ((keymap (make-sparse-keymap "keymap-at-point"))) |
| 533 | (use-local-map (make-sparse-keymap "buffer-local-map")) | ||
| 524 | (insert (propertize "string" 'keymap keymap)) | 534 | (insert (propertize "string" 'keymap keymap)) |
| 525 | (goto-char (point-min)) | 535 | (goto-char (point-min)) |
| 526 | (should (memq keymap (current-active-maps))) | 536 | (should (memq keymap (keymap-test--get-maps nil))) |
| 527 | (should-not (memq keymap (keymap-test--maps-for-posn 'mode-line nil))) | 537 | (should-not (memq keymap (keymap-test--maps-for-posn 'mode-line nil))) |
| 528 | (should-not (memq keymap (keymap-test--maps-for-posn 'mode-line "s"))) | 538 | (should-not (memq keymap (keymap-test--maps-for-posn 'mode-line "s"))) |
| 529 | (should-not (memq keymap (keymap-test--maps-for-posn nil "s"))) | 539 | (should (memq keymap (keymap-test--maps-for-posn nil "s"))) |
| 530 | (should (memq keymap (keymap-test--maps-for-posn nil nil))) | 540 | (should (memq keymap (keymap-test--maps-for-posn nil nil))) |
| 541 | (should (memq keymap (keymap-test--get-maps "a"))) | ||
| 531 | (let* ((mode-line-keymap (make-sparse-keymap "keymap-in-mode-line")) | 542 | (let* ((mode-line-keymap (make-sparse-keymap "keymap-in-mode-line")) |
| 532 | (s (propertize "string" 'keymap mode-line-keymap))) | 543 | (s (propertize "string" 'keymap mode-line-keymap))) |
| 533 | ;; Respect `keymap' in the string clicked on. | 544 | ;; Respect `keymap' in the string clicked on. |