* Auto-Prompt for Password and Raise Privilegies when needed
@ 2010-05-03 16:50 Nordlöw
2010-05-03 21:10 ` Andreas Politz
0 siblings, 1 reply; 8+ messages in thread
From: Nordlöw @ 2010-05-03 16:50 UTC (permalink / raw)
To: help-gnu-emacs
The Finder in Mac OS X is very much ahead of everything else when it
comes to cleverness in auto-raising the user when needed to reach or
execute resources that require other privilegies than the current.
I tried to teach new Emacs user to remember the tramp-prefix /sudo::
or /su:: but unfortunately many users (ever engineers) are to lazy to
remember these things and in stressed situations they tend to revert
to really cumbersome and error-prone alternatives using copy, edit
elsewhere and copy-back which unfortunately may change permissions and
modes on the file.
Instead I really believe Emacs should use the Mac OS x way of thinking
when opening and modifying files (using find-file and alikes) that are
owned by other users.
When we try to edit a file that is owned by another user but writable
by the current user Emacs (through TRAMP) should ask for the password
of the user owning the file and then reopen the file by adding TRAMP-
sudo/su-prefix to the url.
I strongly believe this should be default behavior in Emacs.
The implement this we need a hook that is called every time the user
tries to change the content of the file. How do define that function?
I grepped for the string
Buffer is read-only
in the emacs cvs sources. This is printed each time we try to modify a
buffer that is read-only. This is an internal string. Do we have to
modify the builtins in this case? Or is there another way of solving
this problem?
Thanks in advance for any comments,
Per Nordlöw
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Auto-Prompt for Password and Raise Privilegies when needed
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
0 siblings, 1 reply; 8+ messages in thread
From: Andreas Politz @ 2010-05-03 21:10 UTC (permalink / raw)
To: help-gnu-emacs
Nordlöw <per.nordlow@gmail.com> writes:
>[...]
>
> Instead I really believe Emacs should use the Mac OS x way of thinking
> when opening and modifying files (using find-file and alikes) that are
> owned by other users.
>
> When we try to edit a file that is owned by another user but writable
> by the current user Emacs (through TRAMP) should ask for the password
> of the user owning the file and then reopen the file by adding TRAMP-
> sudo/su-prefix to the url.
>
> [...]
>
> The implement this we need a hook that is called every time the user
> tries to change the content of the file. How do define that function?
> I grepped for the string
>
> Buffer is read-only
>
> in the emacs cvs sources. This is printed each time we try to modify a
> buffer that is read-only. This is an internal string. Do we have to
> modify the builtins in this case? Or is there another way of solving
> this problem?
>
> Thanks in advance for any comments,
> Per Nordlöw
Sounds trivial. Though this may not scale well with other hooks.
(defun find-file-maybe-change-me ()
(when (not (file-writable-p buffer-file-name))
(find-alternate-file
(format "/sudo::%s" buffer-file-name))))
(add-hook 'find-file-hook 'find-file-maybe-change-me)
-ap
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Auto-Prompt for Password and Raise Privilegies when needed
2010-05-03 21:10 ` Andreas Politz
@ 2010-05-04 14:00 ` quodlibetor
2010-05-04 14:17 ` Andreas Politz
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: quodlibetor @ 2010-05-04 14:00 UTC (permalink / raw)
To: help-gnu-emacs
> Sounds trivial. Though this may not scale well with other hooks.
>
> (defun find-file-maybe-change-me ()
> (when (not (file-writable-p buffer-file-name))
> (find-alternate-file
> (format "/sudo::%s" buffer-file-name))))
>
> (add-hook 'find-file-hook 'find-file-maybe-change-me)
I think what Per was looking for was more something along the lines of
(defun edit-read-only-file-maybe ()
(when (y-or-n-p "file is read-only, raise privileges to edit? ")
(find-alternate-file
(format "/sudo::%s" buffer-file-name))))
(add-hook 'first-edit-read-only-file-hook 'edit-read-only-file-maybe)
with the main problem being that `first-edit-read-only-file-hook'
doesn't exist, and there don't seem to be any analogs. Or at least I
can't find them.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Auto-Prompt for Password and Raise Privilegies when needed
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
2 siblings, 1 reply; 8+ messages in thread
From: Andreas Politz @ 2010-05-04 14:17 UTC (permalink / raw)
To: help-gnu-emacs
quodlibetor <quodlibetor@gmail.com> writes:
>> Sounds trivial. Though this may not scale well with other hooks.
>>
>> (defun find-file-maybe-change-me ()
>> (when (not (file-writable-p buffer-file-name))
>> (find-alternate-file
>> (format "/sudo::%s" buffer-file-name))))
>>
>> (add-hook 'find-file-hook 'find-file-maybe-change-me)
>
> I think what Per was looking for was more something along the lines of
>
> (defun edit-read-only-file-maybe ()
> (when (y-or-n-p "file is read-only, raise privileges to edit? ")
> (find-alternate-file
> (format "/sudo::%s" buffer-file-name))))
>
> (add-hook 'first-edit-read-only-file-hook 'edit-read-only-file-maybe)
>
> with the main problem being that `first-edit-read-only-file-hook'
> doesn't exist, and there don't seem to be any analogs. Or at least I
> can't find them.
What about view-mode-hook ?
-ap
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Auto-Prompt for Password and Raise Privilegies when needed
2010-05-04 14:17 ` Andreas Politz
@ 2010-05-04 20:11 ` Andreas Politz
0 siblings, 0 replies; 8+ messages in thread
From: Andreas Politz @ 2010-05-04 20:11 UTC (permalink / raw)
To: help-gnu-emacs
Andreas Politz <politza@fh-trier.de> writes:
> quodlibetor <quodlibetor@gmail.com> writes:
>
>>> Sounds trivial. Though this may not scale well with other hooks.
>>>
>>> (defun find-file-maybe-change-me ()
>>> (when (not (file-writable-p buffer-file-name))
>>> (find-alternate-file
>>> (format "/sudo::%s" buffer-file-name))))
>>>
>>> (add-hook 'find-file-hook 'find-file-maybe-change-me)
>>
>> I think what Per was looking for was more something along the lines of
>>
>> (defun edit-read-only-file-maybe ()
>> (when (y-or-n-p "file is read-only, raise privileges to edit? ")
>> (find-alternate-file
>> (format "/sudo::%s" buffer-file-name))))
>>
>> (add-hook 'first-edit-read-only-file-hook 'edit-read-only-file-maybe)
>>
>> with the main problem being that `first-edit-read-only-file-hook'
>> doesn't exist, and there don't seem to be any analogs. Or at least I
>> can't find them.
>
We can try to implement one. I have not tested this, but the idea
should clear.
(add-hook 'find-file-hook
(unless (file-readable-p buffer-file-name)
(toggle-read-only -1)
(add-hook 'before-change-functions 'first-edit-hook nil t)))
(defvar first-edit-hook nil)
(defun first-edit-hook (&rest _)
(unwind-protect
(run-hooks 'first-edit-hook)
(remove-hook 'before-change-functions 'before-change-magic)))
-ap
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Auto-Prompt for Password and Raise Privilegies when needed
2010-05-04 14:00 ` quodlibetor
2010-05-04 14:17 ` Andreas Politz
@ 2010-05-04 14:56 ` Nordlöw
2010-05-05 4:14 ` Stefan Monnier
2 siblings, 0 replies; 8+ messages in thread
From: Nordlöw @ 2010-05-04 14:56 UTC (permalink / raw)
To: help-gnu-emacs
On May 4, 4:00 pm, quodlibetor <quodlibe...@gmail.com> wrote:
> > Sounds trivial. Though this may not scale well with other hooks.
>
> > (defun find-file-maybe-change-me ()
> > (when (not (file-writable-p buffer-file-name))
> > (find-alternate-file
> > (format "/sudo::%s" buffer-file-name))))
>
> > (add-hook 'find-file-hook 'find-file-maybe-change-me)
>
> I think what Per was looking for was more something along the lines of
>
> (defun edit-read-only-file-maybe ()
> (when (y-or-n-p "file is read-only, raise privileges to edit? ")
> (find-alternate-file
> (format "/sudo::%s" buffer-file-name))))
>
> (add-hook 'first-edit-read-only-file-hook 'edit-read-only-file-maybe)
>
> with the main problem being that `first-edit-read-only-file-hook'
> doesn't exist, and there don't seem to be any analogs. Or at least I
> can't find them.
You are right! This is what I was looking for.
But as you say, the first-edit-read-only-file-hook still remains to be
implemented. For now we could advice
- self-insert-command
- delete-char, delete-word, etc.
- tranpose, etc.
of course it is impossible to keep track of all these functions.
We should add a hook somewhere in the c sources that detects any
buffer modifications. Something for the Emacs developers...
We could of course also make the query a bit more clever;
Query for permission-change only if we open a that is read-only by
me but writable by another user or group.
But
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Auto-Prompt for Password and Raise Privilegies when needed
2010-05-04 14:00 ` quodlibetor
2010-05-04 14:17 ` Andreas Politz
2010-05-04 14:56 ` Nordlöw
@ 2010-05-05 4:14 ` Stefan Monnier
2010-05-05 20:25 ` Nordlöw
2 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2010-05-05 4:14 UTC (permalink / raw)
To: help-gnu-emacs
> 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Auto-Prompt for Password and Raise Privilegies when needed
2010-05-05 4:14 ` Stefan Monnier
@ 2010-05-05 20:25 ` Nordlöw
0 siblings, 0 replies; 8+ messages in thread
From: Nordlöw @ 2010-05-05 20:25 UTC (permalink / raw)
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2010-05-05 20:25 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).