all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Changing name format for backup files
@ 2016-01-11 22:21 Nick Helm
  2016-01-11 23:26 ` Óscar Fuentes
  0 siblings, 1 reply; 6+ messages in thread
From: Nick Helm @ 2016-01-11 22:21 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-01-15  9:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-11 22:21 Changing name format for backup files Nick Helm
2016-01-11 23:26 ` Óscar Fuentes
2016-01-12 22:32   ` Nick Helm
2016-01-13 14:34     ` Alex Kost
2016-01-15  4:53       ` Nick Helm
2016-01-15  9:53         ` Alex Kost

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.