<div dir="ltr"><div><div><div>Here's the latest version. I switched to the new style of advising functions, which is actually convenient because now I can use the same definition to advise multiple functions. It also now keeps track of point, and retries the attempted command.<br><br></div><div>(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.<br><br><br>(defun autosudo--edit-command-wrapper (orig-fun &rest args)<br>  "Ask to re-open a file with sudo if it's read-only and you try to edit it.<br>Will maintain the position of point, and insert the typed character after<br>switching to the sudo version."<br>  (if buffer-read-only<br>      (let ((path (buffer-file-name))<br>            (buff (current-buffer))<br>            (point (point))<br>            (event last-command-event))<br>        (when (and path (y-or-n-p "File is read-only, reopen with sudo?"))<br>          (find-file (concat "/sudo:root@localhost:" path))<br>          (goto-char point)<br>          (kill-buffer buff)<br>          (setq last-command-event event)))))<br><br>(advice-add 'self-insert-command :before #'autosudo--edit-command-wrapper)<br>(advice-add 'newline :before #'autosudo--edit-command-wrapper)<br>(advice-add 'yank :before #'autosudo--edit-command-wrapper)<br>(advice-add 'indent-for-tab-command :before #'autosudo--edit-command-wrapper)<br><br><br></div><div>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?<br><br></div><div>I also like the idea to make it work on remote servers, but that might be a next step.<br><br></div><div>I'd like to make this a repo on Github under Emacs Berlin, and submit it to MELPA, since I try to avoid custom functions in my config that aren't on (M)ELPA :)<br></div><br></div><div>Til, your version is also cool, for instance Magnar Sveen and Spacemacs include this function<br><br>(defun sudo-edit (&optional arg)<br>  (interactive "p")<br>  (if (or arg (not buffer-file-name))<br>      (find-file (concat "/sudo:root@localhost:" (ido-read-file-name "File: ")))<br>    (find-alternate-file (concat "/sudo:root@localhost:" buffer-file-name))))<br><br></div><div>But I like the automatic version personally.<br></div><div><br></div>Cheers,<br></div>Arne<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 24 August 2015 at 23:30, Rasmus <span dir="ltr"><<a href="mailto:rasmus@gmx.us" target="_blank">rasmus@gmx.us</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">Arne Brasseur <<a href="mailto:arne.brasseur@gmail.com">arne.brasseur@gmail.com</a>> writes:<br>
<br>
> (defadvice self-insert-command (around automagic-sudo activate)<br>
<br>
</span>Perhaps you'd also want to look into the "nadvice" style of advising,<br>
<br>
        <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Named-Functions.html" rel="noreferrer" target="_blank">https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Named-Functions.html</a><br>
        <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Porting-old-advices.html" rel="noreferrer" target="_blank">https://www.gnu.org/software/emacs/manual/html_node/elisp/Porting-old-advices.html</a><br>
<span class="HOEnZb"><font color="#888888"><br>
Rasmus<br>
<br>
--<br>
Hooray!<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
emacs-berlin mailing list<br>
<a href="mailto:emacs-berlin@emacs-berlin.org">emacs-berlin@emacs-berlin.org</a><br>
<a href="https://mailb.org/mailman/listinfo/emacs-berlin" rel="noreferrer" target="_blank">https://mailb.org/mailman/listinfo/emacs-berlin</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature"><div dir="ltr"><div>| <a href="https://twitter.com/plexus" target="_blank">@plexus</a>  | <a href="http://arnebrasseur.net" target="_blank">arnebrasseur.net</a> | <a href="https://leanpub.com/happylambda" target="_blank">The Happy Lambda</a> |<br></div></div></div>
</div>