aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman2002-07-23 19:23:08 +0000
committerRichard M. Stallman2002-07-23 19:23:08 +0000
commitf3f9e207784cd341c9a2bf4d627b65ab8422b028 (patch)
tree5f096c0dddabf3d7e0b36d7a7d2e5515ffcad22b
parentab6a36685e9e503485e6cff1533f81eb22c072c9 (diff)
downloademacs-f3f9e207784cd341c9a2bf4d627b65ab8422b028.tar.gz
emacs-f3f9e207784cd341c9a2bf4d627b65ab8422b028.zip
(basic-save-buffer-2): If there's an error writing the file,
unrename the backup file if it was just made. (backup-buffer, basic-save-buffer-2, basic-save-buffer-1) (basic-save-buffer): Value now has form (MODES . BACKUPNAME) when a backup was just made by renaming. Otherwise it's nil.
-rw-r--r--lisp/files.el39
1 files changed, 27 insertions, 12 deletions
diff --git a/lisp/files.el b/lisp/files.el
index 51fde3fd271..549bd1de8a0 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -2326,13 +2326,17 @@ Interactively, confirmation is required unless you supply a prefix argument."
2326(defun backup-buffer () 2326(defun backup-buffer ()
2327 "Make a backup of the disk file visited by the current buffer, if appropriate. 2327 "Make a backup of the disk file visited by the current buffer, if appropriate.
2328This is normally done before saving the buffer the first time. 2328This is normally done before saving the buffer the first time.
2329If the value is non-nil, it is the result of `file-modes' on the original
2330file; this means that the caller, after saving the buffer, should change
2331the modes of the new file to agree with the old modes.
2332 2329
2333A backup may be done by renaming or by copying; see documentation of 2330A backup may be done by renaming or by copying; see documentation of
2334variable `make-backup-files'. If it's done by renaming, then the file is 2331variable `make-backup-files'. If it's done by renaming, then the file is
2335no longer accessible under its old name." 2332no longer accessible under its old name.
2333
2334The value is non-nil after a backup was made by renaming.
2335It has the form (MODES . BACKUPNAME).
2336MODES is the result of `file-modes' on the original
2337file; this means that the caller, after saving the buffer, should change
2338the modes of the new file to agree with the old modes.
2339BACKUPNAME is the backup file name, which is the old file renamed."
2336 (if (and make-backup-files (not backup-inhibited) 2340 (if (and make-backup-files (not backup-inhibited)
2337 (not buffer-backed-up) 2341 (not buffer-backed-up)
2338 (file-exists-p buffer-file-name) 2342 (file-exists-p buffer-file-name)
@@ -2386,7 +2390,8 @@ no longer accessible under its old name."
2386 (copy-file real-file-name backupname t t))) 2390 (copy-file real-file-name backupname t t)))
2387 ;; rename-file should delete old backup. 2391 ;; rename-file should delete old backup.
2388 (rename-file real-file-name backupname t) 2392 (rename-file real-file-name backupname t)
2389 (setq setmodes (file-modes backupname))) 2393 (setq setmodes
2394 (cons (file-modes backupname) backupname)))
2390 (file-error 2395 (file-error
2391 ;; If trouble writing the backup, write it in ~. 2396 ;; If trouble writing the backup, write it in ~.
2392 (setq backupname (expand-file-name 2397 (setq backupname (expand-file-name
@@ -2882,7 +2887,7 @@ After saving the buffer, this function runs `after-save-hook'."
2882 (nthcdr 10 (file-attributes buffer-file-name))) 2887 (nthcdr 10 (file-attributes buffer-file-name)))
2883 (if setmodes 2888 (if setmodes
2884 (condition-case () 2889 (condition-case ()
2885 (set-file-modes buffer-file-name setmodes) 2890 (set-file-modes buffer-file-name (car setmodes))
2886 (error nil)))) 2891 (error nil))))
2887 ;; If the auto-save file was recent before this command, 2892 ;; If the auto-save file was recent before this command,
2888 ;; delete it now. 2893 ;; delete it now.
@@ -2895,13 +2900,14 @@ After saving the buffer, this function runs `after-save-hook'."
2895;; This does the "real job" of writing a buffer into its visited file 2900;; This does the "real job" of writing a buffer into its visited file
2896;; and making a backup file. This is what is normally done 2901;; and making a backup file. This is what is normally done
2897;; but inhibited if one of write-file-functions returns non-nil. 2902;; but inhibited if one of write-file-functions returns non-nil.
2898;; It returns a value to store in setmodes. 2903;; It returns a value (MODES . BACKUPNAME), like backup-buffer.
2899(defun basic-save-buffer-1 () 2904(defun basic-save-buffer-1 ()
2900 (if save-buffer-coding-system 2905 (if save-buffer-coding-system
2901 (let ((coding-system-for-write save-buffer-coding-system)) 2906 (let ((coding-system-for-write save-buffer-coding-system))
2902 (basic-save-buffer-2)) 2907 (basic-save-buffer-2))
2903 (basic-save-buffer-2))) 2908 (basic-save-buffer-2)))
2904 2909
2910;; This returns a value (MODES . BACKUPNAME), like backup-buffer.
2905(defun basic-save-buffer-2 () 2911(defun basic-save-buffer-2 ()
2906 (let (tempsetmodes setmodes) 2912 (let (tempsetmodes setmodes)
2907 (if (not (file-writable-p buffer-file-name)) 2913 (if (not (file-writable-p buffer-file-name))
@@ -2960,7 +2966,8 @@ After saving the buffer, this function runs `after-save-hook'."
2960 ;; Since we have created an entirely new file 2966 ;; Since we have created an entirely new file
2961 ;; and renamed it, make sure it gets the 2967 ;; and renamed it, make sure it gets the
2962 ;; right permission bits set. 2968 ;; right permission bits set.
2963 (setq setmodes (or setmodes (file-modes buffer-file-name))) 2969 (setq setmodes (or setmodes (cons (file-modes buffer-file-name)
2970 buffer-file-name)))
2964 ;; We succeeded in writing the temp file, 2971 ;; We succeeded in writing the temp file,
2965 ;; so rename it. 2972 ;; so rename it.
2966 (rename-file tempname buffer-file-name t)) 2973 (rename-file tempname buffer-file-name t))
@@ -2970,10 +2977,18 @@ After saving the buffer, this function runs `after-save-hook'."
2970 ;; (setmodes is set) because that says we're superseding. 2977 ;; (setmodes is set) because that says we're superseding.
2971 (cond ((and tempsetmodes (not setmodes)) 2978 (cond ((and tempsetmodes (not setmodes))
2972 ;; Change the mode back, after writing. 2979 ;; Change the mode back, after writing.
2973 (setq setmodes (file-modes buffer-file-name)) 2980 (setq setmodes (cons (file-modes buffer-file-name) buffer-file-name))
2974 (set-file-modes buffer-file-name (logior setmodes 128)))) 2981 (set-file-modes buffer-file-name (logior (car setmodes) 128))))
2975 (write-region (point-min) (point-max) 2982 (let (success)
2976 buffer-file-name nil t buffer-file-truename))) 2983 (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))))))
2977 setmodes)) 2992 setmodes))
2978 2993
2979(defun save-some-buffers (&optional arg pred) 2994(defun save-some-buffers (&optional arg pred)