blob: 47674ff03a340be17595a4f99289551f06904ef9 (
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
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
|
;;; eshell-additions.el --- Some aliases for Eshell -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Aryadev Chavali
;; Author: Aryadev Chavali <aryadev@aryadevchavali.com>
;; Keywords:
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License Version
;; 2 as published by the Free Software Foundation.
;; 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:
;;
;;; Code:
(autoload #'eshell "eshell")
(autoload #'eshell/cd "eshell")
(autoload #'eshell/echo "eshell")
(autoload #'eshell/send-input "eshell")
;; Aliases
(defun eshell/goto (&rest args)
"Use `read-directory-name' to change directories"
(let* ((name (read-file-name "Choose file: "))
(dir (file-name-directory name)))
(eshell/cd (list dir))
(if (not (file-directory-p name))
(find-file name))))
(defun eshell/project-root (&rest args)
"Change to directory `project-root'"
(if (project-current)
(eshell/cd (list (project-root (project-current))))
(setq eshell-last-command-status 1)
(eshell/echo
(format "[%s]: No project in current directory"
(propertize "Error" 'font-lock-face '(:foreground "red"))))))
(defun eshell/sudo-switch (&rest args)
"Switch to and from administrative (sudo) mode in Eshell.
Uses tramp to figure out if we're in sudo mode or not. "
(let ((user (file-remote-p default-directory 'user)))
(cond
((null user)
(let ((wrapped-dir (concat "/sudo::" default-directory)))
(eshell/cd wrapped-dir)))
((string= user "root")
(eshell/cd (file-remote-p default-directory 'localname))))))
;; Additional functions
(defun +eshell/at-cwd (&optional arg)
"Open an instance of eshell at the current working directory.
Pass argument to `+eshell/open'."
(interactive "P")
(let ((dir (if buffer-file-name
(file-name-directory buffer-file-name)
default-directory))
(buf (+eshell/open arg)))
(with-current-buffer buf
(eshell/cd dir)
(eshell-send-input))))
(defun +eshell/--current-instances ()
(cl-loop for buffer being the buffers
if (with-current-buffer buffer
(eq major-mode 'eshell-mode))
collect
(cons (buffer-name buffer) buffer)))
(defun +eshell/--choose-instance ()
(let* ((current-instances (+eshell/--current-instances))
(answer (completing-read "Enter name: " (mapcar #'car current-instances)))
(result (assoc answer current-instances)))
(cond
(result (switch-to-buffer (cdr result))
(cdr result))
((not (string= answer ""))
(let ((eshell-buffer-name (format "*%s-eshell*" answer)))
(eshell nil)))
(t
(eshell)))))
(defun +eshell/open (&optional arg)
"Open an instance of EShell, displaying it.
If there exists only one instance of EShell, display it. Otherwise, prompt with
a list of open instances. If user selects an instance, display that instance.
Otherwise, create an instance with the name given.
If `arg' is non nil, then always prompt user to select an instance."
(interactive "P")
(cond
((null arg)
;; No arg => Choose a default instance
(let* ((candidates (+eshell/--current-instances))
(default-cand (assoc "*eshell*" candidates #'string=))
(vacuous-cand (car candidates)))
(if-let ((cand (or default-cand vacuous-cand)))
(switch-to-buffer (cdr cand))
(eshell))))
((= (car arg) 4)
;; Arg => Choose an instance
(+eshell/--choose-instance))
(t
;; Double arg => Choose an instance then choose the directory
(let ((instance (+eshell/--choose-instance)))
(with-current-buffer instance
(thread-last (read-file-name "Enter directory: ")
file-name-directory
list
eshell/cd)
(eshell-send-input))
instance))))
(provide 'eshell-additions)
;;; eshell-additions.el ends here
|