From: Eshel Yaron Date: Mon, 16 May 2022 05:40:10 +0000 (+0300) Subject: Cleanup esy/buttons X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=a09622c891f4cbf83c9d189d62575bc2a440345a;p=dotfiles.git Cleanup esy/buttons --- diff --git a/.emacs.d/esy.org b/.emacs.d/esy.org index d74fc85..974b693 100644 --- a/.emacs.d/esy.org +++ b/.emacs.d/esy.org @@ -1313,40 +1313,37 @@ Add the timezones of places of interest to the list of clocks shown by (recenter) (pulse-momentary-highlight-one-line)))) - (defun esy/buttons (&optional buffer) + (defun esy/button-completion-candidate (index button) + "Return a completion candidate for BUTTON prefixed by INDEX." + (cons (concat (propertize (format "%d " index) + 'invisible t) + (truncate-string-to-width + (esy/inline (button-label button)) + 64 nil ?\s t)) + (button-start button))) + + (defun esy/buttons (&optional buffer pom) "Return an alist of buttons in BUFFER. - Buttons following point appear first in the resulting list." - (let ((buf (or buffer (current-buffer)))) - (with-current-buffer buf - (let ((button (next-button (point-min)))) - (let ((index 1) - (buttons-before nil) - (buttons-after nil)) - (while (and button (< (button-start button) (point))) - (setq buttons-before - (cons `(,(concat (propertize (format "%d " index) - 'invisible t) - (truncate-string-to-width - (esy/inline (button-label button)) - 64 nil ?\s t)) - . - ,(button-start button)) - buttons-before)) - (setq index (1+ index)) - (setq button (next-button (button-end button)))) - (while button - (setq buttons-after - (cons `(,(concat (propertize (format "%d " index) - 'invisible t) - (truncate-string-to-width - (esy/inline (button-label button)) - 64 nil ?\s t)) - . - ,(button-start button)) - buttons-after)) - (setq index (1+ index)) - (setq button (next-button (button-end button)))) - (append (reverse buttons-after) (reverse buttons-before))))))) + Buttons following POM appear first in the resulting list." + (with-current-buffer (or buffer (current-buffer)) + (let ((pos (or pom (point))) + (button (next-button (point-min))) + (index 1) + (buttons-before nil) + (buttons-after nil)) + (while (and button (< (button-start button) pos)) + (setq buttons-before + (cons (esy/button-completion-candidate index button) + buttons-before)) + (setq index (1+ index)) + (setq button (next-button (button-end button)))) + (while button + (setq buttons-after + (cons (esy/button-completion-candidate index button) + buttons-after)) + (setq index (1+ index)) + (setq button (next-button (button-end button)))) + (append (reverse buttons-after) (reverse buttons-before))))) (defun esy/inline (str) "Inline STR." @@ -1354,7 +1351,9 @@ Add the timezones of places of interest to the list of clocks shown by (string-replace "\n" " " str))) (defun esy/completing-read-with-preview (prompt collection window buffer) - "PROMPT, COLLECTION, WINDOW, BUFFER." + "Prompt for an element of COLLECTION with preview of the selection. + PROMPT passed on to `completing-read'. WINDOW is the window in + which to show preview for locations in BUFFER." (advice-add #'next-completion :after #'esy/preview-completion) (unwind-protect (let ((esy/preview-window window)