1 ;;; em-tramp.el --- Eshell features that require TRAMP -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2013 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 <http://www.gnu.org/licenses/>.
24 ;; Eshell features that require TRAMP.
37 (defgroup eshell-tramp nil
38 "This module defines commands that use TRAMP in a way that is
39 not transparent to the user. So far, this includes only the
40 built-in su and sudo commands, which are not compatible with
41 the full, external su and sudo commands, and require the user
42 to understand how to use the TRAMP sudo method."
43 :tag
"TRAMP Eshell features"
44 :group
'eshell-module
))
46 (defun eshell-tramp-initialize ()
47 "Initialize the TRAMP-using commands code."
48 (when (eshell-using-module 'eshell-cmpl
)
49 (add-hook 'pcomplete-try-first-hook
50 'eshell-complete-host-reference nil t
))
51 (make-local-variable 'eshell-complex-commands
)
52 (setq eshell-complex-commands
53 (append '("su" "sudo")
54 eshell-complex-commands
)))
56 (autoload 'eshell-parse-command
"esh-cmd")
58 (defun eshell/su
(&rest args
)
59 "Alias \"su\" to call TRAMP.
61 Uses the system su through TRAMP's su method."
62 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
63 (let ((orig-args (copy-tree args
)))
64 (eshell-eval-using-options
66 '((?h
"help" nil nil
"show this usage screen")
67 (?l
"login" nil login
"provide a login environment")
68 (? nil nil login
"provide a login environment")
69 :usage
"[- | -l | --login] [USER]
70 Become another USER during a login session.")
71 (throw 'eshell-replace-command
73 (host (or (file-remote-p default-directory
'host
)
75 (dir (or (file-remote-p default-directory
'localname
)
76 (expand-file-name default-directory
)))
77 (prefix (file-remote-p default-directory
)))
79 (if (string-equal arg
"-") (setq login t
) (setq user arg
)))
80 ;; `eshell-eval-using-options' does not handle "-".
81 (if (member "-" orig-args
) (setq login t
))
82 (if login
(setq dir
"~/"))
86 "su" (file-remote-p default-directory
'method
)))
88 user
(file-remote-p default-directory
'user
)))))
90 "cd" (list (format "%s|su:%s@%s:%s"
91 (substring prefix
0 -
1) user host dir
)))
93 "cd" (list (format "/su:%s@%s:%s" user host dir
)))))))))
95 (put 'eshell
/su
'eshell-no-numeric-conversions t
)
97 (defun eshell/sudo
(&rest args
)
98 "Alias \"sudo\" to call Tramp.
100 Uses the system sudo through TRAMP's sudo method."
101 (setq args
(eshell-stringify-list (eshell-flatten-list args
)))
102 (let ((orig-args (copy-tree args
)))
103 (eshell-eval-using-options
105 '((?h
"help" nil nil
"show this usage screen")
106 (?u
"user" t user
"execute a command as another USER")
108 :usage
"[(-u | --user) USER] COMMAND
109 Execute a COMMAND as the superuser or another USER.")
110 (throw 'eshell-external
111 (let ((user (or user
"root"))
112 (host (or (file-remote-p default-directory
'host
)
114 (dir (or (file-remote-p default-directory
'localname
)
115 (expand-file-name default-directory
)))
116 (prefix (file-remote-p default-directory
)))
117 ;; `eshell-eval-using-options' reads options of COMMAND.
118 (while (and (stringp (car orig-args
))
119 (member (car orig-args
) '("-u" "--user")))
120 (setq orig-args
(cddr orig-args
)))
121 (let ((default-directory
127 (file-remote-p default-directory
'method
)))
131 (file-remote-p default-directory
'user
)))))
132 (format "%s|sudo:%s@%s:%s"
133 (substring prefix
0 -
1) user host dir
)
134 (format "/sudo:%s@%s:%s" user host dir
))))
135 (eshell-named-command (car orig-args
) (cdr orig-args
))))))))
137 (put 'eshell
/sudo
'eshell-no-numeric-conversions t
)
142 ;; generated-autoload-file: "esh-groups.el"
145 ;;; em-tramp.el ends here