[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Changing name format for backup files
From: |
Nick Helm |
Subject: |
Changing name format for backup files |
Date: |
Tue, 12 Jan 2016 11:21:33 +1300 |
User-agent: |
mu4e 0.9.15; emacs 24.5.1 |
I like to keep my emacs backup files in the same directory as the file I'm
editing, but I want the backups to be hidden in the GUI by default. I achieve
this by adding a prefix dot to the backup filenames by tweaking
`make-backup-file-name-1' like this:
(defun make-backup-file-name-1 (file)
"Subroutine of `make-backup-file-name--default-function'.
The function `find-backup-file-name' also uses this."
(let ((alist backup-directory-alist)
elt backup-directory abs-backup-directory)
(while alist
(setq elt (pop alist))
(if (string-match (car elt) file)
(setq backup-directory (cdr elt)
alist nil)))
;; If backup-directory is relative, it should be relative to the
;; file's directory. By expanding explicitly here, we avoid
;; depending on default-directory.
(if backup-directory
(setq abs-backup-directory
(expand-file-name backup-directory
(file-name-directory file))))
(if (and abs-backup-directory (not (file-exists-p abs-backup-directory)))
(condition-case nil
(make-directory abs-backup-directory 'parents)
(file-error (setq backup-directory nil
abs-backup-directory nil))))
(if (null backup-directory)
;; -----------8<------------------CHANGE HERE-----------------8<-------------
;; file
(let* ((file-path (file-name-directory file))
(file-name (file-name-nondirectory file)))
(concat file-path "." file-name))
;; -----------8<------------------CHANGE HERE-----------------8<-------------
(if (file-name-absolute-p backup-directory)
(progn
(when (memq system-type '(windows-nt ms-dos cygwin))
;; Normalize DOSish file names: downcase the drive
;; letter, if any, and replace the leading "x:" with
;; "/drive_x".
(or (file-name-absolute-p file)
(setq file (expand-file-name file))) ; make defaults explicit
;; Replace any invalid file-name characters (for the
;; case of backing up remote files).
(setq file (expand-file-name (convert-standard-filename file)))
(if (eq (aref file 1) ?:)
(setq file (concat "/"
"drive_"
(char-to-string (downcase (aref file 0)))
(if (eq (aref file 2) ?/)
""
"/")
(substring file 2)))))
;; Make the name unique by substituting directory
;; separators. It may not really be worth bothering about
;; doubling `!'s in the original name...
(expand-file-name
(subst-char-in-string
?/ ?!
(replace-regexp-in-string "!" "!!" file))
backup-directory))
(expand-file-name (file-name-nondirectory file)
(file-name-as-directory abs-backup-directory))))))
I do something similar to `make-auto-save-file-name' to achieve the same result
for auto save files.
Redefining core functions seems like a pretty heavy handed approach to the
problem though. Is there better way to achieve the same result? Or perhaps
there's a user option I missed in the manual?
Thanks,
Nick
- Changing name format for backup files,
Nick Helm <=