aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorMichael Albinus2025-03-14 16:31:51 +0100
committerMichael Albinus2025-03-14 16:31:51 +0100
commit24ffcbb3da9a010cf564bb496af3f5ce0b805f17 (patch)
tree526559c170333bcaf9241d693e19aef863dd7d88 /lisp
parent21371aa106e6924377e916237d8418bfff2a754c (diff)
downloademacs-24ffcbb3da9a010cf564bb496af3f5ce0b805f17.tar.gz
emacs-24ffcbb3da9a010cf564bb496af3f5ce0b805f17.zip
Improve tramp-*-with-sudo commands
* doc/emacs/dired.texi (Dired Visiting): Add tramp-dired-find-file-with-sudo. * doc/emacs/files.texi (Reverting): Add tramp-revert-buffer-with-sudo. * doc/misc/tramp.texi (Ad-hoc multi-hops): Extend wrt `tramp-*-with-sudo' commands. * etc/NEWS: Add tramp-dired-find-file-with-sudo. Fix typos. * lisp/bindings.el (ctl-x-x-map): * lisp/dired.el (dired-mode-map): Add "@" binding. (Bug#76974) * lisp/net/tramp-cmds.el (dired-get-file-for-visit): Declare. (with-tramp-file-name-with-method): New macro. (tramp-revert-buffer-with-sudo): Autoload. Preserve position. Use `with-tramp-file-name-with-method'. (tramp-dired-find-file-with-sudo): New command.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/bindings.el3
-rw-r--r--lisp/dired.el1
-rw-r--r--lisp/net/tramp-cmds.el55
3 files changed, 43 insertions, 16 deletions
diff --git a/lisp/bindings.el b/lisp/bindings.el
index f829c1bea26..a5737db840b 100644
--- a/lisp/bindings.el
+++ b/lisp/bindings.el
@@ -1606,7 +1606,8 @@ if `inhibit-field-text-motion' is non-nil."
1606 "u" #'rename-uniquely 1606 "u" #'rename-uniquely
1607 "n" #'clone-buffer 1607 "n" #'clone-buffer
1608 "i" #'insert-buffer 1608 "i" #'insert-buffer
1609 "t" #'toggle-truncate-lines) 1609 "t" #'toggle-truncate-lines
1610 "@" #'tramp-revert-buffer-with-sudo)
1610(define-key ctl-x-map "x" ctl-x-x-map) 1611(define-key ctl-x-map "x" ctl-x-x-map)
1611 1612
1612(define-key esc-map "\C-l" 'reposition-window) 1613(define-key esc-map "\C-l" 'reposition-window)
diff --git a/lisp/dired.el b/lisp/dired.el
index 68e1da13171..b5bb658b503 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2416,6 +2416,7 @@ Do so according to the former subdir alist OLD-SUBDIR-ALIST."
2416 "x" #'dired-do-flagged-delete 2416 "x" #'dired-do-flagged-delete
2417 "y" #'dired-show-file-type 2417 "y" #'dired-show-file-type
2418 "+" #'dired-create-directory 2418 "+" #'dired-create-directory
2419 "@" #'tramp-dired-find-file-with-sudo
2419 ;; moving 2420 ;; moving
2420 "<" #'dired-prev-dirline 2421 "<" #'dired-prev-dirline
2421 ">" #'dired-next-dirline 2422 ">" #'dired-next-dirline
diff --git a/lisp/net/tramp-cmds.el b/lisp/net/tramp-cmds.el
index cba300049ae..be122642b96 100644
--- a/lisp/net/tramp-cmds.el
+++ b/lisp/net/tramp-cmds.el
@@ -32,6 +32,7 @@
32 32
33;; Pacify byte-compiler. 33;; Pacify byte-compiler.
34(declare-function dired-advertise "dired") 34(declare-function dired-advertise "dired")
35(declare-function dired-get-file-for-visit "dired")
35(declare-function dired-unadvertise "dired") 36(declare-function dired-unadvertise "dired")
36(declare-function mml-mode "mml") 37(declare-function mml-mode "mml")
37(declare-function mml-insert-empty-tag "mml") 38(declare-function mml-insert-empty-tag "mml")
@@ -637,6 +638,19 @@ For details, see `tramp-rename-files'."
637 (const "ksu")) 638 (const "ksu"))
638 :link '(tramp-info-link :tag "Tramp manual" tramp-file-name-with-method)) 639 :link '(tramp-info-link :tag "Tramp manual" tramp-file-name-with-method))
639 640
641(defmacro with-tramp-file-name-with-method (&rest body)
642 "Ask user for `tramp-file-name-with-method' if needed.
643Run BODY."
644 (declare (indent 0) (debug t))
645 `(let ((tramp-file-name-with-method
646 (if current-prefix-arg
647 (completing-read
648 "Tramp method: "
649 (mapcar #'cadr (cdr (get 'tramp-file-name-with-method 'custom-type)))
650 nil t tramp-file-name-with-method)
651 tramp-file-name-with-method)))
652 ,@body))
653
640(defun tramp-file-name-with-sudo (filename) 654(defun tramp-file-name-with-sudo (filename)
641 "Convert FILENAME into a multi-hop file name with \"sudo\". 655 "Convert FILENAME into a multi-hop file name with \"sudo\".
642An alternative method could be chosen with `tramp-file-name-with-method'." 656An alternative method could be chosen with `tramp-file-name-with-method'."
@@ -669,27 +683,38 @@ An alternative method could be chosen with `tramp-file-name-with-method'."
669 (make-tramp-file-name 683 (make-tramp-file-name
670 :method tramp-file-name-with-method :localname filename)))) 684 :method tramp-file-name-with-method :localname filename))))
671 685
672;;;###tramp-autoload 686;;;###autoload
673(defun tramp-revert-buffer-with-sudo () 687(defun tramp-revert-buffer-with-sudo ()
674 "Revert current buffer to visit with \"sudo\" permissions. 688 "Revert current buffer to visit with \"sudo\" permissions.
675An alternative method could be chosen with `tramp-file-name-with-method'. 689An alternative method could be chosen with `tramp-file-name-with-method'.
676If the buffer visits a file, the file is replaced. 690If the buffer visits a file, the file is replaced.
677If the buffer runs `dired', the buffer is reverted." 691If the buffer runs `dired', the buffer is reverted."
678 (interactive) 692 (interactive)
679 (cond 693 (with-tramp-file-name-with-method
680 ((buffer-file-name) 694 (cond
681 (find-alternate-file (tramp-file-name-with-sudo (buffer-file-name)))) 695 ((buffer-file-name)
682 ((tramp-dired-buffer-p) 696 (let ((pos (point)))
683 (dired-unadvertise (expand-file-name default-directory)) 697 (find-alternate-file (tramp-file-name-with-sudo (buffer-file-name)))
684 (setq default-directory (tramp-file-name-with-sudo default-directory) 698 (goto-char pos)))
685 list-buffers-directory 699 ((tramp-dired-buffer-p)
686 (tramp-file-name-with-sudo list-buffers-directory)) 700 (dired-unadvertise (expand-file-name default-directory))
687 (if (consp dired-directory) 701 (setq default-directory (tramp-file-name-with-sudo default-directory)
688 (setcar 702 list-buffers-directory
689 dired-directory (tramp-file-name-with-sudo (car dired-directory))) 703 (tramp-file-name-with-sudo list-buffers-directory))
690 (setq dired-directory (tramp-file-name-with-sudo dired-directory))) 704 (if (consp dired-directory)
691 (dired-advertise) 705 (setcar
692 (revert-buffer)))) 706 dired-directory (tramp-file-name-with-sudo (car dired-directory)))
707 (setq dired-directory (tramp-file-name-with-sudo dired-directory)))
708 (dired-advertise)
709 (revert-buffer)))))
710
711;;;###autoload
712(defun tramp-dired-find-file-with-sudo ()
713 "In Dired, visit the file or directory named on this line.
714This is performed with \"sudo\" permissions."
715 (interactive)
716 (with-tramp-file-name-with-method
717 (find-file (tramp-file-name-with-sudo (dired-get-file-for-visit)))))
693 718
694;;; Recompile on ELPA 719;;; Recompile on ELPA
695 720