forked from abo-abo/swiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ivy-hydra.el
159 lines (138 loc) · 5.69 KB
/
ivy-hydra.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
;;; ivy-hydra.el --- Additional key bindings for Ivy -*- lexical-binding: t -*-
;; Copyright (C) 2015-2021 Free Software Foundation, Inc.
;; Author: Oleh Krehel <[email protected]>
;; URL: https://github.com/abo-abo/swiper
;; Version: 0.13.5
;; Package-Requires: ((emacs "24.5") (ivy "0.13.4") (hydra "0.14.0"))
;; Keywords: convenience
;; This file is part of GNU Emacs.
;; This file 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, 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.
;; For a full copy of the GNU General Public License
;; see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides the `hydra-ivy/body' command, which is a
;; quasi-prefix map, with many useful bindings. These bindings are
;; shorter than usual, using mostly unprefixed keys.
;;; Code:
(require 'ivy)
(require 'hydra)
(defun ivy--matcher-desc ()
"Return description of `ivy--regex-function'."
(let ((cell (assq ivy--regex-function ivy-preferred-re-builders)))
(if cell
(cdr cell)
"other")))
(defun ivy-minibuffer-grow ()
"Grow the minibuffer window by 1 line."
(interactive)
(setq-local max-mini-window-height
(cl-incf ivy-height)))
(defun ivy-minibuffer-shrink ()
"Shrink the minibuffer window by 1 line."
(interactive)
(when (> ivy-height 2)
(setq-local max-mini-window-height
(cl-decf ivy-height))
(window-resize nil -1)))
(defhydra hydra-ivy (:hint nil :color pink)
"
^ ^ ^ ^ ^ ^ | ^Call^ ^ ^ | ^Cancel^ | ^Options^ | Action _w_/_s_/_a_: %-14s(ivy-action-name)
^-^-^-^-^-^-+-^-^---------^-^--+-^-^------+-^-^-------+-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---------------------------
^ ^ _k_ ^ ^ | _f_ollow occ_U_r | _i_nsert | _c_: calling %-5s(if ivy-calling \"on\" \"off\") _C_ase-fold: %-10`ivy-case-fold-search
_h_ ^+^ _l_ | _d_one ^ ^ | _o_ops | _M_: matcher %-5s(ivy--matcher-desc)^^^^^^^^^^^^ _T_runcate: %-11`truncate-lines
^ ^ _j_ ^ ^ | _g_o ^ ^ | ^ ^ | _<_/_>_: shrink/grow^^^^^^^^^^^^^^^^^^^^^^^^^^^^ _D_efinition of this menu
"
;; arrows
("h" ivy-beginning-of-buffer)
("j" ivy-next-line)
("k" ivy-previous-line)
("l" ivy-end-of-buffer)
;; mark
("m" ivy-mark)
("u" ivy-unmark)
("DEL" ivy-unmark-backward)
("t" ivy-toggle-marks)
;; actions
("o" keyboard-escape-quit :exit t)
("r" ivy-dispatching-done :exit t)
("C-g" keyboard-escape-quit :exit t)
("i" nil)
("C-o" nil)
("f" ivy-alt-done :exit nil)
("C-j" ivy-alt-done :exit nil)
("d" ivy-done :exit t)
("g" ivy-call)
("C-m" ivy-done :exit t)
("c" ivy-toggle-calling)
("M" ivy-rotate-preferred-builders)
(">" ivy-minibuffer-grow)
("<" ivy-minibuffer-shrink)
("w" ivy-prev-action)
("s" ivy-next-action)
("a" (let ((ivy-read-action-function #'ivy-read-action-by-key))
(ivy-read-action)))
("T" (setq truncate-lines (not truncate-lines)))
("C" ivy-toggle-case-fold)
("U" ivy-occur :exit t)
("D" (ivy-exit-with-action
(lambda (_) (find-function 'hydra-ivy/body)))
:exit t))
(dolist (sym '(
;; these cmds have a binding here
ivy-next-action ivy-prev-action
ivy-unmark-backward ivy-toggle-case-fold
ivy-minibuffer-grow ivy-minibuffer-shrink
ivy-rotate-preferred-builders ivy-toggle-calling
;; no binding
ivy-next-line-or-history ivy-previous-line-or-history
ivy-toggle-fuzzy ivy-yank-symbol
ivy-occur-next-error))
(put sym 'no-counsel-M-x t))
(defvar ivy-dispatching-done-columns 2
"Number of columns to use if the hint does not fit on one line.")
(defvar ivy-dispatching-done-idle nil
"When non-nil, the hint will be delayed by this many seconds.")
(defvar ivy-dispatching-done-hydra-exit-keys '(("M-o" nil "back")
("C-g" nil))
"Keys that can be used to exit `ivy-hydra-read-action'.")
(defun ivy-hydra-read-action (actions)
"Select one of the available actions and call `ivy-done'."
(let* ((extra-actions ivy-dispatching-done-hydra-exit-keys)
(doc (concat "action: "
(mapconcat
(lambda (x) (format "[%s] %s" (nth 0 x) (nth 2 x)))
(append (cdr actions)
extra-actions) ", ")))
(estimated-len (length doc))
(n-columns (if (> estimated-len (window-width))
ivy-dispatching-done-columns
nil))
(i 0))
(if (null (ivy--actionp actions))
(ivy-done)
(funcall
(eval
`(defhydra ivy-read-action (:color teal :columns ,n-columns :idle ,ivy-dispatching-done-idle)
"action"
,@(mapcar (lambda (x)
(list (nth 0 x)
`(progn
(let ((prev-idx (car (ivy-state-action ivy-last))))
(setcar (ivy-state-action ivy-last) ,(cl-incf i))
,@(if (eq ivy-exit 'ivy-dispatching-done)
'((ivy-done))
'((ivy-call)
(setcar (ivy-state-action ivy-last) prev-idx)))))
(nth 2 x)))
(cdr actions))
,@extra-actions)))
nil)))
(provide 'ivy-hydra)
;;; ivy-hydra.el ends here