Re: "Buttons" in a message buffer that evaluate to plain text?

Tim Landscheidt <[email protected]>
Newsgroups gmane.emacs.help
Organization https://www.tim-landscheidt.de/
Message-ID <[email protected]>
Michael Heerdegen <[email protected]> wrote:

> […]

>> any (in my case hypothetical) marker would still be moved.

>> How can I actually replace the character/text at point
>> without deleting and inserting?

> I dunno - maybe none?  `replace-region-contents' has been improved
> recently, see bug#76313 for the discussion.  I have not investigated,
> but I guess this question has been thought about.  If you are
> interested, you could have a look at what other kinds of button
> implementations do - the customize buttons and widgets for example.

> Side note: there is the nontrivial case of point's old position being
> outside the button when the new text is smaller than the replaced text.

Ah, okay.  Before reading that bug's discussion, I had not
realized that replace-region-contents and
replace-buffer-contents are so magic.

I now went with:

| (save-mark-and-excursion
|   (let
|       ((current-label (button-label button)))
|     (if (string-match "\\`\\[ *\\([^ ]*\\) *\\]\\'" current-label)
|         (save-restriction
|           (narrow-to-region
|            (+ (button-start button) 1)
|            (- (button-end button) 1))
|           (goto-char (point-min))
|           (let*
|               ((current-choice (match-string 1 current-label))
|                (next-choice (if (string-empty-p current-choice)
|                                 (car choices)
|                               (or (cadr (member current-choice choices)) "")))
|                (original-field-length (- (point-max) (point-min)))
|                (overwrite-mode 'overwrite-mode-textual))
|             (mapc (apply-partially #'self-insert-command 1)
|                   (concat
|                    (make-string (max (/ (- original-field-length (length next-choice)) 2) 0) ?\s)
|                    next-choice
|                    (make-string (max (/ (- (+ original-field-length 1) (length next-choice)) 2) 0) ?\s))))))))

For my use case this is a good compromise between effort and
effect.

Tim
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.