1 ;;; em-tramp.el --- Eshell features that require TRAMP -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2018 Free Software Foundation, Inc.
5 ;; Author: Aidan Gauland <aidalgol@no8wireless.co.nz>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;; Eshell features that require TRAMP.
36 ;; There are no items in this custom group, but eshell modules (ab)use
40 (defgroup eshell-tramp nil
41 "This module defines commands that use TRAMP in a way that is
42 not transparent to the user. So far, this includes only the
43 built-in su and sudo commands, which are not compatible with
44 the full, external su and sudo commands, and require the user
45 to understand how to use the TRAMP sudo method."
46 :tag
"TRAMP Eshell features"
47 :group
'eshell-module
))
49 (defun eshell-tramp-initialize ()
50 "Initialize the TRAMP-using commands code."
51 (when (eshell-using-module 'eshell-cmpl
)
52 (add-hook 'pcomplete-try-first-hook
53 'eshell-complete-host-reference nil t
))
54 (make-local-variable 'eshell-complex-commands
)
55 (setq eshell-complex-commands
56 (append '("su" "sudo")
57 eshell-complex-commands
)))
59 (autoload 'eshell-parse-command
"esh-cmd")
61 (defun eshell/su
(&rest args
)
62 "Alias \"su\" to call TRAMP.
64 Uses the system su through TRAMP's su method."
65 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
66 (let ((orig-args (copy-tree args
)))
67 (eshell-eval-using-options
69 '((?h
"help" nil nil
"show this usage screen")
70 (?l
"login" nil login
"provide a login environment")
71 (? nil nil login
"provide a login environment")
72 :usage
"[- | -l | --login] [USER]
73 Become another USER during a login session.")
74 (throw 'eshell-replace-command
76 (host (or (file-remote-p default-directory
'host
)
78 (dir (file-local-name (expand-file-name default-directory
)))
79 (prefix (file-remote-p default-directory
)))
81 (if (string-equal arg
"-") (setq login t
) (setq user arg
)))
82 ;; `eshell-eval-using-options' does not handle "-".
83 (if (member "-" orig-args
) (setq login t
))
84 (if login
(setq dir
"~/"))
88 "su" (file-remote-p default-directory
'method
)))
90 user
(file-remote-p default-directory
'user
)))))
92 "cd" (list (format "%s|su:%s@%s:%s"
93 (substring prefix
0 -
1) user host dir
)))
95 "cd" (list (format "/su:%s@%s:%s" user host dir
)))))))))
97 (put 'eshell
/su
'eshell-no-numeric-conversions t
)
99 (defun eshell/sudo
(&rest args
)
100 "Alias \"sudo\" to call Tramp.
102 Uses the system sudo through TRAMP's sudo method."
103 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
104 (let ((orig-args (copy-tree args
)))
105 (eshell-eval-using-options
107 '((?h
"help" nil nil
"show this usage screen")
108 (?u
"user" t user
"execute a command as another USER")
110 :usage
"[(-u | --user) USER] COMMAND
111 Execute a COMMAND as the superuser or another USER.")
112 (throw 'eshell-external
113 (let ((user (or user
"root"))
114 (host (or (file-remote-p default-directory
'host
)
116 (dir (file-local-name (expand-file-name default-directory
)))
117 (prefix (file-remote-p default-directory
)))
118 ;; `eshell-eval-using-options' reads options of COMMAND.
119 (while (and (stringp (car orig-args
))
120 (member (car orig-args
) '("-u" "--user")))
121 (setq orig-args
(cddr orig-args
)))
122 (let ((default-directory
128 (file-remote-p default-directory
'method
)))
132 (file-remote-p default-directory
'user
)))))
133 (format "%s|sudo:%s@%s:%s"
134 (substring prefix
0 -
1) user host dir
)
135 (format "/sudo:%s@%s:%s" user host dir
))))
136 (eshell-named-command (car orig-args
) (cdr orig-args
))))))))
138 (put 'eshell
/sudo
'eshell-no-numeric-conversions t
)
143 ;; generated-autoload-file: "esh-groups.el"
146 ;;; em-tramp.el ends here