all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Nordlöw <per.nordlow@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Auto-Prompt for Password and Raise Privilegies when needed
Date: Wed, 5 May 2010 13:25:09 -0700 (PDT)	[thread overview]
Message-ID: <e550c8f7-a48c-4818-b158-788167ff7215@b18g2000yqb.googlegroups.com> (raw)
In-Reply-To: jwv4oinq95g.fsf-monnier+gnu.emacs.help@gnu.org

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	other threads:[~2010-05-05 20:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-03 16:50 Auto-Prompt for Password and Raise Privilegies when needed Nordlöw
2010-05-03 21:10 ` Andreas Politz
2010-05-04 14:00   ` quodlibetor
2010-05-04 14:17     ` Andreas Politz
2010-05-04 20:11       ` Andreas Politz
2010-05-04 14:56     ` Nordlöw
2010-05-05  4:14     ` Stefan Monnier
2010-05-05 20:25       ` Nordlöw [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e550c8f7-a48c-4818-b158-788167ff7215@b18g2000yqb.googlegroups.com \
    --to=per.nordlow@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.