x-mouse.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. ;; Mouse support for X window system.
  2. ;; Copyright (C) 1985, 1987 Free Software Foundation, Inc.
  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. (provide 'x-mouse)
  18. (defconst x-button-right (char-to-string 0))
  19. (defconst x-button-middle (char-to-string 1))
  20. (defconst x-button-left (char-to-string 2))
  21. (defconst x-button-right-up (char-to-string 4))
  22. (defconst x-button-middle-up (char-to-string 5))
  23. (defconst x-button-left-up (char-to-string 6))
  24. (defconst x-button-s-right (char-to-string 16))
  25. (defconst x-button-s-middle (char-to-string 17))
  26. (defconst x-button-s-left (char-to-string 18))
  27. (defconst x-button-s-right-up (char-to-string 20))
  28. (defconst x-button-s-middle-up (char-to-string 21))
  29. (defconst x-button-s-left-up (char-to-string 22))
  30. (defconst x-button-m-right (char-to-string 32))
  31. (defconst x-button-m-middle (char-to-string 33))
  32. (defconst x-button-m-left (char-to-string 34))
  33. (defconst x-button-m-right-up (char-to-string 36))
  34. (defconst x-button-m-middle-up (char-to-string 37))
  35. (defconst x-button-m-left-up (char-to-string 38))
  36. (defconst x-button-c-right (char-to-string 64))
  37. (defconst x-button-c-middle (char-to-string 65))
  38. (defconst x-button-c-left (char-to-string 66))
  39. (defconst x-button-c-right-up (char-to-string 68))
  40. (defconst x-button-c-middle-up (char-to-string 69))
  41. (defconst x-button-c-left-up (char-to-string 70))
  42. (defconst x-button-m-s-right (char-to-string 48))
  43. (defconst x-button-m-s-middle (char-to-string 49))
  44. (defconst x-button-m-s-left (char-to-string 50))
  45. (defconst x-button-m-s-right-up (char-to-string 52))
  46. (defconst x-button-m-s-middle-up (char-to-string 53))
  47. (defconst x-button-m-s-left-up (char-to-string 54))
  48. (defconst x-button-c-s-right (char-to-string 80))
  49. (defconst x-button-c-s-middle (char-to-string 81))
  50. (defconst x-button-c-s-left (char-to-string 82))
  51. (defconst x-button-c-s-right-up (char-to-string 84))
  52. (defconst x-button-c-s-middle-up (char-to-string 85))
  53. (defconst x-button-c-s-left-up (char-to-string 86))
  54. (defconst x-button-c-m-right (char-to-string 96))
  55. (defconst x-button-c-m-middle (char-to-string 97))
  56. (defconst x-button-c-m-left (char-to-string 98))
  57. (defconst x-button-c-m-right-up (char-to-string 100))
  58. (defconst x-button-c-m-middle-up (char-to-string 101))
  59. (defconst x-button-c-m-left-up (char-to-string 102))
  60. (defconst x-button-c-m-s-right (char-to-string 112))
  61. (defconst x-button-c-m-s-middle (char-to-string 113))
  62. (defconst x-button-c-m-s-left (char-to-string 114))
  63. (defconst x-button-c-m-s-right-up (char-to-string 116))
  64. (defconst x-button-c-m-s-middle-up (char-to-string 117))
  65. (defconst x-button-c-m-s-left-up (char-to-string 118))
  66. (defvar x-process-mouse-hook nil
  67. "Hook to run after each mouse event is processed. Should take two
  68. arguments; the first being a list (XPOS YPOS) corresponding to character
  69. offset from top left of screen and the second being a specifier for the
  70. buttons/keys.
  71. This will normally be set on a per-buffer basis.")
  72. (defun x-flush-mouse-queue ()
  73. "Process all queued mouse events."
  74. ;; A mouse event causes a special character sequence to be given
  75. ;; as keyboard input. That runs this function, which process all
  76. ;; queued mouse events and returns.
  77. (interactive)
  78. (while (> (x-mouse-events) 0)
  79. (x-proc-mouse-event)
  80. (and (boundp 'x-process-mouse-hook)
  81. (symbol-value 'x-process-mouse-hook)
  82. (funcall x-process-mouse-hook x-mouse-pos x-mouse-item))))
  83. (define-key global-map "\C-c\C-m" 'x-flush-mouse-queue)
  84. (define-key global-map "\C-x\C-@" 'x-flush-mouse-queue)
  85. (defun x-mouse-select (arg)
  86. "Select Emacs window the mouse is on."
  87. (let ((start-w (selected-window))
  88. (done nil)
  89. (w (selected-window))
  90. (rel-coordinate nil))
  91. (while (and (not done)
  92. (null (setq rel-coordinate
  93. (coordinates-in-window-p arg w))))
  94. (setq w (next-window w))
  95. (if (eq w start-w)
  96. (setq done t)))
  97. (select-window w)
  98. rel-coordinate))
  99. (defun x-mouse-keep-one-window (arg)
  100. "Select Emacs window mouse is on, then kill all other Emacs windows."
  101. (if (x-mouse-select arg)
  102. (delete-other-windows)))
  103. (defun x-mouse-select-and-split (arg)
  104. "Select Emacs window mouse is on, then split it vertically in half."
  105. (if (x-mouse-select arg)
  106. (split-window-vertically nil)))
  107. (defun x-mouse-set-point (arg)
  108. "Select Emacs window mouse is on, and move point to mouse position."
  109. (let* ((relative-coordinate (x-mouse-select arg))
  110. (rel-x (car relative-coordinate))
  111. (rel-y (car (cdr relative-coordinate))))
  112. (if relative-coordinate
  113. (progn
  114. (move-to-window-line rel-y)
  115. (move-to-column (+ rel-x (current-column)))))))
  116. (defun x-mouse-set-mark (arg)
  117. "Select Emacs window mouse is on, and set mark at mouse position.
  118. Display cursor at that position for a second."
  119. (if (x-mouse-select arg)
  120. (let ((point-save (point)))
  121. (unwind-protect
  122. (progn (x-mouse-set-point arg)
  123. (push-mark nil t)
  124. (sit-for 1))
  125. (goto-char point-save)))))
  126. (defun x-cut-text (arg &optional kill)
  127. "Copy text between point and mouse position into window system cut buffer.
  128. Save in Emacs kill ring also."
  129. (if (coordinates-in-window-p arg (selected-window))
  130. (save-excursion
  131. (let ((opoint (point))
  132. beg end)
  133. (x-mouse-set-point arg)
  134. (setq beg (min opoint (point))
  135. end (max opoint (point)))
  136. (x-store-cut-buffer (buffer-substring beg end))
  137. (copy-region-as-kill beg end)
  138. (if kill (delete-region beg end))))
  139. (message "Mouse not in selected window")))
  140. (defun x-paste-text (arg)
  141. "Move point to mouse position and insert window system cut buffer contents."
  142. (x-mouse-set-point arg)
  143. (insert (x-get-cut-buffer)))
  144. (defun x-cut-and-wipe-text (arg)
  145. "Kill text between point and mouse; also copy to window system cut buffer."
  146. (x-cut-text arg t))
  147. (defun x-mouse-ignore (arg)
  148. "Don't do anything.")
  149. (defun x-buffer-menu (arg)
  150. "Pop up a menu of buffers for selection with the mouse."
  151. (let ((menu
  152. (list "Buffer Menu"
  153. (cons "Select Buffer"
  154. (let ((tail (buffer-list))
  155. head)
  156. (while tail
  157. (let ((elt (car tail)))
  158. (if (not (string-match "^ "
  159. (buffer-name elt)))
  160. (setq head (cons
  161. (cons
  162. (format
  163. "%14s %s"
  164. (buffer-name elt)
  165. (or (buffer-file-name elt) ""))
  166. elt)
  167. head))))
  168. (setq tail (cdr tail)))
  169. (reverse head))))))
  170. (switch-to-buffer (or (x-popup-menu arg menu) (current-buffer)))))
  171. (defun x-help (arg)
  172. "Enter a menu-based help system."
  173. (let ((selection
  174. (x-popup-menu
  175. arg
  176. '("Help" ("Is there a command that..."
  177. ("Command apropos" . command-apropos)
  178. ("Apropos" . apropos))
  179. ("Key Commands <==> Functions"
  180. ("List all keystroke commands" . describe-bindings)
  181. ("Describe key briefly" . describe-key-briefly)
  182. ("Describe key verbose" . describe-key)
  183. ("Describe Lisp function" . describe-function)
  184. ("Where is this command" . where-is))
  185. ("Manual and tutorial"
  186. ("Info system" . info)
  187. ("Invoke Emacs tutorial" . help-with-tutorial))
  188. ("Odds and ends"
  189. ("Last 100 Keystrokes" . view-lossage)
  190. ("Describe syntax table" . describe-syntax))
  191. ("Modes"
  192. ("Describe current major mode" . describe-mode)
  193. ("List all keystroke commands" . describe-bindings))
  194. ("Administrivia"
  195. ("View Emacs news" . view-emacs-news)
  196. ("View the GNU Emacs license" . describe-copying)
  197. ("Describe distribution" . describe-distribution)
  198. ("Describe (non)warranty" . describe-no-warranty))))))
  199. (and selection (call-interactively selection))))
  200. (define-key mouse-map x-button-c-s-middle 'x-help)
  201. (define-key mouse-map x-button-c-s-left 'x-buffer-menu)
  202. (define-key mouse-map x-button-right 'x-mouse-select)
  203. (define-key mouse-map x-button-left 'x-mouse-set-mark)
  204. (define-key mouse-map x-button-c-s-right 'x-mouse-keep-one-window)
  205. (define-key mouse-map x-button-c-right 'x-mouse-select-and-split)
  206. (define-key mouse-map x-button-middle 'x-mouse-set-point)
  207. (define-key mouse-map x-button-s-middle 'x-cut-text)
  208. (define-key mouse-map x-button-s-right 'x-paste-text)
  209. (define-key mouse-map x-button-c-middle 'x-cut-and-wipe-text)