-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk_eshell.el
209 lines (180 loc) · 6.07 KB
/
mk_eshell.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
;;; mk_eshell.el --- Config for the eshell
;;; Commentary:
;;
;;; Code:
(require 'eshell)
(require 'em-smart)
(setq eshell-where-to-jump 'begin
eshell-review-quick-commands nil
eshell-smart-space-goes-to-end t)
;; Visual commands
(add-hook 'eshell-mode-hook
(lambda ()
(add-to-list 'eshell-visual-commands "htop")))
;; (setq eshell-glob-case-insensitive t)
;; (setq eshell-cmpl-cycle-completions nil)
;; Stop with that cycling thing
(setq eshell-list-files-after-cd nil)
;; =============
;; toggle eshell
;; =============
(defvar mk/eshell-popup-buffer nil)
(defun mk/eshell-popup (&optional arg)
"Toggle a eshell popup buffer with the current file's directory
as cwd."
(interactive "P")
(if (consp arg)
(pop-to-buffer "*eshell*")
(unless (buffer-live-p mk/eshell-popup-buffer)
(save-window-excursion (eshell))
(setq mk/eshell-popup-buffer (get-buffer "*eshell*")))
(let ((win (get-buffer-window mk/eshell-popup-buffer))
(dir (file-name-directory (or (buffer-file-name)
;; dired
dired-directory
;; use HOME
"~/"))))
(if win
(quit-window nil win)
(pop-to-buffer mk/eshell-popup-buffer nil t)
(insert dir)
(eshell-send-input)))))
;; ====================
;; completion functions
;; ====================
;; ---------
;; bookmarks
;; ---------
(defun pcomplete/eshell-mode/bmk ()
"Completion for `bmk'"
(pcomplete-here (bookmark-all-names)))
(defun eshell/bmk (&rest args)
"Integration between EShell and bookmarks.
For usage, execute without arguments."
(setq args (eshell-flatten-list args))
(let ((bookmark (car args))
filename name)
(cond
((eq nil args)
(format "Usage: * bmk BOOKMARK to ** either change
directory pointed to by BOOKMARK ** or bookmark-jump to the
BOOKMARK if it is not a directory. * bmk . BOOKMARK to bookmark
current directory in BOOKMARK. Completion is available."))
((string= "." bookmark)
;; Store current path in EShell as a bookmark
(if (setq name (car (cdr args)))
(progn
(bookmark-set name)
(bookmark-set-filename name (eshell/pwd))
(format "Saved current directory in bookmark %s" name))
(error "You must enter a bookmark name")))
(t
;; Check whether an existing bookmark has been specified
(if (setq filename (cdr (car (bookmark-get-bookmark-record bookmark))))
;; If it points to a directory, change to it.
(if (file-directory-p filename)
(eshell/cd filename)
;; otherwise, just jump to the bookmark
(bookmark-jump bookmark))
(error "%s is not a bookmark" bookmark))))))
;; --------------
;; git completion
;; --------------
(defun pcmpl-git-commands ()
"Return the most common git commands by parsing the git output."
(with-temp-buffer
(call-process-shell-command "git" nil (current-buffer) nil "help" "--all")
(goto-char 0)
(search-forward "available git commands in")
(let (commands)
(while (re-search-forward
"^[[:blank:]]+\\([[:word:]-.]+\\)[[:blank:]]*\\([[:word:]-.]+\\)?"
nil t)
(push (match-string 1) commands)
(when (match-string 2)
(push (match-string 2) commands)))
(sort commands #'string<))))
(defconst pcmpl-git-commands (pcmpl-git-commands)
"List of `git' commands.")
(defvar pcmpl-git-ref-list-cmd "git for-each-ref refs/ --format='%(refname)'"
"The `git' command to run to get a list of refs.")
(defun pcmpl-git-get-refs (type)
"Return a list of `git' refs filtered by TYPE."
(with-temp-buffer
(insert (shell-command-to-string pcmpl-git-ref-list-cmd))
(goto-char (point-min))
(let (refs)
(while (re-search-forward (concat "^refs/" type "/\\(.+\\)$") nil t)
(push (match-string 1) refs))
(nreverse refs))))
(defun pcmpl-git-remotes ()
"Return a list of remote repositories."
(split-string (shell-command-to-string "git remote")))
(defun pcomplete/git ()
"Completion for `git'."
;; Completion for the command argument.
(pcomplete-here* pcmpl-git-commands)
(cond
((pcomplete-match "help" 1)
(pcomplete-here* pcmpl-git-commands))
((pcomplete-match (regexp-opt '("pull" "push")) 1)
(pcomplete-here (pcmpl-git-remotes)))
;; provide branch completion for the command `checkout'.
((pcomplete-match "checkout" 1)
(pcomplete-here* (append (pcmpl-git-get-refs "heads")
(pcmpl-git-get-refs "tags"))))
(t
(while (pcomplete-here (pcomplete-entries))))))
;; ======
;; prompt
;; ======
(require 'vc-git)
(defun fg/eshell-git-info ()
(let* ((branch (vc-git-working-revision (eshell/pwd))))
(if (not (string-equal "" branch))
(concat branch " ")
"")))
(defun fg/eshell-replace-prompt-prefixes ()
(let ((absolute-path (eshell/pwd)))
(cond ((string-match (getenv "HOME") absolute-path)
(replace-match "~" nil nil absolute-path))
((string-match "/ssh:\\(.+\\):" absolute-path)
(replace-match (concat "@" (match-string 1 absolute-path) " ") nil nil absolute-path))
(t
absolute-path))))
(defun fg/eshell-prompt-function ()
(concat
(fg/eshell-git-info)
(fg/eshell-replace-prompt-prefixes)
"/ "))
(setq eshell-prompt-function #'fg/eshell-prompt-function)
(setq eshell-prompt-regexp "^[^\n]*/ ")
;; =============
;; execute shell
;; =============
;;;###autoload
(defun mk/eshell-execute-current-line ()
"Insert text of current line in eshell and execute."
(interactive)
(require 'eshell)
(let ((command (buffer-substring
(save-excursion
(beginning-of-line)
(point))
(save-excursion
(end-of-line)
(point)))))
(let ((buf (current-buffer)))
(unless (get-buffer eshell-buffer-name)
(eshell))
(display-buffer eshell-buffer-name t)
(switch-to-buffer-other-window eshell-buffer-name)
(end-of-buffer)
(eshell-kill-input)
(insert command)
(eshell-send-input)
(end-of-buffer)
(switch-to-buffer-other-window buf))))
;; (global-set-key (kbd "") 'mk/eshell-execute-current-line)
(provide 'mk_eshell)
;;; mk_eshell.el ends here