sun-mouse.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. ;; Mouse handling for Sun windows
  2. ;; Copyright (C) 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. ;;; Jeff Peck, Sun Microsystems, Jan 1987.
  18. ;;; Original idea by Stan Jefferson
  19. (provide 'sun-mouse)
  20. ;;;
  21. ;;; Modelled after the GNUEMACS keymap interface.
  22. ;;;
  23. ;;; User Functions:
  24. ;;; make-mousemap, copy-mousemap,
  25. ;;; define-mouse, global-set-mouse, local-set-mouse,
  26. ;;; use-global-mousemap, use-local-mousemap,
  27. ;;; mouse-lookup, describe-mouse-bindings
  28. ;;;
  29. ;;; Options:
  30. ;;; extra-click-wait, scrollbar-width
  31. ;;;
  32. (defvar extra-click-wait 150
  33. "*Number of milliseconds to wait for an extra click.
  34. Set this to zero if you don't want chords or double clicks.")
  35. (defvar scrollbar-width 5
  36. "*The character width of the scrollbar.
  37. The cursor is deemed to be in the right edge scrollbar if it is this near the
  38. right edge, and more than two chars past the end of the indicated line.
  39. Setting to nil limits the scrollbar to the edge or vertical dividing bar.")
  40. ;;;
  41. ;;; Mousemaps
  42. ;;;
  43. (defun make-mousemap ()
  44. "Returns a new mousemap."
  45. (cons 'mousemap nil))
  46. (defun copy-mousemap (mousemap)
  47. "Return a copy of mousemap."
  48. (copy-alist mousemap))
  49. (defun define-mouse (mousemap mouse-list def)
  50. "Args MOUSEMAP, MOUSE-LIST, DEF. Define MOUSE-LIST in MOUSEMAP as DEF.
  51. MOUSE-LIST is a list of atoms specifing a mouse hit according to these rules:
  52. * One of these atoms specifies the active region of the definition.
  53. text, scrollbar, modeline, minibuffer
  54. * One or two or these atoms specify the button or button combination.
  55. left, middle, right, double
  56. * Any combination of these atoms specify the active shift keys.
  57. control, shift, meta
  58. * With a single unshifted button, you can add
  59. up
  60. to indicate an up-click.
  61. The atom `double' is used with a button designator to denote a double click.
  62. Two button chords are denoted by listing the two buttons.
  63. See sun-mouse-handler for the treatment of the form DEF."
  64. (mousemap-set (mouse-list-to-mouse-code mouse-list) mousemap def))
  65. (defun global-set-mouse (mouse-list def)
  66. "Give MOUSE-EVENT-LIST a local definition of DEF.
  67. See define-mouse for a description of MOUSE-EVENT-LIST and DEF.
  68. Note that if MOUSE-EVENT-LIST has a local definition in the current buffer,
  69. that local definition will continue to shadow any global definition."
  70. (interactive "xMouse event: \nxDefinition: ")
  71. (define-mouse current-global-mousemap mouse-list def))
  72. (defun local-set-mouse (mouse-list def)
  73. "Give MOUSE-EVENT-LIST a local definition of DEF.
  74. See define-mouse for a description of the arguments.
  75. The definition goes in the current buffer's local mousemap.
  76. Normally buffers in the same major mode share a local mousemap."
  77. (interactive "xMouse event: \nxDefinition: ")
  78. (if (null current-local-mousemap)
  79. (setq current-local-mousemap (make-mousemap)))
  80. (define-mouse current-local-mousemap mouse-list def))
  81. (defun use-global-mousemap (mousemap)
  82. "Selects MOUSEMAP as the global mousemap."
  83. (setq current-global-mousemap mousemap))
  84. (defun use-local-mousemap (mousemap)
  85. "Selects MOUSEMAP as the local mousemap.
  86. nil for MOUSEMAP means no local mousemap."
  87. (setq current-local-mousemap mousemap))
  88. ;;;
  89. ;;; Interface to the Mouse encoding defined in Emacstool.c
  90. ;;;
  91. ;;; Called when mouse-prefix is sent to emacs, additional
  92. ;;; information is read in as a list (button x y time-delta)
  93. ;;;
  94. ;;; First, some generally useful functions:
  95. ;;;
  96. (defun logtest (x y)
  97. "True if any bits set in X are also set in Y.
  98. Just like the Common Lisp function of the same name."
  99. (not (zerop (logand x y))))
  100. ;;;
  101. ;;; Hit accessors.
  102. ;;;
  103. (defconst sm::ButtonBits 7) ; Lowest 3 bits.
  104. (defconst sm::ShiftmaskBits 56) ; Second lowest 3 bits (56 = 63 - 7).
  105. (defconst sm::DoubleBits 64) ; Bit 7.
  106. (defconst sm::UpBits 128) ; Bit 8.
  107. ;;; All the useful code bits
  108. (defmacro sm::hit-code (hit)
  109. (` (nth 0 (, hit))))
  110. ;;; The button, or buttons if a chord.
  111. (defmacro sm::hit-button (hit)
  112. (` (logand sm::ButtonBits (nth 0 (, hit)))))
  113. ;;; The shift, control, and meta flags.
  114. (defmacro sm::hit-shiftmask (hit)
  115. (` (logand sm::ShiftmaskBits (nth 0 (, hit)))))
  116. ;;; Set if a double click (but not a chord).
  117. (defmacro sm::hit-double (hit)
  118. (` (logand sm::DoubleBits (nth 0 (, hit)))))
  119. ;;; Set on button release (as opposed to button press).
  120. (defmacro sm::hit-up (hit)
  121. (` (logand sm::UpBits (nth 0 (, hit)))))
  122. ;;; Screen x position.
  123. (defmacro sm::hit-x (hit) (list 'nth 1 hit))
  124. ;;; Screen y position.
  125. (defmacro sm::hit-y (hit) (list 'nth 2 hit))
  126. ;;; Millisconds since last hit.
  127. (defmacro sm::hit-delta (hit) (list 'nth 3 hit))
  128. (defmacro sm::hit-up-p (hit) ; A predicate.
  129. (` (not (zerop (sm::hit-up (, hit))))))
  130. ;;;
  131. ;;; Loc accessors. for sm::window-xy
  132. ;;;
  133. (defmacro sm::loc-w (loc) (list 'nth 0 loc))
  134. (defmacro sm::loc-x (loc) (list 'nth 1 loc))
  135. (defmacro sm::loc-y (loc) (list 'nth 2 loc))
  136. (defmacro eval-in-buffer (buffer &rest forms)
  137. "Macro to switches to BUFFER, evaluates FORMS, returns to original buffer."
  138. ;; When you don't need the complete window context of eval-in-window
  139. (` (let ((StartBuffer (current-buffer)))
  140. (unwind-protect
  141. (progn
  142. (set-buffer (, buffer))
  143. (,@ forms))
  144. (set-buffer StartBuffer)))))
  145. (put 'eval-in-buffer 'lisp-indent-hook 1)
  146. ;;; this is used extensively by sun-mouse-fns.el
  147. ;;;
  148. (defmacro eval-in-window (window &rest forms)
  149. "Switch to WINDOW, evaluate FORMS, return to original window."
  150. (` (let ((OriginallySelectedWindow (selected-window)))
  151. (unwind-protect
  152. (progn
  153. (select-window (, window))
  154. (,@ forms))
  155. (select-window OriginallySelectedWindow)))))
  156. (put 'eval-in-window 'lisp-indent-hook 1)
  157. ;;;
  158. ;;; handy utility, generalizes window_loop
  159. ;;;
  160. ;;; It's a macro (and does not evaluate its arguments).
  161. (defmacro eval-in-windows (form &optional yesmini)
  162. "Switches to each window and evaluates FORM. Optional argument
  163. YESMINI says to include the minibuffer as a window.
  164. This is a macro, and does not evaluate its arguments."
  165. (` (let ((OriginallySelectedWindow (selected-window)))
  166. (unwind-protect
  167. (while (progn
  168. (, form)
  169. (not (eq OriginallySelectedWindow
  170. (select-window
  171. (next-window nil (, yesmini)))))))
  172. (select-window OriginallySelectedWindow)))))
  173. (put 'eval-in-window 'lisp-indent-hook 0)
  174. (defun move-to-loc (x y)
  175. "Move cursor to window location X, Y.
  176. Handles wrapped and horizontally scrolled lines correctly."
  177. (move-to-window-line y)
  178. ;; window-line-end expects this to return the window column it moved to.
  179. (let ((cc (current-column))
  180. (nc (move-to-column
  181. (if (zerop (window-hscroll))
  182. (+ (current-column)
  183. (min (- (window-width) 2) ; To stay on the line.
  184. x))
  185. (+ (window-hscroll) -1
  186. (min (1- (window-width)) ; To stay on the line.
  187. x))))))
  188. (- nc cc)))
  189. (defun minibuffer-window-p (window)
  190. "True iff this WINDOW is minibuffer."
  191. (= (screen-height)
  192. (nth 3 (window-edges window)) ; The bottom edge.
  193. ))
  194. (defun sun-mouse-handler (&optional hit)
  195. "Evaluates the function or list associated with a mouse hit.
  196. Expecting to read a hit, which is a list: (button x y delta).
  197. A form bound to button by define-mouse is found by mouse-lookup.
  198. The variables: *mouse-window*, *mouse-x*, *mouse-y* are bound.
  199. If the form is a symbol (symbolp), it is funcall'ed with *mouse-window*,
  200. *mouse-x*, and *mouse-y* as arguments; if the form is a list (listp),
  201. the form is eval'ed; if the form is neither of these, it is an error.
  202. Returns nil."
  203. (interactive)
  204. (if (null hit) (setq hit (sm::combined-hits)))
  205. (let ((loc (sm::window-xy (sm::hit-x hit) (sm::hit-y hit))))
  206. (let ((*mouse-window* (sm::loc-w loc))
  207. (*mouse-x* (sm::loc-x loc))
  208. (*mouse-y* (sm::loc-y loc))
  209. (mouse-code (mouse-event-code hit loc)))
  210. (let ((form (eval-in-buffer (window-buffer *mouse-window*)
  211. (mouse-lookup mouse-code))))
  212. (cond ((null form)
  213. (if (not (sm::hit-up-p hit)) ; undefined up hits are ok.
  214. (error "Undefined mouse event: %s"
  215. (prin1-to-string
  216. (mouse-code-to-mouse-list mouse-code)))))
  217. ((symbolp form)
  218. (setq this-command form)
  219. (funcall form *mouse-window* *mouse-x* *mouse-y*))
  220. ((listp form)
  221. (setq this-command (car form))
  222. (eval form))
  223. (t
  224. (error "Mouse action must be symbol or list, but was: %s"
  225. form))))))
  226. ;; Don't let 'sun-mouse-handler get on last-command,
  227. ;; since this function should be transparent.
  228. (if (eq this-command 'sun-mouse-handler)
  229. (setq this-command last-command))
  230. ;; (message (prin1-to-string this-command)) ; to see what your buttons did
  231. nil)
  232. (defun sm::combined-hits ()
  233. "Read and return next mouse-hit, include possible double click"
  234. (let ((hit1 (mouse-hit-read)))
  235. (if (not (sm::hit-up-p hit1)) ; Up hits dont start doubles or chords.
  236. (let ((hit2 (mouse-second-hit extra-click-wait)))
  237. (if hit2 ; we cons'd it, we can smash it.
  238. ; (setf (sm::hit-code hit1) (logior (sm::hit-code hit1) ...))
  239. (setcar hit1 (logior (sm::hit-code hit1)
  240. (sm::hit-code hit2)
  241. (if (= (sm::hit-button hit1)
  242. (sm::hit-button hit2))
  243. sm::DoubleBits 0))))))
  244. hit1))
  245. (defun mouse-hit-read ()
  246. "Read mouse-hit list from keyboard. Like (read 'read-char),
  247. but that uses minibuffer, and mucks up last-command."
  248. (let ((char-list nil) (char nil))
  249. (while (not (equal 13 ; Carriage return.
  250. (prog1 (setq char (read-char))
  251. (setq char-list (cons char char-list))))))
  252. (read (mapconcat 'char-to-string (nreverse char-list) ""))
  253. ))
  254. ;;; Second Click Hackery....
  255. ;;; if prefix is not mouse-prefix, need a way to unread the char...
  256. ;;; or else have mouse flush input queue, or else need a peek at next char.
  257. ;;; There is no peek, but since one character can be unread, we only
  258. ;;; have to flush the queue when the command after a mouse click
  259. ;;; starts with mouse-prefix1 (see below).
  260. ;;; Something to do later: We could buffer the read commands and
  261. ;;; execute them ourselves after doing the mouse command (using
  262. ;;; lookup-key ??).
  263. (defvar mouse-prefix1 24 ; C-x
  264. "First char of mouse-prefix. Used to detect double clicks and chords.")
  265. (defvar mouse-prefix2 0 ; C-@
  266. "Second char of mouse-prefix. Used to detect double clicks and chords.")
  267. (defun mouse-second-hit (hit-wait)
  268. "Returns the next mouse hit occurring within HIT-WAIT milliseconds."
  269. (if (sit-for-millisecs hit-wait) nil ; No input within hit-wait millisecs.
  270. (let ((pc1 (read-char)))
  271. (if (or (not (equal pc1 mouse-prefix1))
  272. (sit-for-millisecs 3)) ; a mouse prefix will have second char
  273. (progn (setq unread-command-char pc1) ; Can get away with one unread.
  274. nil) ; Next input not mouse event.
  275. (let ((pc2 (read-char)))
  276. (if (not (equal pc2 mouse-prefix2))
  277. (progn (setq unread-command-char pc1) ; put back the ^X
  278. ;;; Too bad can't do two: (setq unread-command-char (list pc1 pc2))
  279. (ding) ; user will have to retype that pc2.
  280. nil) ; This input is not a mouse event.
  281. ;; Next input has mouse prefix and is within time limit.
  282. (let ((new-hit (mouse-hit-read))) ; Read the new hit.
  283. (if (sm::hit-up-p new-hit) ; Ignore up events when timing.
  284. (mouse-second-hit (- hit-wait (sm::hit-delta new-hit)))
  285. new-hit ; New down hit within limit, return it.
  286. ))))))))
  287. (defun sm::window-xy (x y)
  288. "Find window containing screen coordinates X and Y.
  289. Returns list (window x y) where x and y are relative to window."
  290. (or
  291. (catch 'found
  292. (eval-in-windows
  293. (let ((we (window-edges (selected-window))))
  294. (let ((le (nth 0 we))
  295. (te (nth 1 we))
  296. (re (nth 2 we))
  297. (be (nth 3 we)))
  298. (if (= re (screen-width))
  299. ;; include the continuation column with this window
  300. (setq re (1+ re)))
  301. (if (= be (screen-height))
  302. ;; include partial line at bottom of screen with this window
  303. ;; id est, if window is not multple of char size.
  304. (setq be (1+ be)))
  305. (if (and (>= x le) (< x re)
  306. (>= y te) (< y be))
  307. (throw 'found
  308. (list (selected-window) (- x le) (- y te))))))
  309. 'include-minibuffer-in-eval-in-windows))
  310. ;;If x,y from a real mouse click, we shouldn't get here.
  311. (list nil x y)
  312. ))
  313. (defun sm::window-region (loc)
  314. "Parse LOC into a region symbol.
  315. Returns one of (text scrollbar modeline minibuffer)"
  316. (let ((w (sm::loc-w loc))
  317. (x (sm::loc-x loc))
  318. (y (sm::loc-y loc)))
  319. (let ((right (1- (window-width w)))
  320. (bottom (1- (window-height w))))
  321. (cond ((minibuffer-window-p w) 'minibuffer)
  322. ((>= y bottom) 'modeline)
  323. ((>= x right) 'scrollbar)
  324. ;; far right column (window seperator) is always a scrollbar
  325. ((and scrollbar-width
  326. ;; mouse within scrollbar-width of edge.
  327. (>= x (- right scrollbar-width))
  328. ;; mouse a few chars past the end of line.
  329. (>= x (+ 2 (window-line-end w x y))))
  330. 'scrollbar)
  331. (t 'text)))))
  332. (defun window-line-end (w x y)
  333. "Return WINDOW column (ignore X) containing end of line Y"
  334. (eval-in-window w (save-excursion (move-to-loc (screen-width) y))))
  335. ;;;
  336. ;;; The encoding of mouse events into a mousemap.
  337. ;;; These values must agree with coding in emacstool:
  338. ;;;
  339. (defconst sm::keyword-alist
  340. '((left . 1) (middle . 2) (right . 4)
  341. (shift . 8) (control . 16) (meta . 32) (double . 64) (up . 128)
  342. (text . 256) (scrollbar . 512) (modeline . 1024) (minibuffer . 2048)
  343. ))
  344. (defun mouse-event-code (hit loc)
  345. "Maps MOUSE-HIT and LOC into a mouse-code."
  346. ;;;Region is a code for one of text, modeline, scrollbar, or minibuffer.
  347. (logior (sm::hit-code hit)
  348. (mouse-region-to-code (sm::window-region loc))))
  349. (defun mouse-region-to-code (region)
  350. "Returns partial mouse-code for specified REGION."
  351. (cdr (assq region sm::keyword-alist)))
  352. (defun mouse-list-to-mouse-code (mouse-list)
  353. "Map a MOUSE-LIST to a mouse-code."
  354. (apply 'logior
  355. (mapcar (function (lambda (x)
  356. (cdr (assq x sm::keyword-alist))))
  357. mouse-list)))
  358. (defun mouse-code-to-mouse-list (mouse-code)
  359. "Map a MOUSE-CODE to a mouse-list."
  360. (apply 'nconc (mapcar
  361. (function (lambda (x)
  362. (if (logtest mouse-code (cdr x))
  363. (list (car x)))))
  364. sm::keyword-alist)))
  365. (defun mousemap-set (code mousemap value)
  366. (let* ((alist (cdr mousemap))
  367. (assq-result (assq code alist)))
  368. (if assq-result
  369. (setcdr assq-result value)
  370. (setcdr mousemap (cons (cons code value) alist)))))
  371. (defun mousemap-get (code mousemap)
  372. (cdr (assq code (cdr mousemap))))
  373. (defun mouse-lookup (mouse-code)
  374. "Look up MOUSE-EVENT and return the definition. nil means undefined."
  375. (or (mousemap-get mouse-code current-local-mousemap)
  376. (mousemap-get mouse-code current-global-mousemap)))
  377. ;;;
  378. ;;; I (jpeck) don't understand the utility of the next four functions
  379. ;;; ask Steven Greenbaum <froud@kestrel>
  380. ;;;
  381. (defun mouse-mask-lookup (mask list)
  382. "Args MASK (a bit mask) and LIST (a list of (code . form) pairs).
  383. Returns a list of elements of LIST whose code or'ed with MASK is non-zero."
  384. (let ((result nil))
  385. (while list
  386. (if (logtest mask (car (car list)))
  387. (setq result (cons (car list) result)))
  388. (setq list (cdr list)))
  389. result))
  390. (defun mouse-union (l l-unique)
  391. "Return the union of list of mouse (code . form) pairs L and L-UNIQUE,
  392. where L-UNIQUE is considered to be union'ized already."
  393. (let ((result l-unique))
  394. (while l
  395. (let ((code-form-pair (car l)))
  396. (if (not (assq (car code-form-pair) result))
  397. (setq result (cons code-form-pair result))))
  398. (setq l (cdr l)))
  399. result))
  400. (defun mouse-union-first-prefered (l1 l2)
  401. "Return the union of lists of mouse (code . form) pairs L1 and L2,
  402. based on the code's, with preference going to elements in L1."
  403. (mouse-union l2 (mouse-union l1 nil)))
  404. (defun mouse-code-function-pairs-of-region (region)
  405. "Return a list of (code . function) pairs, where each code is
  406. currently set in the REGION."
  407. (let ((mask (mouse-region-to-code region)))
  408. (mouse-union-first-prefered
  409. (mouse-mask-lookup mask (cdr current-local-mousemap))
  410. (mouse-mask-lookup mask (cdr current-global-mousemap))
  411. )))
  412. ;;;
  413. ;;; Functions for DESCRIBE-MOUSE-BINDINGS
  414. ;;; And other mouse documentation functions
  415. ;;; Still need a good procedure to print out a help sheet in readable format.
  416. ;;;
  417. (defun one-line-doc-string (function)
  418. "Returns first line of documentation string for FUNCTION.
  419. If there is no documentation string, then the string
  420. \"No documentation\" is returned."
  421. (while (consp function) (setq function (car function)))
  422. (let ((doc (documentation function)))
  423. (if (null doc)
  424. "No documentation."
  425. (string-match "^.*$" doc)
  426. (substring doc 0 (match-end 0)))))
  427. (defun print-mouse-format (binding)
  428. (princ (car binding))
  429. (princ ": ")
  430. (mapcar (function
  431. (lambda (mouse-list)
  432. (princ mouse-list)
  433. (princ " ")))
  434. (cdr binding))
  435. (terpri)
  436. (princ " ")
  437. (princ (one-line-doc-string (car binding)))
  438. (terpri)
  439. )
  440. (defun print-mouse-bindings (region)
  441. "Prints mouse-event bindings for REGION."
  442. (mapcar 'print-mouse-format (sm::event-bindings region)))
  443. (defun sm::event-bindings (region)
  444. "Returns an alist of (function . (mouse-list1 ... mouse-listN)) for REGION,
  445. where each mouse-list is bound to the function in REGION."
  446. (let ((mouse-bindings (mouse-code-function-pairs-of-region region))
  447. (result nil))
  448. (while mouse-bindings
  449. (let* ((code-function-pair (car mouse-bindings))
  450. (current-entry (assoc (cdr code-function-pair) result)))
  451. (if current-entry
  452. (setcdr current-entry
  453. (cons (mouse-code-to-mouse-list (car code-function-pair))
  454. (cdr current-entry)))
  455. (setq result (cons (cons (cdr code-function-pair)
  456. (list (mouse-code-to-mouse-list
  457. (car code-function-pair))))
  458. result))))
  459. (setq mouse-bindings (cdr mouse-bindings))
  460. )
  461. result))
  462. (defun describe-mouse-bindings ()
  463. "Lists all current mouse-event bindings."
  464. (interactive)
  465. (with-output-to-temp-buffer "*Help*"
  466. (princ "Text Region") (terpri)
  467. (princ "---- ------") (terpri)
  468. (print-mouse-bindings 'text) (terpri)
  469. (princ "Modeline Region") (terpri)
  470. (princ "-------- ------") (terpri)
  471. (print-mouse-bindings 'modeline) (terpri)
  472. (princ "Scrollbar Region") (terpri)
  473. (princ "--------- ------") (terpri)
  474. (print-mouse-bindings 'scrollbar)))
  475. (defun describe-mouse-briefly (mouse-list)
  476. "Print a short description of the function bound to MOUSE-LIST."
  477. (interactive "xDescibe mouse list briefly: ")
  478. (let ((function (mouse-lookup (mouse-list-to-mouse-code mouse-list))))
  479. (if function
  480. (message "%s runs the command %s" mouse-list function)
  481. (message "%s is undefined" mouse-list))))
  482. (defun mouse-help-menu (function-and-binding)
  483. (cons (prin1-to-string (car function-and-binding))
  484. (menu-create ; Two sub-menu items of form ("String" . nil)
  485. (list (list (one-line-doc-string (car function-and-binding)))
  486. (list (prin1-to-string (cdr function-and-binding)))))))
  487. (defun mouse-help-region (w x y &optional region)
  488. "Displays a menu of mouse functions callable in this region."
  489. (let* ((region (or region (sm::window-region (list w x y))))
  490. (mlist (mapcar (function mouse-help-menu)
  491. (sm::event-bindings region)))
  492. (menu (menu-create (cons (list (symbol-name region)) mlist)))
  493. (item (sun-menu-evaluate w 0 y menu))
  494. )))
  495. ;;;
  496. ;;; Menu interface functions
  497. ;;;
  498. ;;; use defmenu, because this interface is subject to change
  499. ;;; really need a menu-p, but we use vectorp and the context...
  500. ;;;
  501. (defun menu-create (items)
  502. "Functional form for defmenu, given a list of ITEMS returns a menu.
  503. Each ITEM is a (STRING . VALUE) pair."
  504. (apply 'vector items)
  505. )
  506. (defmacro defmenu (menu &rest itemlist)
  507. "Defines MENU to be a menu, the ITEMS are (STRING . VALUE) pairs.
  508. See sun-menu-evaluate for interpretation of ITEMS."
  509. (list 'defconst menu (funcall 'menu-create itemlist))
  510. )
  511. (defun sun-menu-evaluate (*menu-window* *menu-x* *menu-y* menu)
  512. "Display a pop-up menu in WINDOW at X Y and evaluate selected item
  513. of MENU. MENU (or its symbol-value) should be a menu defined by defmenu.
  514. A menu ITEM is a (STRING . FORM) pair;
  515. the FORM associated with the selected STRING is evaluated,
  516. and the resulting value is returned. Generally these FORMs are
  517. evaluated for their side-effects rather than their values.
  518. If the selected form is a menu or a symbol whose value is a menu,
  519. then it is displayed and evaluated as a pullright menu item.
  520. If the the FORM of the first ITEM is nil, the STRING of the item
  521. is used as a label for the menu, i.e. it's inverted and not selectible."
  522. (if (symbolp menu) (setq menu (symbol-value menu)))
  523. (eval (sun-menu-internal *menu-window* *menu-x* *menu-y* 4 menu)))
  524. (defun sun-get-frame-data (code)
  525. "Sends the tty-sub-window escape sequence CODE to terminal,
  526. and returns a cons of the two numbers in returned escape sequence.
  527. That is it returns (cons <car> <cdr>) from \"\\E[n;<car>;<cdr>t\".
  528. CODE values: 13 = Tool-Position, 14 = Size-in-Pixels, 18 = Size-in-Chars."
  529. (send-string-to-terminal (concat "\033[" (int-to-string code) "t"))
  530. (let (char str x y)
  531. (while (not (equal 116 (setq char (read-char)))) ; #\t = 116
  532. (setq str (cons char str)))
  533. (setq str (mapconcat 'char-to-string (nreverse str) ""))
  534. (string-match ";[0-9]*" str)
  535. (setq y (substring str (1+ (match-beginning 0)) (match-end 0)))
  536. (setq str (substring str (match-end 0)))
  537. (string-match ";[0-9]*" str)
  538. (setq x (substring str (1+ (match-beginning 0)) (match-end 0)))
  539. (cons (string-to-int y) (string-to-int x))))
  540. (defun sm::font-size ()
  541. "Returns font size in pixels: (cons Ysize Xsize)"
  542. (let ((pix (sun-get-frame-data 14)) ; returns size in pixels
  543. (chr (sun-get-frame-data 18))) ; returns size in chars
  544. (cons (/ (car pix) (car chr)) (/ (cdr pix) (cdr chr)))))
  545. (defvar sm::menu-kludge-x nil
  546. "Cached frame-to-window X-Offset for sm::menu-kludge")
  547. (defvar sm::menu-kludge-y nil
  548. "Cached frame-to-window Y-Offset for sm::menu-kludge")
  549. (defun sm::menu-kludge ()
  550. "If sunfns.c uses <Menu_Base_Kludge> this function must be here!"
  551. (or sm::menu-kludge-y
  552. (let ((fs (sm::font-size)))
  553. (setq sm::menu-kludge-y (+ 8 (car fs)) ; a title line and borders
  554. sm::menu-kludge-x 4))) ; best values depend on .defaults/Menu
  555. (let ((wl (sun-get-frame-data 13))) ; returns frame location
  556. (cons (+ (car wl) sm::menu-kludge-y)
  557. (+ (cdr wl) sm::menu-kludge-x))))
  558. ;;;
  559. ;;; Function interface to selection/region
  560. ;;; primative functions are defined in sunfns.c
  561. ;;;
  562. (defun sun-yank-selection ()
  563. "Set mark and yank the contents of the current sunwindows selection
  564. into the current buffer at point."
  565. (interactive "*")
  566. (set-mark-command nil)
  567. (insert-string (sun-get-selection)))
  568. (defun sun-select-region (beg end)
  569. "Set the sunwindows selection to the region in the current buffer."
  570. (interactive "r")
  571. (sun-set-selection (buffer-substring beg end)))
  572. ;;;
  573. ;;; Support for emacstool
  574. ;;; This closes the window instead of stopping emacs.
  575. ;;;
  576. (defun suspend-emacstool (&optional stuffstring)
  577. "If running under as a detached process emacstool,
  578. you don't want to suspend (there is no way to resume),
  579. just close the window, and wait for reopening."
  580. (interactive)
  581. (if (and (boundp 'suspend-hook) suspend-hook)
  582. (funcall suspend-hook))
  583. (if stuffstring (send-string-to-terminal stuffstring))
  584. (send-string-to-terminal "\033[2t") ; To close EmacsTool window.
  585. (if (and (boundp 'suspend-resume-hook) suspend-resume-hook)
  586. (funcall suspend-resume-hook)))
  587. ;;;
  588. ;;; initialize mouse maps
  589. ;;;
  590. (make-variable-buffer-local 'current-local-mousemap)
  591. (setq-default current-local-mousemap nil)
  592. (defvar current-global-mousemap (make-mousemap))