Fix imenu--sort-by-position for non-pairs parameters (bug#26457)
[emacs.git] / lisp / eshell / em-tramp.el
blobd2697227bc053986a3d11ec115f20ad82f5d4916
1 ;;; em-tramp.el --- Eshell features that require TRAMP -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2017 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/>.
22 ;;; Commentary:
24 ;; Eshell features that require TRAMP.
26 ;;; Code:
28 (require 'esh-util)
30 (eval-when-compile
31 (require 'esh-mode)
32 (require 'eshell)
33 (require 'tramp))
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))
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
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 (file-local-name (expand-file-name default-directory)))
76 (prefix (file-remote-p default-directory)))
77 (dolist (arg args)
78 (if (string-equal arg "-") (setq login t) (setq user arg)))
79 ;; `eshell-eval-using-options' does not handle "-".
80 (if (member "-" orig-args) (setq login t))
81 (if login (setq dir "~/"))
82 (if (and prefix
83 (or
84 (not (string-equal
85 "su" (file-remote-p default-directory 'method)))
86 (not (string-equal
87 user (file-remote-p default-directory 'user)))))
88 (eshell-parse-command
89 "cd" (list (format "%s|su:%s@%s:%s"
90 (substring prefix 0 -1) user host dir)))
91 (eshell-parse-command
92 "cd" (list (format "/su:%s@%s:%s" user host dir)))))))))
94 (put 'eshell/su 'eshell-no-numeric-conversions t)
96 (defun eshell/sudo (&rest args)
97 "Alias \"sudo\" to call Tramp.
99 Uses the system sudo through TRAMP's sudo method."
100 (setq args (eshell-stringify-list (eshell-flatten-list args)))
101 (let ((orig-args (copy-tree args)))
102 (eshell-eval-using-options
103 "sudo" args
104 '((?h "help" nil nil "show this usage screen")
105 (?u "user" t user "execute a command as another USER")
106 :show-usage
107 :usage "[(-u | --user) USER] COMMAND
108 Execute a COMMAND as the superuser or another USER.")
109 (throw 'eshell-external
110 (let ((user (or user "root"))
111 (host (or (file-remote-p default-directory 'host)
112 "localhost"))
113 (dir (file-local-name (expand-file-name default-directory)))
114 (prefix (file-remote-p default-directory)))
115 ;; `eshell-eval-using-options' reads options of COMMAND.
116 (while (and (stringp (car orig-args))
117 (member (car orig-args) '("-u" "--user")))
118 (setq orig-args (cddr orig-args)))
119 (let ((default-directory
120 (if (and prefix
122 (not
123 (string-equal
124 "sudo"
125 (file-remote-p default-directory 'method)))
126 (not
127 (string-equal
128 user
129 (file-remote-p default-directory 'user)))))
130 (format "%s|sudo:%s@%s:%s"
131 (substring prefix 0 -1) user host dir)
132 (format "/sudo:%s@%s:%s" user host dir))))
133 (eshell-named-command (car orig-args) (cdr orig-args))))))))
135 (put 'eshell/sudo 'eshell-no-numeric-conversions t)
137 (provide 'em-tramp)
139 ;; Local Variables:
140 ;; generated-autoload-file: "esh-groups.el"
141 ;; End:
143 ;;; em-tramp.el ends here