Created
June 2, 2012 10:42
-
-
Save saitoha/2857756 to your computer and use it in GitHub Desktop.
SGR-styled mouse reporting. (for http://www.cs.toronto.edu/~ryanjohn/ext-mouse.el).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- ext-mouse.el.bak 2012-05-04 19:11:15.000000000 +0900 | |
+++ ext-mouse.el 2012-05-08 00:04:12.000000000 +0900 | |
@@ -59,6 +59,8 @@ | |
(defvar xterm-mouse-last) | |
+(defvar xterm-mouse-type "normal") | |
+ | |
;; Mouse events symbols must have an 'event-kind property with | |
;; the value 'mouse-click. | |
(dolist (event-type '(mouse-1 mouse-2 mouse-3 | |
@@ -85,6 +87,48 @@ | |
) | |
) | |
+(defun parse-number (terminal-chars) | |
+ (let ((last-char 0)) | |
+ (defun parse-number-impl (n) | |
+ (let ((c (read-char))) | |
+ (cond | |
+ ((and (> c #x2f) (< c #x3a)) | |
+ (parse-number-impl (+ (* n 10) (- c #x30)))) | |
+ ((some (lambda (term) (= term c)) terminal-chars) | |
+ (progn (setq last-char c) n)) | |
+ (t 1) | |
+ ))) | |
+ (list (parse-number-impl 0) last-char) | |
+ )) | |
+ | |
+(defun xterm-mouse-translate-sgr (event) | |
+ "Convert XTerm SGR-styled mouse event to Emacs mouse event." | |
+ | |
+ ;; | |
+ ;; SGR-style mouse reporting sequence is formatted as follow: | |
+ ;; | |
+ ;; CSI < Ps ; Ps ; Ps M <- mouse button down or move event | |
+ ;; | |
+ ;; or | |
+ ;; | |
+ ;; CSI < Ps ; Ps ; Ps m <- mouse button release event | |
+ ;; | |
+ (let* ( | |
+ (first-result (parse-number ";")) | |
+ (middle-result (parse-number ";")) | |
+ (last-result (parse-number "Mm")) | |
+ (x (- (car middle-result) 1)) | |
+ (y (- (car last-result) 1)) | |
+ (type (if (= (car (cdr last-result)) #x6d) | |
+ (logior (car last-result) 3) | |
+ (car first-result))) | |
+ (posn (xterm-mouse-posn x y)) | |
+ (event (xterm-mouse-event type posn ""))) | |
+ (set-terminal-parameter nil 'xterm-mouse-x x) | |
+ (set-terminal-parameter nil 'xterm-mouse-y y) | |
+ (vector (setq last-input-event event)) | |
+ )) | |
+ | |
;; These two variables have been converted to terminal parameters. | |
;; | |
;;(defvar xterm-mouse-x 0 | |
@@ -311,8 +355,14 @@ | |
(unless (terminal-parameter terminal 'xterm-mouse-mode) | |
;; Simulate selecting a terminal by selecting one of its frames ;-( | |
(with-selected-frame (car (frames-on-display-list terminal)) | |
- (define-key input-decode-map "\e[M" 'xterm-mouse-translate)) | |
+ (cond ((string= xterm-mouse-type 'sgr) | |
+ (define-key input-decode-map "\e[<" 'xterm-mouse-translate-sgr)) | |
+ (t | |
+ (define-key input-decode-map "\e[M" 'xterm-mouse-translate)) | |
+ )) | |
(set-terminal-parameter terminal 'xterm-mouse-mode t)) | |
+ (if (string= xterm-mouse-type 'sgr) | |
+ (send-string-to-terminal "\e[?1006h" terminal)) | |
(send-string-to-terminal "\e[?1002h" terminal))) | |
(defun turn-off-xterm-mouse-tracking-on-terminal (terminal) | |
@@ -329,6 +379,8 @@ | |
;; command too many times (or to catch an unintended key sequence), than | |
;; to send it too few times (or to fail to let xterm-mouse events | |
;; pass by untranslated). | |
+ (if (string= xterm-mouse-type 'sgr) | |
+ (send-string-to-terminal "\e[?1006l" terminal)) | |
(send-string-to-terminal "\e[?1002l" terminal))) | |
(provide 'ext-mouse) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment