For the fun of it, let me describe a radically different approach, based on directory variables.
In short, you would put in your work-directory/
a file named .dir-locals.el
containing:
((nil . ((eval . (set (make-local-variable 'backup-directory-alist)
(list (cons "."
(file-relative-name
(file-name-directory (buffer-file-name))
(file-name-directory (file-truename
(buffer-file-name)))))))))))
What this does is abusing somewhat the backup-directory-alist
, and install a local version
of it for all your files in work-directory/
. That local version will in turn make sure that any backup file is kept within work-directory
.
In order to achieve that, we need 2 things:
- have something like
'(("." . "path/to/work-directory/"))
as the local value
- make sure this path is relative to
location_a/
The reason for the second point is that as noted elsewhere, the starting point of backup-buffer
is indeed the location of the actual file, once symlinks are resolved. And we can't simply put the absolute path without having backup files changing shape (in case of absolute path for the backup directory, the backup filenames encode the complete path, so that there is no collision)
Notes:
- you'll need to make sure that specific local variable is recorded in the
safe-local-variable-values
. Since it's a generic form, it's a one time job though (just hit "!" the first time you're asked about it)
- this assumes
find-file-visit-truename
is set to nil, but I guess you wouldn't ask that question if that was not the case :)
Pros of the approach:
- no need for advice (which is always a good thing)
- reasonably portable although it assumes your Emacs supports directory variables
- you keep the flexibility to put that in place only where you need it
Cons of the approach:
- well, obviously you might have to copy that
.dir-locals.el
in several places
Also note that if you wanted a one-shot approach, you could make it much simpler, such as:
((nil . ((backup-directory-alist (("." . "../path/to/work-directory"))))))
where you actually compute the relative name yourself, once and for all.