diff options
| author | Lars Ingebrigtsen | 2019-07-30 15:21:29 +0200 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2019-07-30 15:21:29 +0200 |
| commit | b75fb81e362b8afbf37da0d2480676269430694c (patch) | |
| tree | 82e1f67de4a7f4340b46a2219ed573c9be1c1def | |
| parent | 99156a03bfee8304cf2644470dceb668e6262c98 (diff) | |
| download | emacs-b75fb81e362b8afbf37da0d2480676269430694c.tar.gz emacs-b75fb81e362b8afbf37da0d2480676269430694c.zip | |
Extend button.el to take callback data
* doc/lispref/display.texi (Button Buffer Commands)
(Button Buffer Commands): Document this.
* lisp/button.el (backward-button, forward-button): Accept a
NO-ERROR parameter.
(button-activate): Make it possible to have specific data in the
callback action.
| -rw-r--r-- | doc/lispref/display.texi | 14 | ||||
| -rw-r--r-- | etc/NEWS | 6 | ||||
| -rw-r--r-- | lisp/button.el | 32 |
3 files changed, 41 insertions, 11 deletions
diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi index 3c91092906c..cf0008df86c 100644 --- a/doc/lispref/display.texi +++ b/doc/lispref/display.texi | |||
| @@ -6473,7 +6473,9 @@ that is the value of that property, passing it the single argument | |||
| 6473 | @var{button}). If @var{use-mouse-action} is non-@code{nil}, try to | 6473 | @var{button}). If @var{use-mouse-action} is non-@code{nil}, try to |
| 6474 | invoke the button's @code{mouse-action} property instead of | 6474 | invoke the button's @code{mouse-action} property instead of |
| 6475 | @code{action}; if the button has no @code{mouse-action} property, use | 6475 | @code{action}; if the button has no @code{mouse-action} property, use |
| 6476 | @code{action} as normal. | 6476 | @code{action} as normal. If the @code{button-data} property is |
| 6477 | present in @var{button}, use that as the argument for the | ||
| 6478 | @code{action} function instead of @var{button}. | ||
| 6477 | @end defun | 6479 | @end defun |
| 6478 | 6480 | ||
| 6479 | @defun button-label button | 6481 | @defun button-label button |
| @@ -6541,14 +6543,16 @@ event's position is used. If there's no button at @var{pos}, do | |||
| 6541 | nothing and return @code{nil}, otherwise return @code{t}. | 6543 | nothing and return @code{nil}, otherwise return @code{t}. |
| 6542 | @end deffn | 6544 | @end deffn |
| 6543 | 6545 | ||
| 6544 | @deffn Command forward-button n &optional wrap display-message | 6546 | @deffn Command forward-button n &optional wrap display-message no-error |
| 6545 | Move to the @var{n}th next button, or @var{n}th previous button if | 6547 | Move to the @var{n}th next button, or @var{n}th previous button if |
| 6546 | @var{n} is negative. If @var{n} is zero, move to the start of any | 6548 | @var{n} is negative. If @var{n} is zero, move to the start of any |
| 6547 | button at point. If @var{wrap} is non-@code{nil}, moving past either | 6549 | button at point. If @var{wrap} is non-@code{nil}, moving past either |
| 6548 | end of the buffer continues from the other end. If | 6550 | end of the buffer continues from the other end. If |
| 6549 | @var{display-message} is non-@code{nil}, the button's help-echo string | 6551 | @var{display-message} is non-@code{nil}, the button's help-echo string |
| 6550 | is displayed. Any button with a non-@code{nil} @code{skip} property | 6552 | is displayed. Any button with a non-@code{nil} @code{skip} property |
| 6551 | is skipped over. Returns the button found. | 6553 | is skipped over. Returns the button found, and signals an error if no |
| 6554 | buttons can be found. If @var{no-error} in non-@code{nil}, return nil | ||
| 6555 | instead of signalling the error. | ||
| 6552 | @end deffn | 6556 | @end deffn |
| 6553 | 6557 | ||
| 6554 | @deffn Command backward-button n &optional wrap display-message | 6558 | @deffn Command backward-button n &optional wrap display-message |
| @@ -6558,7 +6562,9 @@ button at point. If @var{wrap} is non-@code{nil}, moving past either | |||
| 6558 | end of the buffer continues from the other end. If | 6562 | end of the buffer continues from the other end. If |
| 6559 | @var{display-message} is non-@code{nil}, the button's help-echo string | 6563 | @var{display-message} is non-@code{nil}, the button's help-echo string |
| 6560 | is displayed. Any button with a non-@code{nil} @code{skip} property | 6564 | is displayed. Any button with a non-@code{nil} @code{skip} property |
| 6561 | is skipped over. Returns the button found. | 6565 | is skipped over. Returns the button found, and signals an error if no |
| 6566 | buttons can be found. If @var{no-error} in non-@code{nil}, return nil | ||
| 6567 | instead of signalling the error. | ||
| 6562 | @end deffn | 6568 | @end deffn |
| 6563 | 6569 | ||
| 6564 | @defun next-button pos &optional count-current | 6570 | @defun next-button pos &optional count-current |
| @@ -1968,6 +1968,12 @@ valid event type. | |||
| 1968 | 1968 | ||
| 1969 | * Lisp Changes in Emacs 27.1 | 1969 | * Lisp Changes in Emacs 27.1 |
| 1970 | 1970 | ||
| 1971 | +++ | ||
| 1972 | ** Buttons (created with 'make-button' and related functions) can | ||
| 1973 | now use the 'button-data' property. If present, the data in this | ||
| 1974 | property will be passed on to the 'action' function instead of the | ||
| 1975 | button itself in 'button-activate'. | ||
| 1976 | |||
| 1971 | ** 'defcustom' now takes a ':local' keyword that can be either t or | 1977 | ** 'defcustom' now takes a ':local' keyword that can be either t or |
| 1972 | 'permanent', which mean that the variable should be automatically | 1978 | 'permanent', which mean that the variable should be automatically |
| 1973 | buffer-local. 'permanent' also sets the variable's 'permanent-local' | 1979 | buffer-local. 'permanent' also sets the variable's 'permanent-local' |
diff --git a/lisp/button.el b/lisp/button.el index 921e84dfa68..ca6f0d3b6ea 100644 --- a/lisp/button.el +++ b/lisp/button.el | |||
| @@ -235,15 +235,19 @@ The action can either be a marker or a function. If it's a | |||
| 235 | marker then goto it. Otherwise if it is a function then it is | 235 | marker then goto it. Otherwise if it is a function then it is |
| 236 | called with BUTTON as only argument. BUTTON is either an | 236 | called with BUTTON as only argument. BUTTON is either an |
| 237 | overlay, a buffer position, or (for buttons in the mode-line or | 237 | overlay, a buffer position, or (for buttons in the mode-line or |
| 238 | header-line) a string." | 238 | header-line) a string. |
| 239 | |||
| 240 | If BUTTON has a `button-data' value, call the function with this | ||
| 241 | value instad of BUTTON." | ||
| 239 | (let ((action (or (and use-mouse-action (button-get button 'mouse-action)) | 242 | (let ((action (or (and use-mouse-action (button-get button 'mouse-action)) |
| 240 | (button-get button 'action)))) | 243 | (button-get button 'action))) |
| 244 | (data (button-get button 'button-data))) | ||
| 241 | (if (markerp action) | 245 | (if (markerp action) |
| 242 | (save-selected-window | 246 | (save-selected-window |
| 243 | (select-window (display-buffer (marker-buffer action))) | 247 | (select-window (display-buffer (marker-buffer action))) |
| 244 | (goto-char action) | 248 | (goto-char action) |
| 245 | (recenter 0)) | 249 | (recenter 0)) |
| 246 | (funcall action button)))) | 250 | (funcall action (or data button))))) |
| 247 | 251 | ||
| 248 | (defun button-label (button) | 252 | (defun button-label (button) |
| 249 | "Return BUTTON's text label." | 253 | "Return BUTTON's text label." |
| @@ -324,6 +328,10 @@ using `make-text-button'. Note, however, that if there is an existing | |||
| 324 | face property at the site of the button, the button face may not be visible. | 328 | face property at the site of the button, the button face may not be visible. |
| 325 | You may want to use `make-button' in that case. | 329 | You may want to use `make-button' in that case. |
| 326 | 330 | ||
| 331 | If the property `button-data' is present, it will later be used | ||
| 332 | as the argument for the `action' callback function instead of the | ||
| 333 | default argument, which is the button itself. | ||
| 334 | |||
| 327 | BEG can also be a string, in which case it is made into a button. | 335 | BEG can also be a string, in which case it is made into a button. |
| 328 | 336 | ||
| 329 | Also see `insert-text-button'." | 337 | Also see `insert-text-button'." |
| @@ -462,13 +470,17 @@ return t." | |||
| 462 | (button-activate button use-mouse-action) | 470 | (button-activate button use-mouse-action) |
| 463 | t)))) | 471 | t)))) |
| 464 | 472 | ||
| 465 | (defun forward-button (n &optional wrap display-message) | 473 | (defun forward-button (n &optional wrap display-message no-error) |
| 466 | "Move to the Nth next button, or Nth previous button if N is negative. | 474 | "Move to the Nth next button, or Nth previous button if N is negative. |
| 467 | If N is 0, move to the start of any button at point. | 475 | If N is 0, move to the start of any button at point. |
| 468 | If WRAP is non-nil, moving past either end of the buffer continues from the | 476 | If WRAP is non-nil, moving past either end of the buffer continues from the |
| 469 | other end. | 477 | other end. |
| 470 | If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed. | 478 | If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed. |
| 471 | Any button with a non-nil `skip' property is skipped over. | 479 | Any button with a non-nil `skip' property is skipped over. |
| 480 | |||
| 481 | If NO-ERROR, return nil if no further buttons could be found | ||
| 482 | instead of erroring out. | ||
| 483 | |||
| 472 | Returns the button found." | 484 | Returns the button found." |
| 473 | (interactive "p\nd\nd") | 485 | (interactive "p\nd\nd") |
| 474 | (let (button) | 486 | (let (button) |
| @@ -497,22 +509,28 @@ Returns the button found." | |||
| 497 | (unless (button-get button 'skip) | 509 | (unless (button-get button 'skip) |
| 498 | (setq n (1- n))))))) | 510 | (setq n (1- n))))))) |
| 499 | (if (null button) | 511 | (if (null button) |
| 500 | (user-error (if wrap "No buttons!" "No more buttons")) | 512 | (if no-error |
| 513 | nil | ||
| 514 | (user-error (if wrap "No buttons!" "No more buttons"))) | ||
| 501 | (let ((msg (and display-message (button-get button 'help-echo)))) | 515 | (let ((msg (and display-message (button-get button 'help-echo)))) |
| 502 | (when msg | 516 | (when msg |
| 503 | (message "%s" msg))) | 517 | (message "%s" msg))) |
| 504 | button))) | 518 | button))) |
| 505 | 519 | ||
| 506 | (defun backward-button (n &optional wrap display-message) | 520 | (defun backward-button (n &optional wrap display-message no-error) |
| 507 | "Move to the Nth previous button, or Nth next button if N is negative. | 521 | "Move to the Nth previous button, or Nth next button if N is negative. |
| 508 | If N is 0, move to the start of any button at point. | 522 | If N is 0, move to the start of any button at point. |
| 509 | If WRAP is non-nil, moving past either end of the buffer continues from the | 523 | If WRAP is non-nil, moving past either end of the buffer continues from the |
| 510 | other end. | 524 | other end. |
| 511 | If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed. | 525 | If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed. |
| 512 | Any button with a non-nil `skip' property is skipped over. | 526 | Any button with a non-nil `skip' property is skipped over. |
| 527 | |||
| 528 | If NO-ERROR, return nil if no further buttons could be found | ||
| 529 | instead of erroring out. | ||
| 530 | |||
| 513 | Returns the button found." | 531 | Returns the button found." |
| 514 | (interactive "p\nd\nd") | 532 | (interactive "p\nd\nd") |
| 515 | (forward-button (- n) wrap display-message)) | 533 | (forward-button (- n) wrap display-message no-error)) |
| 516 | 534 | ||
| 517 | 535 | ||
| 518 | (provide 'button) | 536 | (provide 'button) |