all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Follow-Up on Auto-Prompt for Password and Raise Privilegies when  needed
@ 2010-05-07  9:14 Nordlöw
  0 siblings, 0 replies; only message in thread
From: Nordlöw @ 2010-05-07  9:14 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2010-05-07  9:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-07  9:14 Follow-Up on Auto-Prompt for Password and Raise Privilegies when needed Nordlöw

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.