[comp.emacs] how to change backup file names?

pausch@uvacs.cs.Virginia.EDU (Randy Pausch) (01/19/89)

Is there any way to change the way GNUemacs names backup files?  File
name completion in the ksh makes it a real pain to have the backup of
'foo' be in 'foo~'; optimally, I'd like GNUemacs to make a directory
that contains backup files, but I'd settle for backing up 'foo' in
'BAK.foo' or the like.  Any ideas?

thanks,

    pausch@virginia.edu

mike@arizona.edu (Mike Coffin) (01/19/89)

From article <2926@uvacs.cs.Virginia.EDU>, by pausch@uvacs.cs.Virginia.EDU (Randy Pausch):
> Is there any way to change the way GNUemacs names backup files?

Yes.  Change the function find-backup-file-name to do whatever you
want.  I prefer to prepend a "." on the front so that ls doesn't show
backups, so the following is in my .emacs file:

------------------------------------------------------------------------
;;; changed from original to put `.' before name of backup version
(defun find-backup-file-name (fn)
"Find a file name for a backup file, and suggestions for deletions.
Value is a list whose car is the name for the backup file
and whose cdr is a list of old versions to consider deleting now."
  (if (eq version-control 'never)
      (list (concat fn "~"))
    (let* ((base-versions (concat "." (file-name-nondirectory fn) ".~"))
	   (bv-length (length base-versions))
	   (possibilities (file-name-all-completions
			   base-versions
			   (file-name-directory fn)))
	   (versions (sort (mapcar 'backup-extract-version possibilities)
			   '<))
	   (high-water-mark (apply 'max (cons 0 versions)))
	   (deserve-versions-p
	    (or version-control
		(> high-water-mark 0)))
	   (number-to-delete (- (length versions)
				kept-old-versions kept-new-versions -1)))
      (if (not deserve-versions-p)
	  (list (concat fn "~"))
	(cons 
	 (concat (file-name-directory fn)
		 "."
		 (file-name-nondirectory fn)
		 ".~"
		 (int-to-string (1+ high-water-mark)) "~")
	 (if (> number-to-delete 0)
	     (mapcar 
	      (function (lambda (n)
			  (concat 
			   (file-name-directory fn)
			   "."
			   (file-name-nondirectory fn)
			   ".~" (int-to-string n) "~")))
	      (let ((v (nthcdr kept-old-versions versions)))
		(rplacd (nthcdr (1- number-to-delete) v) ())
		v))))))))
-- 
Mike Coffin				mike@arizona.edu
Univ. of Ariz. Dept. of Comp. Sci.	{allegra,cmcl2}!arizona!mike
Tucson, AZ  85721			(602)621-2858

Ram-Ashwin@cs.yale.edu (Ashwin Ram) (01/20/89)

In article <8776@megaron.arizona.edu>, mike@arizona.edu (Mike Coffin) writes:
> From article <2926@uvacs.cs.Virginia.EDU>, by pausch@uvacs.cs.Virginia.EDU (Randy Pausch):
> > Is there any way to change the way GNUemacs names backup files?
> 
> Yes.  Change the function find-backup-file-name to do whatever you
> want.

Hmmm... an 'apropos' on 'backup' reveals:

    find-backup-file-name:
    Find a file name for a backup file, and suggestions for deletions.
    Value is a list whose car is the name for the backup file
     and whose cdr is a list of old versions to consider deleting now.

and:

    make-backup-file-name:
    Create the non-numeric backup file name for FILE.
    This is a separate function so you can redefine it for customization.

Why are there two functions?  Does one call the other?  Which is the right
one to redefine for customization?

-- Ashwin.

jr@bbn.com (John Robinson) (01/20/89)

In article <47991@yale-celray.yale.UUCP>, Ram-Ashwin@cs (Ashwin Ram) writes:
>Hmmm... an 'apropos' on 'backup' reveals:
>
>    find-backup-file-name:
>
>and:
>
>    make-backup-file-name:
>
>Why are there two functions?  Does one call the other?  Which is the right
>one to redefine for customization?

A find-emacs-tag on one of these gets you to files.el, which has:

(defun make-backup-file-name (file)
  "Create the non-numeric backup file name for FILE.
This is a separate function so you can redefine it for customization."
  (concat file "~"))

;[... other defun deleted]

(defun find-backup-file-name (fn)
  "Find a file name for a backup file, and suggestions for deletions.
Value is a list whose car is the name for the backup file
 and whose cdr is a list of old versions to consider deleting now."
  (if (eq version-control 'never)
      (list (make-backup-file-name fn))
    (let* ((base-versions (concat (file-name-nondirectory fn) ".~"))
	   (bv-length (length base-versions))

;...

So the former (of Ashwin's list) calls the latter.  Redefining the
former is sufficient, unless you want to change the way
version-controlled backups look.  Version-control creates file
versions using backups of the form foo.~1~, foo.~2~, etc.  foo~ is
linked to the most recent backup.  More in the manual.

I think version-control will someday be part of GNU (the operating
system), so changing the form of these files if you use them may be a
little more serious departure from common practice.
--
/jr
jr@bbn.com or bbn!jr

mike@arizona.edu (Mike Coffin) (01/20/89)

From article <47991@yale-celray.yale.UUCP> by Ram-Ashwin@cs.yale.edu:
> Hmmm... an 'apropos' on 'backup' reveals:
>     find-backup-file-name: [...]
> and:
>     make-backup-file-name: [...]
> Why are there two functions?  Does one call the other?  Which is the right
> one to redefine for customization?

A quick look at the source reveals that find-backup-file-name calls
make-backup-file-name.  The latter is the one to change; I should have
looked before I shot my mouth off.  In my defense, I did the
modifications to find-backup-file-name quite a while ago; at that time
it did the file-name creation in-line.  Since it kept working, I never
noticed that there's now an easier way to do it.

The right way to do things now is to just fix make-backup-file-name.
-- 
Mike Coffin				mike@arizona.edu
Univ. of Ariz. Dept. of Comp. Sci.	{allegra,cmcl2}!arizona!mike
Tucson, AZ  85721			(602)621-2858