summaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2022-05-05 13:03:06 -0700
committerSean Whitton <spwhitton@spwhitton.name>2022-05-09 18:49:13 -0700
commit054062060e9f57fd037578378c23ad9ec294edac (patch)
treec9eaec1c5c4ce19e6be0c0c1000d0fb58bebd1b1 /lisp
parent54ab2b36740166d379c713e843870310f1ccf7a1 (diff)
downloademacs-054062060e9f57fd037578378c23ad9ec294edac.tar.gz
Factor out *scratch* initialization
* lisp/simple.el (get-scratch-buffer-create): New function, factored out of scratch-buffer, and additionally clearing the modification flag and calling substitute-command-keys (bug#55257). (scratch-buffer): * lisp/server.el (server-execute): * lisp/startup.el (normal-no-mouse-startup-screen, command-line-1): * lisp/window.el (last-buffer, window-normalize-buffer-to-switch-to): * src/buffer.c (Fother_buffer, other_buffer_safely): Use it. (syms_of_buffer): Add Qget_scratch_buffer_create. * lisp/startup.el (startup--get-buffer-create-scratch): Delete now-unused function. * doc/lispref/os.texi (Summary: Sequence of Actions at Startup): * NEWS (Incompatible changes in Emacs 29.1): Document the change.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/server.el2
-rw-r--r--lisp/simple.el20
-rw-r--r--lisp/startup.el12
-rw-r--r--lisp/window.el18
4 files changed, 26 insertions, 26 deletions
diff --git a/lisp/server.el b/lisp/server.el
index 763cf27f7ac..8f47a99a31a 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1367,7 +1367,7 @@ The following commands are accepted by the client:
((functionp initial-buffer-choice)
(funcall initial-buffer-choice)))))
(switch-to-buffer
- (if (buffer-live-p buf) buf (get-buffer-create "*scratch*"))
+ (if (buffer-live-p buf) buf (get-scratch-buffer-create))
'norecord)))
;; Delete the client if necessary.
diff --git a/lisp/simple.el b/lisp/simple.el
index 861d9eefde9..edcc226bfa8 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -10213,16 +10213,24 @@ This is an integer indicating the UTC offset in seconds, i.e.,
the number of seconds east of Greenwich.")
)
+(defun get-scratch-buffer-create ()
+ "Return the \*scratch\* buffer, creating a new one if needed."
+ (or (get-buffer "*scratch*")
+ (let ((scratch (get-buffer-create "*scratch*")))
+ ;; Don't touch the buffer contents or mode unless we know that
+ ;; we just created it.
+ (with-current-buffer scratch
+ (when initial-scratch-message
+ (insert (substitute-command-keys initial-scratch-message))
+ (set-buffer-modified-p nil))
+ (funcall initial-major-mode))
+ scratch)))
+
(defun scratch-buffer ()
"Switch to the \*scratch\* buffer.
If the buffer doesn't exist, create it first."
(interactive)
- (if (get-buffer "*scratch*")
- (pop-to-buffer-same-window "*scratch*")
- (pop-to-buffer-same-window (get-buffer-create "*scratch*"))
- (when initial-scratch-message
- (insert initial-scratch-message))
- (funcall initial-major-mode)))
+ (pop-to-buffer-same-window (get-scratch-buffer-create)))
diff --git a/lisp/startup.el b/lisp/startup.el
index 0b7d90ecf2b..433a58bf2c4 100644
--- a/lisp/startup.el
+++ b/lisp/startup.el
@@ -2358,7 +2358,7 @@ If you have no Meta key, you may instead type ESC followed by the character.)"))
(insert "\t\t")
(insert-button "Open *scratch* buffer"
'action (lambda (_button) (switch-to-buffer
- (startup--get-buffer-create-scratch)))
+ (get-scratch-buffer-create)))
'follow-link t)
(insert "\n")
(save-restriction
@@ -2490,12 +2490,6 @@ A fancy display is used on graphic displays, normal otherwise."
(defalias 'about-emacs 'display-about-screen)
(defalias 'display-splash-screen 'display-startup-screen)
-(defun startup--get-buffer-create-scratch ()
- (or (get-buffer "*scratch*")
- (with-current-buffer (get-buffer-create "*scratch*")
- (set-buffer-major-mode (current-buffer))
- (current-buffer))))
-
;; This avoids byte-compiler warning in the unexec build.
(declare-function pdumper-stats "pdumper.c" ())
@@ -2787,7 +2781,7 @@ nil default-directory" name)
(when (eq initial-buffer-choice t)
;; When `initial-buffer-choice' equals t make sure that *scratch*
;; exists.
- (startup--get-buffer-create-scratch))
+ (get-scratch-buffer-create))
;; If *scratch* exists and is empty, insert initial-scratch-message.
;; Do this before switching to *scratch* below to handle bug#9605.
@@ -2811,7 +2805,7 @@ nil default-directory" name)
((functionp initial-buffer-choice)
(funcall initial-buffer-choice))
((eq initial-buffer-choice t)
- (startup--get-buffer-create-scratch))
+ (get-scratch-buffer-create))
(t
(error "`initial-buffer-choice' must be a string, a function, or t")))))
(unless (buffer-live-p buf)
diff --git a/lisp/window.el b/lisp/window.el
index 52003f7b7b4..dd16b83377b 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -4886,10 +4886,7 @@ the buffer `*scratch*', creating it if necessary."
(setq frame (or frame (selected-frame)))
(or (get-next-valid-buffer (nreverse (buffer-list frame))
buffer visible-ok frame)
- (get-buffer "*scratch*")
- (let ((scratch (get-buffer-create "*scratch*")))
- (set-buffer-major-mode scratch)
- scratch)))
+ (get-scratch-buffer-create)))
(defcustom frame-auto-hide-function #'iconify-frame
"Function called to automatically hide frames.
@@ -8621,12 +8618,13 @@ If BUFFER-OR-NAME is nil, return the buffer returned by
`other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
exists, return that buffer. If no such buffer exists, create a
buffer with the name BUFFER-OR-NAME and return that buffer."
- (if buffer-or-name
- (or (get-buffer buffer-or-name)
- (let ((buffer (get-buffer-create buffer-or-name)))
- (set-buffer-major-mode buffer)
- buffer))
- (other-buffer)))
+ (pcase buffer-or-name
+ ('nil (other-buffer))
+ ("*scratch*" (get-scratch-buffer-create))
+ (_ (or (get-buffer buffer-or-name)
+ (let ((buffer (get-buffer-create buffer-or-name)))
+ (set-buffer-major-mode buffer)
+ buffer)))))
(defcustom switch-to-buffer-preserve-window-point t
"If non-nil, `switch-to-buffer' tries to preserve `window-point'.