9

I want to use emacs auto-save feature with tramp, but only for file that I don't open using sudo. My current configuration, based on the tramp documentation, looks like this:

;;;;;;;; BAKUCP ;;;;;;;;
;; Backup remote files locally to stop autosave pain
(setq tramp-backup-directory "~/.emacs-backup")
(unless (file-directory-p tramp-backup-directory)
  (make-directory tramp-backup-directory))
(if (file-accessible-directory-p tramp-backup-directory)
    (setq tramp-auto-save-directory tramp-backup-directory)
  (error "Cannot write to ~/.emacs-backup"))
;; Don't backup su and sudo files
(setq backup-enable-predicate
      (lambda (name)
        (and (normal-backup-enable-predicate name)
             (not
              (let ((method (file-remote-p name 'method)))
                (when (stringp method)
                  (member method '("su" "sudo"))))))))

It is forbidding creating remote backups of files edited as superuser but auto-saved files are still created on my local machine. Is there any way to disable that without disabling auto-save at all?

radious
  • 231
  • 1
  • 3

1 Answers1

2

Backup and auto-save are different operations, controlled by different variables.

If you want to discard auto-save for files opened as root, you might change the buffer-local variable auto-save-file-name-transforms. Something like this (untested):

(add-hook
 'find-file-hook
 (lambda ()
   (when (and (stringp buffer-file-name)
          (string-equal (file-remote-p buffer-file-name 'user) "root"))
     (setq buffer-auto-save-file-name nil))))
Michael Albinus
  • 6,647
  • 14
  • 20
  • If you put this in the users' `.emacs` file, it will not be read if emacs is opened with `sudo` though correct? So when would this code be in effect? – Startec Aug 16 '16 at 19:34
  • I don't understand your question. Everything in ~/.emacs is evaluated at start of Emacs, unless you start Emacs with the -Q or similar option. – Michael Albinus Aug 17 '16 at 07:25
  • Sorry, I meant that opening `emacs` with `sudo emacs` would not read the config file in the users' home directory. I realize now this is probably a good answer for when something is opened as sudo from an instance of emacs opened as a user. – Startec Aug 17 '16 at 07:32
  • What variable would check if the file were opened as root? (i.e. opened with the `/sudo::/` tramp method)? – Startec Aug 17 '16 at 07:33
  • `default-directory` is a buffer-local variable, which tells you the current directory of that buffer. `(file-remote-p default-directory)` returns non-nil, when you are remote. `(file-remote-p default-directory 'user)` gives you the remote user name, `"root"` for example. – Michael Albinus Aug 17 '16 at 08:58