-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
ivy-avy.el
117 lines (101 loc) · 3.77 KB
/
ivy-avy.el
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
;;; ivy-avy.el --- Avy integration for Ivy -*- lexical-binding: t -*-
;; Copyright (C) 2020-2024 Free Software Foundation, Inc.
;; Author: Oleh Krehel <[email protected]>
;; Maintainer: Basil L. Contovounesios <[email protected]>
;; URL: https://github.com/abo-abo/swiper
;; Version: 0.14.2
;; Package-Requires: ((emacs "24.5") (ivy "0.14.2") (avy "0.5.0"))
;; Keywords: convenience
;; 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 package adds a "C-'" binding to Ivy minibuffer that uses Avy.
;;; Code:
(require 'ivy)
(require 'avy)
(defcustom ivy-avy-style 'pre
"The `avy-style' setting for `ivy-avy'."
:type '(choice
(const :tag "Pre" pre)
(const :tag "At" at)
(const :tag "At Full" at-full)
(const :tag "Post" post)
(const :tag "De Bruijn" de-bruijn)
(const :tag "Words" words))
:group 'ivy)
(defun ivy-avy--candidates ()
"List of candidates for `ivy-avy'."
(let (candidates)
(save-excursion
(save-restriction
(narrow-to-region
(window-start)
(window-end))
(goto-char (point-min))
(forward-line)
(while (< (point) (point-max))
(push
(cons (point)
(selected-window))
candidates)
(forward-line))))
(nreverse candidates)))
(defun ivy-avy--action (pt)
"Select the candidate represented by PT."
(when (number-or-marker-p pt)
(let* ((bnd (ivy--minibuffer-index-bounds
ivy--index ivy--length ivy-height))
(idx (+ (car bnd) (- (line-number-at-pos pt) 2)))
(coll (if (ivy-state-dynamic-collection ivy-last)
ivy--all-candidates
ivy--old-cands)))
(ivy--done
(substring-no-properties
(nth idx coll))))))
(defun ivy-avy--handler-function (char)
"Handle CHAR that's not on `avy-keys'."
(let (cmd)
(cond ((memq char '(?\C-\[ ?\C-g))
;; exit silently
(throw 'done 'abort))
((memq (setq cmd (lookup-key ivy-minibuffer-map (vector char)))
'(ivy-scroll-up-command
ivy-scroll-down-command))
(funcall cmd)
(ivy--exhibit)
(throw 'done 'exit))
;; ignore wrong key
(t
(throw 'done 'restart)))))
(defun ivy-avy ()
"Jump to one of the current ivy candidates."
(interactive)
(if (= (minibuffer-depth) 0)
(user-error
"This command is intended to be called from within `ivy-read'")
(let* ((avy-all-windows nil)
(avy-keys (or (cdr (assq 'ivy-avy avy-keys-alist))
avy-keys))
(avy-style (or (cdr (assq 'ivy-avy avy-styles-alist))
avy-style))
(avy-action #'identity)
(avy-handler-function #'ivy-avy--handler-function)
res)
(while (eq (setq res (avy-process (ivy-avy--candidates))) t))
(when res
(ivy-avy--action res)))))
(put 'ivy-avy 'no-counsel-M-x t)
(unless (lookup-key ivy-minibuffer-map (kbd "C-'"))
(define-key ivy-minibuffer-map (kbd "C-'") 'ivy-avy))
(add-to-list 'avy-styles-alist `(ivy-avy . ,ivy-avy-style))
(provide 'ivy-avy)
;;; ivy-avy.el ends here