all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Get current buffer name from command line?
@ 2014-08-13 11:32 Marcin Borkowski
  2014-08-13 12:23 ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Marcin Borkowski @ 2014-08-13 11:32 UTC (permalink / raw)
  To: GNU Emacs users list

Hello,

this is not something I really need, but I was wondering whether I can
get the (name of) the current buffer from command line?  Here is an
interesting exchange I had with Eshell ("*GNU Emacs*" being the
*other* buffer):

Welcome to the Emacs shell

~ $ emacsclient -e "(current-buffer)"
#<buffer  *server*>
~ $ emacsclient -e "(other-buffer)"
#<buffer *GNU Emacs*>
~ $ emacsclient -e "(progn (bury-buffer) (current-buffer))"
#<buffer  *server*>
~ $ emacsclient -e "(progn (kill-buffer) (current-buffer))"
#<buffer *GNU Emacs*>
~ $ emacsclient -e "(progn (kill-buffer) (other-buffer))"
#<buffer *GNU Emacs*>
~ $ 

I looked into both the Emacs manual and the Elisp manual, but found
nothing to explain this behavior (I expected to see #<buffer *eshell*>
at least once...)

Best,

-- 
Marcin Borkowski
room B3-46, phone no +48 61 829 5375
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Adam Mickiewicz University
-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Adam Mickiewicz University



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

* Re: Get current buffer name from command line?
  2014-08-13 11:32 Get current buffer name from command line? Marcin Borkowski
@ 2014-08-13 12:23 ` Stefan Monnier
  2014-08-13 20:49   ` Marcin Borkowski
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2014-08-13 12:23 UTC (permalink / raw)
  To: help-gnu-emacs

> this is not something I really need, but I was wondering whether I can
> get the (name of) the current buffer from command line?

I think your notion of "current buffer" is not the same as
`current-buffer'.  You probably want "the buffer displayed in the
`selected-window'".

Of course, the selected-window may also be temporarily changed, but
I think

   emacsclient -e "(window-buffer (selected-window))"

will more often give you what you're looking for.


        Stefan




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

* Re: Get current buffer name from command line?
  2014-08-13 12:23 ` Stefan Monnier
@ 2014-08-13 20:49   ` Marcin Borkowski
  2014-08-13 21:40     ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Marcin Borkowski @ 2014-08-13 20:49 UTC (permalink / raw)
  To: help-gnu-emacs

Dnia 2014-08-13, o godz. 08:23:34
Stefan Monnier <monnier@iro.umontreal.ca> napisał(a):

> > this is not something I really need, but I was wondering whether I
> > can get the (name of) the current buffer from command line?
> 
> I think your notion of "current buffer" is not the same as
> `current-buffer'.  You probably want "the buffer displayed in the
> `selected-window'".
> 
> Of course, the selected-window may also be temporarily changed, but
> I think
> 
>    emacsclient -e "(window-buffer (selected-window))"
> 
> will more often give you what you're looking for.

Thanks!  A quick test showed that indeed, this is the case.  But I
have two questions now:

1. So what exactly is "current-buffer"?  Or, more precisely, why
(progn (kill-buffer) (current-buffer))
didn't work?

2. Why "more often"?  Is it possible that it won't?  To be more
precise on my requirements: I thought about writing a script (to be
put into cron) which would gather data on what I'm doing at various
times so that I can measure time spent on different activities.  Since
most of my computer time is spent in Emacs, this would be an important
part of it.  From the docstring of selected-window, it seems to be
exactly what I'm looking for.  Still, the words "more often" made me a
bit suspicious;).

>         Stefan

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Adam Mickiewicz University



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

* Re: Get current buffer name from command line?
  2014-08-13 20:49   ` Marcin Borkowski
@ 2014-08-13 21:40     ` Stefan Monnier
  2014-08-13 22:08       ` Marcin Borkowski
  0 siblings, 1 reply; 5+ messages in thread
From: Stefan Monnier @ 2014-08-13 21:40 UTC (permalink / raw)
  To: help-gnu-emacs

> 1. So what exactly is "current-buffer"?  Or, more precisely, why
> (progn (kill-buffer) (current-buffer))
> didn't work?

current-buffer is something that changes all the time for
internal reasons.  E.g. while processing the request coming from
emacsclient, the current-buffer will be temporarily changed to the
*server* buffer.  That has no influence on display.

It's simply that most operations that touch a buffer do it on the
current-buffer, so if you want to look at a buffer's content, or change
it, or consult some of its variables, the way you typically do it is by
temporarily changing current-buffer.

The same holds for manipulating windows, which is why selected-window
may not always return what you think (as a user) as "the selected
window" (e.g. it may get temporarily changed by process-filter handling
a subprocess's output).

> 2. Why "more often"?  Is it possible that it won't?  To be more
> precise on my requirements: I thought about writing a script (to be
> put into cron) which would gather data on what I'm doing at various
> times so that I can measure time spent on different activities.  Since
> most of my computer time is spent in Emacs, this would be an important
> part of it.  From the docstring of selected-window, it seems to be
> exactly what I'm looking for.  Still, the words "more often" made me a
> bit suspicious;).

I think "emacsclient -e '(window-buffer (selected-window))'" will work
fine for your use case, but if you want to take idle time into account,
you might like to use a post-command-hook which sets a global var to the
"current time after last command" at which point you could also set
a global var to "the selected-window after last command" which should be
more reliable.


        Stefan




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

* Re: Get current buffer name from command line?
  2014-08-13 21:40     ` Stefan Monnier
@ 2014-08-13 22:08       ` Marcin Borkowski
  0 siblings, 0 replies; 5+ messages in thread
From: Marcin Borkowski @ 2014-08-13 22:08 UTC (permalink / raw)
  To: help-gnu-emacs

Dnia 2014-08-13, o godz. 17:40:07
Stefan Monnier <monnier@iro.umontreal.ca> napisał(a):

> > 1. So what exactly is "current-buffer"?  Or, more precisely, why
> > (progn (kill-buffer) (current-buffer))
> > didn't work?
> 
> current-buffer is something that changes all the time for
> internal reasons.  E.g. while processing the request coming from
> emacsclient, the current-buffer will be temporarily changed to the
> *server* buffer.  That has no influence on display.
> 
> It's simply that most operations that touch a buffer do it on the
> current-buffer, so if you want to look at a buffer's content, or
> change it, or consult some of its variables, the way you typically do
> it is by temporarily changing current-buffer.
> 
> The same holds for manipulating windows, which is why selected-window
> may not always return what you think (as a user) as "the selected
> window" (e.g. it may get temporarily changed by process-filter
> handling a subprocess's output).

I see.

> > 2. Why "more often"?  Is it possible that it won't?  To be more
> > precise on my requirements: I thought about writing a script (to be
> > put into cron) which would gather data on what I'm doing at various
> > times so that I can measure time spent on different activities.
> > Since most of my computer time is spent in Emacs, this would be an
> > important part of it.  From the docstring of selected-window, it
> > seems to be exactly what I'm looking for.  Still, the words "more
> > often" made me a bit suspicious;).
> 
> I think "emacsclient -e '(window-buffer (selected-window))'" will work
> fine for your use case, but if you want to take idle time into
> account, you might like to use a post-command-hook which sets a
> global var to the "current time after last command" at which point
> you could also set a global var to "the selected-window after last
> command" which should be more reliable.

OK, now I get it.  Thanks for taking the time to explain this to me!

>         Stefan

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Adam Mickiewicz University



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

end of thread, other threads:[~2014-08-13 22:08 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-08-13 11:32 Get current buffer name from command line? Marcin Borkowski
2014-08-13 12:23 ` Stefan Monnier
2014-08-13 20:49   ` Marcin Borkowski
2014-08-13 21:40     ` Stefan Monnier
2014-08-13 22:08       ` Marcin Borkowski

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.