emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/crux e67d040 078/112: Allow crux-sudo-edit on remote files


From: ELPA Syncer
Subject: [nongnu] elpa/crux e67d040 078/112: Allow crux-sudo-edit on remote files
Date: Wed, 11 Aug 2021 09:57:57 -0400 (EDT)

branch: elpa/crux
commit e67d040f506c3feaf936d3ee8b5b4fda0d5826ae
Author: dieggsy <dieggsy@protonmail.com>
Commit: Bozhidar Batsov <bozhidar.batsov@gmail.com>

    Allow crux-sudo-edit on remote files
    
    - If editing with sudo method or similar alternative, or user is 'root',
      do nothing.
    - If on localhost, use sudo method and switch to root user - if on
      remote host, switch to root user.
    
    - Restore place.
    - Remove ido dependency.
---
 crux.el | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/crux.el b/crux.el
index bb46f31..ed33230 100644
--- a/crux.el
+++ b/crux.el
@@ -456,11 +456,24 @@ See `file-attributes' for more info."
   (equal (crux-file-owner-uid filename)
          (user-uid)))
 
+(defun crux-already-root-p ()
+  (let ((remote-method (file-remote-p default-directory 'method))
+        (remote-user (file-remote-p default-directory 'user)))
+    (and remote-method
+         (or (member remote-method '("sudo" "su" "ksu" "doas"))
+             (string= remote-user "root")))))
+
 (defun crux-find-alternate-file-as-root (filename)
   "Wraps `find-alternate-file' with opening FILENAME as root."
-  (find-alternate-file (concat "/sudo:root@localhost:" filename)))
+  (let ((remote-method (file-remote-p default-directory 'method))
+        (remote-user (file-remote-p default-directory 'user))
+        (remote-host (file-remote-p default-directory 'host))
+        (remote-localname (file-remote-p filename 'localname)))
+    (find-alternate-file (format "/%s:root@%s:%s"
+                                 (or remote-method "sudo")
+                                 (or remote-host "localhost")
+                                 (or remote-localname filename)))))
 
-(require 'ido)
 ;;;###autoload
 (defun crux-sudo-edit (&optional arg)
   "Edit currently visited file as root.
@@ -470,9 +483,20 @@ Will also prompt for a file to visit if current
 buffer is not visiting a file."
   (interactive "P")
   (if (or arg (not buffer-file-name))
-      (find-file (concat "/sudo:root@localhost:"
-                         (ido-read-file-name "Find file (as root): ")))
-    (crux-find-alternate-file-as-root buffer-file-name)))
+      (let ((remote-method (file-remote-p default-directory 'method))
+            (remote-host (file-remote-p default-directory 'host))
+            (remote-localname (file-remote-p default-directory 'localname)))
+        (find-file (format "/%s:root@%s:%s"
+                           (or remote-method "sudo")
+                           (or remote-host "localhost")
+                           (or remote-localname
+                               (read-file-name "Find file (as root): ")))))
+
+    (if (crux-already-root-p)
+        (message "Already editing this file as root.")
+      (let ((place (point)))
+        (crux-find-alternate-file-as-root buffer-file-name)
+        (goto-char place)))))
 
 ;;;###autoload
 (defun crux-reopen-as-root ()



reply via email to

[Prev in Thread] Current Thread [Next in Thread]