-(defun xterm-mouse-event ()
- "Convert XTerm mouse event to Emacs mouse event."
- (let* ((type (- (xterm-mouse-event-read) #o40))
- (x (- (xterm-mouse-event-read) #o40 1))
- (y (- (xterm-mouse-event-read) #o40 1))
- ;; Emulate timestamp information. This is accurate enough
- ;; for default value of mouse-1-click-follows-link (450msec).
- (timestamp (xterm-mouse-truncate-wrap
- (* 1000
- (- (float-time)
- (or xt-mouse-epoch
- (setq xt-mouse-epoch (float-time)))))))
- (mouse (intern
- ;; For buttons > 3, the release-event looks
- ;; differently (see xc/programs/xterm/button.c,
- ;; function EditorButton), and there seems to come in
- ;; a release-event only, no down-event.
- (cond ((>= type 64)
- (format "mouse-%d" (- type 60)))
- ((memq type '(8 9 10))
- (setq xterm-mouse-last type)
- (format "M-down-mouse-%d" (- type 7)))
- ((= type 11)
- (format "mouse-%d" (- xterm-mouse-last 7)))
- ((= type 3)
- ;; For buttons > 5 xterm only reports a
- ;; button-release event. Avoid error by mapping
- ;; them all to mouse-1.
- (format "mouse-%d" (+ 1 (or xterm-mouse-last 0))))
- (t
- (setq xterm-mouse-last type)
- (format "down-mouse-%d" (+ 1 type))))))
- (w (window-at x y))
- (ltrb (window-edges w))
- (left (nth 0 ltrb))
- (top (nth 1 ltrb)))
-
- (set-terminal-parameter nil 'xterm-mouse-x x)
- (set-terminal-parameter nil 'xterm-mouse-y y)
- (setq
- last-input-event
- (list mouse
- (let ((event (if w
- (posn-at-x-y (- x left) (- y top) w t)
- (append (list nil 'menu-bar)
- (nthcdr 2 (posn-at-x-y x y))))))
- (setcar (nthcdr 3 event) timestamp)
- event)))))
+;; Normal terminal mouse click reporting: expect three bytes, of the
+;; form <BUTTON+32> <X+32> <Y+32>. Return a list (EVENT-TYPE X Y).
+(defun xterm-mouse--read-event-sequence-1000 ()
+ (let* ((code (- (read-event) 32))
+ (type
+ ;; For buttons > 3, the release-event looks differently
+ ;; (see xc/programs/xterm/button.c, function EditorButton),
+ ;; and come in a release-event only, no down-event.
+ (cond ((>= code 64)
+ (format "mouse-%d" (- code 60)))
+ ((memq code '(8 9 10))
+ (format "M-down-mouse-%d" (- code 7)))
+ ((memq code '(3 11))
+ (let ((down (car (terminal-parameter
+ nil 'xterm-mouse-last-down))))
+ (when (and down (string-match "[0-9]" (symbol-name down)))
+ (format (if (eq code 3) "mouse-%s" "M-mouse-%s")
+ (match-string 0 (symbol-name down))))))
+ ((memq code '(0 1 2))
+ (format "down-mouse-%d" (+ 1 code)))))
+ (x (- (read-event) 33))
+ (y (- (read-event) 33)))
+ (and type (wholenump x) (wholenump y)
+ (list (intern type) x y))))
+
+;; XTerm's 1006-mode terminal mouse click reporting has the form
+;; <BUTTON> ; <X> ; <Y> <M or m>, where the button and ordinates are
+;; in encoded (decimal) form. Return a list (EVENT-TYPE X Y).
+(defun xterm-mouse--read-event-sequence-1006 ()
+ (let (button-bytes x-bytes y-bytes c)
+ (while (not (eq (setq c (read-event)) ?\;))
+ (push c button-bytes))
+ (while (not (eq (setq c (read-event)) ?\;))
+ (push c x-bytes))
+ (while (not (memq (setq c (read-event)) '(?m ?M)))
+ (push c y-bytes))
+ (list (let* ((code (string-to-number
+ (apply 'string (nreverse button-bytes))))
+ (wheel (>= code 64))
+ (down (and (not wheel)
+ (eq c ?M))))
+ (intern (format "%s%smouse-%d"
+ (cond (wheel "")
+ ((< code 4) "")
+ ((< code 8) "S-")
+ ((< code 12) "M-")
+ ((< code 16) "M-S-")
+ ((< code 20) "C-")
+ ((< code 24) "C-S-")
+ ((< code 28) "C-M-")
+ ((< code 32) "C-M-S-")
+ (t
+ (error "Unexpected escape sequence from XTerm")))
+ (if down "down-" "")
+ (if wheel
+ (- code 60)
+ (1+ (mod code 4))))))
+ (1- (string-to-number (apply 'string (nreverse x-bytes))))
+ (1- (string-to-number (apply 'string (nreverse y-bytes)))))))
+
+(defun xterm-mouse--set-click-count (event click-count)
+ (setcdr (cdr event) (list click-count))
+ (let ((name (symbol-name (car event))))
+ (when (string-match "\\(.*?\\)\\(\\(?:down-\\)?mouse-.*\\)" name)
+ (setcar event
+ (intern (concat (match-string 1 name)
+ (if (= click-count 2)
+ "double-" "triple-")
+ (match-string 2 name)))))))
+
+(defun xterm-mouse-event (&optional extension)
+ "Convert XTerm mouse event to Emacs mouse event.
+EXTENSION, if non-nil, means to use an extension to the usual
+terminal mouse protocol; we currently support the value 1006,
+which is the \"1006\" extension implemented in Xterm >= 277."
+ (let* ((click (cond ((null extension)
+ (xterm-mouse--read-event-sequence-1000))
+ ((eq extension 1006)
+ (xterm-mouse--read-event-sequence-1006))
+ (t
+ (error "Unsupported XTerm mouse protocol")))))
+ (when click
+ (let* ((type (nth 0 click))
+ (x (nth 1 click))
+ (y (nth 2 click))
+ ;; Emulate timestamp information. This is accurate enough
+ ;; for default value of mouse-1-click-follows-link (450msec).
+ (timestamp (xterm-mouse-truncate-wrap
+ (* 1000
+ (- (float-time)
+ (or xt-mouse-epoch
+ (setq xt-mouse-epoch (float-time)))))))
+ (w (window-at x y))
+ (ltrb (window-edges w))
+ (left (nth 0 ltrb))
+ (top (nth 1 ltrb))
+ (posn (if w
+ (posn-at-x-y (- x left) (- y top) w t)
+ (append (list nil 'menu-bar)
+ (nthcdr 2 (posn-at-x-y x y)))))
+ (event (list type posn)))
+ (setcar (nthcdr 3 posn) timestamp)
+
+ ;; Try to handle double/triple clicks.
+ (let* ((last-click (terminal-parameter nil 'xterm-mouse-last-click))
+ (last-type (nth 0 last-click))
+ (last-name (symbol-name last-type))
+ (last-time (nth 1 last-click))
+ (click-count (nth 2 last-click))
+ (this-time (float-time))
+ (name (symbol-name type)))
+ (cond
+ ((not (string-match "down-" name))
+ ;; For up events, make the up side match the down side.
+ (setq this-time last-time)
+ (when (and (> click-count 1)
+ (string-match "down-" last-name)
+ (equal name (replace-match "" t t last-name)))
+ (xterm-mouse--set-click-count event click-count)))
+ ((not last-time) nil)
+ ((and (> double-click-time (* 1000 (- this-time last-time)))
+ (equal last-name (replace-match "" t t name)))
+ (setq click-count (1+ click-count))
+ (xterm-mouse--set-click-count event click-count))
+ (t (setq click-count 1)))
+ (set-terminal-parameter nil 'xterm-mouse-last-click
+ (list type this-time click-count)))
+
+ (set-terminal-parameter nil 'xterm-mouse-x x)
+ (set-terminal-parameter nil 'xterm-mouse-y y)
+ (setq last-input-event event)))))