[emacs-berlin] Automatic sudo

Michael Albinus michael.albinus at gmx.de
Thu Aug 27 13:32:35 UTC 2015

Arne Brasseur <arne.brasseur at gmail.com> writes:

Hi Arne,

> (setq last-command-event) is needed because, when prompted for a
> password, and then re-executing self-insert-command afterwards, it
> will insert the <return> that terminated the password into the buffer,
> instead of the key that triggered this advice.

Overwriting `last-command-event' (or `'last-input-event') is regarded
risky, because in the meantime other events could have arrived, while
you are in `autosudo--edit-command-wrapper'. Think about file
notification events, or D-Bus events, whatever. Their handlers would be
surprised, if they see an event of an unexpected type.

The proper way to handle this is to push the event back on top of
`unread-command-events', like

(setq unread-command-events (cons event unread-command-events))

> (defun autosudo--edit-command-wrapper (orig-fun &rest args)

Pls remove `orig-fun'; you don't need it, and I even believe it isn't
passed as argument.

> So this is already pretty close. It works fine for self-insert-command
> and for tab, but not for newline and yank. Any ideas what the
> difference would be?

Its the `interactive' spec of the functions:

(interactive-form 'self-insert-command)
=> (interactive "p")

(interactive-form 'indent-for-tab-command)
=> (interactive "P")

(interactive-form 'newline)
=> (interactive "*P\np")

(interactive-form 'yank)
=> (interactive "*P")

The "*" as the first character in `'newline' and `'yank' specs means,
that an error is signalled if the current buffer is read-only. See

(info "(elisp) Interactive Codes")

> I also like the idea to make it work on remote servers, but that might
> be a next step.

I'll be happy to help you if needed :-)

Another addition which might be needed: Pls remember when the user
denies to reopen the file as sudo. Otherwise, she will be asked several
times. A buffer-local variable shall do.

> Cheers,
> Arne

Best regards, Michael.

More information about the emacs-berlin mailing list