4

In general I want Emacs to save a backup file for me, except in some special directories.

I tried to define a Directory Local Variables, and add:

((nil . ((make-backup-files . nil))))

But it seem it won't take effect. I think it is because make-backup-files is a global variable.

Is there a equivalent local variable to control the backup creation function?

Drew
  • 75,699
  • 9
  • 109
  • 225
David S.
  • 395
  • 2
  • 13

1 Answers1

3

Backup decisions seem to pass through the function normal-backup-enable-predicate, via the variable backup-enable-predicate which is set to this by default. You can provide your own function to return nil if no backup is to be done. For example,

(defun my-backup-enable-predicate (name)
  (let (found)
    (dolist (specialdir '("/somedir/" "/some/other/") found)
      (if (string-prefix-p specialdir name)
          (setq found t)))
    (if found
        nil
      (normal-backup-enable-predicate name))))

(setq backup-enable-predicate #'my-backup-enable-predicate)

This tests if the provided full pathname begins with one of the listed directories (/somedir/ or /some/other/ in this example) and if so returns nil, otherwise calls the usual backup test function. The final line make emacs use your function. To go back to the real function use

(setq backup-enable-predicate #'normal-backup-enable-predicate)

I reread your question and realised I hadn't answered it. To allow you to set make-backup-files in your .dir-locals.el I think all you need is to make it buffer local in your startup file:

(make-variable-buffer-local 'make-backup-files)

The documentation says "Buffer-local bindings are normally cleared while setting up a new major mode, unless they have a 'permanent-local' property.". You can set this property with

(put 'make-backup-files 'permanent-local 't)
meuh
  • 160
  • 2
  • 7
  • This is not what I want...but seems to be the closest I could get. Thx~ – David S. May 16 '16 at 12:31
  • @davidshen84 Sorry, I think I had forgotten the important part of your question when I got round to my answer. I think there is a much simpler solution available for you, see updated answer. – meuh May 16 '16 at 14:25
  • I found `make-variable-buffer-local` do not work for me. I have to use `make-local-variable`. Any idea? – David S. May 24 '16 at 06:29
  • @davidshen84 Perhaps the major-mode is clearing the buffer-local binding, as mentioned in the doc. See the new last line in my answer for how to set a property to avoid this (in your startup file). – meuh May 24 '16 at 07:18