unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Functions behaving according to button presses
@ 2023-08-28 18:11 Heime
  2023-08-29  7:04 ` Jean Louis
  0 siblings, 1 reply; 2+ messages in thread
From: Heime @ 2023-08-28 18:11 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor


Have noticed that I can have a function like this, which I can
use with elisp code, using for example 

 (insert-button "[Next]" 'action 'action-snapshot 'follow-link t)

The above will also work with any button property name like "[Next]".
Alternatively I can execute only the "[B5]" part using

(insert-button "[B5]" 'action 'action-snapshot 'follow-link t)

Or call it interactively with 'M-x action-snapshot'.  In such case,
the function would execute '(insert "that")'.

(defun action-snapshot (&optional button)

  (pcase (button-label button)
    ("[B5]"
        (insert "this"))
    (_ 
        (insert "that"))))

Is the aforementioned description correct.  Is there anything else, alternative
ways of using function that react according to button clicks ?







^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Functions behaving according to button presses
  2023-08-28 18:11 Functions behaving according to button presses Heime
@ 2023-08-29  7:04 ` Jean Louis
  0 siblings, 0 replies; 2+ messages in thread
From: Jean Louis @ 2023-08-29  7:04 UTC (permalink / raw)
  To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor

* Heime <heimeborgia@protonmail.com> [2023-08-28 21:13]:
> 
> Have noticed that I can have a function like this, which I can
> use with elisp code, using for example 
> 
>  (insert-button "[Next]" 'action 'action-snapshot 'follow-link t)
> 
> The above will also work with any button property name like "[Next]".
> Alternatively I can execute only the "[B5]" part using
> 
> (insert-button "[B5]" 'action 'action-snapshot 'follow-link t)
> 
> Or call it interactively with 'M-x action-snapshot'.  In such case,
> the function would execute '(insert "that")'.
> 
> (defun action-snapshot (&optional button)
> 
>   (pcase (button-label button)
>     ("[B5]"
>         (insert "this"))
>     (_ 
>         (insert "that"))))
> 
> Is the aforementioned description correct.  Is there anything else, alternative
> ways of using function that react according to button clicks ?

I understand you wish to have function that recognize the button label
and act accordingly.

(rcd-button-insert "CLICK ME" 'action-function)CLICK ME ➜ nil

(defun action-function (&optional button)
  (message "%s" (button-label button)))

The above worked for me.

(rcd-button-insert "CLICK ME" 'action-function)CLICK ME ➜ nil
(rcd-button-insert "CLICK ME HARD" 'action-function)CLICK ME HARD ➜ nil

(defun action-function (&optional button)
  (cond ((and button (string= (button-label button) "CLICK ME")) (message "Clicked me"))
	((and button (string= (button-label button) "CLICK ME HARD")) (message "Clicked me hard"))))

action-function
Clicked me [2 times]
Clicked me hard
Auto-saving...done

I see it works, you can have function that acts different according to (button-label) results.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-08-29  7:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-28 18:11 Functions behaving according to button presses Heime
2023-08-29  7:04 ` Jean Louis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).