Commit | Line | Data |
---|---|---|
ae5e4c48 | 1 | ;;; em-tramp.el --- Eshell features that require TRAMP -*- lexical-binding:t -*- |
15e4ed9c | 2 | |
ba318903 | 3 | ;; Copyright (C) 1999-2014 Free Software Foundation, Inc. |
15e4ed9c AG |
4 | |
5 | ;; Author: Aidan Gauland <aidalgol@no8wireless.co.nz> | |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
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. | |
13 | ||
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. | |
18 | ||
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/>. | |
21 | ||
22 | ;;; Commentary: | |
23 | ||
24 | ;; Eshell features that require TRAMP. | |
25 | ||
26 | ;;; Code: | |
27 | ||
f87b1284 GM |
28 | (require 'esh-util) |
29 | ||
15e4ed9c AG |
30 | (eval-when-compile |
31 | (require 'esh-mode) | |
32 | (require 'eshell) | |
33 | (require 'tramp)) | |
34 | ||
15e4ed9c AG |
35 | ;;;###autoload |
36 | (progn | |
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)) | |
45 | ||
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))) | |
55 | ||
f87b1284 GM |
56 | (autoload 'eshell-parse-command "esh-cmd") |
57 | ||
15e4ed9c AG |
58 | (defun eshell/su (&rest args) |
59 | "Alias \"su\" to call TRAMP. | |
60 | ||
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 | |
65 | "su" args | |
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 | |
72 | (let ((user "root") | |
73 | (host (or (file-remote-p default-directory 'host) | |
74 | "localhost")) | |
75 | (dir (or (file-remote-p default-directory 'localname) | |
76 | (expand-file-name default-directory))) | |
77 | (prefix (file-remote-p default-directory))) | |
78 | (dolist (arg args) | |
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 "~/")) | |
83 | (if (and prefix | |
84 | (or | |
85 | (not (string-equal | |
86 | "su" (file-remote-p default-directory 'method))) | |
87 | (not (string-equal | |
88 | user (file-remote-p default-directory 'user))))) | |
89 | (eshell-parse-command | |
90 | "cd" (list (format "%s|su:%s@%s:%s" | |
91 | (substring prefix 0 -1) user host dir))) | |
92 | (eshell-parse-command | |
93 | "cd" (list (format "/su:%s@%s:%s" user host dir))))))))) | |
94 | ||
95 | (put 'eshell/su 'eshell-no-numeric-conversions t) | |
96 | ||
97 | (defun eshell/sudo (&rest args) | |
98 | "Alias \"sudo\" to call Tramp. | |
99 | ||
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 | |
104 | "sudo" args | |
105 | '((?h "help" nil nil "show this usage screen") | |
106 | (?u "user" t user "execute a command as another USER") | |
107 | :show-usage | |
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) | |
113 | "localhost")) | |
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 | |
122 | (if (and prefix | |
123 | (or | |
124 | (not | |
125 | (string-equal | |
126 | "sudo" | |
127 | (file-remote-p default-directory 'method))) | |
128 | (not | |
129 | (string-equal | |
130 | user | |
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)))))))) | |
136 | ||
137 | (put 'eshell/sudo 'eshell-no-numeric-conversions t) | |
138 | ||
139 | (provide 'em-tramp) | |
140 | ||
141 | ;; Local Variables: | |
142 | ;; generated-autoload-file: "esh-groups.el" | |
143 | ;; End: | |
144 | ||
145 | ;;; em-tramp.el ends here |