]> git.eshelyaron.com Git - dotfiles.git/commitdiff
Remove dependency on consult for esy/push-button
authorEshel Yaron <eshel@areionsec.com>
Sun, 15 May 2022 08:36:30 +0000 (11:36 +0300)
committerEshel Yaron <eshel@areionsec.com>
Sun, 15 May 2022 08:36:30 +0000 (11:36 +0300)
.emacs.d/esy.org

index 98b161780226bb502e16e6947343853ac0b5c01b..0ed8b26eb2ebc4edbaccda8dd6fc0fd3e0777c7b 100644 (file)
@@ -271,7 +271,7 @@ For a list of available frame parameters, see [[info:elisp#Frame Parameters][eli
   (add-hook 'after-init-hook #'esy/setup-display)
 #+end_src
 
-* Org-mode settings                                                     :org:
+* Org-mode settings
 :PROPERTIES:
 :CUSTOM_ID: org
 :END:
@@ -354,7 +354,7 @@ refiling directly into deeper headings as well.
           org-refile-use-outline-path t))
 #+end_src
 
-** Interactively fill missing CUSTOM_ID properties
+** Interactively fill missing CUSTOM_ID properties                     :cmd:
 :PROPERTIES:
 :CUSTOM_ID: orgs-custom-ids
 :END:
@@ -637,7 +637,7 @@ refiling directly into deeper headings as well.
         '(("P" (list project-current))))
 #+end_src
 
-* Email settings                                                       :mail:
+* Email settings
 :PROPERTIES:
 :CUSTOM_ID: email
 :END:
@@ -796,7 +796,7 @@ refiling directly into deeper headings as well.
     (global-set-key [remap ns-print-buffer] #'move-dup-duplicate-up)
     (global-set-key (kbd "M-p") #'move-dup-move-lines-up)
     (global-set-key (kbd "M-n") #'move-dup-move-lines-down)
-    (global-set-key (kbd "M-o") #'esy/consult-push-button)
+    (global-set-key (kbd "M-o") #'esy/push-button)
     (global-set-key (kbd "C-,") #'backward-delete-char)
     (global-set-key (kbd "C-.") #'embark-act)
     (global-set-key (kbd "C-;") #'avy-goto-char-timer)
@@ -1286,19 +1286,28 @@ Add the timezones of places of interest to the list of clocks shown by
     (add-to-list 'zoneinfo-style-world-list '("Europe/Amsterdam" "Amsterdam")))
 #+end_src
 
-** Push a button in the current buffer with completions    
+** Push a button in the current buffer with completions                :cmd:
   :PROPERTIES:
-  :CUSTOM_ID: consult-push-button
+  :CUSTOM_ID: completing-push-button
   :CreatedAt: <2022-05-14 Sat>
   :CapturedAt: 
   :CapturedAs: Emacs configuration fragment
   :END:
 
 #+begin_src emacs-lisp
-  (defun esy/preview-button (action candidate)
-    "If ACTION is `preview', preview CANDIDATE."
-    (when (eq action 'preview)
-      (let ((pos (cdr candidate)))
+  (defvar esy/preview-table nil)
+  (defvar esy/preview-window nil)
+  (defvar esy/preview-buffer nil)
+
+  (defun esy/preview-completion (&rest _)
+    "Preview the current completion."
+    (let* ((completion (with-minibuffer-completions-window
+                         (substring-no-properties
+                          (get-text-property (point)
+                                             'completion--string))))
+           (pos (cdr (assoc completion esy/preview-table))))
+      (message "%S" completion-extra-properties)
+      (with-selected-window esy/preview-window
         (unless (= (goto-char pos) (point))
           (widen)
           (goto-char pos))
@@ -1314,11 +1323,11 @@ Add the timezones of places of interest to the list of clocks shown by
                 (buttons nil))
             (while button
               (setq buttons
-                    (cons `(,(concat (truncate-string-to-width
+                    (cons `(,(concat (propertize (format "%d " index)
+                                                 'invisible t)
+                                     (truncate-string-to-width
                                       (esy/inline (button-label button))
-                                      64 nil ?\s t)
-                                     " "
-                                     (propertize (format "[%d]" index) 'invisible t))
+                                      64 nil ?\s t))
                             .
                             ,(button-start button))
                           buttons))
@@ -1331,36 +1340,49 @@ Add the timezones of places of interest to the list of clocks shown by
     (when str
       (string-replace "\n" " " str)))
 
-  (defun esy/consult-push-button ()
-    "Choose a button in the current buffer with `consult--read' and push it."
+  (defun esy/completing-read-with-preview (prompt collection window buffer)
+    "PROMPT, COLLECTION, WINDOW, BUFFER."
+    (advice-add #'next-completion :after #'esy/preview-completion)
+    (unwind-protect
+        (let ((esy/preview-window window)
+              (esy/preview-buffer buffer)
+              (esy/preview-table collection))
+          (completing-read prompt collection nil t))
+      (advice-remove #'next-completion #'esy/preview-completion)))
+
+  (defun esy/annotate-button (key)
+    "Annotate completion candidate KEY with its button properties."
+    (with-current-buffer esy/preview-buffer
+      (let* ((button (button-at
+                      (cdr
+                       (assoc key esy/preview-table))))
+             (type (button-type button))
+             (action (button-get button 'action)))
+        (esy/inline
+         (if type
+             (format "%32S #'%S"
+                     type
+                     action)
+           (when action (format "#'%S" action)))))))
+
+  (defun esy/push-button ()
+    "Push a button in the current buffer with comlpetions and preview."
     (interactive)
-    (require 'consult)
     (save-excursion
-      (if-let ((buf (current-buffer))
-               (table (esy/buttons)))
-          (push-button (cdr (consult--read
-                             table
-                             :state #'esy/preview-button
-                             :prompt "Button: "
-                             :require-match t
-                             :category 'button
-                             :lookup #'consult--lookup-cons
-                             :annotate
-                             (lambda (key)
-                               (with-current-buffer buf
-                                 (let* ((button (button-at
-                                                 (cdr
-                                                  (assoc key table))))
-                                        (type (button-type button))
-                                        (action (button-get button 'action)))
-                                   (esy/inline
-                                    (if type
-                                        (format "%32S #'%S"
-                                                type
-                                                action)
-                                      (when action (format "#'%S" action)))))))
-                             :sort nil)))
-        (user-error "No buttons found in current buffer"))))
+      (let* ((buf (current-buffer))
+             (win (selected-window))
+             (table (esy/buttons))
+             (completions-sort nil)
+             (completion-annotate-function #'esy/annotate-button))
+        (if table
+            (push-button
+             (cdr (assoc
+                   (esy/completing-read-with-preview "Button: "
+                                                     table
+                                                     win
+                                                     buf)
+                   table)))
+          (user-error "No buttons found in current buffer")))))
 
 #+end_src