unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#57450: [PATCH] Option to choose the major model new scratch buffers
@ 2022-08-27 13:01 Augusto Stoffel
  2022-08-27 13:06 ` Eli Zaretskii
  2022-08-27 13:12 ` Lars Ingebrigtsen
  0 siblings, 2 replies; 5+ messages in thread
From: Augusto Stoffel @ 2022-08-27 13:01 UTC (permalink / raw)
  To: 57450

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

Tags: patch

I just noticed the new `scratch-buffer' command and I think it would be
nice to provide an option to create scratch buffers with different major
modes.  WDYT?

What is the best way to get a list of all available major modes?  I can
think of two methods, none of which is perfect.  One is commented out in
the patch; I like the uncommented one (using the auto-mode-alist) a bit
better.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Provide-option-to-choose-a-major-mode-in-scratch-buf.patch --]
[-- Type: text/x-patch, Size: 3664 bytes --]

From c710c8d65d62292e22282da90fa471c16b1d3cb6 Mon Sep 17 00:00:00 2001
From: Augusto Stoffel <arstoffel@gmail.com>
Date: Sat, 27 Aug 2022 09:50:17 +0200
Subject: [PATCH] Provide option to choose a major mode in 'scratch-buffer'

* lisp/simple.e (get-scratch-buffer-create, scratch-buffer): New
optional MODE argument.
---
 lisp/simple.el | 61 ++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 44 insertions(+), 17 deletions(-)

diff --git a/lisp/simple.el b/lisp/simple.el
index 460aff8bd8..bc9c71dee9 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -10650,24 +10650,51 @@ capitalize-dwim
 
 (fn CL-X)")
 
-(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 ()
+(defun get-scratch-buffer-create (&optional mode)
+  "Return a scratch buffer for MODE, creating a new one if needed.
+If MODE is nil, use `initial-major-mode' and insert
+`initial-scratch-message'."
+  (let ((name (if mode
+                  (format "*scratch-%s*"
+                          (string-trim-right (symbol-name mode) "-mode"))
+                "*scratch*")))
+    (or (get-buffer name)
+        (let ((scratch (get-buffer-create name)))
+          ;; Don't touch the buffer contents or mode unless we know that
+          ;; we just created it.
+          (with-current-buffer scratch
+            (when (and (not mode) initial-scratch-message)
+              (insert (substitute-command-keys initial-scratch-message))
+              (set-buffer-modified-p nil))
+            (funcall (or mode initial-major-mode)))
+          scratch))))
+
+(defun scratch-buffer (&optional mode)
   "Switch to the *scratch* buffer.
-If the buffer doesn't exist, create it first."
-  (interactive)
-  (pop-to-buffer-same-window (get-scratch-buffer-create)))
+If the buffer doesn't exist, create it first.
+With a prefix argument, select a major mode for the scratch buffer."
+  (interactive
+   (list (when current-prefix-arg
+           (intern
+            (completing-read "Scratch buffer mode: "
+                             (delete-dups
+                              (delq nil
+                                    (mapcar (pcase-lambda (`(_ . ,it))
+                                              (and (symbolp it)
+                                                   (commandp it)
+                                                   it))
+                                            auto-mode-alist))))))))
+            ;; (completing-read "Scratch buffer mode: "
+            ;;                  obarray
+            ;;                  (lambda (sym)
+            ;;                    (and (commandp sym)
+            ;;                         (if (autoloadp (symbol-function sym))
+            ;;                             (string-suffix-p "-mode"
+            ;;                                              (symbol-name sym))
+            ;;                           (memq 'derived-mode-parent
+            ;;                                 (symbol-plist sym)))))
+            ;;                  t)))))
+  (pop-to-buffer-same-window (get-scratch-buffer-create mode)))
 
 (defun kill-buffer--possibly-save (buffer)
   (let ((response
-- 
2.37.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* bug#57450: [PATCH] Option to choose the major model new scratch buffers
  2022-08-27 13:01 bug#57450: [PATCH] Option to choose the major model new scratch buffers Augusto Stoffel
@ 2022-08-27 13:06 ` Eli Zaretskii
       [not found]   ` <CAHixrvZjE7nbG37WxXP7wTN=pj4DEF5-hMb6z+ufMJWr5v8PXg@mail.gmail.com>
  2022-08-27 13:12 ` Lars Ingebrigtsen
  1 sibling, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2022-08-27 13:06 UTC (permalink / raw)
  To: Augusto Stoffel; +Cc: 57450

> From: Augusto Stoffel <arstoffel@gmail.com>
> Date: Sat, 27 Aug 2022 15:01:28 +0200
> 
> I just noticed the new `scratch-buffer' command and I think it would be
> nice to provide an option to create scratch buffers with different major
> modes.  WDYT?

What's wrong with setting initial-major-mode?





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#57450: [PATCH] Option to choose the major model new scratch buffers
  2022-08-27 13:01 bug#57450: [PATCH] Option to choose the major model new scratch buffers Augusto Stoffel
  2022-08-27 13:06 ` Eli Zaretskii
@ 2022-08-27 13:12 ` Lars Ingebrigtsen
  1 sibling, 0 replies; 5+ messages in thread
From: Lars Ingebrigtsen @ 2022-08-27 13:12 UTC (permalink / raw)
  To: Augusto Stoffel; +Cc: 57450

Augusto Stoffel <arstoffel@gmail.com> writes:

> I just noticed the new `scratch-buffer' command and I think it would be
> nice to provide an option to create scratch buffers with different major
> modes.  WDYT?

I don't think that sounds very useful.  The point of the scratch-buffer
command is to help people who wondered what to do if they've
(accidentally) killed the *scratch* buffer.  Most people didn't know
that you could just say `C-x b *scratch* RET' and it'd be created (with
the correct mode) just fine, so having something on `M-x scratch TAB'
for them to find is nice.

But complicating this by (optionally) prompting for a mode isn't
helpful.  They can just say `M-x foo-mode' afterwards, and it's the same
amount of key strokes, I think.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#57450: [PATCH] Option to choose the major model new scratch buffers
       [not found]   ` <CAHixrvZjE7nbG37WxXP7wTN=pj4DEF5-hMb6z+ufMJWr5v8PXg@mail.gmail.com>
@ 2022-08-27 13:36     ` Eli Zaretskii
  2022-08-29 15:25       ` Lars Ingebrigtsen
  0 siblings, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2022-08-27 13:36 UTC (permalink / raw)
  To: Augusto Stoffel; +Cc: 57450

> From: Augusto Stoffel <arstoffel@gmail.com>
> Date: Sat, 27 Aug 2022 15:16:15 +0200
> 
> What if you want to do some quick thing in a mode different from initial-major-mode?
> 
> I sometimes what a text-mode scratch, sometimes a lisp-interaction scratch, and more rarely some other
> prog-mode.

You can either set initial-major-mode, or invoke the different
major-mode explicitly.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#57450: [PATCH] Option to choose the major model new scratch buffers
  2022-08-27 13:36     ` Eli Zaretskii
@ 2022-08-29 15:25       ` Lars Ingebrigtsen
  0 siblings, 0 replies; 5+ messages in thread
From: Lars Ingebrigtsen @ 2022-08-29 15:25 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 57450, Augusto Stoffel

Eli Zaretskii <eliz@gnu.org> writes:

> You can either set initial-major-mode, or invoke the different
> major-mode explicitly.

So I think the conclusion here is that we don't want to add this, so I'm
closing this bug report.





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-08-29 15:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-27 13:01 bug#57450: [PATCH] Option to choose the major model new scratch buffers Augusto Stoffel
2022-08-27 13:06 ` Eli Zaretskii
     [not found]   ` <CAHixrvZjE7nbG37WxXP7wTN=pj4DEF5-hMb6z+ufMJWr5v8PXg@mail.gmail.com>
2022-08-27 13:36     ` Eli Zaretskii
2022-08-29 15:25       ` Lars Ingebrigtsen
2022-08-27 13:12 ` Lars Ingebrigtsen

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).