问题
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
?
回答1:
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)
来源:https://stackoverflow.com/questions/7616761/even-when-emacsclient-is-started-in-a-terminal-window-system-is-non-nil