sup-mouse.el 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ;;; sup-mouse.el --- supdup mouse support for lisp machines
  2. ;; Copyright (C) 1985-1986, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Wolfgang Rupprecht
  4. ;; Maintainer: FSF
  5. ;; Created: 21 Nov 1986
  6. ;; Keywords: hardware
  7. ;; (from code originally written by John Robinson@bbn for the bitgraph)
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;;; Code:
  21. ;;; User customization option:
  22. (defcustom sup-mouse-fast-select-window nil
  23. "Non-nil means mouse hits select new window, then execute.
  24. Otherwise just select."
  25. :type 'boolean
  26. :group 'mouse)
  27. (defconst mouse-left 0)
  28. (defconst mouse-center 1)
  29. (defconst mouse-right 2)
  30. (defconst mouse-2left 4)
  31. (defconst mouse-2center 5)
  32. (defconst mouse-2right 6)
  33. (defconst mouse-3left 8)
  34. (defconst mouse-3center 9)
  35. (defconst mouse-3right 10)
  36. ;;; Defuns:
  37. (defun sup-mouse-report ()
  38. "This function is called directly by the mouse, it parses and
  39. executes the mouse commands.
  40. L move point * |---- These apply for mouse click in a window.
  41. 2L delete word |
  42. 3L copy word | If sup-mouse-fast-select-window is nil,
  43. C move point and yank * | just selects that window.
  44. 2C yank pop |
  45. R set mark * |
  46. 2R delete region |
  47. 3R copy region |
  48. on modeline on \"scroll bar\" in minibuffer
  49. L scroll-up line to top execute-extended-command
  50. C proportional goto-char line to middle mouse-help
  51. R scroll-down line to bottom eval-expression"
  52. (interactive)
  53. (let*
  54. ;; expect a string of <esc>:<buttons>;<x-pos>;<y-pos>c
  55. ((buttons (sup-get-tty-num ?\;))
  56. (x (sup-get-tty-num ?\;))
  57. (y (sup-get-tty-num ?c))
  58. (window (sup-pos-to-window x y))
  59. (edges (window-edges window))
  60. (old-window (selected-window))
  61. (in-minibuf-p (eq y (1- (frame-height))))
  62. (same-window-p (and (not in-minibuf-p) (eq window old-window)))
  63. (in-modeline-p (eq y (1- (nth 3 edges))))
  64. (in-scrollbar-p (>= x (1- (nth 2 edges)))))
  65. (setq x (- x (nth 0 edges)))
  66. (setq y (- y (nth 1 edges)))
  67. ; (error "mouse-hit %d %d %d" buttons x y) ;;;; debug
  68. (cond (in-modeline-p
  69. (select-window window)
  70. (cond ((= buttons mouse-left)
  71. (scroll-up))
  72. ((= buttons mouse-right)
  73. (scroll-down))
  74. ((= buttons mouse-center)
  75. (goto-char (/ (* x
  76. (- (point-max) (point-min)))
  77. (1- (window-width))))
  78. (beginning-of-line)
  79. (what-cursor-position)))
  80. (select-window old-window))
  81. (in-scrollbar-p
  82. (select-window window)
  83. (scroll-up
  84. (cond ((= buttons mouse-left)
  85. y)
  86. ((= buttons mouse-right)
  87. (+ y (- 2 (window-height))))
  88. ((= buttons mouse-center)
  89. (/ (+ 2 y y (- (window-height))) 2))
  90. (t
  91. 0)))
  92. (select-window old-window))
  93. (same-window-p
  94. (cond ((= buttons mouse-left)
  95. (sup-move-point-to-x-y x y))
  96. ((= buttons mouse-2left)
  97. (sup-move-point-to-x-y x y)
  98. (kill-word 1))
  99. ((= buttons mouse-3left)
  100. (sup-move-point-to-x-y x y)
  101. (save-excursion
  102. (copy-region-as-kill
  103. (point) (progn (forward-word 1) (point))))
  104. (setq this-command 'yank)
  105. )
  106. ((= buttons mouse-right)
  107. (push-mark)
  108. (sup-move-point-to-x-y x y)
  109. (exchange-point-and-mark))
  110. ((= buttons mouse-2right)
  111. (push-mark)
  112. (sup-move-point-to-x-y x y)
  113. (kill-region (mark) (point)))
  114. ((= buttons mouse-3right)
  115. (push-mark)
  116. (sup-move-point-to-x-y x y)
  117. (copy-region-as-kill (mark) (point))
  118. (setq this-command 'yank))
  119. ((= buttons mouse-center)
  120. (sup-move-point-to-x-y x y)
  121. (setq this-command 'yank)
  122. (yank))
  123. ((= buttons mouse-2center)
  124. (yank-pop 1))
  125. )
  126. )
  127. (in-minibuf-p
  128. (cond ((= buttons mouse-right)
  129. (call-interactively 'eval-expression))
  130. ((= buttons mouse-left)
  131. (call-interactively 'execute-extended-command))
  132. ((= buttons mouse-center)
  133. (describe-function 'sup-mouse-report)); silly self help
  134. ))
  135. (t ;in another window
  136. (select-window window)
  137. (cond ((not sup-mouse-fast-select-window))
  138. ((= buttons mouse-left)
  139. (sup-move-point-to-x-y x y))
  140. ((= buttons mouse-right)
  141. (push-mark)
  142. (sup-move-point-to-x-y x y)
  143. (exchange-point-and-mark))
  144. ((= buttons mouse-center)
  145. (sup-move-point-to-x-y x y)
  146. (setq this-command 'yank)
  147. (yank))
  148. ))
  149. )))
  150. (defun sup-get-tty-num (term-char)
  151. "Read from terminal until TERM-CHAR is read, and return intervening number.
  152. Upon non-numeric not matching TERM-CHAR signal an error."
  153. (let
  154. ((num 0)
  155. (char (read-char)))
  156. (while (and (>= char ?0)
  157. (<= char ?9))
  158. (setq num (+ (* num 10) (- char ?0)))
  159. (setq char (read-char)))
  160. (or (eq term-char char)
  161. (error "Invalid data format in mouse command"))
  162. num))
  163. (defun sup-move-point-to-x-y (x y)
  164. "Position cursor in window coordinates.
  165. X and Y are 0-based character positions in the window."
  166. (move-to-window-line y)
  167. (move-to-column x)
  168. )
  169. (defun sup-pos-to-window (x y)
  170. "Find window corresponding to frame coordinates.
  171. X and Y are 0-based character positions on the frame."
  172. (get-window-with-predicate (lambda (w)
  173. (coordinates-in-window-p (cons x y) w))))
  174. ;;; sup-mouse.el ends here