annotate lisp/packages/backup-dir.el @ 207:e45d5e7c476e r20-4b2

Import from CVS: tag r20-4b2
author cvs
date Mon, 13 Aug 2007 10:03:52 +0200
parents bcdc7deadc19
children 78f53ef88e17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
1 ;;; BACKUP-DIR.EL: Emacs functions to allow backup files to live in
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
2 ;;; some other directory(s). Version 2.0
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
3 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
4 ;;; Copyright (C) 1992-97 Greg Klanderman
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
5 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
6 ;;; This program is free software; you can redistribute it and/or modify
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
7 ;;; it under the terms of the GNU General Public License as published by
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
8 ;;; the Free Software Foundation; either version 1, or (at your option)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
9 ;;; any later version.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
10 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
11 ;;; This program is distributed in the hope that it will be useful,
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
14 ;;; GNU General Public License for more details.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
15 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
16 ;;; A copy of the GNU General Public License can be obtained from
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
17 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
18 ;;; 02139, USA.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
19 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
20 ;;; Send bug reports, etc. to greg@alphatech.com or gregk@ai.mit.edu.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
21 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
22 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
23 ;;; Modification History
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
24 ;;; ====================
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
25 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
26 ;;; 12/28/1996 Version 2.0
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
27 ;;; Updated for XEmacs 19.15b4, much of code reorganized & cleaned up
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
28 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
29 ;;; 12/27/1996 Version 1.6
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
30 ;;; explicit loading of dired replaced to use dired-load-hook
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
31 ;;; (suggested by Thomas Feuster, feuster@tp4.physik.uni-giessen.de)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
32 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
33 ;;; 12/2/1996 Version 1.5
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
34 ;;; Took out obsolete byte compiler options
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
35 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
36 ;;; 9/24/1996 Version 1.4
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
37 ;;; Fix some bugs, change to alist OPTIONS list (ok-create, full-path..) from
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
38 ;;; separate fields for each option variable. Added search-upward option.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
39 ;;; Added new function `find-file-latest-backup' to find a file's latest backup.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
40 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
41 ;;; 1/26/1996 Version 1.3
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
42 ;;; Name change to backup-dir.el
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
43 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
44 ;;; 3/22/1995 Version 1.2
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
45 ;;; Added new definitions for functions `file-newest-backup', `latest-backup-file',
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
46 ;;; and `diff-latest-backup-file' so various other emacs functions will find the
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
47 ;;; right backup files.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
48 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
49 ;;; 4/23/1993 Version 1.1
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
50 ;;; Reworked to allow different behavior for different files based on the
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
51 ;;; alist `bkup-backup-directory-info'.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
52 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
53 ;;; Fall 1992 Version 1.0
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
54 ;;; Name change and added ability to make directories absolute. Added the
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
55 ;;; full path stuff to make backup name unique for absolute directories.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
56 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
57 ;;; Spring 1992 Version 0.0
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
58 ;;; Original
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
59 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
60 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
61 ;;; Description:
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
62 ;;; ============
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
63 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
64 ;;; Allows backup files to be optionally stored in some directories, based on
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
65 ;;; the value of the alist, `bkup-backup-directory-info'. This variable is a
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
66 ;;; list of lists of the form (FILE-REGEXP BACKUP-DIR OPTIONS ...). If the
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
67 ;;; filename to be backed up matches FILE-REGEXP, or FILE-REGEXP is t, then
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
68 ;;; BACKUP-DIR is used as the path for its backups. Directories may begin with
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
69 ;;; "/" to specify an absolute pathname. If BACKUP-DIR does not exist and
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
70 ;;; OPTIONS contains the symbol `ok-create', then it is created if possible.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
71 ;;; Otherwise the usual behavior (backup in the same directory as the file)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
72 ;;; results. If OPTIONS contains the symbol `full-path', then the full path of
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
73 ;;; the file being backed up is prepended to the backup file name, with each "/"
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
74 ;;; replaced by a "!". This is intended for cases where an absolute backup path
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
75 ;;; is used. If OPTIONS contains the symbol `search-upward' and the backup
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
76 ;;; directory BACKUP-DIR is a relative path, then a directory with that name is
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
77 ;;; searched for starting at the current directory and proceeding upward (..,
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
78 ;;; ../.., etc) until one is found of that name or the root is reached, and if
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
79 ;;; one is found it is used as the backup directory. Finally, if no FILE-REGEXP
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
80 ;;; matches the file name being backed up, then the usual behavior results.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
81 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
82 ;;; These lines from my .emacs load this file and set the values I like:
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
83 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
84 ;;; (require 'backup-dir)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
85 ;;; (setq bkup-backup-directory-info
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
86 ;;; '(("/home/greg/.*" "/~/.backups/" ok-create full-path)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
87 ;;; (t ".backups/" full-path search-upward)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
88 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
89 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
90 ;;; The package also provides a new function, `find-file-latest-backup' to find
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
91 ;;; the latest backup file for the current buffer's file.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
92 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
93 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
94 ;;; This file is based on `files.el' from XEmacs 19.15b4.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
95 ;;; It has not been extensively tested on GNU Emacs past 18.58.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
96 ;;; It does not work under ms-dos.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
97
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
98
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
99 (byte-compiler-options
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
100 (optimize t)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
101 (warnings (- free-vars)) ; Don't warn about free variables
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
102 )
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
103
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
104
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
105 ;;; New variables affecting backup file behavior
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
106 ;;; This is the only user-customizable variable for this package.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
107 ;;;
207
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
108 (defcustom bkup-backup-directory-info nil
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
109 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
12
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
110 If the filename to be backed up matches FILE-REGEXP, or FILE-REGEXP is t,
207
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
111 then BACKUP-DIR is used as the path for its backups.
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
112
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
113 Directories may begin with \"/\" to specify an absolute pathname.
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
114
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
115 If BACKUP-DIR does not exist and OPTIONS contains the symbol `ok-create',
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
116 then it is created if possible. Otherwise the usual behavior (backup in the
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
117 same directory as the file) results.
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
118
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
119 If OPTIONS contains the symbol `full-path', then the full path of the file
12
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
120 being backed up is prepended to the backup file name, with each \"/\"
207
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
121 replaced by a \"!\". This is intended for cases where an absolute backup
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
122 path is used.
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
123
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
124 If OPTIONS contains the symbol `search-upward' and the backup directory
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
125 BACKUP-DIR is a relative path, then a directory with that name is searched
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
126 for starting at the current directory and proceeding upward (.., ../.., etc)
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
127 until one is found of that name, or the root is reached, and if one is found
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
128 it is used as the backup directory.
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
129
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
130 Finally, if no FILE-REGEXP matches the file name being backed up, then the
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
131 usual behavior results.
12
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
132
207
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
133 Once you save this variable with `M-x customize-variable',
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
134 `backup-dir' will be loaded for you each time you start XEmacs."
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
135 :type '(repeat
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
136 (list (regexp :tag "File regexp")
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
137 (string :tag "Backup Dir")
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
138 (set :inline t
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
139 (const ok-create)
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
140 (const full-path)
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
141 (const search-upward))))
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
142 :require 'backup-dir
e45d5e7c476e Import from CVS: tag r20-4b2
cvs
parents: 12
diff changeset
143 :group 'backup)
12
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
144
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
145 ;;; New functions
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
146 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
147 (defun bkup-search-upward-for-backup-dir (base bd-name)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
148 "search upward for a directory named BD-NAME, starting in the
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
149 directory BASE and continuing with its parent directories until
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
150 one is found or the root is reached."
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
151 (let ((prev nil) (curr base) (gotit nil) (tryit nil))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
152 (while (and (not gotit)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
153 (not (equal prev curr))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
154 (not (equal curr "//")))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
155 (setq prev curr)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
156 (setq curr (expand-file-name (concat curr "../")))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
157 (setq tryit (expand-file-name bd-name curr))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
158 (if (and (file-directory-p tryit) (file-exists-p tryit))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
159 (setq gotit tryit)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
160 (if (and gotit
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
161 (eq (aref gotit (1- (length gotit))) ?/))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
162 (setq gotit (substring gotit 0 (1- (length gotit)))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
163 gotit))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
164
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
165 (defun bkup-replace-slashes-with-exclamations (s)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
166 "Replaces slashes in the string S with exclamations.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
167 A new string is produced and returned."
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
168 (let ((ns (copy-sequence s))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
169 (i (1- (length s))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
170 (while (>= i 0)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
171 (if (= (aref ns i) ?/)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
172 (aset ns i ?!))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
173 (setq i (1- i)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
174 ns))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
175
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
176 (defun bkup-try-making-directory (dir)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
177 "try making directory DIR, return non-nil if successful"
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
178 (condition-case ()
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
179 (progn (make-directory dir t)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
180 t)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
181 (t
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
182 nil)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
183
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
184 (defun bkup-backup-basename (file full-path)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
185 "Gives the base part of the backup name for FILE, according to FULL-PATH."
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
186 (if full-path
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
187 (bkup-replace-slashes-with-exclamations file)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
188 (file-name-nondirectory file)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
189
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
190 (defun bkup-backup-directory-and-basename (file)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
191 "Return the cons of the backup directory name
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
192 and backup file name base for FILE."
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
193 (let ((file (expand-file-name file)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
194 (let ((dir (file-name-directory file))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
195 (alist bkup-backup-directory-info)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
196 (bk-dir nil)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
197 (bk-base nil))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
198 (if (listp alist)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
199 (while (and (not bk-dir) alist)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
200 (if (or (eq (car (car alist)) t)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
201 (eq (string-match (car (car alist)) file) 0))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
202 (let* ((bd (car (cdr (car alist))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
203 (bd-rel-p (and (> (length bd) 0)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
204 (not (eq (aref bd 0) ?/))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
205 (bd-expn (expand-file-name bd dir))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
206 (bd-noslash (if (eq (aref bd-expn (1- (length bd-expn))) ?/)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
207 (substring bd-expn 0 (1- (length bd-expn)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
208 bd-expn))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
209 (options (cdr (cdr (car alist))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
210 (ok-create (and (memq 'ok-create options) t))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
211 (full-path (and (memq 'full-path options) t))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
212 (search-upward (and (memq 'search-upward options) t)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
213 (if bd-expn
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
214 (cond ((or (file-directory-p bd-expn)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
215 (and ok-create
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
216 (not (file-exists-p bd-expn))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
217 (bkup-try-making-directory bd-noslash)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
218 (setq bk-dir (concat bd-noslash "/")
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
219 bk-base (bkup-backup-basename file full-path)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
220 ((and bd-rel-p search-upward)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
221 (let ((bd-up (bkup-search-upward-for-backup-dir dir bd)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
222 (if bd-up
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
223 (setq bk-dir (concat bd-up "/")
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
224 bk-base (bkup-backup-basename file full-path)))))))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
225 (setq alist (cdr alist))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
226 (if (and bk-dir bk-base)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
227 (cons bk-dir bk-base)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
228 (cons dir (bkup-backup-basename file nil))))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
229
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
230
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
231 ;;; This next one is based on the following from `files.el'
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
232 ;;; but accepts a second optional argument
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
233
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
234 ;;(defun make-backup-file-name (file)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
235 ;; "Create the non-numeric backup file name for FILE.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
236 ;;This is a separate function so you can redefine it for customization."
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
237 ;; (if (and (eq system-type 'ms-dos)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
238 ;; (not (msdos-long-file-names)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
239 ;; (let ((fn (file-name-nondirectory file)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
240 ;; (concat (file-name-directory file)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
241 ;; (if (string-match "\\([^.]*\\)\\(\\..*\\)?" fn)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
242 ;; (substring fn 0 (match-end 1)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
243 ;; ".bak"))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
244 ;; (concat file "~")))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
245
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
246 (defun bkup-make-backup-file-name (file &optional dir-n-base)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
247 "Create the non-numeric backup file name for FILE.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
248 Optionally accept a list containing the backup directory and
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
249 backup basename. NB: we don't really handle ms-dos."
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
250 (if (and (eq system-type 'ms-dos)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
251 (not (and (fboundp 'msdos-long-file-names) (msdos-long-file-names))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
252 (let ((fn (file-name-nondirectory file)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
253 (concat (file-name-directory file)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
254 (if (string-match "\\([^.]*\\)\\(\\..*\\)?" fn)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
255 (substring fn 0 (match-end 1)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
256 ".bak"))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
257 (let ((d-n-b (or dir-n-base
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
258 (bkup-backup-directory-and-basename file))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
259 (concat (car d-n-b) (cdr d-n-b) "~"))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
260
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
261 (defun bkup-existing-backup-files (fn)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
262 "Return list of existing backup files for file"
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
263 (let* ((efn (expand-file-name fn))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
264 (dir-n-base (bkup-backup-directory-and-basename efn))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
265 (non-num-bk-name (bkup-make-backup-file-name efn dir-n-base))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
266 (non-num-bk (file-exists-p non-num-bk-name))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
267 (backup-dir (car dir-n-base))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
268 (base-versions (concat (cdr dir-n-base) ".~"))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
269 (possibilities (file-name-all-completions base-versions backup-dir))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
270 (poss (mapcar #'(lambda (name) (concat backup-dir name)) possibilities)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
271 (mapcar #'expand-file-name
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
272 (if non-num-bk (cons non-num-bk-name poss) poss))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
273
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
274 (defun find-file-latest-backup (file)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
275 "Find the latest backup file for FILE"
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
276 (interactive (list (read-file-name (format "Find latest backup of file (default %s): "
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
277 (file-name-nondirectory (buffer-file-name)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
278 nil (buffer-file-name) t)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
279 (let ((backup (file-newest-backup file)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
280 (if backup
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
281 (find-file backup)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
282 (message "no backups found for `%s'" file))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
283
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
284
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
285 ;;; Functions changed from `files.el' and elsewhere -- originals precede new versions
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
286
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
287 ;;(defun make-backup-file-name (file)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
288 ;; "Create the non-numeric backup file name for FILE.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
289 ;;This is a separate function so you can redefine it for customization."
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
290 ;; (if (and (eq system-type 'ms-dos)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
291 ;; (not (msdos-long-file-names)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
292 ;; (let ((fn (file-name-nondirectory file)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
293 ;; (concat (file-name-directory file)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
294 ;; (if (string-match "\\([^.]*\\)\\(\\..*\\)?" fn)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
295 ;; (substring fn 0 (match-end 1)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
296 ;; ".bak"))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
297 ;; (concat file "~")))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
298
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
299 (defun make-backup-file-name (file)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
300 "Create the non-numeric backup file name for FILE.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
301 This is a separate function so you can redefine it for customization.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
302 *** Changed by \"backup-dir.el\""
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
303 (bkup-make-backup-file-name file))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
304
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
305
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
306 ;;(defun find-backup-file-name (fn)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
307 ;; "Find a file name for a backup file, and suggestions for deletions.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
308 ;;Value is a list whose car is the name for the backup file
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
309 ;; and whose cdr is a list of old versions to consider deleting now.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
310 ;;If the value is nil, don't make a backup."
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
311 ;; (let ((handler (find-file-name-handler fn 'find-backup-file-name)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
312 ;; ;; Run a handler for this function so that ange-ftp can refuse to do it.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
313 ;; (if handler
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
314 ;; (funcall handler 'find-backup-file-name fn)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
315 ;; (if (eq version-control 'never)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
316 ;; (list (make-backup-file-name fn))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
317 ;; (let* ((base-versions (concat (file-name-nondirectory fn) ".~"))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
318 ;; ;; used by backup-extract-version:
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
319 ;; (bv-length (length base-versions))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
320 ;; possibilities
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
321 ;; (versions nil)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
322 ;; (high-water-mark 0)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
323 ;; (deserve-versions-p nil)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
324 ;; (number-to-delete 0))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
325 ;; (condition-case ()
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
326 ;; (setq possibilities (file-name-all-completions
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
327 ;; base-versions
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
328 ;; (file-name-directory fn))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
329 ;; versions (sort (mapcar
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
330 ;; #'backup-extract-version
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
331 ;; possibilities)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
332 ;; '<)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
333 ;; high-water-mark (apply #'max 0 versions)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
334 ;; deserve-versions-p (or version-control
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
335 ;; (> high-water-mark 0))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
336 ;; number-to-delete (- (length versions)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
337 ;; kept-old-versions kept-new-versions -1))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
338 ;; (file-error
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
339 ;; (setq possibilities nil)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
340 ;; (if (not deserve-versions-p)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
341 ;; (list (make-backup-file-name fn))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
342 ;; (cons (concat fn ".~" (int-to-string (1+ high-water-mark)) "~")
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
343 ;; (if (and (> number-to-delete 0)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
344 ;; ;; Delete nothing if there is overflow
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
345 ;; ;; in the number of versions to keep.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
346 ;; (>= (+ kept-new-versions kept-old-versions -1) 0))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
347 ;; (mapcar #'(lambda (n)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
348 ;; (concat fn ".~" (int-to-string n) "~"))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
349 ;; (let ((v (nthcdr kept-old-versions versions)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
350 ;; (rplacd (nthcdr (1- number-to-delete) v) ())
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
351 ;; v))))))))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
352
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
353 (defun find-backup-file-name (fn)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
354 "Find a file name for a backup file, and suggestions for deletions.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
355 Value is a list whose car is the name for the backup file
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
356 and whose cdr is a list of old versions to consider deleting now.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
357 If the value is nil, don't make a backup.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
358 *** Changed by \"backup-dir.el\""
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
359 (let ((handler (find-file-name-handler fn 'find-backup-file-name)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
360 ;; Run a handler for this function so that ange-ftp can refuse to do it.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
361 (if handler
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
362 (funcall handler 'find-backup-file-name fn)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
363 (if (eq version-control 'never)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
364 (list (make-backup-file-name fn))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
365 (let* ((dir-n-base (bkup-backup-directory-and-basename fn)) ;add
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
366 (non-num-bk-name (bkup-make-backup-file-name fn dir-n-base)) ;add
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
367 (bk-dir (car dir-n-base)) ;add
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
368 (bk-base (cdr dir-n-base)) ;add
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
369 (base-versions (concat bk-base ".~")) ;mod
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
370 ;; used by backup-extract-version:
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
371 (bv-length (length base-versions))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
372 possibilities
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
373 (versions nil)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
374 (high-water-mark 0)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
375 (deserve-versions-p nil)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
376 (number-to-delete 0))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
377 (condition-case ()
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
378 (setq possibilities (file-name-all-completions
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
379 base-versions
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
380 bk-dir) ;mod
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
381 versions (sort (mapcar
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
382 #'backup-extract-version
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
383 possibilities)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
384 '<)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
385 high-water-mark (apply #'max 0 versions)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
386 deserve-versions-p (or version-control
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
387 (> high-water-mark 0))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
388 number-to-delete (- (length versions)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
389 kept-old-versions kept-new-versions -1))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
390 (file-error
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
391 (setq possibilities nil)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
392 (if (not deserve-versions-p)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
393 (list (bkup-make-backup-file-name fn dir-n-base)) ;mod
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
394 (cons (concat bk-dir base-versions (int-to-string (1+ high-water-mark)) "~") ;mod
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
395 (if (and (> number-to-delete 0)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
396 ;; Delete nothing if there is overflow
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
397 ;; in the number of versions to keep.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
398 (>= (+ kept-new-versions kept-old-versions -1) 0))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
399 (mapcar #'(lambda (n)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
400 (concat bk-dir base-versions (int-to-string n) "~")) ;mod
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
401 (let ((v (nthcdr kept-old-versions versions)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
402 (rplacd (nthcdr (1- number-to-delete) v) ())
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
403 v))))))))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
404
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
405
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
406 ;;(defun file-newest-backup (filename)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
407 ;; "Return most recent backup file for FILENAME or nil if no backups exist."
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
408 ;; (let* ((filename (expand-file-name filename))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
409 ;; (file (file-name-nondirectory filename))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
410 ;; (dir (file-name-directory filename))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
411 ;; (comp (file-name-all-completions file dir))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
412 ;; newest tem)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
413 ;; (while comp
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
414 ;; (setq tem (car comp)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
415 ;; comp (cdr comp))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
416 ;; (cond ((and (backup-file-name-p tem)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
417 ;; (string= (file-name-sans-versions tem) file))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
418 ;; (setq tem (concat dir tem))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
419 ;; (if (or (null newest)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
420 ;; (file-newer-than-file-p tem newest))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
421 ;; (setq newest tem)))))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
422 ;; newest))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
423
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
424 (defun file-newest-backup (filename)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
425 "Return most recent backup file for FILENAME or nil if no backups exist.
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
426 *** Changed by \"backup-dir.el\""
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
427 (let ((comp (bkup-existing-backup-files filename))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
428 (newest nil)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
429 (file nil))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
430 (while comp
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
431 (setq file (car comp)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
432 comp (cdr comp))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
433 (if (and (backup-file-name-p file)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
434 (or (null newest) (file-newer-than-file-p file newest)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
435 (setq newest file)))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
436 newest))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
437
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
438
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
439 ;;; patch `latest-backup-file' from "dired"
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
440 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
441 ;;; we use `dired-load-hook' to avoid loading dired now. This speeds things up
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
442 ;;; considerably according to Thomas Feuster, feuster@tp4.physik.uni-giessen.de
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
443 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
444 ;;; one really wonders why there are 3 functions to do the same thing...
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
445 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
446 (defun bkup-patch-latest-backup-file ()
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
447 (fset 'latest-backup-file (symbol-function 'file-newest-backup))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
448 (remove-hook 'dired-load-hook 'bkup-patch-latest-backup-file))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
449
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
450 (if (featurep 'dired)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
451 ;; if loaded, patch it now
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
452 (fset 'latest-backup-file (symbol-function 'file-newest-backup))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
453 ;; otherwise do it later
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
454 (add-hook 'dired-load-hook 'bkup-patch-latest-backup-file))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
455
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
456
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
457 ;;; patch `diff-latest-backup-file' from "diff"
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
458 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
459 (require 'diff)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
460 (fset 'diff-latest-backup-file (symbol-function 'file-newest-backup))
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
461
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
462
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
463 ;;; finally, add to list of features
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
464 ;;;
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
465 (provide 'backup-dir)
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
466
bcdc7deadc19 Import from CVS: tag r19-15b7
cvs
parents:
diff changeset
467 ;;; backup-dir.el ends here