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

Overriding switch to *scratch* buffer after creating new frame with 'emacsclient -c'

81 views
Skip to first unread message

Константин Куликов

unread,
Dec 19, 2012, 8:14:50 AM12/19/12
to help-gnu-emacs
If you run command emacsclient -c the new frame will be created and then selected window(that have cursor and input focus) of this frame is switched to display *scratch* buffer.
I added hook to `after-make-frame-functions', that already perform switching to another buffer that I need, but it's then switched to *scratch*.
Can I somehow override this behaviour, by disabling switching to *scratch* buffer in new frame, that was created by 'emacsclient -c' ?

Константин Куликов

unread,
Dec 19, 2012, 7:43:12 PM12/19/12
to help-gnu-emacs
Heh, found, that it I can set `initial-buffer-choice' to some file-name and it will be opened instead of switching to *scratch*.
But can't specify buffer without underlying file though.


2012/12/19 Константин Куликов <zxno...@gmail.com>

martin rudalics

unread,
Dec 20, 2012, 8:52:31 AM12/20/12
to Константин Куликов, help-gn...@gnu.org
>> add an appropriate customization type for `initial-buffer-choice'
> I don't think that it's will be useful for users to customise, but who
> knows...

`initial-buffer-choice' _is_ an option specified via `defcustom' in
startup.el. We cannot implicitly ignore this fact in `command-line-1'.
But we could, for example, allow a function here and you could specify a
function to provide the buffer of your choice here.

> Here the scenario:
> I want new frames to switch to some buffer so I added hook to
> `after-make-frame-functions'.
> Inside this hook I do `(switch-to-buffer <some-buffer>)' the window on this
> frame is switched to that <some-buffer> at first, but after a short time
> it's
> switched to *scratch*. So as I think it would be better that
> `after-make-frame-functions' will be called after this 'default switching to
> *scratch* behaviour' is performed.

I'm afraid that this would mean a quite intrusive change to the sequence
of operations performed by the startup code. Are there any reasons why
you can't use `emacs-startup-hook' or `term-setup-hook' instead?

> And than will be no need for me to
> add code to server.el.
> // New version:
> (unless (or files commands)
> (let ((type (type-of initial-buffer-choice))
> (buf "*scratch*"))
> (cond
> ((eq 'string type) (setq buf (find-file-noselect
> initial-buffer-choice)))
> ((eq 'buffer type) (when (buffer-live-p
> initial-buffer-choice)
> (setq buf initial-buffer-choice))))
> (switch-to-buffer (get-buffer-create buf) 'norecord)))

Looks good to me.

martin

Thien-Thi Nguyen

unread,
Dec 20, 2012, 9:46:07 AM12/20/12
to martin rudalics, help-gn...@gnu.org
() martin rudalics <ruda...@gmx.at>
() Thu, 20 Dec 2012 14:52:31 +0100

> (unless (or files commands)
> (let ((type (type-of initial-buffer-choice))
> (buf "*scratch*"))
> (cond
> ((eq 'string type) (setq buf (find-file-noselect initial-buffer-choice)))
> ((eq 'buffer type) (when (buffer-live-p initial-buffer-choice)
> (setq buf initial-buffer-choice))))
> (switch-to-buffer (get-buffer-create buf) 'norecord)))

Looks good to me.

It would be better w/o local vars and ‘setq’, though.

--
Thien-Thi Nguyen ..................................... GPG key: 4C807502
. NB: ttn at glug dot org is not me .
. (and has not been since 2007 or so) .
. ACCEPT NO SUBSTITUTES .
........... please send technical questions to mailing lists ...........

martin rudalics

unread,
Dec 20, 2012, 4:59:53 AM12/20/12
to zxno...@gmail.com, help-gn...@gnu.org
> Heh, found, that it I can set `initial-buffer-choice' to some
> file-name and it will be opened instead of switching to *scratch*.
> But can't specify buffer without underlying file though.

I wonder which buffer you want to display here if it doesn't correspond
to a file.

martin

Stefan Monnier

unread,
Dec 20, 2012, 12:06:54 PM12/20/12
to help-gn...@gnu.org
> (unless (or files commands)
> (let ((type (type-of initial-buffer-choice))
> (buf "*scratch*"))
> (cond
> ((eq 'string type) (setq buf (find-file-noselect initial-buffer-choice)))
> ((eq 'buffer type) (when (buffer-live-p initial-buffer-choice)
> (setq buf initial-buffer-choice))))
> (switch-to-buffer (get-buffer-create buf) 'norecord)))

> It would be better w/o local vars and ‘setq’, though.

You mean like

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


-- Stefan


Thien-Thi Nguyen

unread,
Dec 21, 2012, 8:10:31 AM12/21/12
to Stefan Monnier, help-gn...@gnu.org
() Stefan Monnier <mon...@iro.umontreal.ca>
() Thu, 20 Dec 2012 12:06:54 -0500

You mean like

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

Almost. To rid ourselves of the last local var:

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

or (hewing closer to OP approach):

(unless (or files commands)
(switch-to-buffer
(get-buffer-create
(or (case (type-of initial-buffer-choice)
(string (find-file-noselect initial-buffer-choice))
(buffer (when (buffer-live-p initial-buffer-choice)
initial-buffer-choice)))
"*scratch*"))
'norecord))

Same difference; more stack, less state... Since we're talking style,
i'd say the most important thing is sane indentation, but luckily we
have Emacs for that. :-D

Constantin Kulikov

unread,
Dec 21, 2012, 10:43:18 AM12/21/12
to help-gnu-emacs
:-) nice.
And I think that martin was right, it's more useful to allow setting initial-buffer-choice to a function returning a buffer.

Stefan Monnier

unread,
Dec 22, 2012, 12:46:34 PM12/22/12
to help-gn...@gnu.org
> (or (case (type-of initial-buffer-choice)

I really dislike type-of (e.g. what's the type of nil? Is `bold'
a symbol or a face?).

> Same difference; more stack, less state... Since we're talking style,

Actually (let ((foo ...)) ...foo...) does not use more "state".
State only creeps in when we use things like setq.

As for whether the use of a let-binding for a single-use variable is
warranted: it mostly depends on aesthetics. In this specific case, I'd
favor the let-form since it gives a name to the intermediate value
without costing you an extra line.


Stefan


0 new messages