bg-mouse.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ;; GNU Emacs code for BBN Bitgraph mouse.
  2. ;; Copyright (C) Free Software Foundation, Inc. Oct 1985.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. ;;; Original version by John Robinson (jr@bbn-unix.arpa, bbncca!jr), Oct 1985
  18. ;;; User customization option:
  19. (defvar bg-mouse-fast-select-window nil
  20. "*Non-nil for mouse hits to select new window, then execute; else just select.")
  21. ;;; Defuns:
  22. (defun bg-mouse-report ()
  23. "Read and parse BBN BitGraph mouse report, and do what it asks.
  24. L-- move point * |---- These apply for mouse click in a window.
  25. --R set mark * | If bg-mouse-fast-select-window is nil,
  26. L-R kill region | a starred command on a nonselected window
  27. -C- move point and yank * | just selects that window.
  28. LC- yank-pop
  29. -CR or LCR undo | \"Scroll bar\" is right-hand window column.
  30. on modeline on \"scroll bar\" in minibuffer
  31. L-- scroll-up line to top execute-extended-command
  32. --R scroll-down line to bottom eval-expression
  33. -C- proportional goto-char line to middle suspend-emacs
  34. To reenable the mouse if terminal is reset, type ESC : RET ."
  35. (interactive)
  36. (bg-get-tty-num ?\;)
  37. (let*
  38. ((x (min (1- (screen-width))
  39. (/ (bg-get-tty-num ?\;) 9))) ; Don't hit column 86!
  40. (y (- (1- (screen-height))
  41. (/ (bg-get-tty-num ?\;) 16))) ; Assume default font size.
  42. (buttons (% (bg-get-tty-num ?c) 8))
  43. (window (bg-pos-to-window x y))
  44. (edges (window-edges window))
  45. (old-window (selected-window))
  46. (in-minibuf-p (eq y (1- (screen-height))))
  47. (same-window-p (and (not in-minibuf-p) (eq window old-window)))
  48. (in-modeline-p (eq y (1- (nth 3 edges))))
  49. (in-scrollbar-p (>= x (1- (nth 2 edges)))))
  50. (setq x (- x (nth 0 edges)))
  51. (setq y (- y (nth 1 edges)))
  52. (cond (in-modeline-p
  53. (select-window window)
  54. (cond ((= buttons 4)
  55. (scroll-up))
  56. ((= buttons 1)
  57. (scroll-down))
  58. ((= buttons 2)
  59. (goto-char (/ (* x
  60. (- (point-max) (point-min)))
  61. (1- (window-width))))
  62. (beginning-of-line)
  63. (what-cursor-position)))
  64. (select-window old-window))
  65. (in-scrollbar-p
  66. (select-window window)
  67. (scroll-up
  68. (cond ((= buttons 4)
  69. y)
  70. ((= buttons 1)
  71. (+ y (- 2 (window-height))))
  72. ((= buttons 2)
  73. (/ (+ 2 y y (- (window-height))) 2))
  74. (t
  75. 0)))
  76. (select-window old-window))
  77. (same-window-p
  78. (cond ((= buttons 4)
  79. (bg-move-point-to-x-y x y))
  80. ((= buttons 1)
  81. (push-mark)
  82. (bg-move-point-to-x-y x y)
  83. (exchange-point-and-mark))
  84. ((= buttons 5)
  85. (kill-region (mark) (point)))
  86. ((= buttons 2)
  87. (bg-move-point-to-x-y x y)
  88. (setq this-command 'yank)
  89. (yank))
  90. ((= buttons 6)
  91. (yank-pop 1))
  92. ((or (= buttons 3) (= buttons 7))
  93. (undo))
  94. )
  95. )
  96. (in-minibuf-p
  97. (cond ((= buttons 1)
  98. (call-interactively 'eval-expression))
  99. ((= buttons 4)
  100. (call-interactively 'execute-extended-command))
  101. ((= buttons 2)
  102. (suspend-emacs))
  103. ))
  104. (t ;in another window
  105. (select-window window)
  106. (cond ((not bg-mouse-fast-select-window))
  107. ((= buttons 4)
  108. (bg-move-point-to-x-y x y))
  109. ((= buttons 1)
  110. (push-mark)
  111. (bg-move-point-to-x-y x y)
  112. (exchange-point-and-mark))
  113. ((= buttons 2)
  114. (bg-move-point-to-x-y x y)
  115. (setq this-command 'yank)
  116. (yank))
  117. ))
  118. )))
  119. (defun bg-get-tty-num (term-char)
  120. "Read from terminal until TERM-CHAR is read, and return intervening number.
  121. Upon non-numeric not matching TERM-CHAR, reprogram the mouse and signal an error."
  122. (let
  123. ((num 0)
  124. (char (- (read-char) 48)))
  125. (while (and (>= char 0)
  126. (<= char 9))
  127. (setq num (+ (* num 10) char))
  128. (setq char (- (read-char) 48)))
  129. (or (eq term-char (+ char 48))
  130. (progn
  131. (bg-program-mouse)
  132. (error "Invalid data format in mouse command")))
  133. num))
  134. (defun bg-move-point-to-x-y (x y)
  135. "Position cursor in window coordinates.
  136. X and Y are 0-based character positions in the window."
  137. (move-to-window-line y)
  138. (move-to-column x)
  139. )
  140. (defun bg-pos-to-window (x y)
  141. "Find window corresponding to screen coordinates.
  142. X and Y are 0-based character positions on the screen."
  143. (let ((edges (window-edges))
  144. (window nil))
  145. (while (and (not (eq window (selected-window)))
  146. (or (< y (nth 1 edges))
  147. (>= y (nth 3 edges))
  148. (< x (nth 0 edges))
  149. (>= x (nth 2 edges))))
  150. (setq window (next-window window))
  151. (setq edges (window-edges window))
  152. )
  153. (or window (selected-window))
  154. )
  155. )
  156. (defun bg-program-mouse ()
  157. (send-string-to-terminal "\e:0;7;;;360;512;9;16;9;16c"))