all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to determine (from Elisp) whether the Emacs frame has focus?
@ 2015-11-13 21:45 Marcin Borkowski
  2015-11-14  3:48 ` Emanuel Berg
                   ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Marcin Borkowski @ 2015-11-13 21:45 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi all,

I'd like to know whether the selected Emacs frame has focus.  Currently,
I'm using an xdotool/xprop combo (I'm on GNU/Linux), but I'd like to
know whether there is a better way.  I know about focus-in-hook and
focus-out-hook, and I could make them set/unset some global variable,
but this looks hackish.

Any ideas?

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-13 21:45 How to determine (from Elisp) whether the Emacs frame has focus? Marcin Borkowski
@ 2015-11-14  3:48 ` Emanuel Berg
  2015-11-14  4:05 ` John Mastro
  2015-11-14  8:06 ` Eli Zaretskii
  2 siblings, 0 replies; 26+ messages in thread
From: Emanuel Berg @ 2015-11-14  3:48 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:

> I'd like to know whether the selected Emacs frame
> has focus. Currently, I'm using an xdotool/xprop
> combo (I'm on GNU/Linux), but I'd like to know
> whether there is a better way. I know about
> focus-in-hook and focus-out-hook, and I could make
> them set/unset some global variable, but this
> looks hackish.

Try this:

(defun emacs-window-has-focus ()
  (interactive) ; interactive to test, with the line below...
  (sleep-for 3) ; ...so you have time to change the window
  (let ((focused-window-pid
         (string-to-number
          (shell-command-to-string
           "wmctrl -l -p | grep `xprop -root _NET_ACTIVE_WINDOW | cut -d x -f 2` | cut -d ' ' -f 4")
          )))
    (message (if (= (emacs-pid) focused-window-pid) "has" "not so")) ))

-- 
underground experts united
http://user.it.uu.se/~embe8573




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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-13 21:45 How to determine (from Elisp) whether the Emacs frame has focus? Marcin Borkowski
  2015-11-14  3:48 ` Emanuel Berg
@ 2015-11-14  4:05 ` John Mastro
  2015-11-14  7:30   ` tomas
  2015-11-14  8:06 ` Eli Zaretskii
  2 siblings, 1 reply; 26+ messages in thread
From: John Mastro @ 2015-11-14  4:05 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Marcin Borkowski <mbork@mbork.pl> wrote:
> I'd like to know whether the selected Emacs frame has focus.  Currently,
> I'm using an xdotool/xprop combo (I'm on GNU/Linux), but I'd like to
> know whether there is a better way.  I know about focus-in-hook and
> focus-out-hook, and I could make them set/unset some global variable,
> but this looks hackish.

Not that I can tell.

I'm not sure if it would end up less hackish, but an alternative to
global variables might be to use frame parameters. See the info nodes
"(Elisp) Frame Parameters" and "(Elisp) Parameter Access".

-- 
john



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14  4:05 ` John Mastro
@ 2015-11-14  7:30   ` tomas
  2015-11-14  8:44     ` Marcin Borkowski
  0 siblings, 1 reply; 26+ messages in thread
From: tomas @ 2015-11-14  7:30 UTC (permalink / raw)
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, Nov 13, 2015 at 08:05:11PM -0800, John Mastro wrote:
> Marcin Borkowski <mbork@mbork.pl> wrote:
> > I'd like to know whether the selected Emacs frame has focus.  Currently,
> > I'm using an xdotool/xprop combo (I'm on GNU/Linux), but I'd like to
> > know whether there is a better way.  I know about focus-in-hook and
> > focus-out-hook, and I could make them set/unset some global variable,
> > but this looks hackish.
> 
> Not that I can tell.
> 
> I'm not sure if it would end up less hackish, but an alternative to
> global variables might be to use frame parameters. See the info nodes
> "(Elisp) Frame Parameters" and "(Elisp) Parameter Access".

To be more specific, the function selected-frame (Info menu Frames / submenu
Input Focus) gives you the selected frame.

Thanks, Jon for prompting me to read manuals and learn something :)

- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlZG4yEACgkQBcgs9XrR2ka2qQCaAykYO4/O/hbbw9HdEu0vE+r1
xJEAniwH8eRjpaeuKF6clq0OLAl9oIRB
=nfvW
-----END PGP SIGNATURE-----



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-13 21:45 How to determine (from Elisp) whether the Emacs frame has focus? Marcin Borkowski
  2015-11-14  3:48 ` Emanuel Berg
  2015-11-14  4:05 ` John Mastro
@ 2015-11-14  8:06 ` Eli Zaretskii
  2015-11-14  8:36   ` Random832
                     ` (2 more replies)
  2 siblings, 3 replies; 26+ messages in thread
From: Eli Zaretskii @ 2015-11-14  8:06 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Marcin Borkowski <mbork@mbork.pl>
> Date: Fri, 13 Nov 2015 22:45:10 +0100
> 
> I'd like to know whether the selected Emacs frame has focus.

I'm probably missing something very important, because I don't
understand what you are looking for.  The selected frame by definition
has focus, at least AFAIK.  What am I missing?



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14  8:06 ` Eli Zaretskii
@ 2015-11-14  8:36   ` Random832
  2015-11-14  9:36     ` Eli Zaretskii
  2015-11-14  8:42   ` Marcin Borkowski
  2015-11-14  8:44   ` Michael Heerdegen
  2 siblings, 1 reply; 26+ messages in thread
From: Random832 @ 2015-11-14  8:36 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii <eliz@gnu.org> writes:
> I'm probably missing something very important, because I don't
> understand what you are looking for.  The selected frame by definition
> has focus, at least AFAIK.  What am I missing?

Well that may be true who never leave their Emacs instance, but for the
rest of us some non-Emacs application might have focus instead.

(Also I suspect by "selected" he meant a frame the programmer "selects"
to pass in to e.g. a function that answers the question, rather than
selected-frame.)




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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14  8:06 ` Eli Zaretskii
  2015-11-14  8:36   ` Random832
@ 2015-11-14  8:42   ` Marcin Borkowski
  2015-11-14  8:59     ` Eli Zaretskii
  2015-11-14  8:44   ` Michael Heerdegen
  2 siblings, 1 reply; 26+ messages in thread
From: Marcin Borkowski @ 2015-11-14  8:42 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs


On 2015-11-14, at 09:06, Eli Zaretskii <eliz@gnu.org> wrote:

>> From: Marcin Borkowski <mbork@mbork.pl>
>> Date: Fri, 13 Nov 2015 22:45:10 +0100
>> 
>> I'd like to know whether the selected Emacs frame has focus.
>
> I'm probably missing something very important, because I don't
> understand what you are looking for.  The selected frame by definition
> has focus, at least AFAIK.  What am I missing?

"Focus" in the window manager sense, not in Emacs sense.  I.e., I want
to determine whether it is Emacs which receives keystrokes, or some
other X application.

Hope that clarifies the question.

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14  8:06 ` Eli Zaretskii
  2015-11-14  8:36   ` Random832
  2015-11-14  8:42   ` Marcin Borkowski
@ 2015-11-14  8:44   ` Michael Heerdegen
  2015-11-14 10:15     ` Marcin Borkowski
  2 siblings, 1 reply; 26+ messages in thread
From: Michael Heerdegen @ 2015-11-14  8:44 UTC (permalink / raw)
  To: help-gnu-emacs

Eli Zaretskii <eliz@gnu.org> writes:

> I'm probably missing something very important, because I don't
> understand what you are looking for.  The selected frame by definition
> has focus, at least AFAIK.  What am I missing?

I think he means whether that frame has input focus.

When you select e.g. an xterm in X, you have still the same
`selected-frame' in Emacs, though no Emacs frame has input focus.


Michael.




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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14  7:30   ` tomas
@ 2015-11-14  8:44     ` Marcin Borkowski
  2015-11-14 10:45       ` tomas
  0 siblings, 1 reply; 26+ messages in thread
From: Marcin Borkowski @ 2015-11-14  8:44 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs


On 2015-11-14, at 08:30, tomas@tuxteam.de wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Fri, Nov 13, 2015 at 08:05:11PM -0800, John Mastro wrote:
>> Marcin Borkowski <mbork@mbork.pl> wrote:
>> > I'd like to know whether the selected Emacs frame has focus.  Currently,
>> > I'm using an xdotool/xprop combo (I'm on GNU/Linux), but I'd like to
>> > know whether there is a better way.  I know about focus-in-hook and
>> > focus-out-hook, and I could make them set/unset some global variable,
>> > but this looks hackish.
>> 
>> Not that I can tell.
>> 
>> I'm not sure if it would end up less hackish, but an alternative to
>> global variables might be to use frame parameters. See the info nodes
>> "(Elisp) Frame Parameters" and "(Elisp) Parameter Access".
>
> To be more specific, the function selected-frame (Info menu Frames / submenu
> Input Focus) gives you the selected frame.

That I know, I did my homework.  Still, evaluating

(progn (sit-for 3) (selected-frame))

and immediately going to, say, Evince, does not make Emacs say "nil", as
I (somehow) expected.

> Thanks, Jon for prompting me to read manuals and learn something :)

That's usually good;-).

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14  8:42   ` Marcin Borkowski
@ 2015-11-14  8:59     ` Eli Zaretskii
  2015-11-14 10:54       ` Yuri Khan
  0 siblings, 1 reply; 26+ messages in thread
From: Eli Zaretskii @ 2015-11-14  8:59 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Marcin Borkowski <mbork@mbork.pl>
> Cc: help-gnu-emacs@gnu.org
> Date: Sat, 14 Nov 2015 09:42:09 +0100
> 
> >> I'd like to know whether the selected Emacs frame has focus.
> >
> > I'm probably missing something very important, because I don't
> > understand what you are looking for.  The selected frame by definition
> > has focus, at least AFAIK.  What am I missing?
> 
> "Focus" in the window manager sense, not in Emacs sense.  I.e., I want
> to determine whether it is Emacs which receives keystrokes, or some
> other X application.
> 
> Hope that clarifies the question.

Not really, sorry.  The function selected-frame returns the frame that
receives keystrokes, at least as far as I understand what you are
saying.  The ELisp manual explicitly says:

   -- Function: selected-frame
       This function returns the selected frame.

     Some window systems and window managers direct keyboard input to the
  window object that the mouse is in; others require explicit clicks or
  commands to "shift the focus" to various window objects.  Either way,
  Emacs automatically keeps track of which frame has the focus.

Perhaps you are talking about some very specialized situation or need,
in which case please describe in more details why the call to
selected-frame and comparison with the frame you want to test is not
what you need.



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14  8:36   ` Random832
@ 2015-11-14  9:36     ` Eli Zaretskii
  2015-11-14 12:34       ` Marcin Borkowski
  0 siblings, 1 reply; 26+ messages in thread
From: Eli Zaretskii @ 2015-11-14  9:36 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Random832 <random832@fastmail.com>
> Date: Sat, 14 Nov 2015 03:36:03 -0500
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> > I'm probably missing something very important, because I don't
> > understand what you are looking for.  The selected frame by definition
> > has focus, at least AFAIK.  What am I missing?
> 
> Well that may be true who never leave their Emacs instance, but for the
> rest of us some non-Emacs application might have focus instead.

Ah, so he meant detect when some other application has focus?  That's
what I was missing, thanks.

Then I don't understand why the solution of focus-in/out-hook was
rejected as "hackish".  These hooks are there precisely for situations
like these, AFAIK.  IMO, there's nothing hackish about that.

> (Also I suspect by "selected" he meant a frame the programmer "selects"
> to pass in to e.g. a function that answers the question, rather than
> selected-frame.)

I indeed feel there might still be aspects of the original question
that need to be further clarified.



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14  8:44   ` Michael Heerdegen
@ 2015-11-14 10:15     ` Marcin Borkowski
  0 siblings, 0 replies; 26+ messages in thread
From: Marcin Borkowski @ 2015-11-14 10:15 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs


On 2015-11-14, at 09:44, Michael Heerdegen <michael_heerdegen@web.de> wrote:

> Eli Zaretskii <eliz@gnu.org> writes:
>
>> I'm probably missing something very important, because I don't
>> understand what you are looking for.  The selected frame by definition
>> has focus, at least AFAIK.  What am I missing?
>
> I think he means whether that frame has input focus.
>
> When you select e.g. an xterm in X, you have still the same
> `selected-frame' in Emacs, though no Emacs frame has input focus.

Exactly, as I wrote in another post.  I want to be able to determine
what is the "active application".

> Michael.

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14  8:44     ` Marcin Borkowski
@ 2015-11-14 10:45       ` tomas
  2015-11-14 20:28         ` Marcin Borkowski
  0 siblings, 1 reply; 26+ messages in thread
From: tomas @ 2015-11-14 10:45 UTC (permalink / raw)
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sat, Nov 14, 2015 at 09:44:25AM +0100, Marcin Borkowski wrote:

[...]

> That I know, I did my homework.  Still, evaluating

Sorry, I didn't want to imply otherwise -- I just misunderstood your
problem.

> (progn (sit-for 3) (selected-frame))
> 
> and immediately going to, say, Evince, does not make Emacs say "nil", as
> I (somehow) expected.

Got it. Yes, it seems you'll have to hook on focus events, then.

> > Thanks, Jon for prompting me to read manuals and learn something :)
> 
> That's usually good;-).

:-)

regards
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlZHEN8ACgkQBcgs9XrR2kZUXQCfX5cZuGFY655hPP3dYc4togk/
TEUAn12VE7uLjYIWYvqa2G+bqv5EP3c9
=3fVc
-----END PGP SIGNATURE-----



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14  8:59     ` Eli Zaretskii
@ 2015-11-14 10:54       ` Yuri Khan
  2015-11-14 11:04         ` Eli Zaretskii
  0 siblings, 1 reply; 26+ messages in thread
From: Yuri Khan @ 2015-11-14 10:54 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs@gnu.org

On Sat, Nov 14, 2015 at 2:59 PM, Eli Zaretskii <eliz@gnu.org> wrote:
>>
>> "Focus" in the window manager sense, not in Emacs sense.  I.e., I want
>> to determine whether it is Emacs which receives keystrokes, or some
>> other X application.
>>
>> Hope that clarifies the question.
>
> Not really, sorry.  The function selected-frame returns the frame that
> receives keystrokes, at least as far as I understand what you are
> saying.  The ELisp manual explicitly says:
>
>    -- Function: selected-frame
>        This function returns the selected frame.

OK, simple demonstration.

I have a couple Emacs frames on my desktop, along with a Firefox and
an Xterm, all under the i3 window manager (but the concepts should
apply to any wm).

In one of the Emacs frames, I bring up *scratch* and type:

(progn (sit-for 3) (message "%s" (selected-frame)))

I press C-x C-e on that and wait for three seconds. It says: "#<frame
*scratch* – Emacs 0x1140678>".

Now I press C-x C-e again, and before three seconds elapse, I switch
the wm focus to Firefox. However, after three seconds, the minibuffer
says again: "#<frame *scratch* – Emacs #1140678>", although this frame
doesn’t have focus. Neither does any, in fact.


Now let’s imagine a situation where there is a single Emacs server,
and a couple of emacsclients connected to it, both displaying their
respective frames *on different X servers*. I believe in that setup
both frames could simultaneously have focus in their respective wms.
Which of them is “the” selected frame, from Emacs’ point of view?



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14 10:54       ` Yuri Khan
@ 2015-11-14 11:04         ` Eli Zaretskii
  0 siblings, 0 replies; 26+ messages in thread
From: Eli Zaretskii @ 2015-11-14 11:04 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Yuri Khan <yuri.v.khan@gmail.com>
> Date: Sat, 14 Nov 2015 16:54:35 +0600
> Cc: "help-gnu-emacs@gnu.org" <help-gnu-emacs@gnu.org>
> 
> Now let’s imagine a situation where there is a single Emacs server,
> and a couple of emacsclients connected to it, both displaying their
> respective frames *on different X servers*. I believe in that setup
> both frames could simultaneously have focus in their respective wms.
> Which of them is “the” selected frame, from Emacs’ point of view?

The last one that received input.




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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14  9:36     ` Eli Zaretskii
@ 2015-11-14 12:34       ` Marcin Borkowski
  2015-11-14 13:40         ` Eli Zaretskii
  2015-11-14 21:10         ` Stefan Monnier
  0 siblings, 2 replies; 26+ messages in thread
From: Marcin Borkowski @ 2015-11-14 12:34 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs


On 2015-11-14, at 10:36, Eli Zaretskii <eliz@gnu.org> wrote:

>> From: Random832 <random832@fastmail.com>
>> Date: Sat, 14 Nov 2015 03:36:03 -0500
>> 
>> Eli Zaretskii <eliz@gnu.org> writes:
>> > I'm probably missing something very important, because I don't
>> > understand what you are looking for.  The selected frame by definition
>> > has focus, at least AFAIK.  What am I missing?
>> 
>> Well that may be true who never leave their Emacs instance, but for the
>> rest of us some non-Emacs application might have focus instead.
>
> Ah, so he meant detect when some other application has focus?  That's
> what I was missing, thanks.
>
> Then I don't understand why the solution of focus-in/out-hook was
> rejected as "hackish".  These hooks are there precisely for situations
> like these, AFAIK.  IMO, there's nothing hackish about that.

AFAIU, these hooks are best suited to situations like "run LaTeX on
switching to a pdf viewer".  That does not require global variables; my
use case does.

Would it be possible to introduce a function like (emacs-has-focus-p)
for that?  Would it make sense?

>> (Also I suspect by "selected" he meant a frame the programmer "selects"
>> to pass in to e.g. a function that answers the question, rather than
>> selected-frame.)
>
> I indeed feel there might still be aspects of the original question
> that need to be further clarified.

For starters, I only want to know whether it is Emacs which has focus or
not.  Then, I would like to know e.g. the mode of the current buffer; in
that case, I'm not sure what I should do when the current buffer is the
minibuffer or something.  But this is less important for me now.

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14 12:34       ` Marcin Borkowski
@ 2015-11-14 13:40         ` Eli Zaretskii
  2015-11-15  7:45           ` Marcin Borkowski
  2015-11-14 21:10         ` Stefan Monnier
  1 sibling, 1 reply; 26+ messages in thread
From: Eli Zaretskii @ 2015-11-14 13:40 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Marcin Borkowski <mbork@mbork.pl>
> Cc: help-gnu-emacs@gnu.org
> Date: Sat, 14 Nov 2015 13:34:06 +0100
> 
> > Then I don't understand why the solution of focus-in/out-hook was
> > rejected as "hackish".  These hooks are there precisely for situations
> > like these, AFAIK.  IMO, there's nothing hackish about that.
> 
> AFAIU, these hooks are best suited to situations like "run LaTeX on
> switching to a pdf viewer".  That does not require global variables; my
> use case does.

Sorry, I still don't understand: why cannot the hook modify values of
global variables, and thus serve your needs in this context?

> Would it be possible to introduce a function like (emacs-has-focus-p)
> for that?  Would it make sense?

What would that function do except test the value of some global
variable, set by the focus-in and focus-out hooks?

> For starters, I only want to know whether it is Emacs which has focus or
> not.  Then, I would like to know e.g. the mode of the current buffer; in
> that case, I'm not sure what I should do when the current buffer is the
> minibuffer or something.  But this is less important for me now.

There's only one current buffer in the entire Emacs session, and it
doesn't change when Emacs loses focus.  Its value is returned by the
function current-buffer, as I'm sure you know.

Or did you mean the buffer displayed in the selected window of the
selected frame?



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14 10:45       ` tomas
@ 2015-11-14 20:28         ` Marcin Borkowski
  0 siblings, 0 replies; 26+ messages in thread
From: Marcin Borkowski @ 2015-11-14 20:28 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs


On 2015-11-14, at 11:45, tomas@tuxteam.de wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Sat, Nov 14, 2015 at 09:44:25AM +0100, Marcin Borkowski wrote:
>
> [...]
>
>> That I know, I did my homework.  Still, evaluating
>
> Sorry, I didn't want to imply otherwise -- I just misunderstood your
> problem.

No need to be sorry, it's fine.  I do not /always/ do my homework;-).

>> (progn (sit-for 3) (selected-frame))
>> 
>> and immediately going to, say, Evince, does not make Emacs say "nil", as
>> I (somehow) expected.
>
> Got it. Yes, it seems you'll have to hook on focus events, then.

Not necessarily - I can also ask xdotool for that info.

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14 12:34       ` Marcin Borkowski
  2015-11-14 13:40         ` Eli Zaretskii
@ 2015-11-14 21:10         ` Stefan Monnier
  2015-11-16 17:00           ` Marcin Borkowski
  1 sibling, 1 reply; 26+ messages in thread
From: Stefan Monnier @ 2015-11-14 21:10 UTC (permalink / raw)
  To: help-gnu-emacs

> AFAIU, these hooks are best suited to situations like "run LaTeX on
> switching to a pdf viewer".

What makes you think so?

> Would it be possible to introduce a function like (emacs-has-focus-p)
> for that?  Would it make sense?

Of course.  But its implementation would most likely be based on
focus-*-hook anyway.


        Stefan




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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14 13:40         ` Eli Zaretskii
@ 2015-11-15  7:45           ` Marcin Borkowski
  2015-11-15 19:45             ` Eli Zaretskii
  2015-11-16 14:08             ` Nicolas Richard
  0 siblings, 2 replies; 26+ messages in thread
From: Marcin Borkowski @ 2015-11-15  7:45 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: help-gnu-emacs


On 2015-11-14, at 14:40, Eli Zaretskii <eliz@gnu.org> wrote:

>> From: Marcin Borkowski <mbork@mbork.pl>
>> Cc: help-gnu-emacs@gnu.org
>> Date: Sat, 14 Nov 2015 13:34:06 +0100
>> 
>> > Then I don't understand why the solution of focus-in/out-hook was
>> > rejected as "hackish".  These hooks are there precisely for situations
>> > like these, AFAIK.  IMO, there's nothing hackish about that.
>> 
>> AFAIU, these hooks are best suited to situations like "run LaTeX on
>> switching to a pdf viewer".  That does not require global variables; my
>> use case does.
>
> Sorry, I still don't understand: why cannot the hook modify values of
> global variables, and thus serve your needs in this context?

Well, you're probably right, I'm just overthinking it.

>> Would it be possible to introduce a function like (emacs-has-focus-p)
>> for that?  Would it make sense?
>
> What would that function do except test the value of some global
> variable, set by the focus-in and focus-out hooks?

Well, it could do it without using those hooks, if it were in C.  But
you're probably right (again), there's no need to add that to the core.

>> For starters, I only want to know whether it is Emacs which has focus or
>> not.  Then, I would like to know e.g. the mode of the current buffer; in
>> that case, I'm not sure what I should do when the current buffer is the
>> minibuffer or something.  But this is less important for me now.
>
> There's only one current buffer in the entire Emacs session, and it
> doesn't change when Emacs loses focus.  Its value is returned by the
> function current-buffer, as I'm sure you know.

Of course.  However, what I really mean is not exactly (current-buffer),
but "the buffer I'm working in now".  This means that if
e.g. current-buffer is the minibuffer (as might be the case during
search or M-x or whatever), I would prefer /the buffer I'll get back to
when I finish doing whatever I'm doing in the minibuffer/, or /the
buffer I was in when I switched to the minibuffer/ (these two need not
coincide, of course, e.g. in case of C-x b - in such a case I'm fine
with whichever one).  This might be the other-buffer, I'm not sure -
I'll have to study the docs a bit more.

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-15  7:45           ` Marcin Borkowski
@ 2015-11-15 19:45             ` Eli Zaretskii
  2015-11-16 14:08             ` Nicolas Richard
  1 sibling, 0 replies; 26+ messages in thread
From: Eli Zaretskii @ 2015-11-15 19:45 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Marcin Borkowski <mbork@mbork.pl>
> Cc: help-gnu-emacs@gnu.org
> Date: Sun, 15 Nov 2015 08:45:03 +0100
> 
> >> Would it be possible to introduce a function like (emacs-has-focus-p)
> >> for that?  Would it make sense?
> >
> > What would that function do except test the value of some global
> > variable, set by the focus-in and focus-out hooks?
> 
> Well, it could do it without using those hooks, if it were in C.  But
> you're probably right (again), there's no need to add that to the core.

In any case, I see no way for Emacs to provide this information,
except by tracking these focus events.  So doing it in an application
will not lose anything, I think.

> >> For starters, I only want to know whether it is Emacs which has focus or
> >> not.  Then, I would like to know e.g. the mode of the current buffer; in
> >> that case, I'm not sure what I should do when the current buffer is the
> >> minibuffer or something.  But this is less important for me now.
> >
> > There's only one current buffer in the entire Emacs session, and it
> > doesn't change when Emacs loses focus.  Its value is returned by the
> > function current-buffer, as I'm sure you know.
> 
> Of course.  However, what I really mean is not exactly (current-buffer),
> but "the buffer I'm working in now".  This means that if
> e.g. current-buffer is the minibuffer (as might be the case during
> search or M-x or whatever), I would prefer /the buffer I'll get back to
> when I finish doing whatever I'm doing in the minibuffer/, or /the
> buffer I was in when I switched to the minibuffer/ (these two need not
> coincide, of course, e.g. in case of C-x b - in such a case I'm fine
> with whichever one).  This might be the other-buffer, I'm not sure -
> I'll have to study the docs a bit more.

At least wrt to being in the minibuffer, Emacs already does what you
want, otherwise "M-: (current-buffer) RET" would not do what you
expect.



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
       [not found] <mailman.2293.1447451126.7904.help-gnu-emacs@gnu.org>
@ 2015-11-16  6:53 ` Dan Espen
  2015-11-17  0:10   ` Emanuel Berg
  0 siblings, 1 reply; 26+ messages in thread
From: Dan Espen @ 2015-11-16  6:53 UTC (permalink / raw)
  To: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:

> Hi all,
>
> I'd like to know whether the selected Emacs frame has focus.  Currently,
> I'm using an xdotool/xprop combo (I'm on GNU/Linux), but I'd like to
> know whether there is a better way.  I know about focus-in-hook and
> focus-out-hook, and I could make them set/unset some global variable,
> but this looks hackish.
>
> Any ideas?

Since there doesn't seem to be an existing Emacs global variable
creating your own using those hooks isn't all that hackish.
Go for it.

-- 
Dan Espen


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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-15  7:45           ` Marcin Borkowski
  2015-11-15 19:45             ` Eli Zaretskii
@ 2015-11-16 14:08             ` Nicolas Richard
  2015-11-16 16:58               ` Marcin Borkowski
  1 sibling, 1 reply; 26+ messages in thread
From: Nicolas Richard @ 2015-11-16 14:08 UTC (permalink / raw)
  To: Marcin Borkowski; +Cc: help-gnu-emacs

Marcin Borkowski <mbork@mbork.pl> writes:
> Of course. However, what I really mean is not exactly
> (current-buffer), but "the buffer I'm working in now". This means that
> if e.g. current-buffer is the minibuffer (as might be the case during
> search or M-x or whatever), I would prefer /the buffer I'll get back
> to when I finish doing whatever I'm doing in the minibuffer/,

I think you want (window-buffer (minibuffer-selected-window))

-- 
Nico.



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-16 14:08             ` Nicolas Richard
@ 2015-11-16 16:58               ` Marcin Borkowski
  0 siblings, 0 replies; 26+ messages in thread
From: Marcin Borkowski @ 2015-11-16 16:58 UTC (permalink / raw)
  To: Nicolas Richard; +Cc: help-gnu-emacs


On 2015-11-16, at 15:08, Nicolas Richard <youngfrog@members.fsf.org> wrote:

> Marcin Borkowski <mbork@mbork.pl> writes:
>> Of course. However, what I really mean is not exactly
>> (current-buffer), but "the buffer I'm working in now". This means that
>> if e.g. current-buffer is the minibuffer (as might be the case during
>> search or M-x or whatever), I would prefer /the buffer I'll get back
>> to when I finish doing whatever I'm doing in the minibuffer/,
>
> I think you want (window-buffer (minibuffer-selected-window))

Great, thanks!  And it even seems that I can use it whether I'm in
a minibuffer or not - since if I'm not, (minibuffer-selected-window)
will return nil, and (window-buffer nil) will return the buffer of the
selected window, which (assuming I'm editing and not manipulating
anything programmatically, which will be the case, since I want to
install this in a timer) should just return the current buffer.

Thans again,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-14 21:10         ` Stefan Monnier
@ 2015-11-16 17:00           ` Marcin Borkowski
  0 siblings, 0 replies; 26+ messages in thread
From: Marcin Borkowski @ 2015-11-16 17:00 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs


On 2015-11-14, at 22:10, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

>> AFAIU, these hooks are best suited to situations like "run LaTeX on
>> switching to a pdf viewer".
>
> What makes you think so?

Well, probably my tunnel vision;-).

>> Would it be possible to introduce a function like (emacs-has-focus-p)
>> for that?  Would it make sense?
>
> Of course.  But its implementation would most likely be based on
> focus-*-hook anyway.

Assuming they would be written in Elisp and not in C.  But (as I've
written before) it probably doesn't make sense to add them to the core.

>         Stefan

Best,

-- 
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Faculty of Mathematics and Computer Science
Adam Mickiewicz University



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

* Re: How to determine (from Elisp) whether the Emacs frame has focus?
  2015-11-16  6:53 ` Dan Espen
@ 2015-11-17  0:10   ` Emanuel Berg
  0 siblings, 0 replies; 26+ messages in thread
From: Emanuel Berg @ 2015-11-17  0:10 UTC (permalink / raw)
  To: help-gnu-emacs

Dan Espen <despen@verizon.net> writes:

> Since there doesn't seem to be an existing Emacs
> global variable creating your own using those hooks
> isn't all that hackish. Go for it.

Most definitely.

Also remember, sometimes when there is a "standard"
way to do a thing, that may be an even more hackish
implementation under the surface.

Perhaps the OPs supposedly hackish solution will once
replace the "standard" one because it actually is
less hackish?

-- 
underground experts united
http://user.it.uu.se/~embe8573




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

end of thread, other threads:[~2015-11-17  0:10 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-11-13 21:45 How to determine (from Elisp) whether the Emacs frame has focus? Marcin Borkowski
2015-11-14  3:48 ` Emanuel Berg
2015-11-14  4:05 ` John Mastro
2015-11-14  7:30   ` tomas
2015-11-14  8:44     ` Marcin Borkowski
2015-11-14 10:45       ` tomas
2015-11-14 20:28         ` Marcin Borkowski
2015-11-14  8:06 ` Eli Zaretskii
2015-11-14  8:36   ` Random832
2015-11-14  9:36     ` Eli Zaretskii
2015-11-14 12:34       ` Marcin Borkowski
2015-11-14 13:40         ` Eli Zaretskii
2015-11-15  7:45           ` Marcin Borkowski
2015-11-15 19:45             ` Eli Zaretskii
2015-11-16 14:08             ` Nicolas Richard
2015-11-16 16:58               ` Marcin Borkowski
2015-11-14 21:10         ` Stefan Monnier
2015-11-16 17:00           ` Marcin Borkowski
2015-11-14  8:42   ` Marcin Borkowski
2015-11-14  8:59     ` Eli Zaretskii
2015-11-14 10:54       ` Yuri Khan
2015-11-14 11:04         ` Eli Zaretskii
2015-11-14  8:44   ` Michael Heerdegen
2015-11-14 10:15     ` Marcin Borkowski
     [not found] <mailman.2293.1447451126.7904.help-gnu-emacs@gnu.org>
2015-11-16  6:53 ` Dan Espen
2015-11-17  0:10   ` Emanuel Berg

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.