summaryrefslogtreecommitdiff
path: root/lisp/files.el
diff options
context:
space:
mode:
authorRichard M. Stallman <rms@gnu.org>2004-12-31 14:48:12 +0000
committerRichard M. Stallman <rms@gnu.org>2004-12-31 14:48:12 +0000
commitf4206092a2de1d5c064791e8dde78bf2923725c0 (patch)
tree5cb62697773b8a6799f0b42a352a5b875fe3b52b /lisp/files.el
parenta10e1fb7dabc325fc6d7e1e99c6f65287e19f12a (diff)
downloademacs-f4206092a2de1d5c064791e8dde78bf2923725c0.tar.gz
(require-final-newline): Allow `visit' and `visit-save'.
(mode-require-final-newline): New option. (after-find-file): Handle require-final-newline with new values. (basic-save-buffer): Handle new values of require-final-newline.
Diffstat (limited to 'lisp/files.el')
-rw-r--r--lisp/files.el46
1 files changed, 41 insertions, 5 deletions
diff --git a/lisp/files.el b/lisp/files.el
index 4853e46095d..d1340235ea5 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -273,14 +273,40 @@ Includes the new backup. Must be > 0"
:group 'backup)
(defcustom require-final-newline nil
- "*Value of t says silently ensure a file ends in a newline when it is saved.
-Non-nil but not t says ask user whether to add a newline when there isn't one.
-nil means don't add newlines."
- :type '(choice (const :tag "Off" nil)
- (const :tag "Add" t)
+ "*Whether to add a newline automatically at the end of the file.
+
+A value of t means do this only when the file is about to be saved.
+A value of `visit' means do this right after the file is visited.
+A value of `visit-save' means do it at both of those times.
+Any other non-nil value means ask user whether to add a newline, when saving.
+nil means don't add newlines.
+
+Certain major modes set this locally to the value obtained
+from `mode-require-final-newline'."
+ :type '(choice (const :tag "When visiting" visit)
+ (const :tag "When saving" t)
+ (const :tag "When visiting or saving" visit-save)
+ (const :tag "Never" nil)
(other :tag "Ask" ask))
:group 'editing-basics)
+(defcustom mode-require-final-newline t
+ "*Whether to add a newline at the end of the file, in certain major modes.
+Those modes set `require-final-newline' to this value when you enable them.
+They do so because they are used for files that are supposed
+to end in newlines, and the question is how to arrange that.
+
+A value of t means do this only when the file is about to be saved.
+A value of `visit' means do this right after the file is visited.
+A value of `visit-save' means do it at both of those times.
+Any other non-nil value means ask user whether to add a newline, when saving."
+ :type '(choice (const :tag "When visiting" visit)
+ (const :tag "When saving" t)
+ (const :tag "When visiting or saving" visit-save)
+ (other :tag "Ask" ask))
+ :group 'editing-basics
+ :version "21.4")
+
(defcustom auto-save-default t
"*Non-nil says by default do auto-saving of every file-visiting buffer."
:type 'boolean
@@ -1627,6 +1653,15 @@ unless NOMODES is non-nil."
(when (and view-read-only view-mode)
(view-mode-disable))
(normal-mode t)
+ ;; If requested, add a newline at the end of the file.
+ (and (memq require-final-newline '(visit visit-save))
+ (> (point-max) (point-min))
+ (/= (char-after (1- (point-max))) ?\n)
+ (not (and (eq selective-display t)
+ (= (char-after (1- (point-max))) ?\r)))
+ (save-excursion
+ (goto-char (point-max))
+ (insert "\n")))
(when (and buffer-read-only
view-read-only
(not (eq (get major-mode 'mode-class) 'special)))
@@ -3194,6 +3229,7 @@ Before and after saving the buffer, this function runs
(not (and (eq selective-display t)
(= (char-after (1- (point-max))) ?\r)))
(or (eq require-final-newline t)
+ (eq require-final-newline 'visit-save)
(and require-final-newline
(y-or-n-p
(format "Buffer %s does not end in newline. Add one? "