help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Dedicated buffer with buttons that show messages in a central displa


From: Jean Louis
Subject: Re: Dedicated buffer with buttons that show messages in a central display area
Date: Fri, 11 Nov 2022 21:38:42 +0300
User-agent: Mutt/2.2.7+37 (a90f69b) (2022-09-02)

* Heime <heimeborgia@protonmail.com> [2022-11-11 17:35]:
> I would like to have a dedicated buffer that has a number of buttons that 
> display messages in a central display area
> when pressed.  Can this be done?    

It can be done, but you do not define your desires clearly.

Each buffer in Emacs is dedicated, that I know.

"number of buttons", what kind of buttons? Do you mean like button
links as explained in (info "(elisp) Buttons") ?

Define "central display area", do you mean center of the visible
window? Or center of the buffer? Horizontally and vertically to window
or buffer? Or center of the text width before wrap?

I have this function:

(defun rcd-button-insert (button-text action-function &optional how-many 
revert-key revert-value)
  "Insert button BUTTON-TEXT with ACTION-FUNCTION.

Optional number HOW-MANY adds superscript digits to BUTTON-TEXT."
  (let* ((revert-key (or revert-key "revert-key"))
         (revert-key (intern revert-key))
         (rever-value (or revert-value button-text)))
  (insert-text-button button-text
                      'action
                      action-function
                      'follow-link t
                      revert-key revert-value)
  (when how-many
    (insert (rcd-superscript-digits how-many)))))

Which uses this one:

(defun rcd-superscript-digits (number)
  "Return unicode digits for NUMBER."
  (let* ((string (cond ((numberp number) (number-to-string number))
                       ((stringp number) number)))
         (digits (split-string string "" t " ")))
    (with-temp-buffer
      (while digits
        (insert (rcd-superscript-digit-1 (string-to-number (pop digits)))))
      (buffer-string))))

Then you go like this:

(rcd-button-insert "Display my message" 
 (lambda (_) (message "My message"))) <-- evaluate it here

or like this:

(rcd-button-insert "Display my message" 
 (lambda (_) (message "My message")) 21)Display my message²¹

Function is made so that buttons displayed may be reverted, but that
is not subject of your interest now.

(defun rcd-button-revert-source (&optional revert-key)
  "Revert the button source.

REVERT-KEY is optional and by default the symbol 'REVERT-KEY. The
value of REVERT-KEY will be returned as source."
;; (rcd-button-insert "Hello" (lambda (_) (message-box "Hello")) nil nil "⟦ 
(hyperscope 123) ⟧")
  (let ((point (point))
        (revert-key (or revert-key 'revert-key)))
    (save-excursion
      (goto-char (point-min))
      (let (my-prop)
        (while (setq my-prop 
                     (text-property-search-forward 
                      revert-key))
          (when my-prop
            (let ((begin (prop-match-beginning my-prop))
                  (end (prop-match-end my-prop))
                  (value (prop-match-value my-prop)))
              (set-text-properties (1- begin) end nil)
              (delete-region begin end)
              (goto-char begin)
              (insert (format "%s" value)))))))
    (goto-char point)))

-- 
Jean

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

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]