unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Sean Whitton <spwhitton@spwhitton.name>
To: emacs-devel@gnu.org, 55257@debbugs.gnu.org
Cc: Robert Pluim <rpluim@gmail.com>, Eli Zaretskii <eliz@gnu.org>,
	Stefan Monnier <monnier@iro.umontreal.ca>
Subject: bug#55257: master f2d2fe6fc8: server-execute: Initialize the *scratch* buffer
Date: Sat, 07 May 2022 17:27:38 -0700	[thread overview]
Message-ID: <871qx4lvgl.fsf@melete.silentflame.com> (raw)
In-Reply-To: <871qx74or6.fsf@melete.silentflame.com>

[-- Attachment #1: Type: text/plain, Size: 644 bytes --]

Hello,

On Thu 05 May 2022 at 03:07PM -07, Sean Whitton wrote:

> Hello,
>
> On Wed 04 May 2022 at 12:26PM -04, Stefan Monnier wrote:
>
>>> It looks like Fother_window is called only from Fcall_interactively and
>>> Fkill_buffer, so there probably isn't a bootstrapping issue if I make
>>> those Ffuncall my new `get-initial-buffer-create'.  It looks like
>>> bootstrapping C code just makes an empty *scratch* and leaves it to
>>> startup.el to initialise it.
>>
>> Even better,
>
> Here's my fix.  Haven't quite finished testing each and every call site
> but seemed worth posting it for comments.

Here's an updated patch.

-- 
Sean Whitton

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: v2-0001-Factor-out-scratch-initialization.patch --]
[-- Type: text/x-patch, Size: 9132 bytes --]

From 4b80914abeb67065b22eb0a491cf684a5b1eff71 Mon Sep 17 00:00:00 2001
From: Sean Whitton <spwhitton@spwhitton.name>
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'.
+
 \f
 * 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)))
 
 \f
 
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);
 }
 \f
 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


  parent reply	other threads:[~2022-05-08  0:27 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <165162665935.26821.8964921720746152690@vcs2.savannah.gnu.org>
     [not found] ` <20220504011059.9F667C009A8@vcs2.savannah.gnu.org>
2022-05-04  2:14   ` master f2d2fe6fc8: server-execute: Initialize the *scratch* buffer Stefan Monnier
2022-05-04  5:40     ` Sean Whitton
2022-05-04 12:47       ` Stefan Monnier
2022-05-04 14:23         ` Sean Whitton
2022-05-04 14:34           ` Robert Pluim
2022-05-04 14:46             ` Sean Whitton
2022-05-04 14:56               ` Robert Pluim
2022-05-04 14:42           ` Stefan Monnier
2022-05-04 15:41             ` Sean Whitton
2022-05-04 16:26               ` Stefan Monnier
2022-05-05 22:07                 ` Sean Whitton
2022-05-05 22:13                   ` Sean Whitton
2022-05-06 11:34                     ` Stefan Monnier
2022-05-06 19:20                       ` Sean Whitton
2022-05-06 19:24                         ` Lars Ingebrigtsen
2022-05-06  5:40                   ` Eli Zaretskii
2022-05-06 19:26                     ` Sean Whitton
2022-05-07  5:30                       ` Eli Zaretskii
2022-05-07 13:51                         ` Stefan Monnier
2022-05-07 14:12                           ` Eli Zaretskii
2022-05-07 16:29                         ` Sean Whitton
2022-05-07 16:41                           ` Eli Zaretskii
2022-05-07 17:02                             ` Sean Whitton
2022-05-06  7:41                   ` Juri Linkov
2022-05-06 19:28                     ` Sean Whitton
2022-05-08  0:27                   ` Sean Whitton [this message]
2022-05-09 18:11                     ` Stefan Monnier
2022-05-10  1:49                       ` Sean Whitton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871qx4lvgl.fsf@melete.silentflame.com \
    --to=spwhitton@spwhitton.name \
    --cc=55257@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=rpluim@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).