>From 4b80914abeb67065b22eb0a491cf684a5b1eff71 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Thu, 5 May 2022 13:03:06 -0700 Subject: [PATCH v2] Factor out *scratch* initialization * lisp/simple.el (get-initial-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_initial_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. --- doc/lispref/os.texi | 8 ++++---- etc/NEWS | 8 ++++++++ lisp/server.el | 2 +- lisp/simple.el | 20 ++++++++++++++------ lisp/startup.el | 12 +++--------- lisp/window.el | 18 ++++++++---------- src/buffer.c | 22 +++------------------- 7 files changed, 41 insertions(+), 49 deletions(-) diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi index 9df708532d..f4dd2e7072 100644 --- a/doc/lispref/os.texi +++ b/doc/lispref/os.texi @@ -329,10 +329,10 @@ Startup Summary @end defopt @defopt initial-scratch-message -This variable, if non-@code{nil}, should be a string, which is -treated as documentation to be -inserted into the @file{*scratch*} buffer when Emacs starts up. If it -is @code{nil}, the @file{*scratch*} buffer is empty. +This variable, if non-@code{nil}, should be a string, which is treated +as documentation to be inserted into the @file{*scratch*} buffer when +Emacs starts up or when that buffer is recreated. If it is +@code{nil}, the @file{*scratch*} buffer is empty. @end defopt @noindent diff --git a/etc/NEWS b/etc/NEWS index 6637eda00c..20ef8f7b52 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -238,6 +238,14 @@ encouraged to test timestamp-related code with this variable set to nil, as it will default to nil in a future Emacs version and will be removed some time after that. ++++ +** Functions which recreate the *scratch* buffer now also initialize it. +When functions like 'other-buffer' and 'server-execute' recreate +*scratch*, they now also insert 'initial-scratch-message' and change +the major mode according to 'initial-major-mode', like at Emacs +startup. Previously, these functions ignored +'initial-scratch-message' and left *scratch* in 'fundamental-mode'. + * Changes in Emacs 29.1 diff --git a/lisp/server.el b/lisp/server.el index 763cf27f7a..042962b8e9 100644 --- a/lisp/server.el +++ b/lisp/server.el @@ -1367,7 +1367,7 @@ server-execute ((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-initial-buffer-create)) 'norecord))) ;; Delete the client if necessary. diff --git a/lisp/simple.el b/lisp/simple.el index 861d9eefde..1ed82b0a48 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -10213,16 +10213,24 @@ capitalize-dwim the number of seconds east of Greenwich.") ) +(defun get-initial-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-initial-buffer-create))) diff --git a/lisp/startup.el b/lisp/startup.el index c7cf86a01e..3fa25ddee9 100644 --- a/lisp/startup.el +++ b/lisp/startup.el @@ -2355,7 +2355,7 @@ normal-no-mouse-startup-screen (insert "\t\t") (insert-button "Open *scratch* buffer" 'action (lambda (_button) (switch-to-buffer - (startup--get-buffer-create-scratch))) + (get-initial-buffer-create))) 'follow-link t) (insert "\n") (save-restriction @@ -2487,12 +2487,6 @@ display-about-screen (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" ()) @@ -2784,7 +2778,7 @@ command-line-1 (when (eq initial-buffer-choice t) ;; When `initial-buffer-choice' equals t make sure that *scratch* ;; exists. - (startup--get-buffer-create-scratch)) + (get-initial-buffer-create)) ;; If *scratch* exists and is empty, insert initial-scratch-message. ;; Do this before switching to *scratch* below to handle bug#9605. @@ -2808,7 +2802,7 @@ command-line-1 ((functionp initial-buffer-choice) (funcall initial-buffer-choice)) ((eq initial-buffer-choice t) - (startup--get-buffer-create-scratch)) + (get-initial-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 9f78784612..615c5c078a 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -4886,10 +4886,7 @@ last-buffer (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-initial-buffer-create))) (defcustom frame-auto-hide-function #'iconify-frame "Function called to automatically hide frames. @@ -8621,12 +8618,13 @@ window-normalize-buffer-to-switch-to `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-initial-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'. diff --git a/src/buffer.c b/src/buffer.c index f8a7a4f510..a153ffe78c 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -1634,16 +1634,7 @@ DEFUN ("other-buffer", Fother_buffer, Sother_buffer, 0, 3, 0, if (!NILP (notsogood)) return notsogood; else - { - AUTO_STRING (scratch, "*scratch*"); - buf = Fget_buffer (scratch); - if (NILP (buf)) - { - buf = Fget_buffer_create (scratch, Qnil); - Fset_buffer_major_mode (buf); - } - return buf; - } + return safe_call (1, Qget_initial_buffer_create); } /* The following function is a safe variant of Fother_buffer: It doesn't @@ -1659,15 +1650,7 @@ other_buffer_safely (Lisp_Object buffer) if (candidate_buffer (buf, buffer)) return buf; - AUTO_STRING (scratch, "*scratch*"); - buf = Fget_buffer (scratch); - if (NILP (buf)) - { - buf = Fget_buffer_create (scratch, Qnil); - Fset_buffer_major_mode (buf); - } - - return buf; + return safe_call (1, Qget_initial_buffer_create); } DEFUN ("buffer-enable-undo", Fbuffer_enable_undo, Sbuffer_enable_undo, @@ -5552,6 +5535,7 @@ syms_of_buffer (void) DEFSYM (Qbefore_change_functions, "before-change-functions"); DEFSYM (Qafter_change_functions, "after-change-functions"); DEFSYM (Qkill_buffer_query_functions, "kill-buffer-query-functions"); + DEFSYM (Qget_initial_buffer_create, "get-initial-buffer-create"); DEFSYM (Qvertical_scroll_bar, "vertical-scroll-bar"); Fput (Qvertical_scroll_bar, Qchoice, list4 (Qnil, Qt, Qleft, Qright)); -- 2.30.2