xt-mouse.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. ;;; xt-mouse.el --- support the mouse when emacs run in an xterm
  2. ;; Copyright (C) 1994, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
  4. ;; Keywords: mouse, terminals
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Enable mouse support when running inside an xterm.
  18. ;; This is actually useful when you are running X11 locally, but is
  19. ;; working on remote machine over a modem line or through a gateway.
  20. ;; It works by translating xterm escape codes into generic emacs mouse
  21. ;; events so it should work with any package that uses the mouse.
  22. ;; You don't have to turn off xterm mode to use the normal xterm mouse
  23. ;; functionality, it is still available by holding down the SHIFT key
  24. ;; when you press the mouse button.
  25. ;;; Todo:
  26. ;; Support multi-click -- somehow.
  27. ;;; Code:
  28. (defvar xterm-mouse-debug-buffer nil)
  29. (defvar xterm-mouse-last)
  30. ;; Mouse events symbols must have an 'event-kind property with
  31. ;; the value 'mouse-click.
  32. (dolist (event-type '(mouse-1 mouse-2 mouse-3
  33. M-down-mouse-1 M-down-mouse-2 M-down-mouse-3))
  34. (put event-type 'event-kind 'mouse-click))
  35. (defun xterm-mouse-translate (_event)
  36. "Read a click and release event from XTerm."
  37. (save-excursion
  38. (save-window-excursion
  39. (deactivate-mark)
  40. (let* ((xterm-mouse-last)
  41. (down (xterm-mouse-event))
  42. (down-command (nth 0 down))
  43. (down-data (nth 1 down))
  44. (down-where (nth 1 down-data))
  45. (down-binding (key-binding (if (symbolp down-where)
  46. (vector down-where down-command)
  47. (vector down-command))))
  48. (is-click (string-match "^mouse" (symbol-name (car down)))))
  49. (unless is-click
  50. (unless (and (eq (read-char) ?\e)
  51. (eq (read-char) ?\[)
  52. (eq (read-char) ?M))
  53. (error "Unexpected escape sequence from XTerm")))
  54. (let* ((click (if is-click down (xterm-mouse-event)))
  55. ;; (click-command (nth 0 click))
  56. (click-data (nth 1 click))
  57. (click-where (nth 1 click-data)))
  58. (if (memq down-binding '(nil ignore))
  59. (if (and (symbolp click-where)
  60. (consp click-where))
  61. (vector (list click-where click-data) click)
  62. (vector click))
  63. (setq unread-command-events
  64. (if (eq down-where click-where)
  65. (list click)
  66. (list
  67. ;; Cheat `mouse-drag-region' with move event.
  68. (list 'mouse-movement click-data)
  69. ;; Generate a drag event.
  70. (if (symbolp down-where)
  71. 0
  72. (list (intern (format "drag-mouse-%d"
  73. (+ 1 xterm-mouse-last)))
  74. down-data click-data)))))
  75. (if xterm-mouse-debug-buffer
  76. (print unread-command-events xterm-mouse-debug-buffer))
  77. (if (and (symbolp down-where)
  78. (consp down-where))
  79. (vector (list down-where down-data) down)
  80. (vector down))))))))
  81. ;; These two variables have been converted to terminal parameters.
  82. ;;
  83. ;;(defvar xterm-mouse-x 0
  84. ;; "Position of last xterm mouse event relative to the frame.")
  85. ;;
  86. ;;(defvar xterm-mouse-y 0
  87. ;; "Position of last xterm mouse event relative to the frame.")
  88. (defvar xt-mouse-epoch nil)
  89. ;; Indicator for the xterm-mouse mode.
  90. (defun xterm-mouse-position-function (pos)
  91. "Bound to `mouse-position-function' in XTerm mouse mode."
  92. (when (terminal-parameter nil 'xterm-mouse-x)
  93. (setcdr pos (cons (terminal-parameter nil 'xterm-mouse-x)
  94. (terminal-parameter nil 'xterm-mouse-y))))
  95. pos)
  96. ;; read xterm sequences above ascii 127 (#x7f)
  97. (defun xterm-mouse-event-read ()
  98. ;; We get the characters decoded by the keyboard coding system. Try
  99. ;; to recover the raw character.
  100. (let ((c (read-char)))
  101. (cond ;; If meta-flag is t we get a meta character
  102. ((>= c ?\M-\^@)
  103. (- c (- ?\M-\^@ 128)))
  104. ;; Reencode the character in the keyboard coding system, if
  105. ;; this is a non-ASCII character.
  106. ((>= c #x80)
  107. (aref (encode-coding-string (string c) (keyboard-coding-system)) 0))
  108. (t c))))
  109. (defun xterm-mouse-truncate-wrap (f)
  110. "Truncate with wrap-around."
  111. (condition-case nil
  112. ;; First try the built-in truncate, in case there's no overflow.
  113. (truncate f)
  114. ;; In case of overflow, do wraparound by hand.
  115. (range-error
  116. ;; In our case, we wrap around every 3 days or so, so if we assume
  117. ;; a maximum of 65536 wraparounds, we're safe for a couple years.
  118. ;; Using a power of 2 makes rounding errors less likely.
  119. (let* ((maxwrap (* 65536 2048))
  120. (dbig (truncate (/ f maxwrap)))
  121. (fdiff (- f (* 1.0 maxwrap dbig))))
  122. (+ (truncate fdiff) (* maxwrap dbig))))))
  123. (defun xterm-mouse-event ()
  124. "Convert XTerm mouse event to Emacs mouse event."
  125. (let* ((type (- (xterm-mouse-event-read) #o40))
  126. (x (- (xterm-mouse-event-read) #o40 1))
  127. (y (- (xterm-mouse-event-read) #o40 1))
  128. ;; Emulate timestamp information. This is accurate enough
  129. ;; for default value of mouse-1-click-follows-link (450msec).
  130. (timestamp (xterm-mouse-truncate-wrap
  131. (* 1000
  132. (- (float-time)
  133. (or xt-mouse-epoch
  134. (setq xt-mouse-epoch (float-time)))))))
  135. (mouse (intern
  136. ;; For buttons > 3, the release-event looks
  137. ;; differently (see xc/programs/xterm/button.c,
  138. ;; function EditorButton), and there seems to come in
  139. ;; a release-event only, no down-event.
  140. (cond ((>= type 64)
  141. (format "mouse-%d" (- type 60)))
  142. ((memq type '(8 9 10))
  143. (setq xterm-mouse-last type)
  144. (format "M-down-mouse-%d" (- type 7)))
  145. ((= type 11)
  146. (format "mouse-%d" (- xterm-mouse-last 7)))
  147. ((= type 3)
  148. ;; For buttons > 5 xterm only reports a
  149. ;; button-release event. Avoid error by mapping
  150. ;; them all to mouse-1.
  151. (format "mouse-%d" (+ 1 (or xterm-mouse-last 0))))
  152. (t
  153. (setq xterm-mouse-last type)
  154. (format "down-mouse-%d" (+ 1 type))))))
  155. (w (window-at x y))
  156. (ltrb (window-edges w))
  157. (left (nth 0 ltrb))
  158. (top (nth 1 ltrb)))
  159. (set-terminal-parameter nil 'xterm-mouse-x x)
  160. (set-terminal-parameter nil 'xterm-mouse-y y)
  161. (setq
  162. last-input-event
  163. (list mouse
  164. (let ((event (if w
  165. (posn-at-x-y (- x left) (- y top) w t)
  166. (append (list nil 'menu-bar)
  167. (nthcdr 2 (posn-at-x-y x y))))))
  168. (setcar (nthcdr 3 event) timestamp)
  169. event)))))
  170. ;;;###autoload
  171. (define-minor-mode xterm-mouse-mode
  172. "Toggle XTerm mouse mode.
  173. With a prefix argument ARG, enable XTerm mouse mode if ARG is
  174. positive, and disable it otherwise. If called from Lisp, enable
  175. the mode if ARG is omitted or nil.
  176. Turn it on to use Emacs mouse commands, and off to use xterm mouse commands.
  177. This works in terminal emulators compatible with xterm. It only
  178. works for simple uses of the mouse. Basically, only non-modified
  179. single clicks are supported. When turned on, the normal xterm
  180. mouse functionality for such clicks is still available by holding
  181. down the SHIFT key while pressing the mouse button."
  182. :global t :group 'mouse
  183. (let ((do-hook (if xterm-mouse-mode 'add-hook 'remove-hook)))
  184. (funcall do-hook 'terminal-init-xterm-hook
  185. 'turn-on-xterm-mouse-tracking-on-terminal)
  186. (funcall do-hook 'delete-terminal-functions
  187. 'turn-off-xterm-mouse-tracking-on-terminal)
  188. (funcall do-hook 'suspend-tty-functions
  189. 'turn-off-xterm-mouse-tracking-on-terminal)
  190. (funcall do-hook 'resume-tty-functions
  191. 'turn-on-xterm-mouse-tracking-on-terminal)
  192. (funcall do-hook 'suspend-hook 'turn-off-xterm-mouse-tracking)
  193. (funcall do-hook 'suspend-resume-hook 'turn-on-xterm-mouse-tracking)
  194. (funcall do-hook 'kill-emacs-hook 'turn-off-xterm-mouse-tracking))
  195. (if xterm-mouse-mode
  196. ;; Turn it on
  197. (progn
  198. (setq mouse-position-function #'xterm-mouse-position-function)
  199. (turn-on-xterm-mouse-tracking))
  200. ;; Turn it off
  201. (turn-off-xterm-mouse-tracking 'force)
  202. (setq mouse-position-function nil)))
  203. (defun turn-on-xterm-mouse-tracking ()
  204. "Enable Emacs mouse tracking in xterm."
  205. (dolist (terminal (terminal-list))
  206. (turn-on-xterm-mouse-tracking-on-terminal terminal)))
  207. (defun turn-off-xterm-mouse-tracking (&optional _force)
  208. "Disable Emacs mouse tracking in xterm."
  209. (dolist (terminal (terminal-list))
  210. (turn-off-xterm-mouse-tracking-on-terminal terminal)))
  211. (defun turn-on-xterm-mouse-tracking-on-terminal (&optional terminal)
  212. "Enable xterm mouse tracking on TERMINAL."
  213. (when (and xterm-mouse-mode (eq t (terminal-live-p terminal))
  214. ;; Avoid the initial terminal which is not a termcap device.
  215. ;; FIXME: is there more elegant way to detect the initial terminal?
  216. (not (string= (terminal-name terminal) "initial_terminal")))
  217. (unless (terminal-parameter terminal 'xterm-mouse-mode)
  218. ;; Simulate selecting a terminal by selecting one of its frames ;-(
  219. (with-selected-frame (car (frames-on-display-list terminal))
  220. (define-key input-decode-map "\e[M" 'xterm-mouse-translate))
  221. (set-terminal-parameter terminal 'xterm-mouse-mode t))
  222. (send-string-to-terminal "\e[?1000h" terminal)))
  223. (defun turn-off-xterm-mouse-tracking-on-terminal (terminal)
  224. "Disable xterm mouse tracking on TERMINAL."
  225. ;; Only send the disable command to those terminals to which we've already
  226. ;; sent the enable command.
  227. (when (and (terminal-parameter terminal 'xterm-mouse-mode)
  228. (eq t (terminal-live-p terminal))
  229. ;; Avoid the initial terminal which is not a termcap device.
  230. ;; FIXME: is there more elegant way to detect the initial terminal?
  231. (not (string= (terminal-name terminal) "initial_terminal")))
  232. ;; We could remove the key-binding and unset the `xterm-mouse-mode'
  233. ;; terminal parameter, but it seems less harmful to send this escape
  234. ;; command too many times (or to catch an unintended key sequence), than
  235. ;; to send it too few times (or to fail to let xterm-mouse events
  236. ;; pass by untranslated).
  237. (send-string-to-terminal "\e[?1000l" terminal)))
  238. (provide 'xt-mouse)
  239. ;;; xt-mouse.el ends here