5

When I run emacsclient it does not respond to mouse clicks. My main Emacs process runs in a terminal and responds to mouse clicks correctly because I have the following code in my Emacs config file:

(xterm-mouse-mode 1)

Why does emacsclient not respond to mouse clicks? Is there a way to make it do so?

phils
  • 71,335
  • 11
  • 153
  • 198
hekevintran
  • 22,822
  • 32
  • 111
  • 180

1 Answers1

10

This is probably because certain settings in Emacs are specific to the terminal, and manipulating such settings in your init file will only affect the terminal which is active at the time the init file was evaluated.

The following Q+A deals with much the same issue, and goes into the details:

Run command on new frame with daemon/client in Emacs

For your issue, I think this should do the trick:

(defun my-terminal-config (&optional frame)
  "Establish settings for the current terminal."
  (if (not frame) ;; The initial call.
      (xterm-mouse-mode 1)
    ;; Otherwise called via after-make-frame-functions.
    (if xterm-mouse-mode
        ;; Re-initialise the mode in case of a new terminal.
        (xterm-mouse-mode 1))))
;; Evaluate both now (for non-daemon emacs) and upon frame creation
;; (for new terminals via emacsclient).
(my-terminal-config)
(add-hook 'after-make-frame-functions 'my-terminal-config)
Community
  • 1
  • 1
phils
  • 71,335
  • 11
  • 153
  • 198
  • Excellent! I was actually having this problem only in GNU Screen sessions, but this solves it. – hekevintran Jul 24 '11 at 05:35
  • Good to hear. I've just realised that as `xterm-mouse-mode` is a global minor mode, my code was flawed in that if you intentionally disable that mode, and then create a new frame, it would get switched back on again. I've edited the code accordingly. I think the mode is smart enough to not need the `window-system` check I initially included (and which wasn't sufficient to identify an xterm in any case). – phils Jul 24 '11 at 11:27
  • Just wanted to mention that this can cause bug [#28800](https://lists.gnu.org/archive/html/bug-gnu-emacs/2017-10/msg00567.html) with rxvt. – Andrea Richiardi Mar 13 '18 at 04:24
  • @AndreaRichiardi, what do you mean by "this"? Using `xterm-mouse-mode`? Using it in `after-make-frame-functions`? Something else? – phils Mar 13 '18 at 06:37
  • Sorry for the delay, but basically the call in `after-make-frame-functions` works well. The `(my-terminal-config)` calls probably triggers the bug. Commenting it out solves. Also note that this is a `emacsclient -nw` bug only. – Andrea Richiardi Apr 02 '18 at 21:26
  • Anybody stumbling on this: I'm pretty sure that when you use this hook, and then close out the frame and return to the terminal, mouse actions start printing escape sequences instead of working normally. Super annoying side-effect, I'll try and see if I can detect frames closing and restore normal mouse behavior. – rien333 Jun 05 '19 at 10:28
  • @rien333 Assuming that you can reproduce the issue from `emacs -Q`, I would `M-x report-emacs-bug` about that. – phils Jun 05 '19 at 11:28
  • Uhm, so I would run `emacs -Q` (or rather, the emacs-daemon without a config file), then add this hook manually, and then see if the issue persist? – rien333 Jun 05 '19 at 11:33
  • Yes indeed. You can use `emacs --daemon -Q` -- those arguments work together. If it turns out that the problem *doesn't* occur under those conditions, then you might start recursively bisecting your init file to figure out what other factors are involved. – phils Jun 05 '19 at 11:43
  • Hm okay, it did find a way to fix it by sending an [escape sequence](https://unix.stackexchange.com/questions/76628/gnu-screen-weird-characters-on-click) that resets the xterm mouse behavior back to normal. So if you do `(send-string-to-terminal "\033[?1000l")` _before_ emacs closes the frame then the problem goes away. – rien333 Jun 05 '19 at 12:04