em-tramp.el 4.8 KB

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