2326 |
(defun backup-buffer () |
(defun backup-buffer () |
2327 |
"Make a backup of the disk file visited by the current buffer, if appropriate. |
"Make a backup of the disk file visited by the current buffer, if appropriate. |
2328 |
This is normally done before saving the buffer the first time. |
This is normally done before saving the buffer the first time. |
|
If the value is non-nil, it is the result of `file-modes' on the original |
|
|
file; this means that the caller, after saving the buffer, should change |
|
|
the modes of the new file to agree with the old modes. |
|
2329 |
|
|
2330 |
A backup may be done by renaming or by copying; see documentation of |
A backup may be done by renaming or by copying; see documentation of |
2331 |
variable `make-backup-files'. If it's done by renaming, then the file is |
variable `make-backup-files'. If it's done by renaming, then the file is |
2332 |
no longer accessible under its old name." |
no longer accessible under its old name. |
2333 |
|
|
2334 |
|
The value is non-nil after a backup was made by renaming. |
2335 |
|
It has the form (MODES . BACKUPNAME). |
2336 |
|
MODES is the result of `file-modes' on the original |
2337 |
|
file; this means that the caller, after saving the buffer, should change |
2338 |
|
the modes of the new file to agree with the old modes. |
2339 |
|
BACKUPNAME is the backup file name, which is the old file renamed." |
2340 |
(if (and make-backup-files (not backup-inhibited) |
(if (and make-backup-files (not backup-inhibited) |
2341 |
(not buffer-backed-up) |
(not buffer-backed-up) |
2342 |
(file-exists-p buffer-file-name) |
(file-exists-p buffer-file-name) |
2390 |
(copy-file real-file-name backupname t t))) |
(copy-file real-file-name backupname t t))) |
2391 |
;; rename-file should delete old backup. |
;; rename-file should delete old backup. |
2392 |
(rename-file real-file-name backupname t) |
(rename-file real-file-name backupname t) |
2393 |
(setq setmodes (file-modes backupname))) |
(setq setmodes |
2394 |
|
(cons (file-modes backupname) backupname))) |
2395 |
(file-error |
(file-error |
2396 |
;; If trouble writing the backup, write it in ~. |
;; If trouble writing the backup, write it in ~. |
2397 |
(setq backupname (expand-file-name |
(setq backupname (expand-file-name |
2887 |
(nthcdr 10 (file-attributes buffer-file-name))) |
(nthcdr 10 (file-attributes buffer-file-name))) |
2888 |
(if setmodes |
(if setmodes |
2889 |
(condition-case () |
(condition-case () |
2890 |
(set-file-modes buffer-file-name setmodes) |
(set-file-modes buffer-file-name (car setmodes)) |
2891 |
(error nil)))) |
(error nil)))) |
2892 |
;; If the auto-save file was recent before this command, |
;; If the auto-save file was recent before this command, |
2893 |
;; delete it now. |
;; delete it now. |
2900 |
;; This does the "real job" of writing a buffer into its visited file |
;; This does the "real job" of writing a buffer into its visited file |
2901 |
;; and making a backup file. This is what is normally done |
;; and making a backup file. This is what is normally done |
2902 |
;; but inhibited if one of write-file-functions returns non-nil. |
;; but inhibited if one of write-file-functions returns non-nil. |
2903 |
;; It returns a value to store in setmodes. |
;; It returns a value (MODES . BACKUPNAME), like backup-buffer. |
2904 |
(defun basic-save-buffer-1 () |
(defun basic-save-buffer-1 () |
2905 |
(if save-buffer-coding-system |
(if save-buffer-coding-system |
2906 |
(let ((coding-system-for-write save-buffer-coding-system)) |
(let ((coding-system-for-write save-buffer-coding-system)) |
2907 |
(basic-save-buffer-2)) |
(basic-save-buffer-2)) |
2908 |
(basic-save-buffer-2))) |
(basic-save-buffer-2))) |
2909 |
|
|
2910 |
|
;; This returns a value (MODES . BACKUPNAME), like backup-buffer. |
2911 |
(defun basic-save-buffer-2 () |
(defun basic-save-buffer-2 () |
2912 |
(let (tempsetmodes setmodes) |
(let (tempsetmodes setmodes) |
2913 |
(if (not (file-writable-p buffer-file-name)) |
(if (not (file-writable-p buffer-file-name)) |
2966 |
;; Since we have created an entirely new file |
;; Since we have created an entirely new file |
2967 |
;; and renamed it, make sure it gets the |
;; and renamed it, make sure it gets the |
2968 |
;; right permission bits set. |
;; right permission bits set. |
2969 |
(setq setmodes (or setmodes (file-modes buffer-file-name))) |
(setq setmodes (or setmodes (cons (file-modes buffer-file-name) |
2970 |
|
buffer-file-name))) |
2971 |
;; We succeeded in writing the temp file, |
;; We succeeded in writing the temp file, |
2972 |
;; so rename it. |
;; so rename it. |
2973 |
(rename-file tempname buffer-file-name t)) |
(rename-file tempname buffer-file-name t)) |
2977 |
;; (setmodes is set) because that says we're superseding. |
;; (setmodes is set) because that says we're superseding. |
2978 |
(cond ((and tempsetmodes (not setmodes)) |
(cond ((and tempsetmodes (not setmodes)) |
2979 |
;; Change the mode back, after writing. |
;; Change the mode back, after writing. |
2980 |
(setq setmodes (file-modes buffer-file-name)) |
(setq setmodes (cons (file-modes buffer-file-name) buffer-file-name)) |
2981 |
(set-file-modes buffer-file-name (logior setmodes 128)))) |
(set-file-modes buffer-file-name (logior (car setmodes) 128)))) |
2982 |
(write-region (point-min) (point-max) |
(let (success) |
2983 |
buffer-file-name nil t buffer-file-truename))) |
(unwind-protect |
2984 |
|
(progn |
2985 |
|
(write-region (point-min) (point-max) |
2986 |
|
buffer-file-name nil t buffer-file-truename) |
2987 |
|
(setq success t)) |
2988 |
|
;; If we get an error writing the new file, and we made |
2989 |
|
;; the backup by renaming, undo the backing-up. |
2990 |
|
(and setmodes (not success) |
2991 |
|
(rename-file (cdr setmodes) buffer-file-name)))))) |
2992 |
setmodes)) |
setmodes)) |
2993 |
|
|
2994 |
(defun save-some-buffers (&optional arg pred) |
(defun save-some-buffers (&optional arg pred) |