-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfriendly-remote-shell.el
120 lines (87 loc) · 3.99 KB
/
friendly-remote-shell.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
;;; friendly-remote-shell.el --- Human-friendly remote interactive shells -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2020 Jordan Besly
;;
;; Version: 0.2.5
;; Keywords: processes, terminals
;; URL: https://github.com/p3r7/friendly-shell
;; Package-Requires: ((emacs "24.1")(cl-lib "0.6.1")(with-shell-interpreter "0.2.5")(friendly-tramp-path "0.1.0")(friendly-shell "0.2.0"))
;;
;; SPDX-License-Identifier: MIT
;;; Commentary:
;;
;; Smarter and more user-friendly interactive remote shell.
;;
;; Provides `friendly-remote-shell', a command that eases the creation of
;; remote shells. It's basically a wrapper around `shell' with saner
;; defaults and additional keyword arguments.
;;
;; It can also be used as a regular function (programmatically).
;;
;; For detailed instructions, please look at the function documentation or
;; the README.md at
;; https://github.com/p3r7/friendly-shell/blob/master/README.md
;;; Code:
;; REQUIRES
(require 'cl-lib)
(require 'tramp)
(require 'tramp-sh)
(require 'friendly-tramp-path)
(require 'with-shell-interpreter)
(require 'friendly-shell)
;; VARS
(defvar friendly-remote-shell-buffer-name-regexp "^\\*\\(.*\\)@\\(.*\\)\\*\\(.*\\)$")
;; PRIVATE MACROS
(defmacro friendly-remote-shell--make-tramp-file-name (vec)
"Construct Tramp file name from Tramp VEC.
Handles the signature of `tramp-make-tramp-file-name' changing
over time."
(if (>= emacs-major-version 26)
`(let ((method (tramp-file-name-method ,vec))
(user (tramp-file-name-user ,vec))
(domain (tramp-file-name-domain ,vec))
(host (tramp-file-name-host ,vec))
(port (tramp-file-name-port ,vec))
(localname (tramp-file-name-localname ,vec)))
(tramp-make-tramp-file-name method
user domain
host port
localname))
`(let ((method (tramp-file-name-method ,vec))
(user (tramp-file-name-user ,vec))
(host (tramp-file-name-host ,vec))
(localname (tramp-file-name-localname ,vec)))
(tramp-make-tramp-file-name method
user
host
localname))))
;; INTERACTIVE SHELLS
(cl-defun friendly-remote-shell (&key path
interpreter interpreter-args
command-switch
w32-arg-quote)
"Open a remote shell to host (extracted from :path).
User-friendly wrapper around `friendly-shell' for remote connections.
If not specified, prompt the user for :path.
:path can be in any format supported by `friendly-tramp-path-dissect':
- \"/<method>:[<user>[%<domain>]@]<host>[%<port>][:<localname>]\" (regular TRAMP format)
- \"[<user>[%<domain>]@]<host>[%<port>][:<localname>]\" (permissive format)
For more details about all the keyword arguments, see `with-shell-interpreter'."
(interactive)
(let* ((path (or path (read-string "Host: ")))
(path (with-shell-interpreter--normalize-path path))
(vec (friendly-tramp-path-dissect path))
(path (friendly-remote-shell--make-tramp-file-name vec)))
(friendly-shell :path path
:interpreter interpreter
:interpreter-args interpreter-args
:command-switch command-switch
:w32-arg-quote w32-arg-quote)))
;; INIT
(defun friendly-remote-shell-register-display-same-window ()
"Register remote shell buffers to display in same window.
Assumes `friendly-remote-shell-buffer-name-regexp' and `prf/shell-buffer-remote-name-construction-fn' are kept with their default values."
(when (>= emacs-major-version 25)
;; NB: before Emacs 25, shell-mode buffers would display in same window.
(add-to-list 'display-buffer-alist `(,friendly-remote-shell-buffer-name-regexp display-buffer-same-window))))
(provide 'friendly-remote-shell)
;;; friendly-remote-shell.el ends here