summaryrefslogtreecommitdiff
path: root/avy-embark-collect.el
blob: c674e1780f81e60a6f98cec41f17b8d18a37b3ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
;;; avy-embark-collect.el --- Use avy to jump to Embark Collect entries  -*- lexical-binding: t; -*-

;; Copyright (C) 2020-2026  Omar Antolín Camarena

;; Author: Omar Antolín Camarena <omar@matem.unam.mx>
;; Keywords: convenience
;; Version: 0.3
;; URL: https://github.com/oantolin/embark
;; Package-Requires: ((emacs "29.1") (embark "1.1") (avy "0.5"))

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; This packages provides two commands, `avy-embark-collect-choose' and
;; `avy-embark-collect-act', that use avy to jump to an Embark Collect
;; entry and choose it or act on it, respectively.

;;; Code:

(require 'avy)
(require 'embark)
(eval-when-compile (require 'subr-x))

(defun avy-embark-collect--candidates ()
  "Collect all visible Embark collect candidates."
  (let (candidates)
    (avy-dowindows current-prefix-arg   ; avy-dowindows binds wnd! 🤯
      (when (derived-mode-p 'embark-collect-mode)
        (dolist (pair (avy--find-visible-regions
                       (window-start) (window-end wnd t)))
          (save-excursion
            (goto-char (car pair))
            (when (button-at (point))
              (push (cons (point) wnd) candidates))
            (while (and (condition-case nil (forward-button 1)
                          (error nil))
                        (< (point) (cdr pair)))
              (push (cons (point) wnd) candidates))))))
    (nreverse candidates)))

(defun avy-embark-collect--act (pt)
  "Act on the completion at PT."
  (unwind-protect
      (save-excursion
        (goto-char pt)
        (embark-act))
    (select-window (cdr (ring-ref avy-ring 0)))
    t))

(defun avy-embark-collect--choose (pt)
  "Choose on the completion at PT."
  (unwind-protect (push-button pt)
    (select-window (cdr (ring-ref avy-ring 0)))
    t))

(defun avy-embark-collect--jump (action dispatch-alist)
  "Jump to a visible Embark Collect candidate and perform ACTION.
Other actions are listed in the DISPATCH-ALIST."
  (interactive)
  (avy-with avy-embark-collect-choose
    (let ((avy-action action)
          (avy-dispatch-alist dispatch-alist))
      (avy-process (avy-embark-collect--candidates)))))

;;;###autoload
(defun avy-embark-collect-choose ()
  "Choose an Embark Collect candidate."
  (interactive)
  (avy-embark-collect--jump #'avy-embark-collect--choose
                            '((?e . avy-embark-collect--act)
                              (?p . avy-action-goto))))

;;;###autoload
(defun avy-embark-collect-act ()
  "Act on an Embark Collect candidate."
  (interactive)
  (avy-embark-collect--jump #'avy-embark-collect--act
                            '((?e . avy-embark-collect--choose)
                              (?p . avy-action-goto))))

(provide 'avy-embark-collect)
;;; avy-embark-collect.el ends here