-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcounsel-term.el
265 lines (219 loc) · 9.56 KB
/
counsel-term.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
;;; counsel-term.el --- Ivy-based term-mode utils
;; Copyright 2018 Benjamin Lindqvist
;; Author: Benjamin Lindqvist <[email protected]>
;; Maintainer: Benjamin Lindqvist <[email protected]>
;; URL: https://github.com/tautologyclub/counsel-term-history
;; Version: 0.01
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Some hacky but extremely convenient functions for making life inside
;; term-mode easier. All of them make use of two things: first, the excellent
;; 'ivy-read' API and second, the fact that you can send raw control characters
;; such representing C-k, C-u, etc to your terminal using
;; 'term-send-raw-string'.
;;
;; A summary:
;; counsel-term-history -- A simple utility that completing-reads your
;; ~/.bash_history (or whatever other file you want, really) and sends the
;; selected candidate to the terminal. To get going, bind 'counsel-term-history
;; to some nice stroke in your term-mode-map, C-r comes quite naturally to
;; mind.
;; counsel-term-cd -- Recursively find a directory, starting at $PWD, and cd to
;; it.
;; counsel-term-ff -- Find file with completion in current dir. If it's a
;; directory, cd to it and call counsel-term-ff again. If not, open it using
;; find-file. The recursion is really badly implemented ATM using elisp sleep
;; which results in a flickering minibuffer. Advice appreciated :
;; Note: This package has no association with counsel or ivy apart from using
;; the ivy api and kinda feeling lika a counsel package. The author admits to
;; a slighy fanboy-ism towards their creator however -- support him on Patreon!
;; More instructions on his site, oremacs.com.
;;; Code:
(require 'ivy)
(require 'term)
(require 'cl)
;; switch between multi-term buffers using counsel
;-------------------------------------------------------------------------------
(defun counsel-term-switch ()
(interactive)
(let ((ivy-ignore-buffers nil))
(add-hook 'ivy-ignore-buffers 'counsel-term--ignore-non-term-buffers)
(ivy-switch-buffer)))
(defun counsel-term--ignore-non-term-buffers (bufname)
"Return t if BUFNAME does not correspond to term-mode buffer."
(let ((buf (get-buffer bufname)))
(not (and buf (eq (buffer-local-value 'major-mode buf) 'term-mode)))))
;-------------------------------------------------------------------------------
;; switch between multi-term buffers using counsel
;-------------------------------------------------------------------------------
(defun counsel-term--ignore-non-term-buffers (bufname)
"Return t if BUFNAME does not correspond to term-mode buffer."
(let ((buf (get-buffer bufname)))
(not (and buf (eq (buffer-local-value 'major-mode buf) 'term-mode)))))
(defun counsel-term-switch ()
(interactive)
(let ((ivy-ignore-buffers nil))
(add-hook 'ivy-ignore-buffers 'counsel-term--ignore-non-term-buffers)
(ivy-switch-buffer)))
;-------------------------------------------------------------------------------
;; Recursive dir-finder, subject to improvements of course :)
;-------------------------------------------------------------------------------
(defun counsel-term-cd-function (str)
"Use unix util find to recursively search for a subdir matching DIRSTRING."
(if (< (length str) 2)
(counsel-more-chars 2)
(counsel--async-command
(concat "find -type d 2>/dev/null | grep " str " || echo "))
'("" "working...")))
(defun counsel-term-cd-action (cand)
"Clear input, then cd to CAND using term."
(interactive)
(term-send-raw-string (concat "cd " cand "")))
(defun counsel-term-cd ()
"Recursively find directories and cd to them from term."
(interactive)
(ivy-read "cd: " 'counsel-term-cd-function
:dynamic-collection t
:action 'counsel-term-cd-action
:unwind #'counsel-delete-process
:caller 'counsel-term-cd))
;-------------------------------------------------------------------------------
;; Recursive regular file finder
;-------------------------------------------------------------------------------
(defun counsel-find-file-recursively-function (str)
"Use unix util find to recursively search for a subdir matching STR."
(if (< (length str) 2)
(counsel-more-chars 2)
(counsel--async-command
(concat "find . 2>/dev/null | grep " str " || echo "))
'("" "working...")))
(defun counsel-find-file-recursively-action (candidate)
"If CANDIDATE is directory, cd to it.
Else, open it."
(if (file-directory-p candidate)
(if (eq major-mode 'term-mode)
(term-send-raw-string (concat " cd " candidate ""))
(let ((default-directory (expand-file-name candidate)))
(multi-term)))
(find-file candidate)))
(defun counsel-find-file-recursively ()
"Recursively find directories and cd to them from term.
If not called from a term, open multi-term in dir."
(interactive)
(ivy-read "file: "
'counsel-find-file-recursively-function
:dynamic-collection t
:action 'counsel-find-file-recursively-action
:unwind #'counsel-delete-process
:caller 'counsel-find-file-recursively))
;-------------------------------------------------------------------------------
;; Pseudo-dired for people who kinda prefer the terminal
;-------------------------------------------------------------------------------
(defcustom counsel-term-ff-initial-input ""
"Initial input for counsel-term-ff."
:type 'string
:group 'counsel-term)
(defun counsel-term-ff--action (cand)
"If CAND is a dir, cd to it; else open it with 'find-file'."
(with-ivy-window
(if (string-match-p "/$" cand)
(progn ;; super ugly+bad, gotta be a better way
(let ((cur-dir default-directory))
(term-send-raw-string (concat " cd " cand ""))
(while (eq cur-dir default-directory)
(sit-for 0 1 t))
(counsel-term-ff)))
(find-file cand))))
(defun counsel-term-ff--candidates ()
"Moo say teh cow."
(split-string
(concat
(shell-command-to-string "ls -d .?*/ 2> /dev/null")
(shell-command-to-string "ls -d */ 2> /dev/null")
(shell-command-to-string "ls -p | grep -v /"))
"\n" t))
(defun counsel-term-ff ()
"From term-mode, find file and open it in EMACS, or cd to it in term."
(interactive)
(let ((ivy-fixed-height-minibuffer t)
(ivy-case-fold-search t))
(ivy-read
default-directory (counsel-term-ff--candidates)
:initial-input counsel-term-ff-initial-input
:action 'counsel-term-ff--action
:caller 'counsel-term-ff)))
(defface counsel-term-ff-dir-face '((t :inherit 'font-lock-function-face))
"Face for directories in counsel-term-ff."
:group 'counsel-term)
(defun counsel-term-ff--transformer (str)
"Change color if STR is a directory."
(if (string-match-p "/$" str)
(propertize str 'face 'counsel-term-ff-dir-face)
str))
(ivy-set-display-transformer 'counsel-term-ff 'counsel-term-ff--transformer)
;-------------------------------------------------------------------------------
;; Grep your command line history
;-------------------------------------------------------------------------------
(defcustom counsel-th-history-file "~/.bash_history"
"The location of your history file (tildes are fine)."
:type 'string
:group 'counsel-term)
(defcustom counsel-th-filter "^\\(cd\\|ll\\|ls\\|\\\.\\\.\\|pushd\\|popd\\)"
"Regex filter for the uninteresting lines in the history file."
:type 'string
:group 'counsel-term)
(defcustom counsel-th-initial-input "^"
"Initial input for counsel-term-history."
:type 'string
:group 'counsel-term)
(defun counsel-th--read-lines (file)
"Make a reversed list of lines in FILE, applying regex counsel-th-filter."
(with-temp-buffer
(insert-file-contents file)
(remove-if (lambda (x) (string-match counsel-th-filter x))
(reverse (split-string (buffer-string) "\n" t)))))
(defun counsel-th--action (cand)
"Send CAND to term prompt, without executing."
(term-send-raw-string (concat "" cand)))
(defun counsel-term-history--initial-input-function ()
"Check if term-prompt-regexp has been set and use it if so."
(if (string= "" term-prompt-regexp)
counsel-th-initial-input
(concat counsel-th-initial-input (term-get-old-input-default))))
(defun counsel-term-history ()
"You know, do stuff."
(interactive)
(ivy-read "History: "
(counsel-th--read-lines (expand-file-name counsel-th-history-file))
:initial-input (counsel-term-history--initial-input-function)
:action 'counsel-th--action
))
;-------------------------------------------------------------------------------
;; One-stroke 'cd ..', with redo
;-------------------------------------------------------------------------------
(defun term-downdir ()
"Shut up."
(interactive)
(term-send-raw-string " pushd $PWD > /dev/null; cd .."))
(defun term-updir ()
"Shut up."
(interactive)
(term-send-raw-string " popd > /dev/null 2>&1"))
;-------------------------------------------------------------------------------
(provide 'counsel-term)
;;; counsel-term.el ends here