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
?