From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Sean Whitton Newsgroups: gmane.emacs.bugs,gmane.emacs.devel Subject: bug#55257: master f2d2fe6fc8: server-execute: Initialize the *scratch* buffer Date: Sat, 07 May 2022 17:27:38 -0700 Message-ID: <871qx4lvgl.fsf@melete.silentflame.com> References: <165162665935.26821.8964921720746152690@vcs2.savannah.gnu.org> <20220504011059.9F667C009A8@vcs2.savannah.gnu.org> <87levhdfeh.fsf@athena.silentflame.com> <87y1zhe5qz.fsf@athena.silentflame.com> <87levhe24h.fsf@athena.silentflame.com> <871qx74or6.fsf@melete.silentflame.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4050"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Notmuch/0.36 Emacs/29.0.50 (x86_64-pc-linux-gnu) Cc: Robert Pluim , Eli Zaretskii , Stefan Monnier To: emacs-devel@gnu.org, 55257@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun May 08 02:28:14 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nnUmS-0000nT-T5 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 08 May 2022 02:28:14 +0200 Original-Received: from localhost ([::1]:43734 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nnUmR-0008Im-BE for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 07 May 2022 20:28:11 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51468) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nnUmJ-0008Fe-0n for bug-gnu-emacs@gnu.org; Sat, 07 May 2022 20:28:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:59241) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nnUmI-0003CN-No for bug-gnu-emacs@gnu.org; Sat, 07 May 2022 20:28:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nnUmI-000252-I4 for bug-gnu-emacs@gnu.org; Sat, 07 May 2022 20:28:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Sean Whitton Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 08 May 2022 00:28:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 55257 X-GNU-PR-Package: emacs Original-Received: via spool by 55257-submit@debbugs.gnu.org id=B55257.16519696737979 (code B ref 55257); Sun, 08 May 2022 00:28:02 +0000 Original-Received: (at 55257) by debbugs.gnu.org; 8 May 2022 00:27:53 +0000 Original-Received: from localhost ([127.0.0.1]:53138 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nnUm8-00024c-Tn for submit@debbugs.gnu.org; Sat, 07 May 2022 20:27:53 -0400 Original-Received: from wout3-smtp.messagingengine.com ([64.147.123.19]:58963) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nnUm2-00024K-TV for 55257@debbugs.gnu.org; Sat, 07 May 2022 20:27:51 -0400 Original-Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id C3ED7320051E; Sat, 7 May 2022 20:27:40 -0400 (EDT) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Sat, 07 May 2022 20:27:41 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=spwhitton.name; h=cc:cc:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm3; t=1651969660; x=1652056060; bh=mM KQF7g1HvumOPA/p5dIrvU9MnaKokCtyUL4JG3AnkU=; b=cl68Wxc1l2GN6wnuMj ZAruAf/qWlVnvDUXUxiKERzDP+amuIAhNOuS8huP3fGQVRTR+Vl+qTgcu8uKIHjH 5n+QH6BUwH7jMdOs9mhuVLiTP52Dv8PLLkcaWHZ2Z8NmMs92Dpn81xcdu7ROf+wK PX5PPSx1eS+7u58B2xBewU+5zPpBTOAtzzRsl0bfQr+pwVvUmmId3dksWcHt7jX6 DvK730ELeiQX1gOLeY52FJFYQ9DhuMuWQkkATfJvV5pPv8ScKkGlfZgQC4R3It0v cXFalkJU4x94LrNUJG2MopLTliIAcxNvnm9n/xfBjV+taWbbj9uFQG5mVpjGjgQk g6qQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1651969660; x= 1652056060; bh=mMKQF7g1HvumOPA/p5dIrvU9MnaKokCtyUL4JG3AnkU=; b=C v9BWKuTUf9Y19TCn85UNVmTsAFEA9vsPg5Ls/s8lEu/JPTeBjLsmLBcMZsB8Tc3K R6NXxalFqXasZzxO4Ecc1cqZo4yIAWHo5ki8uS+lZsbOcrsvYK9hCF3stzLfBMR7 AzSG9WhGEZGQAHYVN5tE5TT7ZOWmDNcrrOv1mWe2WIJfBB67rDVuYsQ19T4j1fKI 5F3KnIK/1Om2AQG8Y4QBvcxP3yAJRQ+Rc5UDh8m8mz6gWiprZ9mwPXhiIud6ZL9c K34DW/QqTAWmluc2zXZxcZWKE8sveNE+vbOg5ytB4hb+yEvnVQZGT7ndpFlzHmcj 4HvR1SQLKhExvVvuNNv7Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrfeeigdeffecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvfevufgjfhgffffkgggtsehmtddtredtredtnecuhfhrohhmpefuvggrnhcu hghhihhtthhonhcuoehsphifhhhithhtohhnsehsphifhhhithhtohhnrdhnrghmvgeqne cuggftrfgrthhtvghrnhepfeffhfegudeuueehkeejjefhgeelffdvveefheekvedukeeu keeuhedvueejvdfhnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilh hfrhhomhepshhpfihhihhtthhonhesshhpfihhihhtthhonhdrnhgrmhgv X-ME-Proxy: Original-Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 7 May 2022 20:27:39 -0400 (EDT) Original-Received: by melete.silentflame.com (Postfix, from userid 1000) id 032767E4942; Sat, 7 May 2022 17:27:38 -0700 (MST) In-Reply-To: <871qx74or6.fsf@melete.silentflame.com> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:231616 gmane.emacs.devel:289440 Archived-At: --=-=-= Content-Type: text/plain 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 --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=v2-0001-Factor-out-scratch-initialization.patch >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 --=-=-=--