backups.texi 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Emacs Lisp Reference Manual.
  3. @c Copyright (C) 1990-1995, 1999, 2001-2016 Free Software Foundation,
  4. @c Inc.
  5. @c See the file elisp.texi for copying conditions.
  6. @node Backups and Auto-Saving
  7. @chapter Backups and Auto-Saving
  8. @cindex backups and auto-saving
  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. @menu
  15. * Backup Files:: How backup files are made; how their names are chosen.
  16. * Auto-Saving:: How auto-save files are made; how their names are chosen.
  17. * Reverting:: @code{revert-buffer}, and how to customize what it does.
  18. @end menu
  19. @node Backup Files
  20. @section Backup Files
  21. @cindex backup file
  22. A @dfn{backup file} is a copy of the old contents of a file you are
  23. editing. Emacs makes a backup file the first time you save a buffer
  24. into its visited file. Thus, normally, the backup file contains the
  25. contents of the file as it was before the current editing session.
  26. The contents of the backup file normally remain unchanged once it
  27. exists.
  28. Backups are usually made by renaming the visited file to a new name.
  29. Optionally, you can specify that backup files should be made by copying
  30. the visited file. This choice makes a difference for files with
  31. multiple names; it also can affect whether the edited file remains owned
  32. by the original owner or becomes owned by the user editing it.
  33. By default, Emacs makes a single backup file for each file edited.
  34. You can alternatively request numbered backups; then each new backup
  35. file gets a new name. You can delete old numbered backups when you
  36. don't want them any more, or Emacs can delete them automatically.
  37. @menu
  38. * Making Backups:: How Emacs makes backup files, and when.
  39. * Rename or Copy:: Two alternatives: renaming the old file or copying it.
  40. * Numbered Backups:: Keeping multiple backups for each source file.
  41. * Backup Names:: How backup file names are computed; customization.
  42. @end menu
  43. @node Making Backups
  44. @subsection Making Backup Files
  45. @cindex making backup files
  46. @defun backup-buffer
  47. This function makes a backup of the file visited by the current
  48. buffer, if appropriate. It is called by @code{save-buffer} before
  49. saving the buffer the first time.
  50. If a backup was made by renaming, the return value is a cons cell of
  51. the form (@var{modes} @var{extra-alist} @var{backupname}), where
  52. @var{modes} are the mode bits of the original file, as returned by
  53. @code{file-modes} (@pxref{Testing Accessibility}), @var{extra-alist}
  54. is an alist describing the original file's extended attributes, as
  55. returned by @code{file-extended-attributes} (@pxref{Extended
  56. Attributes}), and @var{backupname} is the name of the backup.
  57. In all other cases (i.e., if a backup was made by copying or if no
  58. backup was made), this function returns @code{nil}.
  59. @end defun
  60. @defvar buffer-backed-up
  61. This buffer-local variable says whether this buffer's file has
  62. been backed up on account of this buffer. If it is non-@code{nil},
  63. the backup file has been written. Otherwise, the file should be backed
  64. up when it is next saved (if backups are enabled). This is a
  65. permanent local; @code{kill-all-local-variables} does not alter@tie{}it.
  66. @end defvar
  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---provided that @code{backup-inhibited}
  71. is @code{nil} (see below).
  72. The following example shows how to change the @code{make-backup-files}
  73. variable only in the Rmail buffers and not elsewhere. Setting it
  74. @code{nil} stops Emacs from making backups of these files, which may
  75. save disk space. (You would put this code in your init file.)
  76. @smallexample
  77. @group
  78. (add-hook 'rmail-mode-hook
  79. (lambda () (setq-local make-backup-files nil)))
  80. @end group
  81. @end smallexample
  82. @end defopt
  83. @defvar backup-enable-predicate
  84. This variable's value is a function to be called on certain occasions to
  85. decide whether a file should have backup files. The function receives
  86. one argument, an absolute file name to consider. If the function returns
  87. @code{nil}, backups are disabled for that file. Otherwise, the other
  88. variables in this section say whether and how to make backups.
  89. @findex normal-backup-enable-predicate
  90. The default value is @code{normal-backup-enable-predicate}, which checks
  91. for files in @code{temporary-file-directory} and
  92. @code{small-temporary-file-directory}.
  93. @end defvar
  94. @defvar backup-inhibited
  95. If this variable is non-@code{nil}, backups are inhibited. It records
  96. the result of testing @code{backup-enable-predicate} on the visited file
  97. name. It can also coherently be used by other mechanisms that inhibit
  98. backups based on which file is visited. For example, VC sets this
  99. variable non-@code{nil} to prevent making backups for files managed
  100. with a version control system.
  101. This is a permanent local, so that changing the major mode does not lose
  102. its value. Major modes should not set this variable---they should set
  103. @code{make-backup-files} instead.
  104. @end defvar
  105. @defopt backup-directory-alist
  106. This variable's value is an alist of filename patterns and backup
  107. directory names. Each element looks like
  108. @smallexample
  109. (@var{regexp} . @var{directory})
  110. @end smallexample
  111. @noindent
  112. Backups of files with names matching @var{regexp} will be made in
  113. @var{directory}. @var{directory} may be relative or absolute. If it is
  114. absolute, so that all matching files are backed up into the same
  115. directory, the file names in this directory will be the full name of the
  116. file backed up with all directory separators changed to @samp{!} to
  117. prevent clashes. This will not work correctly if your filesystem
  118. truncates the resulting name.
  119. For the common case of all backups going into one directory, the alist
  120. should contain a single element pairing @samp{"."} with the appropriate
  121. directory name.
  122. If this variable is @code{nil} (the default), or it fails to match a
  123. filename, the backup is made in the original file's directory.
  124. On MS-DOS filesystems without long names this variable is always
  125. ignored.
  126. @end defopt
  127. @defopt make-backup-file-name-function
  128. This variable's value is a function to use for making backup file names.
  129. The function @code{make-backup-file-name} calls it.
  130. @xref{Backup Names,, Naming Backup Files}.
  131. This could be buffer-local to do something special for specific
  132. files. If you change it, you may need to change
  133. @code{backup-file-name-p} and @code{file-name-sans-versions} too.
  134. @end defopt
  135. @node Rename or Copy
  136. @subsection Backup by Renaming or by Copying?
  137. @cindex backup files, rename or copy
  138. There are two ways that Emacs can make a backup file:
  139. @itemize @bullet
  140. @item
  141. Emacs can rename the original file so that it becomes a backup file, and
  142. then write the buffer being saved into a new file. After this
  143. procedure, any other names (i.e., hard links) of the original file now
  144. refer to the backup file. The new file is owned by the user doing the
  145. editing, and its group is the default for new files written by the user
  146. in that directory.
  147. @item
  148. Emacs can copy the original file into a backup file, and then overwrite
  149. the original file with new contents. After this procedure, any other
  150. names (i.e., hard links) of the original file continue to refer to the
  151. current (updated) version of the file. The file's owner and group will
  152. be unchanged.
  153. @end itemize
  154. The first method, renaming, is the default.
  155. The variable @code{backup-by-copying}, if non-@code{nil}, says to use
  156. the second method, which is to copy the original file and overwrite it
  157. with the new buffer contents. The variable @code{file-precious-flag},
  158. if non-@code{nil}, also has this effect (as a sideline of its main
  159. significance). @xref{Saving Buffers}.
  160. @defopt backup-by-copying
  161. If this variable is non-@code{nil}, Emacs always makes backup files by
  162. copying. The default is @code{nil}.
  163. @end defopt
  164. The following three variables, when non-@code{nil}, cause the second
  165. method to be used in certain special cases. They have no effect on the
  166. treatment of files that don't fall into the special cases.
  167. @defopt backup-by-copying-when-linked
  168. If this variable is non-@code{nil}, Emacs makes backups by copying for
  169. files with multiple names (hard links). The default is @code{nil}.
  170. This variable is significant only if @code{backup-by-copying} is
  171. @code{nil}, since copying is always used when that variable is
  172. non-@code{nil}.
  173. @end defopt
  174. @defopt backup-by-copying-when-mismatch
  175. If this variable is non-@code{nil} (the default), Emacs makes backups
  176. by copying in cases where renaming would change either the owner or
  177. the group of the file.
  178. The value has no effect when renaming would not alter the owner or
  179. group of the file; that is, for files which are owned by the user and
  180. whose group matches the default for a new file created there by the
  181. user.
  182. This variable is significant only if @code{backup-by-copying} is
  183. @code{nil}, since copying is always used when that variable is
  184. non-@code{nil}.
  185. @end defopt
  186. @defopt backup-by-copying-when-privileged-mismatch
  187. This variable, if non-@code{nil}, specifies the same behavior as
  188. @code{backup-by-copying-when-mismatch}, but only for certain user-id
  189. values: namely, those less than or equal to a certain number. You set
  190. this variable to that number.
  191. Thus, if you set @code{backup-by-copying-when-privileged-mismatch}
  192. to 0, backup by copying is done for the superuser only,
  193. when necessary to prevent a change in the owner of the file.
  194. The default is 200.
  195. @end defopt
  196. @node Numbered Backups
  197. @subsection Making and Deleting Numbered Backup Files
  198. @cindex numbered backups
  199. If a file's name is @file{foo}, the names of its numbered backup
  200. versions are @file{foo.~@var{v}~}, for various integers @var{v}, like
  201. this: @file{foo.~1~}, @file{foo.~2~}, @file{foo.~3~}, @dots{},
  202. @file{foo.~259~}, and so on.
  203. @defopt version-control
  204. This variable controls whether to make a single non-numbered backup
  205. file or multiple numbered backups.
  206. @table @asis
  207. @item @code{nil}
  208. Make numbered backups if the visited file already has numbered backups;
  209. otherwise, do not. This is the default.
  210. @item @code{never}
  211. Do not make numbered backups.
  212. @item @var{anything else}
  213. Make numbered backups.
  214. @end table
  215. @end defopt
  216. The use of numbered backups ultimately leads to a large number of
  217. backup versions, which must then be deleted. Emacs can do this
  218. automatically or it can ask the user whether to delete them.
  219. @defopt kept-new-versions
  220. The value of this variable is the number of newest versions to keep
  221. when a new numbered backup is made. The newly made backup is included
  222. in the count. The default value is@tie{}2.
  223. @end defopt
  224. @defopt kept-old-versions
  225. The value of this variable is the number of oldest versions to keep
  226. when a new numbered backup is made. The default value is@tie{}2.
  227. @end defopt
  228. If there are backups numbered 1, 2, 3, 5, and 7, and both of these
  229. variables have the value 2, then the backups numbered 1 and 2 are kept
  230. as old versions and those numbered 5 and 7 are kept as new versions;
  231. backup version 3 is excess. The function @code{find-backup-file-name}
  232. (@pxref{Backup Names}) is responsible for determining which backup
  233. versions to delete, but does not delete them itself.
  234. @defopt delete-old-versions
  235. If this variable is @code{t}, then saving a file deletes excess
  236. backup versions silently. If it is @code{nil}, that means
  237. to ask for confirmation before deleting excess backups.
  238. Otherwise, they are not deleted at all.
  239. @end defopt
  240. @defopt dired-kept-versions
  241. This variable specifies how many of the newest backup versions to keep
  242. in the Dired command @kbd{.} (@code{dired-clean-directory}). That's the
  243. same thing @code{kept-new-versions} specifies when you make a new backup
  244. file. The default is@tie{}2.
  245. @end defopt
  246. @node Backup Names
  247. @subsection Naming Backup Files
  248. @cindex naming backup files
  249. The functions in this section are documented mainly because you can
  250. customize the naming conventions for backup files by redefining them.
  251. If you change one, you probably need to change the rest.
  252. @defun backup-file-name-p filename
  253. This function returns a non-@code{nil} value if @var{filename} is a
  254. possible name for a backup file. It just checks the name, not whether
  255. a file with the name @var{filename} exists.
  256. @smallexample
  257. @group
  258. (backup-file-name-p "foo")
  259. @result{} nil
  260. @end group
  261. @group
  262. (backup-file-name-p "foo~")
  263. @result{} 3
  264. @end group
  265. @end smallexample
  266. The standard definition of this function is as follows:
  267. @smallexample
  268. @group
  269. (defun backup-file-name-p (file)
  270. "Return non-nil if FILE is a backup file \
  271. name (numeric or not)..."
  272. (string-match "~\\'" file))
  273. @end group
  274. @end smallexample
  275. @noindent
  276. Thus, the function returns a non-@code{nil} value if the file name ends
  277. with a @samp{~}. (We use a backslash to split the documentation
  278. string's first line into two lines in the text, but produce just one
  279. line in the string itself.)
  280. This simple expression is placed in a separate function to make it easy
  281. to redefine for customization.
  282. @end defun
  283. @defun make-backup-file-name filename
  284. This function returns a string that is the name to use for a
  285. non-numbered backup file for file @var{filename}. On Unix, this is just
  286. @var{filename} with a tilde appended.
  287. The standard definition of this function, on most operating systems, is
  288. as follows:
  289. @smallexample
  290. @group
  291. (defun make-backup-file-name (file)
  292. "Create the non-numeric backup file name for FILE..."
  293. (concat file "~"))
  294. @end group
  295. @end smallexample
  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{.} in addition to appending a tilde:
  299. @smallexample
  300. @group
  301. (defun make-backup-file-name (filename)
  302. (expand-file-name
  303. (concat "." (file-name-nondirectory filename) "~")
  304. (file-name-directory filename)))
  305. @end group
  306. @group
  307. (make-backup-file-name "backups.texi")
  308. @result{} ".backups.texi~"
  309. @end group
  310. @end smallexample
  311. Some parts of Emacs, including some Dired commands, assume that backup
  312. file names end with @samp{~}. If you do not follow that convention, it
  313. will not cause serious problems, but these commands may give
  314. less-than-desirable results.
  315. @end defun
  316. @defun find-backup-file-name filename
  317. This function computes the file name for a new backup file for
  318. @var{filename}. It may also propose certain existing backup files for
  319. deletion. @code{find-backup-file-name} returns a list whose @sc{car} is
  320. the name for the new backup file and whose @sc{cdr} is a list of backup
  321. files whose deletion is proposed. The value can also be @code{nil},
  322. which means not to make a backup.
  323. Two variables, @code{kept-old-versions} and @code{kept-new-versions},
  324. determine which backup versions should be kept. This function keeps
  325. those versions by excluding them from the @sc{cdr} of the value.
  326. @xref{Numbered Backups}.
  327. In this example, the value says that @file{~rms/foo.~5~} is the name
  328. to use for the new backup file, and @file{~rms/foo.~3~} is an excess
  329. version that the caller should consider deleting now.
  330. @smallexample
  331. @group
  332. (find-backup-file-name "~rms/foo")
  333. @result{} ("~rms/foo.~5~" "~rms/foo.~3~")
  334. @end group
  335. @end smallexample
  336. @end defun
  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} if that file has no backup files.
  341. Some file comparison commands use this function so that they can
  342. automatically compare a file with its most recent backup.
  343. @end defun
  344. @node Auto-Saving
  345. @section Auto-Saving
  346. @c @cindex auto-saving Lots of symbols starting with auto-save here.
  347. Emacs periodically saves all files that you are visiting; this is
  348. called @dfn{auto-saving}. Auto-saving prevents you from losing more
  349. than a limited amount of work if the system crashes. By default,
  350. auto-saves happen every 300 keystrokes, or after around 30 seconds of
  351. idle time. @xref{Auto Save, Auto Save, Auto-Saving: Protection Against
  352. Disasters, emacs, The GNU Emacs Manual}, for information on auto-save
  353. for users. Here we describe the functions used to implement auto-saving
  354. and the variables that control them.
  355. @defvar buffer-auto-save-file-name
  356. This buffer-local variable is the name of the file used for
  357. auto-saving the current buffer. It is @code{nil} if the buffer
  358. should not be auto-saved.
  359. @example
  360. @group
  361. buffer-auto-save-file-name
  362. @result{} "/xcssun/users/rms/lewis/#backups.texi#"
  363. @end group
  364. @end example
  365. @end defvar
  366. @deffn Command auto-save-mode arg
  367. This is the mode command for Auto Save mode, a buffer-local minor
  368. mode. When Auto Save mode is enabled, auto-saving is enabled in the
  369. buffer. The calling convention is the same as for other minor mode
  370. commands (@pxref{Minor Mode Conventions}).
  371. Unlike most minor modes, there is no @code{auto-save-mode} variable.
  372. Auto Save mode is enabled if @code{buffer-auto-save-file-name} is
  373. non-@code{nil} and @code{buffer-saved-size} (see below) is non-zero.
  374. @end deffn
  375. @defun auto-save-file-name-p filename
  376. This function returns a non-@code{nil} value if @var{filename} is a
  377. string that could be the name of an auto-save file. It assumes
  378. the usual naming convention for auto-save files: a name that
  379. begins and ends with hash marks (@samp{#}) is a possible auto-save file
  380. name. The argument @var{filename} should not contain a directory part.
  381. @example
  382. @group
  383. (make-auto-save-file-name)
  384. @result{} "/xcssun/users/rms/lewis/#backups.texi#"
  385. @end group
  386. @group
  387. (auto-save-file-name-p "#backups.texi#")
  388. @result{} 0
  389. @end group
  390. @group
  391. (auto-save-file-name-p "backups.texi")
  392. @result{} nil
  393. @end group
  394. @end example
  395. The standard definition of this function is as follows:
  396. @example
  397. @group
  398. (defun auto-save-file-name-p (filename)
  399. "Return non-nil if FILENAME can be yielded by..."
  400. (string-match "^#.*#$" filename))
  401. @end group
  402. @end example
  403. This function exists so that you can customize it if you wish to
  404. change the naming convention for auto-save files. If you redefine it,
  405. be sure to redefine the function @code{make-auto-save-file-name}
  406. correspondingly.
  407. @end defun
  408. @defun make-auto-save-file-name
  409. This function returns the file name to use for auto-saving the current
  410. buffer. This is just the file name with hash marks (@samp{#}) prepended
  411. and appended to it. This function does not look at the variable
  412. @code{auto-save-visited-file-name} (described below); callers of this
  413. function should check that variable first.
  414. @example
  415. @group
  416. (make-auto-save-file-name)
  417. @result{} "/xcssun/users/rms/lewis/#backups.texi#"
  418. @end group
  419. @end example
  420. Here is a simplified version of the standard definition of this
  421. function:
  422. @example
  423. @group
  424. (defun make-auto-save-file-name ()
  425. "Return file name to use for auto-saves \
  426. of current buffer.."
  427. (if buffer-file-name
  428. @end group
  429. @group
  430. (concat
  431. (file-name-directory buffer-file-name)
  432. "#"
  433. (file-name-nondirectory buffer-file-name)
  434. "#")
  435. (expand-file-name
  436. (concat "#%" (buffer-name) "#"))))
  437. @end group
  438. @end example
  439. This exists as a separate function so that you can redefine it to
  440. customize the naming convention for auto-save files. Be sure to
  441. change @code{auto-save-file-name-p} in a corresponding way.
  442. @end defun
  443. @defopt auto-save-visited-file-name
  444. If this variable is non-@code{nil}, Emacs auto-saves buffers in
  445. the files they are visiting. That is, the auto-save is done in the same
  446. file that you are editing. Normally, this variable is @code{nil}, so
  447. auto-save files have distinct names that are created by
  448. @code{make-auto-save-file-name}.
  449. When you change the value of this variable, the new value does not take
  450. effect in an existing buffer until the next time auto-save mode is
  451. reenabled in it. If auto-save mode is already enabled, auto-saves
  452. continue to go in the same file name until @code{auto-save-mode} is
  453. called again.
  454. @end defopt
  455. @defun recent-auto-save-p
  456. This function returns @code{t} if the current buffer has been
  457. auto-saved since the last time it was read in or saved.
  458. @end defun
  459. @defun set-buffer-auto-saved
  460. This function marks the current buffer as auto-saved. The buffer will
  461. not be auto-saved again until the buffer text is changed again. The
  462. function returns @code{nil}.
  463. @end defun
  464. @defopt auto-save-interval
  465. The value of this variable specifies how often to do auto-saving, in
  466. terms of number of input events. Each time this many additional input
  467. events are read, Emacs does auto-saving for all buffers in which that is
  468. enabled. Setting this to zero disables autosaving based on the
  469. number of characters typed.
  470. @end defopt
  471. @defopt auto-save-timeout
  472. The value of this variable is the number of seconds of idle time that
  473. should cause auto-saving. Each time the user pauses for this long,
  474. Emacs does auto-saving for all buffers in which that is enabled. (If
  475. the current buffer is large, the specified timeout is multiplied by a
  476. factor that increases as the size increases; for a million-byte
  477. buffer, the factor is almost 4.)
  478. If the value is zero or @code{nil}, then auto-saving is not done as a
  479. result of idleness, only after a certain number of input events as
  480. specified by @code{auto-save-interval}.
  481. @end defopt
  482. @defvar auto-save-hook
  483. This normal hook is run whenever an auto-save is about to happen.
  484. @end defvar
  485. @defopt auto-save-default
  486. If this variable is non-@code{nil}, buffers that are visiting files
  487. have auto-saving enabled by default. Otherwise, they do not.
  488. @end defopt
  489. @deffn Command do-auto-save &optional no-message current-only
  490. This function auto-saves all buffers that need to be auto-saved. It
  491. saves all buffers for which auto-saving is enabled and that have been
  492. changed since the previous auto-save.
  493. If any buffers are auto-saved, @code{do-auto-save} normally displays a
  494. message saying @samp{Auto-saving...} in the echo area while
  495. auto-saving is going on. However, if @var{no-message} is
  496. non-@code{nil}, the message is inhibited.
  497. If @var{current-only} is non-@code{nil}, only the current buffer
  498. is auto-saved.
  499. @end deffn
  500. @defun delete-auto-save-file-if-necessary &optional force
  501. This function deletes the current buffer's auto-save file if
  502. @code{delete-auto-save-files} is non-@code{nil}. It is called every
  503. time a buffer is saved.
  504. Unless @var{force} is non-@code{nil}, this function only deletes the
  505. file if it was written by the current Emacs session since the last
  506. true save.
  507. @end defun
  508. @defopt delete-auto-save-files
  509. This variable is used by the function
  510. @code{delete-auto-save-file-if-necessary}. If it is non-@code{nil},
  511. Emacs deletes auto-save files when a true save is done (in the visited
  512. file). This saves disk space and unclutters your directory.
  513. @end defopt
  514. @defun rename-auto-save-file
  515. This function adjusts the current buffer's auto-save file name if the
  516. visited file name has changed. It also renames an existing auto-save
  517. file, if it was made in the current Emacs session. If the visited
  518. file name has not changed, this function does nothing.
  519. @end defun
  520. @defvar buffer-saved-size
  521. The value of this buffer-local variable is the length of the current
  522. buffer, when it was last read in, saved, or auto-saved. This is
  523. used to detect a substantial decrease in size, and turn off auto-saving
  524. in response.
  525. If it is @minus{}1, that means auto-saving is temporarily shut off in
  526. this buffer due to a substantial decrease in size. Explicitly saving
  527. the buffer stores a positive value in this variable, thus reenabling
  528. auto-saving. Turning auto-save mode off or on also updates this
  529. variable, so that the substantial decrease in size is forgotten.
  530. If it is @minus{}2, that means this buffer should disregard changes in
  531. buffer size; in particular, it should not shut off auto-saving
  532. temporarily due to changes in buffer size.
  533. @end defvar
  534. @defvar auto-save-list-file-name
  535. This variable (if non-@code{nil}) specifies a file for recording the
  536. names of all the auto-save files. Each time Emacs does auto-saving, it
  537. writes two lines into this file for each buffer that has auto-saving
  538. enabled. The first line gives the name of the visited file (it's empty
  539. if the buffer has none), and the second gives the name of the auto-save
  540. file.
  541. When Emacs exits normally, it deletes this file; if Emacs crashes, you
  542. can look in the file to find all the auto-save files that might contain
  543. work that was otherwise lost. The @code{recover-session} command uses
  544. this file to find them.
  545. The default name for this file specifies your home directory and starts
  546. with @samp{.saves-}. It also contains the Emacs process @acronym{ID} and the
  547. host name.
  548. @end defvar
  549. @defopt auto-save-list-file-prefix
  550. After Emacs reads your init file, it initializes
  551. @code{auto-save-list-file-name} (if you have not already set it
  552. non-@code{nil}) based on this prefix, adding the host name and process
  553. ID@. If you set this to @code{nil} in your init file, then Emacs does
  554. not initialize @code{auto-save-list-file-name}.
  555. @end defopt
  556. @node Reverting
  557. @section Reverting
  558. @cindex reverting buffers
  559. If you have made extensive changes to a file and then change your mind
  560. about them, you can get rid of them by reading in the previous version
  561. of the file with the @code{revert-buffer} command. @xref{Reverting, ,
  562. Reverting a Buffer, emacs, The GNU Emacs Manual}.
  563. @deffn Command revert-buffer &optional ignore-auto noconfirm preserve-modes
  564. This command replaces the buffer text with the text of the visited
  565. file on disk. This action undoes all changes since the file was visited
  566. or saved.
  567. By default, if the latest auto-save file is more recent than the visited
  568. file, and the argument @var{ignore-auto} is @code{nil},
  569. @code{revert-buffer} asks the user whether to use that auto-save
  570. instead. When you invoke this command interactively, @var{ignore-auto}
  571. is @code{t} if there is no numeric prefix argument; thus, the
  572. interactive default is not to check the auto-save file.
  573. Normally, @code{revert-buffer} asks for confirmation before it changes
  574. the buffer; but if the argument @var{noconfirm} is non-@code{nil},
  575. @code{revert-buffer} does not ask for confirmation.
  576. Normally, this command reinitializes the buffer's major and minor modes
  577. using @code{normal-mode}. But if @var{preserve-modes} is
  578. non-@code{nil}, the modes remain unchanged.
  579. Reverting tries to preserve marker positions in the buffer by using the
  580. replacement feature of @code{insert-file-contents}. If the buffer
  581. contents and the file contents are identical before the revert
  582. operation, reverting preserves all the markers. If they are not
  583. identical, reverting does change the buffer; in that case, it preserves
  584. the markers in the unchanged text (if any) at the beginning and end of
  585. the buffer. Preserving any additional markers would be problematical.
  586. @end deffn
  587. @defvar revert-buffer-in-progress-p
  588. @code{revert-buffer} binds this variable to a non-@code{nil} value
  589. while it is working.
  590. @end defvar
  591. You can customize how @code{revert-buffer} does its work by setting
  592. the variables described in the rest of this section.
  593. @defopt revert-without-query
  594. This variable holds a list of files that should be reverted without
  595. query. The value is a list of regular expressions. If the visited file
  596. name matches one of these regular expressions, and the file has changed
  597. on disk but the buffer is not modified, then @code{revert-buffer}
  598. reverts the file without asking the user for confirmation.
  599. @end defopt
  600. Some major modes customize @code{revert-buffer} by making
  601. buffer-local bindings for these variables:
  602. @defvar revert-buffer-function
  603. @anchor{Definition of revert-buffer-function}
  604. The value of this variable is the function to use to revert this
  605. buffer. It should be a function with two optional
  606. arguments to do the work of reverting. The two optional arguments,
  607. @var{ignore-auto} and @var{noconfirm}, are the arguments that
  608. @code{revert-buffer} received.
  609. Modes such as Dired mode, in which the text being edited does not
  610. consist of a file's contents but can be regenerated in some other
  611. fashion, can give this variable a buffer-local value that is a special
  612. function to regenerate the contents.
  613. @end defvar
  614. @defvar revert-buffer-insert-file-contents-function
  615. The value of this variable specifies the function to use to
  616. insert the updated contents when reverting this buffer. The function
  617. receives two arguments: first the file name to use; second, @code{t} if
  618. the user has asked to read the auto-save file.
  619. The reason for a mode to change this variable instead of
  620. @code{revert-buffer-function} is to avoid duplicating or replacing the
  621. rest of what @code{revert-buffer} does: asking for confirmation,
  622. clearing the undo list, deciding the proper major mode, and running the
  623. hooks listed below.
  624. @end defvar
  625. @defvar before-revert-hook
  626. This normal hook is run by the default @code{revert-buffer-function}
  627. before inserting the modified contents. A custom @code{revert-buffer-function}
  628. may or may not run this hook.
  629. @end defvar
  630. @defvar after-revert-hook
  631. This normal hook is run by the default @code{revert-buffer-function}
  632. after inserting the modified contents. A custom @code{revert-buffer-function}
  633. may or may not run this hook.
  634. @end defvar
  635. @c FIXME? Move this section from arevert-xtra to here?
  636. @defvar buffer-stale-function
  637. The value of this variable specifies a function to call to check
  638. whether a buffer needs reverting. The default value only handles
  639. buffers that are visiting files, by checking their modification time.
  640. Buffers that are not visiting files require a custom function
  641. @iftex
  642. (@pxref{Supporting additional buffers,,, emacs-xtra, Specialized Emacs Features}).
  643. @end iftex
  644. @ifnottex
  645. (@pxref{Supporting additional buffers,,, emacs}).
  646. @end ifnottex
  647. @end defvar