help-gnu-emacs
[Top][All Lists]
Advanced

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

Follow-Up on Auto-Prompt for Password and Raise Privilegies when needed


From: Nordlöw
Subject: Follow-Up on Auto-Prompt for Password and Raise Privilegies when needed
Date: Fri, 7 May 2010 02:14:04 -0700 (PDT)
User-agent: G2/1.0

Using the answers from my previous post
http://groups.google.se/group/gnu.emacs.help/browse_thread/thread/25024cc86611ea35
I managed to put together something that closesly matches my wishes:


(defvar post-ro-edit-hook nil
  "Hooks to run first time a root-read-only buffer gets modified.")

(defun post-ro-edit-hook ()
  "Hook run when the user tries to modify a read-only buffer file
owned by root."
  (if (y-or-n-p (format "Reopen buffer file %s as root to edit? "
buffer-file-name))
      (let ((p (point)))
        (find-alternate-file (concat "/sudo::" buffer-file-
name)) ;open with sudo instead
        (goto-char p)) ;goto same point in reopened file so that
pending changes occurr at right position
    (unwind-protect (run-hooks 'post-ro-edit-hook))
    ;; TODO: drop-all pending buffers inserts
    ))

(defun find-file-auto-raise-privs ()
  "If current buffer is read-only, but writable by root, raise
priviligies."
  (interactive "")
  (when (and buffer-read-only                         ;buffer is
readonly
             buffer-file-name                         ;buffer is a
file
             (not (file-writable-p buffer-file-name)) ;file it is not
writable
             (= (nth 2 (file-attributes buffer-file-name)) 0)) ;file
is owned by root
    (toggle-read-only -1) ;make it writable so that `before-change-
functions' is run
    (add-hook 'first-change-hook 'post-ro-edit-hook nil t) ;activate
before-change-hook locally in this buffer
    ))
(add-hook 'find-file-hook 'find-file-auto-raise-privs)


Some details still remains.
The most important is what to do if the user answer no to the y-or-n-p
question:
- Continue to edit as normal user.
- Make buffer read-only and remove the pending char that will be
inserted into the buffer when post-ro-edit-hook completes (how do I
achieve that?).
- Leave buffer writable and remove pending char but ask the same
question again next time the user tries to change the buffer using the
hook before-change-functions.

Another improvement would be to be able to, trough TRAMP, become the
owner of the file when it is not equal to root nor (user-uid).

Any feedback is very welcome,
Per Nordlöw


reply via email to

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