]> git.eshelyaron.com Git - dotfiles.git/commitdiff
Cleanup esy/buttons
authorEshel Yaron <eshel@areionsec.com>
Mon, 16 May 2022 05:40:10 +0000 (08:40 +0300)
committerEshel Yaron <eshel@areionsec.com>
Mon, 16 May 2022 05:40:10 +0000 (08:40 +0300)
.emacs.d/esy.org

index d74fc859f8b5fbfad3fd9b90cf28906a3b59facd..974b6932b39780242e48b45ab24de9100f997cd6 100644 (file)
@@ -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)