diff options
| author | Kévin Le Gouguec | 2025-02-20 22:37:13 +0100 |
|---|---|---|
| committer | Kévin Le Gouguec | 2025-03-08 17:29:34 +0100 |
| commit | f23ee932509e0a67aaafc499f0cd6b25535e4b95 (patch) | |
| tree | 4d9127f6b4b6813c11d9c95c461af30c41e66f37 | |
| parent | 269d337f9191cb58e70f93f8aae47a9bd8d635f9 (diff) | |
| download | emacs-f23ee932509e0a67aaafc499f0cd6b25535e4b95.tar.gz emacs-f23ee932509e0a67aaafc499f0cd6b25535e4b95.zip | |
Prevent button.el from clearing help-echo strings
In order to fix one of the issues discussed in bug#61413, i.e.
'buttonize' clobbering the help-echo property set by
'icon-string'.
This is a reasonable interpretation of the button.el
docstrings - "if HELP-ECHO, use that as the `help-echo'
property"; conversely, if not HELP-ECHO, then do not do
anything, preserving existing values for that property.
* lisp/button.el (button--properties): Only add a help-echo
property if HELP-ECHO is non-nil. Add an additional property
for bookkeeping.
(unbuttonize-region): Check for that bookkeeping property
before clearing help-echo.
* test/lisp/button-tests.el (button--preserve-help-echo):
Validate these changes.
| -rw-r--r-- | lisp/button.el | 32 | ||||
| -rw-r--r-- | test/lisp/button-tests.el | 28 |
2 files changed, 49 insertions, 11 deletions
diff --git a/lisp/button.el b/lisp/button.el index 674de1bb4fa..58f00c4c2ad 100644 --- a/lisp/button.el +++ b/lisp/button.el | |||
| @@ -652,15 +652,19 @@ Also see `buttonize-region'." | |||
| 652 | string)) | 652 | string)) |
| 653 | 653 | ||
| 654 | (defun button--properties (callback data help-echo) | 654 | (defun button--properties (callback data help-echo) |
| 655 | (list 'font-lock-face 'button | 655 | (append |
| 656 | 'mouse-face 'highlight | 656 | (list 'font-lock-face 'button |
| 657 | 'help-echo help-echo | 657 | 'mouse-face 'highlight |
| 658 | 'button t | 658 | 'button t |
| 659 | 'follow-link t | 659 | 'follow-link t |
| 660 | 'category t | 660 | 'category t |
| 661 | 'button-data data | 661 | 'button-data data |
| 662 | 'keymap button-map | 662 | 'keymap button-map |
| 663 | 'action callback)) | 663 | 'action callback) |
| 664 | (and help-echo | ||
| 665 | (list 'help-echo help-echo | ||
| 666 | ;; Record that button.el is responsible for this property. | ||
| 667 | 'help-echo-button t)))) | ||
| 664 | 668 | ||
| 665 | (defun buttonize-region (start end callback &optional data help-echo) | 669 | (defun buttonize-region (start end callback &optional data help-echo) |
| 666 | "Make the region between START and END into a button. | 670 | "Make the region between START and END into a button. |
| @@ -681,8 +685,14 @@ This removes both text-property and overlay based buttons." | |||
| 681 | (when (overlay-get o 'button) | 685 | (when (overlay-get o 'button) |
| 682 | (delete-overlay o))) | 686 | (delete-overlay o))) |
| 683 | (with-silent-modifications | 687 | (with-silent-modifications |
| 684 | (remove-text-properties start end | 688 | (remove-text-properties |
| 685 | (button--properties nil nil nil)) | 689 | start end |
| 690 | (append | ||
| 691 | (button--properties nil nil nil) | ||
| 692 | ;; Only remove help-echo if it was added by button.el. | ||
| 693 | (and (get-text-property start 'help-echo-button) | ||
| 694 | (list 'help-echo nil | ||
| 695 | 'help-echo-button nil)))) | ||
| 686 | (add-face-text-property start end | 696 | (add-face-text-property start end |
| 687 | 'button nil))) | 697 | 'button nil))) |
| 688 | 698 | ||
diff --git a/test/lisp/button-tests.el b/test/lisp/button-tests.el index 7f6a5bd52cd..b784cf02e28 100644 --- a/test/lisp/button-tests.el +++ b/test/lisp/button-tests.el | |||
| @@ -101,4 +101,32 @@ | |||
| 101 | (setq button (insert-button "overlay" 'help-echo help)) | 101 | (setq button (insert-button "overlay" 'help-echo help)) |
| 102 | (should (equal (button--help-echo button) "overlay: x"))))) | 102 | (should (equal (button--help-echo button) "overlay: x"))))) |
| 103 | 103 | ||
| 104 | (ert-deftest button--preserve-help-echo () | ||
| 105 | "Ensure buttonizing functions preserve existing `help-echo' properties." | ||
| 106 | ;; buttonize. | ||
| 107 | (let* ((string (propertize "button text" 'help-echo "help text")) | ||
| 108 | (button (buttonize string #'ignore))) | ||
| 109 | (should (equal (get-text-property 0 'help-echo button) | ||
| 110 | "help text"))) | ||
| 111 | ;; buttonize-region. | ||
| 112 | (with-temp-buffer | ||
| 113 | (insert (propertize "button text" 'help-echo "help text")) | ||
| 114 | (buttonize-region (point-min) (point) #'ignore) | ||
| 115 | (should (equal (get-text-property (point-min) 'help-echo) | ||
| 116 | "help text")) | ||
| 117 | ;; unbuttonize-region should not clear the property either. | ||
| 118 | (unbuttonize-region (point-min) (point)) | ||
| 119 | (should (equal (get-text-property (point-min) 'help-echo) | ||
| 120 | "help text"))) | ||
| 121 | ;; unbuttonize-region should still clear properties applied with | ||
| 122 | ;; buttonize. | ||
| 123 | (with-temp-buffer | ||
| 124 | (insert "button text") | ||
| 125 | (buttonize-region (point-min) (point) #'ignore nil "help text") | ||
| 126 | (should (equal (get-text-property (point-min) 'help-echo) | ||
| 127 | "help text")) | ||
| 128 | (unbuttonize-region (point-min) (point)) | ||
| 129 | (should (equal (get-text-property (point-min) 'help-echo) | ||
| 130 | nil)))) | ||
| 131 | |||
| 104 | ;;; button-tests.el ends here | 132 | ;;; button-tests.el ends here |