Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bug#13251: Wishlist: Add ability to set initial buffer for new frames.

3 views
Skip to first unread message

Constantin Kulikov

unread,
Dec 21, 2012, 4:16:58 PM12/21/12
to 13...@debbugs.gnu.org
Short:
I want to be able to set buffer that will be displayed in window of newly created frame.
I need it for my emacs package, that will save and restore some emacs state including
last window configurations, opened and displayed buffers and such.
I tried this: add hook to `after-make-frame-functions'. Inside this hook I do
`(switch-to-buffer <some-buffer>)' and frame is switched to that <some-buffer> at first,
but after a short time it's switched to *scratch*.

The one way I found how to overcome this behaviour is by setting the
`initial-buffer-choice' in the `after-make-frame-functions' hook. One drawback of this
approach is that it could only be set to name of file, t or nil and so there is no way to
force switching to buffer without underlying file. This could be fixed int server.el:1258
replacing

  (unless (or files commands)
        (if (stringp initial-buffer-choice)
        (find-file initial-buffer-choice)
          (switch-to-buffer (get-buffer-create "*scratch*")
                'norecord)))

to
          (unless (or files commands)
            (switch-to-buffer
             (get-buffer-create
              (or (cond
                   ((stringp initial-buffer-choice) (find-file-noselect initial-buffer-choice))
                   ((functionp initial-buffer-choice) (funcall initial-buffer-choice)))
                  "*scratch*"))
             'norecord))

and modified defcustom in in startup.el:41 would be:

(defcustom initial-buffer-choice nil
  "Buffer to show after starting Emacs.
If the value is nil and `inhibit-startup-screen' is nil, show the
startup screen.  If the value is a string, visit the specified file
or directory using `find-file'.  If t, open the `*scratch*'
buffer. If function, switch to a buffer returned by this function.

A string value also causes emacsclient to open the specified file
or directory when no target file is specified."
  :type '(choice
      (const     :tag "Startup screen" nil)
      (directory :tag "Directory" :value "~/")
      (file      :tag "File" :value "~/.emacs")
      (function  :tag "Function")
      (const     :tag "Lisp scratch buffer" t))
  :version "23.1"
  :group 'initialization)

This code allows setting `initial-buffer-choice' to a function returning needed buffer.

Constantin Kulikov

unread,
Dec 21, 2012, 4:30:03 PM12/21/12
to 13...@debbugs.gnu.org
ah sorry, don't need that `or' in server.el:

          (unless (or files commands)
            (switch-to-buffer
             (get-buffer-create
              (cond
               ((stringp initial-buffer-choice) (find-file-noselect initial-buffer-choice))
               ((functionp initial-buffer-choice) (funcall initial-buffer-choice))
               (t "*scratch*")))
             'norecord))



2012/12/22 Constantin Kulikov <zxno...@gmail.com>

martin rudalics

unread,
Dec 22, 2012, 10:42:21 AM12/22/12
to Constantin Kulikov, 13...@debbugs.gnu.org
> (defcustom initial-buffer-choice nil
> "Buffer to show after starting Emacs.
> If the value is nil and `inhibit-startup-screen' is nil, show the
> startup screen. If the value is a string, visit the specified file
> or directory using `find-file'. If t, open the `*scratch*'
> buffer. If function, switch to a buffer returned by this function.

As for the last sentence I'd prefer "If it is a function, switch to the
buffer returned by that function."

> A string value also causes emacsclient to open the specified file
> or directory when no target file is specified."
> :type '(choice
> (const :tag "Startup screen" nil)
> (directory :tag "Directory" :value "~/")
> (file :tag "File" :value "~/.emacs")
> (function :tag "Function")
> (const :tag "Lisp scratch buffer" t))
> :version "23.1"

Too late for "23.1" use "24.4" instead.

> :group 'initialization)
>
> This code allows setting `initial-buffer-choice' to a function returning
> needed buffer.

Could you please send us a patch based on these remarks (including the
change you sketched in your second mail)?

Thanks, martin



martin rudalics

unread,
Dec 23, 2012, 5:14:10 AM12/23/12
to Constantin Kulikov, 13...@debbugs.gnu.org
> Thanks for the tips.

Thanks for the patch. Unfortunately, we are not yet done :-(

> === modified file 'lisp/server.el'
> --- lisp/server.el 2012-11-09 06:28:27 +0000
> +++ lisp/server.el 2012-12-22 19:36:57 +0000
> @@ -1256,12 +1256,18 @@
> (mapc 'funcall (nreverse commands))
>
> ;; If we were told only to open a new client, obey
> - ;; `initial-buffer-choice' if it specifies a file.
> - (unless (or files commands)
> - (if (stringp initial-buffer-choice)
> - (find-file initial-buffer-choice)
> - (switch-to-buffer (get-buffer-create "*scratch*")
> - 'norecord)))
> + ;; `initial-buffer-choice' if it specifies a file
> + ;; or a function
> + (unless (or files commands)

Here we have to make sure that we do _not_ switch to *scratch* when
`initial-buffer-choice' is nil but show the initial start screen.

> + (switch-to-buffer
> + (get-buffer-create
> + (cond
> + ((stringp initial-buffer-choice)
> + (find-file-noselect initial-buffer-choice))
> + ((functionp initial-buffer-choice)
> + (funcall initial-buffer-choice))

Let's make sure that both `find-file-noselect' and the function called
via `initial-buffer-choice' really returned a live buffer. So please
add a call to `buffer-live-p' for these cases. Which also means that
Stefan's initial proposal for a let-bound `buf' is the better choice ;-)
(he's usually always right in these things).

> + (t "*scratch*")))
> + 'norecord))
>
> ;; Delete the client if necessary.
> (cond
>
> === modified file 'lisp/startup.el'
> --- lisp/startup.el 2012-12-01 02:08:30 +0000
> +++ lisp/startup.el 2012-12-22 20:09:27 +0000
> @@ -43,7 +43,7 @@
> If the value is nil and `inhibit-startup-screen' is nil, show the
> startup screen. If the value is a string, visit the specified file
> or directory using `find-file'.

I suppose this is no longer true. Let's say "switch to a buffer
visiting the file or directory specified by the string" instead.

> If t, open the `*scratch*'
> -buffer.

This should come after the function item. Note that we are not overly
precise in the doc-string - any non-nil value of `initial-buffer-choice'
will show *scratch*. But it's better to not tell that in the doc-string
so we have room for future changes - just like the one you proposed.

> +buffer. If function, switch to a buffer returned by this function.

"If the value is a function, switch to the buffer returned by that
function." seems more precise here.

> A string value also causes emacsclient to open the specified file
> or directory when no target file is specified."
> @@ -51,8 +51,9 @@
> (const :tag "Startup screen" nil)
> (directory :tag "Directory" :value "~/")
> (file :tag "File" :value "~/.emacs")
> + (function :tag "Function")
> (const :tag "Lisp scratch buffer" t))
> - :version "23.1"
> + :version "24.4"
> :group 'initialization)
>
> (defcustom inhibit-startup-screen nil

And now comes the last problem. The function `command-line-1' (in
startup.el) contains these lines:

(when (eq initial-buffer-choice t)
;; When initial-buffer-choice equals t make sure that *scratch*
;; exists.
(get-buffer-create "*scratch*"))

I'd remove them because we can handle them here:

(when initial-buffer-choice
(cond ((eq initial-buffer-choice t)
(switch-to-buffer (get-buffer-create "*scratch*")))
((stringp initial-buffer-choice)
(find-file initial-buffer-choice))))

If we allow `initial-buffer-choice' to specify a function, we have to
handle it here in the same way as in server.el.

martin



martin rudalics

unread,
Dec 24, 2012, 1:05:09 PM12/24/12
to Constantin Kulikov, 13...@debbugs.gnu.org
> another attempt...

Committed as a combination of your previous and present patch. Please
have a look.

Thanks, martin



Constantin Kulikov

unread,
Dec 24, 2012, 2:37:04 PM12/24/12
to martin rudalics, 13...@debbugs.gnu.org
seems it works as I need. Thanks.


2012/12/24 martin rudalics <ruda...@gmx.at>

martin rudalics

unread,
Jan 2, 2013, 3:03:18 AM1/2/13
to Constantin Kulikov, 13251...@debbugs.gnu.org
> seems it works as I need. Thanks.

Bug closed.

Thanks, martin




0 new messages