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

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

Re: Auto-Prompt for Password and Raise Privilegies when needed


From: Nordlöw
Subject: Re: Auto-Prompt for Password and Raise Privilegies when needed
Date: Wed, 5 May 2010 13:25:09 -0700 (PDT)
User-agent: G2/1.0

On 5 Maj, 06:14, Stefan Monnier <monn...@iro.umontreal.ca> wrote:
> > with the main problem being that `first-edit-read-only-file-hook'
> > doesn't exist, and there don't seem to be any analogs.
>
> There's first-change-hook, which is somewhat close.
> Of course, this only works if the buffer is writable (i.e. if the buffer
> is read-only, attempts to modify the buffer will signal an error before
> getting a chance to run this hook).
>
>         Stefan

This works really nicely for me.


(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."
  (when (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-point p)) ;goto same point in reopened file so that
pending changes occurr at right position
    (unwind-protect (run-hooks 'post-ro-edit-hook))))

(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)


/Per


reply via email to

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