aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Ingebrigtsen2019-07-30 15:21:29 +0200
committerLars Ingebrigtsen2019-07-30 15:21:29 +0200
commitb75fb81e362b8afbf37da0d2480676269430694c (patch)
tree82e1f67de4a7f4340b46a2219ed573c9be1c1def
parent99156a03bfee8304cf2644470dceb668e6262c98 (diff)
downloademacs-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.texi14
-rw-r--r--etc/NEWS6
-rw-r--r--lisp/button.el32
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
6474invoke the button's @code{mouse-action} property instead of 6474invoke 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
6477present 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
6541nothing and return @code{nil}, otherwise return @code{t}. 6543nothing 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
6545Move to the @var{n}th next button, or @var{n}th previous button if 6547Move 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
6547button at point. If @var{wrap} is non-@code{nil}, moving past either 6549button at point. If @var{wrap} is non-@code{nil}, moving past either
6548end of the buffer continues from the other end. If 6550end 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
6550is displayed. Any button with a non-@code{nil} @code{skip} property 6552is displayed. Any button with a non-@code{nil} @code{skip} property
6551is skipped over. Returns the button found. 6553is skipped over. Returns the button found, and signals an error if no
6554buttons can be found. If @var{no-error} in non-@code{nil}, return nil
6555instead 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
6558end of the buffer continues from the other end. If 6562end 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
6560is displayed. Any button with a non-@code{nil} @code{skip} property 6564is displayed. Any button with a non-@code{nil} @code{skip} property
6561is skipped over. Returns the button found. 6565is skipped over. Returns the button found, and signals an error if no
6566buttons can be found. If @var{no-error} in non-@code{nil}, return nil
6567instead 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
diff --git a/etc/NEWS b/etc/NEWS
index 1587eab1e29..cdf4bb29042 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -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
1973now use the 'button-data' property. If present, the data in this
1974property will be passed on to the 'action' function instead of the
1975button 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
1973buffer-local. 'permanent' also sets the variable's 'permanent-local' 1979buffer-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
235marker then goto it. Otherwise if it is a function then it is 235marker then goto it. Otherwise if it is a function then it is
236called with BUTTON as only argument. BUTTON is either an 236called with BUTTON as only argument. BUTTON is either an
237overlay, a buffer position, or (for buttons in the mode-line or 237overlay, a buffer position, or (for buttons in the mode-line or
238header-line) a string." 238header-line) a string.
239
240If BUTTON has a `button-data' value, call the function with this
241value 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
324face property at the site of the button, the button face may not be visible. 328face property at the site of the button, the button face may not be visible.
325You may want to use `make-button' in that case. 329You may want to use `make-button' in that case.
326 330
331If the property `button-data' is present, it will later be used
332as the argument for the `action' callback function instead of the
333default argument, which is the button itself.
334
327BEG can also be a string, in which case it is made into a button. 335BEG can also be a string, in which case it is made into a button.
328 336
329Also see `insert-text-button'." 337Also 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.
467If N is 0, move to the start of any button at point. 475If N is 0, move to the start of any button at point.
468If WRAP is non-nil, moving past either end of the buffer continues from the 476If WRAP is non-nil, moving past either end of the buffer continues from the
469other end. 477other end.
470If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed. 478If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
471Any button with a non-nil `skip' property is skipped over. 479Any button with a non-nil `skip' property is skipped over.
480
481If NO-ERROR, return nil if no further buttons could be found
482instead of erroring out.
483
472Returns the button found." 484Returns 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.
508If N is 0, move to the start of any button at point. 522If N is 0, move to the start of any button at point.
509If WRAP is non-nil, moving past either end of the buffer continues from the 523If WRAP is non-nil, moving past either end of the buffer continues from the
510other end. 524other end.
511If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed. 525If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
512Any button with a non-nil `skip' property is skipped over. 526Any button with a non-nil `skip' property is skipped over.
527
528If NO-ERROR, return nil if no further buttons could be found
529instead of erroring out.
530
513Returns the button found." 531Returns 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)