aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1994-03-29 06:59:50 +0000
committerRichard M. Stallman1994-03-29 06:59:50 +0000
commit87d26afc28c00a5e92e32104ab7eda20bbf46a23 (patch)
tree8ed683f6ef60d13b3297038d6c8f47a5a55ef9b3
parent26a8d14bf2db399b0e7373b3fd13ea6c63e01dab (diff)
downloademacs-87d26afc28c00a5e92e32104ab7eda20bbf46a23.tar.gz
emacs-87d26afc28c00a5e92e32104ab7eda20bbf46a23.zip
(basic-save-buffer-1): New subroutine, broken out of basic-save-buffer.
(basic-save-buffer): Use basic-save-buffer-1.
-rw-r--r--lisp/files.el111
1 files changed, 60 insertions, 51 deletions
diff --git a/lisp/files.el b/lisp/files.el
index e3d3bbe2dfb..e648143b102 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -1530,57 +1530,7 @@ the last real save, but optional arg FORCE non-nil means delete anyway."
1530 (setq hooks (cdr hooks))) 1530 (setq hooks (cdr hooks)))
1531 ;; If a hook returned t, file is already "written". 1531 ;; If a hook returned t, file is already "written".
1532 (cond ((not done) 1532 (cond ((not done)
1533 (if (not (file-writable-p buffer-file-name)) 1533 (setq setmodes (basic-save-buffer-1)))))
1534 (let ((dir (file-name-directory buffer-file-name)))
1535 (if (not (file-directory-p dir))
1536 (error "%s is not a directory" dir)
1537 (if (not (file-exists-p buffer-file-name))
1538 (error "Directory %s write-protected" dir)
1539 (if (yes-or-no-p
1540 (format "File %s is write-protected; try to save anyway? "
1541 (file-name-nondirectory
1542 buffer-file-name)))
1543 (setq tempsetmodes t)
1544 (error "Attempt to save to a file which you aren't allowed to write"))))))
1545 (or buffer-backed-up
1546 (setq setmodes (backup-buffer)))
1547 (if file-precious-flag
1548 ;; If file is precious, write temp name, then rename it.
1549 (let ((dir (file-name-directory buffer-file-name))
1550 (realname buffer-file-name)
1551 tempname temp nogood i succeed)
1552 (setq i 0)
1553 (setq nogood t)
1554 ;; Find the temporary name to write under.
1555 (while nogood
1556 (setq tempname (format "%s#tmp#%d" dir i))
1557 (setq nogood (file-exists-p tempname))
1558 (setq i (1+ i)))
1559 (unwind-protect
1560 (progn (clear-visited-file-modtime)
1561 (write-region (point-min) (point-max)
1562 tempname nil realname)
1563 (setq succeed t))
1564 ;; If writing the temp file fails,
1565 ;; delete the temp file.
1566 (or succeed (delete-file tempname)))
1567 ;; Since we have created an entirely new file
1568 ;; and renamed it, make sure it gets the
1569 ;; right permission bits set.
1570 (setq setmodes (file-modes buffer-file-name))
1571 ;; We succeeded in writing the temp file,
1572 ;; so rename it.
1573 (rename-file tempname buffer-file-name t))
1574 ;; If file not writable, see if we can make it writable
1575 ;; temporarily while we write it.
1576 ;; But no need to do so if we have just backed it up
1577 ;; (setmodes is set) because that says we're superseding.
1578 (cond ((and tempsetmodes (not setmodes))
1579 ;; Change the mode back, after writing.
1580 (setq setmodes (file-modes buffer-file-name))
1581 (set-file-modes buffer-file-name 511)))
1582 (write-region (point-min) (point-max)
1583 buffer-file-name nil t)))))
1584 (setq buffer-file-number (nth 10 (file-attributes buffer-file-name))) 1534 (setq buffer-file-number (nth 10 (file-attributes buffer-file-name)))
1585 (if setmodes 1535 (if setmodes
1586 (condition-case () 1536 (condition-case ()
@@ -1592,6 +1542,65 @@ the last real save, but optional arg FORCE non-nil means delete anyway."
1592 (run-hooks 'after-save-hook)) 1542 (run-hooks 'after-save-hook))
1593 (message "(No changes need to be saved)"))) 1543 (message "(No changes need to be saved)")))
1594 1544
1545;; This does the "real job" of writing a buffer into its visited file
1546;; and making a backup file. This is what is normally done
1547;; but inhibited if one of write-file-hooks returns non-nil.
1548;; It returns a value to store in setmodes.
1549(defun basic-save-buffer-1 ()
1550 (let (tempsetmodes setmodes)
1551 (if (not (file-writable-p buffer-file-name))
1552 (let ((dir (file-name-directory buffer-file-name)))
1553 (if (not (file-directory-p dir))
1554 (error "%s is not a directory" dir)
1555 (if (not (file-exists-p buffer-file-name))
1556 (error "Directory %s write-protected" dir)
1557 (if (yes-or-no-p
1558 (format "File %s is write-protected; try to save anyway? "
1559 (file-name-nondirectory
1560 buffer-file-name)))
1561 (setq tempsetmodes t)
1562 (error "Attempt to save to a file which you aren't allowed to write"))))))
1563 (or buffer-backed-up
1564 (setq setmodes (backup-buffer)))
1565 (if file-precious-flag
1566 ;; If file is precious, write temp name, then rename it.
1567 (let ((dir (file-name-directory buffer-file-name))
1568 (realname buffer-file-name)
1569 tempname temp nogood i succeed)
1570 (setq i 0)
1571 (setq nogood t)
1572 ;; Find the temporary name to write under.
1573 (while nogood
1574 (setq tempname (format "%s#tmp#%d" dir i))
1575 (setq nogood (file-exists-p tempname))
1576 (setq i (1+ i)))
1577 (unwind-protect
1578 (progn (clear-visited-file-modtime)
1579 (write-region (point-min) (point-max)
1580 tempname nil realname)
1581 (setq succeed t))
1582 ;; If writing the temp file fails,
1583 ;; delete the temp file.
1584 (or succeed (delete-file tempname)))
1585 ;; Since we have created an entirely new file
1586 ;; and renamed it, make sure it gets the
1587 ;; right permission bits set.
1588 (setq setmodes (file-modes buffer-file-name))
1589 ;; We succeeded in writing the temp file,
1590 ;; so rename it.
1591 (rename-file tempname buffer-file-name t))
1592 ;; If file not writable, see if we can make it writable
1593 ;; temporarily while we write it.
1594 ;; But no need to do so if we have just backed it up
1595 ;; (setmodes is set) because that says we're superseding.
1596 (cond ((and tempsetmodes (not setmodes))
1597 ;; Change the mode back, after writing.
1598 (setq setmodes (file-modes buffer-file-name))
1599 (set-file-modes buffer-file-name 511)))
1600 (write-region (point-min) (point-max)
1601 buffer-file-name nil t))
1602 setmodes))
1603
1595(defun save-some-buffers (&optional arg exiting) 1604(defun save-some-buffers (&optional arg exiting)
1596 "Save some modified file-visiting buffers. Asks user about each one. 1605 "Save some modified file-visiting buffers. Asks user about each one.
1597Optional argument (the prefix) non-nil means save all with no questions. 1606Optional argument (the prefix) non-nil means save all with no questions.