]> code.delx.au - gnu-emacs/blob - lispref/backups.texi
Initial revision
[gnu-emacs] / lispref / backups.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/backups
6 @node Backups and Auto-Saving, Buffers, Files, Top
7 @chapter Backups and Auto-Saving
8
9 Backup files and auto-save files are two methods by which Emacs tries
10 to protect the user from the consequences of crashes or of the user's
11 own errors. Auto-saving preserves the text from earlier in the current
12 editing session; backup files preserve file contents prior to the
13 current session.
14
15 @menu
16 * Backup Files:: How backup files are made; how their names are chosen.
17 * Auto-Saving:: How auto-save files are made; how their names are chosen.
18 * Reverting:: @code{revert-buffer}, and how to customize what it does.
19 @end menu
20
21 @node Backup Files, Auto-Saving, Backups and Auto-Saving, Backups and Auto-Saving
22 @section Backup Files
23 @cindex backup file
24
25 A @dfn{backup file} is a copy of the old contents of a file you are
26 editing. Emacs makes a backup file the first time you save a buffer
27 into its visited file. Normally, this means that the backup file
28 contains the contents of the file as it was before the current editing
29 session. The contents of the backup file normally remain unchanged once
30 it exists.
31
32 Backups are usually made by renaming the visited file to a new name.
33 Optionally, you can specify that backup files should be made by copying
34 the visited file. This choice makes a difference for files with
35 multiple names; it also can affect whether the edited file remains owned
36 by the original owner or becomes owned by the user editing it.
37
38 By default, Emacs makes a single backup file for each file edited.
39 You can alternatively request numbered backups; then each new backup
40 file gets a new name. You can delete old numbered backups when you
41 don't want them any more, or Emacs can delete them automatically.
42
43 @menu
44 * Making Backups:: How Emacs makes backup files, and when.
45 * Rename or Copy:: Two alternatives: renaming the old file or copying it.
46 * Numbered Backups:: Keeping multiple backups for each source file.
47 * Backup Names:: How backup file names are computed; customization.
48 @end menu
49
50 @node Making Backups, Rename or Copy, Backup Files, Backup Files
51 @subsection Making Backup Files
52
53 @defun backup-buffer
54 This function makes a backup of the file visited by the current
55 buffer, if appropriate. It is called by @code{save-buffer} before
56 saving the buffer the first time.
57 @end defun
58
59 @defvar buffer-backed-up
60 This buffer-local variable indicates whether this buffer's file has
61 been backed up on account of this buffer. If it is non-@code{nil}, then
62 the backup file has been written. Otherwise, the file should be backed
63 up when it is next saved (if backup files are enabled). This is a
64 permanent local; @code{kill-local-variables} does not alter it.
65 @end defvar
66
67 @defopt make-backup-files
68 This variable determines whether or not to make backup files. If it
69 is non-@code{nil}, then Emacs creates a backup of each file when it is
70 saved for the first time.
71
72 The following example shows how to change the @code{make-backup-files}
73 variable only in the @file{RMAIL} buffer and not elsewhere. Setting it
74 @code{nil} stops Emacs from making backups of the @file{RMAIL} file,
75 which may save disk space. (You would put this code in your
76 @file{.emacs} file.)
77
78 @smallexample
79 @group
80 (add-hook 'rmail-mode-hook
81 (function (lambda ()
82 (make-local-variable
83 'make-backup-files)
84 (setq make-backup-files nil))))
85 @end group
86 @end smallexample
87 @end defopt
88
89 @defvar backup-enable-predicate filename
90 This variable's value is a function to be called on certain occasions to
91 decide whether a there should be backup files for file name
92 @var{filename}. If it returns @code{nil}, backups are disabled.
93 Otherwise, the other variables in this section say whether and how to
94 make backups.
95
96 The default value is this:
97
98 @example
99 (lambda (name)
100 (or (< (length name) 5)
101 (not (string-equal "/tmp/"
102 (substring name 0 5)))))
103 @end example
104 @end defvar
105
106 @defvar backup-inhibited
107 If this variable is non-@code{nil}, backups are inhibited. It records
108 the result of testing @code{backup-enable-predicate} on the visited file
109 name. It can also coherently be used by other mechanisms that inhibit
110 backups based on which file is visited. Major modes should not set this
111 variable.
112 @end defvar
113
114 @node Rename or Copy, Numbered Backups, Making Backups, Backup Files
115 @subsection Backup by Renaming or by Copying?
116 @cindex backup files, how to make them
117
118 There are two ways that Emacs can make a backup file:
119
120 @itemize @bullet
121 @item
122 Emacs can rename the original file so that it becomes a backup file, and
123 then write the buffer being saved into a new file. After this
124 procedure, any other names (i.e., hard links) of the original file now
125 refer to the backup file. The new file is owned by the user doing the
126 editing, and its group is the default for new files written by the user
127 in that directory.
128
129 @item
130 Emacs can copy the original file into a backup file, and then overwrite
131 the original file with new contents. After this procedure, any other
132 names (i.e., hard links) of the original file still refer to the current
133 version of the file. The file's owner and group will be unchanged.
134 @end itemize
135
136 The first method, renaming, is the default.
137
138 The variable @code{backup-by-copying}, if non-@code{nil}, says to use
139 the second method, which is to copy the original file and overwrite it
140 with the new buffer contents. The variable @code{file-precious-flag},
141 if non-@code{nil}, also has this effect (as a sideline of its main
142 significance). @xref{Saving Buffers}.
143
144 @defvar backup-by-copying
145 If this variable is non-@code{nil}, Emacs always makes backup files by
146 copying.
147 @end defvar
148
149 The following two variables, when non-@code{nil}, cause the second
150 method to be used in certain special cases. They have no effect on the
151 treatment of files that don't fall into the special cases.
152
153 @defvar backup-by-copying-when-linked
154 If this variable is non-@code{nil}, Emacs makes backups by copying for
155 files with multiple names (hard links).
156
157 This variable is significant only if @code{backup-by-copying} is
158 @code{nil}, since copying is always used when that variable is
159 non-@code{nil}.
160 @end defvar
161
162 @defvar backup-by-copying-when-mismatch
163 If this variable is non-@code{nil}, Emacs makes backups by copying in cases
164 where renaming would change either the owner or the group of the file.
165
166 The value has no effect when renaming would not alter the owner or
167 group of the file; that is, for files which are owned by the user and
168 whose group matches the default for a new file created there by the
169 user.
170
171 This variable is significant only if @code{backup-by-copying} is
172 @code{nil}, since copying is always used when that variable is
173 non-@code{nil}.
174 @end defvar
175
176 @node Numbered Backups, Backup Names, Rename or Copy, Backup Files
177 @subsection Making and Deleting Numbered Backup Files
178
179 If a file's name is @file{foo}, the names of its numbered backup
180 versions are @file{foo.~@var{v}~}, for various integers @var{v}, like
181 this: @file{foo.~1~}, @file{foo.~2~}, @file{foo.~3~}, @dots{},
182 @file{foo.~259~}, and so on.
183
184 @defopt version-control
185 This variable controls whether to make a single non-numbered backup
186 file or multiple numbered backups.
187
188 @table @asis
189 @item @code{nil}
190 Make numbered backups if the visited file already has numbered backups;
191 otherwise, do not.
192
193 @item @code{never}
194 Do not make numbered backups.
195
196 @item @var{anything else}
197 Do make numbered backups.
198 @end table
199 @end defopt
200
201 The use of numbered backups ultimately leads to a large number of
202 backup versions, which must then be deleted. Emacs can do this
203 automatically.
204
205 @defopt kept-new-versions
206 The value of this variable is the number of oldest versions to keep
207 when a new numbered backup is made. The newly made backup is included
208 in the count. The default value is 2.
209 @end defopt
210
211 @defopt kept-old-versions
212 The value of this variable is the number of oldest versions to keep
213 when a new numbered backup is made. The default value is 2.
214 @end defopt
215
216 If there are backups numbered 1, 2, 3, 5, and 7, and both of these
217 variables have the value 2, then the backups numbered 1 and 2 are kept
218 as old versions and those numbered 5 and 7 are kept as new versions;
219 backup version 3 is deleted. The function @code{find-backup-file-name}
220 (@pxref{Backup Names}) is responsible for determining which backup
221 versions to delete, but does not delete them itself.
222
223 @defopt trim-versions-without-asking
224 If this variable is non-@code{nil}, then saving a file deletes excess
225 backup versions silently. Otherwise, it asks the user whether to delete
226 them.
227 @end defopt
228
229 @defopt dired-kept-versions
230 This variable specifies how many of the newest backup versions to keep
231 in the Dired command @kbd{.} (@code{dired-clean-directory}). That's the
232 same thing @code{kept-new-versions} does when you make a new backup
233 file. The default value is 2.
234 @end defopt
235
236 @node Backup Names, , Numbered Backups, Backup Files
237 @subsection Naming Backup Files
238
239 The functions in this section are documented mainly because you can
240 customize the naming conventions for backup files by redefining them.
241 If you change one, you probably need to change the rest.
242
243 @defun backup-file-name-p filename
244 This function returns a non-@code{nil} value if @var{filename} is a
245 possible name for a backup file. A file with the name @var{filename}
246 need not exist; the function just checks the name.
247
248 @smallexample
249 @group
250 (backup-file-name-p "foo")
251 @result{} nil
252 @end group
253 @group
254 (backup-file-name-p "foo~")
255 @result{} 3
256 @end group
257 @end smallexample
258
259 The standard definition of this function is as follows:
260
261 @smallexample
262 @group
263 (defun backup-file-name-p (file)
264 "Return non-nil if FILE is a backup file \
265 name (numeric or not)..."
266 (string-match "~$" file))
267 @end group
268 @end smallexample
269
270 @noindent
271 Thus, the function returns a non-@code{nil} value if the file name ends
272 with a @samp{~}. (We use a backslash to split the documentation
273 string's first line into two lines in the text, but produce just one
274 line in the string itself.)
275
276 This simple expression is placed in a separate function to make it easy
277 to redefine for customization.
278 @end defun
279
280 @defun make-backup-file-name filename
281 This function returns a string which is the name to use for a
282 non-numbered backup file for file @var{filename}. On Unix, this is just
283 @var{filename} with a tilde appended.
284
285 The standard definition of this function is as follows:
286
287 @smallexample
288 @group
289 (defun make-backup-file-name (file)
290 "Create the non-numeric backup file name for FILE.
291 @dots{}"
292 (concat file "~"))
293 @end group
294 @end smallexample
295
296 You can change the backup file naming convention by redefining this
297 function. The following example redefines @code{make-backup-file-name}
298 to prepend a @samp{.} as well as appending a tilde:
299
300 @smallexample
301 @group
302 (defun make-backup-file-name (filename)
303 (concat "." filename "~"))
304 @end group
305
306 @group
307 (make-backup-file-name "backups.texi")
308 @result{} ".backups.texi~"
309 @end group
310 @end smallexample
311 @end defun
312
313 @defun find-backup-file-name filename
314 This function computes the file name for a new backup file for
315 @var{filename}. It may also propose certain existing backup files for
316 deletion. @code{find-backup-file-name} returns a list whose @sc{car} is
317 the name for the new backup file and whose @sc{cdr} is a list of backup
318 files whose deletion is proposed.
319
320 Two variables, @code{kept-old-versions} and @code{kept-new-versions},
321 determine which backup versions should be kept. This function keeps
322 those versions by excluding them from the @sc{cdr} of the value.
323 @xref{Numbered Backups}.
324
325 In this example, the value says that @file{~rms/foo.~5~} is the name
326 to use for the new backup file, and @file{~rms/foo.~3~} is an ``excess''
327 version that the caller should consider deleting now.
328
329 @smallexample
330 @group
331 (find-backup-file-name "~rms/foo")
332 @result{} ("~rms/foo.~5~" "~rms/foo.~3~")
333 @end group
334 @end smallexample
335 @end defun
336
337 @c Emacs 19 feature
338 @defun file-newest-backup filename
339 This function returns the name of the most recent backup file for
340 @var{filename}, or @code{nil} that file has no backup files.
341
342 Some file comparison commands use this function in order to compare
343 a file by default with its most recent backup.
344 @end defun
345
346 @node Auto-Saving, Reverting, Backup Files, Backups and Auto-Saving
347 @section Auto-Saving
348 @cindex auto-saving
349
350 Emacs periodically saves all files that you are visiting; this is
351 called @dfn{auto-saving}. Auto-saving prevents you from losing more
352 than a limited amount of work if the system crashes. By default,
353 auto-saves happen every 300 keystrokes, or after around 30 seconds of
354 idle time. @xref{Auto-Save, Auto-Save, Auto-Saving: Protection Against
355 Disasters, emacs, The GNU Emacs Manual}, for information on auto-save
356 for users. Here we describe the functions used to implement auto-saving
357 and the variables that control them.
358
359 @defvar buffer-auto-save-file-name
360 This buffer-local variable is the name of the file used for
361 auto-saving the current buffer. It is @code{nil} if the buffer
362 should not be auto-saved.
363
364 @example
365 @group
366 buffer-auto-save-file-name
367 => "/xcssun/users/rms/lewis/#files.texi#"
368 @end group
369 @end example
370 @end defvar
371
372 @deffn Command auto-save-mode arg
373 When used interactively without an argument, this command is a toggle
374 switch: it turns on auto-saving of the current buffer if it is off, and
375 vice-versa. With an argument @var{arg}, the command turns auto-saving
376 on if the value of @var{arg} is @code{t}, a nonempty list, or a positive
377 integer. Otherwise, it turns auto-saving off.
378 @end deffn
379
380 @defun auto-save-file-name-p filename
381 This function returns a non-@code{nil} value if @var{filename} is a
382 string that could be the name of an auto-save file. It works based on
383 knowledge of the naming convention for auto-save files: a name that
384 begins and ends with hash marks (@samp{#}) is a possible auto-save file
385 name. The argument @var{filename} should not contain a directory part.
386
387 @example
388 @group
389 (make-auto-save-file-name)
390 @result{} "/xcssun/users/rms/lewis/#files.texi#"
391 @end group
392 @group
393 (auto-save-file-name-p "#files.texi#")
394 @result{} 0
395 @end group
396 @group
397 (auto-save-file-name-p "files.texi")
398 @result{} nil
399 @end group
400 @end example
401
402 The standard definition of this function is as follows:
403
404 @example
405 @group
406 (defun auto-save-file-name-p (filename)
407 "Return non-nil if FILENAME can be yielded by..."
408 (string-match "^#.*#$" filename))
409 @end group
410 @end example
411
412 This function exists so that you can customize it if you wish to
413 change the naming convention for auto-save files. If you redefine it,
414 be sure to redefine the function @code{make-auto-save-file-name}
415 correspondingly.
416 @end defun
417
418 @defun make-auto-save-file-name
419 This function returns the file name to use for auto-saving the current
420 buffer. This is just the file name with hash marks (@samp{#}) appended
421 and prepended to it. This function does not look at the variable
422 @code{auto-save-visited-file-name}; you should check that before calling
423 this function.
424
425 @example
426 @group
427 (make-auto-save-file-name)
428 @result{} "/xcssun/users/rms/lewis/#backup.texi#"
429 @end group
430 @end example
431
432 The standard definition of this function is as follows:
433
434 @example
435 @group
436 (defun make-auto-save-file-name ()
437 "Return file name to use for auto-saves \
438 of current buffer.
439 @dots{}"
440 (if buffer-file-name
441 @end group
442 @group
443 (concat
444 (file-name-directory buffer-file-name)
445 "#"
446 (file-name-nondirectory buffer-file-name)
447 "#")
448 (expand-file-name
449 (concat "#%" (buffer-name) "#"))))
450 @end group
451 @end example
452
453 This exists as a separate function so that you can redefine it to
454 customize the naming convention for auto-save files. Be sure to
455 change @code{auto-save-file-name-p} in a corresponding way.
456 @end defun
457
458 @defvar auto-save-visited-file-name
459 If this variable is non-@code{nil}, Emacs auto-saves buffers in
460 the files they are visiting. That is, the auto-save is done in the same
461 file which you are editing. Normally, this variable is @code{nil}, so
462 auto-save files have distinct names that are created by
463 @code{make-auto-save-file-name}.
464
465 When you change the value of this variable, the value does not take
466 effect until the next time auto-save mode is reenabled in any given
467 buffer. If auto-save mode is already enabled, auto-saves continue to go
468 in the same file name until @code{auto-save-mode} is called again.
469 @end defvar
470
471 @defun recent-auto-save-p
472 This function returns @code{t} if the current buffer has been
473 auto-saved since the last time it was read in or saved.
474 @end defun
475
476 @defun set-buffer-auto-saved
477 This function marks the current buffer as auto-saved. The buffer will
478 not be auto-saved again until the buffer text is changed again. The
479 function returns @code{nil}.
480 @end defun
481
482 @defopt auto-save-interval
483 The value of this variable is the number of characters that Emacs
484 reads from the keyboard between auto-saves. Each time this many more
485 characters are read, auto-saving is done for all buffers in which it is
486 enabled.
487 @end defopt
488
489 @defopt auto-save-timeout
490 The value of this variable is the number of seconds of idle time that
491 should cause auto-saving. Each time the user pauses for this long,
492 Emacs auto-saves any buffers that need it. (Actually, the specified
493 timeout is multiplied by a factor depending on the size of the current
494 buffer.)
495 @end defopt
496
497 @defvar auto-save-hook
498 This normal hook is run whenever an auto-save is about to happen.
499 @end defvar
500
501 @defopt auto-save-default
502 If this variable is non-@code{nil}, buffers that are visiting files
503 have auto-saving enabled by default. Otherwise, they do not.
504 @end defopt
505
506 @deffn Command do-auto-save &optional no-message
507 This function auto-saves all buffers that need to be auto-saved. It
508 saves all buffers for which auto-saving is enabled and that have been
509 changed since the previous auto-save.
510
511 Normally, if any buffers are auto-saved, a message that says
512 @samp{Auto-saving...} is displayed in the echo area while auto-saving is
513 going on. However, if @var{no-message} is non-@code{nil}, the message
514 is inhibited.
515 @end deffn
516
517 @defun delete-auto-save-file-if-necessary
518 This function deletes the current buffer's auto-save file if
519 @code{delete-auto-save-files} is non-@code{nil}. It is called every
520 time a buffer is saved.
521 @end defun
522
523 @defvar delete-auto-save-files
524 This variable is used by the function
525 @code{delete-auto-save-file-if-necessary}. If it is non-@code{nil},
526 Emacs deletes auto-save files when a true save is done (in the visited
527 file). This saves disk space and unclutters your directory.
528 @end defvar
529
530 @defun rename-auto-save-file
531 This function adjusts the current buffer's auto-save file name if the
532 visited file name has changed. It also renames an existing auto-save
533 file. If the visited file name has not changed, this function does
534 nothing.
535 @end defun
536
537 @node Reverting, , Auto-Saving, Backups and Auto-Saving
538 @section Reverting
539
540 If you have made extensive changes to a file and then change your mind
541 about them, you can get rid of them by reading in the previous version
542 of the file with the @code{revert-buffer} command. @xref{Reverting, ,
543 Reverting a Buffer, emacs, The GNU Emacs Manual}.
544
545 @deffn Command revert-buffer &optional check-auto-save noconfirm
546 This command replaces the buffer text with the text of the visited
547 file on disk. This action undoes all changes since the file was visited
548 or saved.
549
550 If the argument @var{check-auto-save} is non-@code{nil}, and the
551 latest auto-save file is more recent than the visited file,
552 @code{revert-buffer} asks the user whether to use that instead.
553 Otherwise, it always uses the text of the visited file itself.
554 Interactively, @var{check-auto-save} is set if there is a numeric prefix
555 argument.
556
557 Normally, @code{revert-buffer} asks for confirmation before it changes
558 the buffer; but if the argument @var{noconfirm} is non-@code{nil},
559 @code{revert-buffer} does not ask for confirmation.
560
561 Reverting tries to preserve marker positions in the buffer by using the
562 replacement feature of @code{insert-file-contents}. If there is no
563 actual difference between the buffer and the file, before reversion,
564 this preserves all the markers. If reversion does change the buffer,
565 this preserves the markers in the unchanged text (if any) at the
566 beginning and end of the buffer. Preserving any additional markers
567 would be problematical.
568
569 If the value of the @code{revert-buffer-function} variable is
570 non-@code{nil}, it is called as a function with no arguments to do the
571 work.
572 @end deffn
573
574 @defvar revert-buffer-function
575 The value of this variable is the function to use to revert this
576 buffer; but if the value of this variable is @code{nil}, then the
577 @code{revert-buffer} function carries out its default action. Modes
578 such as Dired mode, in which the text being edited does not consist of a
579 file's contents but can be regenerated in some other fashion, give this
580 variable a buffer-local value that is a function to regenerate the
581 contents.
582 @end defvar
583
584 @defvar revert-buffer-insert-file-contents-function
585 The value of this variable, if non-@code{nil}, is the function to use
586 to insert contents when reverting this buffer. The function receives
587 two arguments, first the file name to use, and second, @code{t} if the
588 user has asked to read the auto-save file.
589 @end defvar
590
591 @defvar before-revert-hook
592 This normal hook is run by @code{revert-buffer} before actually
593 inserting the modified contents---but only if
594 @code{revert-buffer-function} is @code{nil}.
595
596 Font Lock mode uses this hook to record that the buffer contents are no
597 longer fontified.
598 @end defvar
599
600 @defvar after-revert-hook
601 This normal hook is run by @code{revert-buffer} after actually inserting
602 the modified contents---but only if @code{revert-buffer-function} is
603 @code{nil}.
604
605 Font Lock mode uses this hook to recompute the fonts for the updated
606 buffer contents.
607 @end defvar
608
609 @deffn Command recover-file filename
610 This function visits @var{filename}, but gets the contents from its
611 last auto-save file. This is useful after the system has crashed, to
612 resume editing the same file without losing all the work done in the
613 previous session.
614
615 An error is signaled if there is no auto-save file for @var{filename},
616 or if @var{filename} is newer than its auto-save file. If
617 @var{filename} does not exist, but its auto-save file does, then the
618 auto-save file is read as usual. This last situation may occur if you
619 visited a nonexistent file and never actually saved it.
620 @end deffn
621