From: Nordlöw <per.nordlow@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Follow-Up on Auto-Prompt for Password and Raise Privilegies when needed
Date: Fri, 7 May 2010 02:14:04 -0700 (PDT) [thread overview]
Message-ID: <6904a137-524d-4891-9b1e-05f580b049ff@d19g2000yqf.googlegroups.com> (raw)
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 other threads:[~2010-05-07 9:14 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=6904a137-524d-4891-9b1e-05f580b049ff@d19g2000yqf.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.