even when emacsclient is started in a terminal, window-system is non-nil
Asked Answered
O

1

6

I want to call some functions when I run emacsclient in a terminal emulator. The code I have works when Emacs is started in a text-only terminal. When I start Emacs in graphical mode and run emacsclient -t in a terminal, the functions do not run so I cannot use the mouse in the terminal emulator.

Here is the code in question:

(defun my-terminal-config (&optional frame)
  "Establish settings for the current terminal."
  (message (format "%s" window-system)) ;; "ns" (Mac OS X) when Emacs is started graphically
  (message (format "%s" (display-graphic-p))) ;; nil when Emacs is started graphically
  (unless (display-graphic-p)
    ;; enable mouse reporting for terminal emulators
    (xterm-mouse-mode 1)
    (global-set-key [mouse-4] '(lambda ()
                                 (interactive)
                                 (scroll-down 1)))
    (global-set-key [mouse-5] '(lambda ()
                                 (interactive)
                                 (scroll-up 1)))))
(add-hook 'after-make-frame-functions 'my-terminal-config)

This is a weird situation. Emacsclient connects to the server and creates a new frame, but because the server is running in a graphical environment, it reports window-system to be "ns" whereas in a terminal environment it would be nil. Therefore when I run emacsclient -t in a terminal, the mouse reporting enabling functions do not run. Once emacsclient is running, if I create a new frame with C-x 5 2, then the mouse reporting enabling functions will run because window-system will be nil in that frame.

It seems that when mixing frames between terminals and window systems, the value of window-system will always be that of the Emacs server.

Is there a way that I can run Emacs graphically and emacsclient in text mode and have the mouse functions run there? In other words, is it possible to detect that a frame being created will end up in a graphical or text environment?

Maybe I should simply always run those functions when a frame is created regardless of the value of window-system?

Overeat answered 30/9, 2011 at 22:38 Comment(4)
Interesting problem. I'm using Emacs 23.2.1 under Linux (RHEL) and when I run emacsclient -t against a graphical server and inspect window-system, it is nil, whereas in the graphical X Emacs it is X. In other words, it seems to be working correctly. What are the details of your setup?Burst
I'm using Emacs 23.3 under Macs OS X 10.6.8. I have the same result as that. Once the emacsclient is running window-system is nil, but inside the after-make-frame-functions hook it is "ns". It seems that the frame doesn't have the destination window-system until some time after the hook is done.Overeat
@Overeat Can you briefly go over why there are so many questions from Mac users about using Emacs under terminal emulation? What exactly is the usage scenario? I'm really curious because I see these questions A LOT.Snapdragon
I cannot speak for others' issues. My issues with Emacs under terminal emulation are not related to Mac OS. I like to be able to use Emacs (both as a server and a client) in graphical and text interfaces without weird differences/problems. I switch between the command-line and the GUI a lot. In fact, up until yesterday I used Emacs exclusively in the console. The other important terminal usage scenario is using Emacs + GNU Screen + SSH for remote pair programming. Having 2 people on different continents able to code together is invaluable.Overeat
A
10

the trick is that window-system and display-graphic-p are now frame specific. you need to be in that frame inside your hook function (seems like it should already be the case, but i don't think it is). i had to add this at the beginning of my after-make-frame-functions hook (to make window-system and display-graphic-p behave correctly):

(select-frame frame)
Arabian answered 1/10, 2011 at 23:16 Comment(1)
You hit the nail on the head! An even better way is that you can pass display-graphic-p the frame directly: (display-graphic-p frame). Thanks for the solution. It was driving me nuts.Overeat

© 2022 - 2024 — McMap. All rights reserved.