blob: dcfa179e01b9695057f456cf67c11c8dbcf12c6c (
plain) (
blame)
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
|
;;; init-tramp.el --- configure tramp -*- lexical-binding:t -*-
;;; Commentary:
;;; Code:
(defun mpolden/sudo-file-name (filename)
"Add a sudo-like TRAMP method to FILENAME.
If FILENAME already contains a sudo-like method, return FILENAME
unchanged. If FILENAME is accessed over SSH, replace
\"/ssh:host\" with \"/ssh:host|sudo:\". Otherwise, assume
FILENAME is a local path and prefix it with \"/sudo::\".
If doas is available, that method is used instead of sudo."
(let* ((parts (split-string filename ":"))
(method (string-trim (car parts) "/"))
(host (car (cdr parts)))
(hop-method (when host (car (cdr (split-string host "|")))))
(sudo-methods '("sudo" "doas")))
(if (or (member method sudo-methods)
(member hop-method sudo-methods))
filename
(let* ((sudo-method (if (executable-find "doas" t) "doas" "sudo"))
(sudo-hop (when (equal method "ssh")
(concat host "|" sudo-method ":"))))
(if sudo-hop
(mapconcat 'identity (append (list (concat "/" method) sudo-hop)
(nthcdr 2 parts))
":")
(concat "/" sudo-method "::" filename))))))
(defun mpolden/sudo-find-file (&optional arg)
"Find file and open it with sudo.
With a prefix ARG prompt edit currently visited file using sudo."
(interactive "P")
(if arg
(find-alternate-file (mpolden/sudo-file-name buffer-file-name))
(find-file (mpolden/sudo-file-name (read-file-name "Find file with sudo: ")))))
(defun mpolden/sudo-current-file ()
"Open current file with sudo."
(interactive)
(mpolden/sudo-find-file t))
(use-package tramp
:bind
(("C-x +" . mpolden/sudo-find-file)
("C-x !" . mpolden/sudo-current-file))
:config
;; use path configured by remote host
(add-to-list 'tramp-remote-path 'tramp-own-remote-path))
(provide 'init-tramp)
;;; init-tramp.el ends here
|