mouse.el 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. ;;; mouse.el --- window system-independent mouse support -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1993-1995, 1999-2015 Free Software Foundation, Inc.
  3. ;; Maintainer: emacs-devel@gnu.org
  4. ;; Keywords: hardware, mouse
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This package provides various useful commands (including help
  19. ;; system access) through the mouse. All this code assumes that mouse
  20. ;; interpretation has been abstracted into Emacs input events.
  21. ;;; Code:
  22. ;;; Utility functions.
  23. ;; Indent track-mouse like progn.
  24. (put 'track-mouse 'lisp-indent-function 0)
  25. (defcustom mouse-yank-at-point nil
  26. "If non-nil, mouse yank commands yank at point instead of at click."
  27. :type 'boolean
  28. :group 'mouse)
  29. (defcustom mouse-drag-copy-region nil
  30. "If non-nil, copy to kill-ring upon mouse adjustments of the region.
  31. This affects `mouse-save-then-kill' (\\[mouse-save-then-kill]) in
  32. addition to mouse drags."
  33. :type 'boolean
  34. :version "24.1"
  35. :group 'mouse)
  36. (defcustom mouse-1-click-follows-link 450
  37. "Non-nil means that clicking Mouse-1 on a link follows the link.
  38. With the default setting, an ordinary Mouse-1 click on a link
  39. performs the same action as Mouse-2 on that link, while a longer
  40. Mouse-1 click \(hold down the Mouse-1 button for more than 450
  41. milliseconds) performs the original Mouse-1 binding \(which
  42. typically sets point where you click the mouse).
  43. If value is an integer, the time elapsed between pressing and
  44. releasing the mouse button determines whether to follow the link
  45. or perform the normal Mouse-1 action (typically set point).
  46. The absolute numeric value specifies the maximum duration of a
  47. \"short click\" in milliseconds. A positive value means that a
  48. short click follows the link, and a longer click performs the
  49. normal action. A negative value gives the opposite behavior.
  50. If value is `double', a double click follows the link.
  51. Otherwise, a single Mouse-1 click unconditionally follows the link.
  52. Note that dragging the mouse never follows the link.
  53. This feature only works in modes that specifically identify
  54. clickable text as links, so it may not work with some external
  55. packages. See `mouse-on-link-p' for details."
  56. :version "22.1"
  57. :type '(choice (const :tag "Disabled" nil)
  58. (const :tag "Double click" double)
  59. (number :tag "Single click time limit" :value 450)
  60. (other :tag "Single click" t))
  61. :group 'mouse)
  62. (defcustom mouse-1-click-in-non-selected-windows t
  63. "If non-nil, a Mouse-1 click also follows links in non-selected windows.
  64. If nil, a Mouse-1 click on a link in a non-selected window performs
  65. the normal mouse-1 binding, typically selects the window and sets
  66. point at the click position."
  67. :type 'boolean
  68. :version "22.1"
  69. :group 'mouse)
  70. (defun mouse--down-1-maybe-follows-link (&optional _prompt)
  71. "Turn `mouse-1' events into `mouse-2' events if follows-link.
  72. Expects to be bound to `down-mouse-1' in `key-translation-map'."
  73. (when (and mouse-1-click-follows-link
  74. (eq (if (eq mouse-1-click-follows-link 'double)
  75. 'double-down-mouse-1 'down-mouse-1)
  76. (car-safe last-input-event))
  77. (mouse-on-link-p (event-start last-input-event))
  78. (or mouse-1-click-in-non-selected-windows
  79. (eq (selected-window)
  80. (posn-window (event-start last-input-event)))))
  81. (let ((timedout
  82. (sit-for (if (numberp mouse-1-click-follows-link)
  83. (/ (abs mouse-1-click-follows-link) 1000.0)
  84. 0))))
  85. (if (if (and (numberp mouse-1-click-follows-link)
  86. (>= mouse-1-click-follows-link 0))
  87. timedout (not timedout))
  88. nil
  89. (let ((event (read-key))) ;Use read-key so it works for xterm-mouse-mode!
  90. (if (eq (car-safe event) (if (eq mouse-1-click-follows-link 'double)
  91. 'double-mouse-1 'mouse-1))
  92. ;; Turn the mouse-1 into a mouse-2 to follow links.
  93. (let ((newup (if (eq mouse-1-click-follows-link 'double)
  94. 'double-mouse-2 'mouse-2)))
  95. ;; If mouse-2 has never been done by the user, it doesn't have
  96. ;; the necessary property to be interpreted correctly.
  97. (unless (get newup 'event-kind)
  98. (put newup 'event-kind (get (car event) 'event-kind)))
  99. (push (cons newup (cdr event)) unread-command-events)
  100. ;; Don't change the down event, only the up-event (bug#18212).
  101. nil)
  102. (push event unread-command-events)
  103. nil))))))
  104. (define-key key-translation-map [down-mouse-1]
  105. #'mouse--down-1-maybe-follows-link)
  106. (define-key key-translation-map [double-down-mouse-1]
  107. #'mouse--down-1-maybe-follows-link)
  108. ;; Provide a mode-specific menu on a mouse button.
  109. (defun minor-mode-menu-from-indicator (indicator)
  110. "Show menu for minor mode specified by INDICATOR.
  111. Interactively, INDICATOR is read using completion.
  112. If there is no menu defined for the minor mode, then create one with
  113. items `Turn Off' and `Help'."
  114. (interactive
  115. (list (completing-read
  116. "Minor mode indicator: "
  117. (describe-minor-mode-completion-table-for-indicator))))
  118. (let* ((minor-mode (lookup-minor-mode-from-indicator indicator))
  119. (mm-fun (or (get minor-mode :minor-mode-function) minor-mode)))
  120. (unless minor-mode (error "Cannot find minor mode for ‘%s’" indicator))
  121. (let* ((map (cdr-safe (assq minor-mode minor-mode-map-alist)))
  122. (menu (and (keymapp map) (lookup-key map [menu-bar]))))
  123. (setq menu
  124. (if menu
  125. (mouse-menu-non-singleton menu)
  126. (if (fboundp mm-fun) ; bug#20201
  127. `(keymap
  128. ,indicator
  129. (turn-off menu-item "Turn Off minor mode" ,mm-fun)
  130. (help menu-item "Help for minor mode"
  131. (lambda () (interactive)
  132. (describe-function ',mm-fun)))))))
  133. (if menu
  134. (popup-menu menu)
  135. (message "No menu available")))))
  136. (defun mouse-minor-mode-menu (event)
  137. "Show minor-mode menu for EVENT on minor modes area of the mode line."
  138. (interactive "@e")
  139. (let ((indicator (car (nth 4 (car (cdr event))))))
  140. (minor-mode-menu-from-indicator indicator)))
  141. (defun mouse-menu-major-mode-map ()
  142. (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
  143. (let* (;; Keymap from which to inherit; may be null.
  144. (ancestor (mouse-menu-non-singleton
  145. (and (current-local-map)
  146. (local-key-binding [menu-bar]))))
  147. ;; Make a keymap in which our last command leads to a menu or
  148. ;; default to the edit menu.
  149. (newmap (if ancestor
  150. (make-sparse-keymap (concat (format-mode-line mode-name)
  151. " Mode"))
  152. menu-bar-edit-menu)))
  153. (if ancestor
  154. (set-keymap-parent newmap ancestor))
  155. newmap))
  156. (defun mouse-menu-non-singleton (menubar)
  157. "Return menu keybar MENUBAR, or a lone submenu inside it.
  158. If MENUBAR defines exactly one submenu, return just that submenu.
  159. Otherwise, return MENUBAR."
  160. (if menubar
  161. (let (submap)
  162. (map-keymap
  163. (lambda (k v) (setq submap (if submap t (cons k v))))
  164. (keymap-canonicalize menubar))
  165. (if (eq submap t)
  166. menubar
  167. (lookup-key menubar (vector (car submap)))))))
  168. (defun mouse-menu-bar-map ()
  169. "Return a keymap equivalent to the menu bar.
  170. The contents are the items that would be in the menu bar whether or
  171. not it is actually displayed."
  172. (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
  173. (let* ((local-menu (and (current-local-map)
  174. (lookup-key (current-local-map) [menu-bar])))
  175. (global-menu (lookup-key global-map [menu-bar]))
  176. ;; If a keymap doesn't have a prompt string (a lazy
  177. ;; programmer didn't bother to provide one), create it and
  178. ;; insert it into the keymap; each keymap gets its own
  179. ;; prompt. This is required for non-toolkit versions to
  180. ;; display non-empty menu pane names.
  181. (minor-mode-menus
  182. (mapcar
  183. (lambda (menu)
  184. (let* ((minor-mode (car menu))
  185. (menu (cdr menu))
  186. (title-or-map (cadr menu)))
  187. (or (stringp title-or-map)
  188. (setq menu
  189. (cons 'keymap
  190. (cons (concat
  191. (capitalize (subst-char-in-string
  192. ?- ?\s (symbol-name
  193. minor-mode)))
  194. " Menu")
  195. (cdr menu)))))
  196. menu))
  197. (minor-mode-key-binding [menu-bar])))
  198. (local-title-or-map (and local-menu (cadr local-menu)))
  199. (global-title-or-map (cadr global-menu)))
  200. (or (null local-menu)
  201. (stringp local-title-or-map)
  202. (setq local-menu (cons 'keymap
  203. (cons (concat (format-mode-line mode-name)
  204. " Mode Menu")
  205. (cdr local-menu)))))
  206. (or (stringp global-title-or-map)
  207. (setq global-menu (cons 'keymap
  208. (cons "Global Menu"
  209. (cdr global-menu)))))
  210. ;; Supplying the list is faster than making a new map.
  211. ;; FIXME: We have a problem here: we have to use the global/local/minor
  212. ;; so they're displayed in the expected order, but later on in the command
  213. ;; loop, they're actually looked up in the opposite order.
  214. (apply 'append
  215. global-menu
  216. local-menu
  217. minor-mode-menus)))
  218. (defun mouse-major-mode-menu (event &optional prefix)
  219. "Pop up a mode-specific menu of mouse commands.
  220. Default to the Edit menu if the major mode doesn't define a menu."
  221. (declare (obsolete mouse-menu-major-mode-map "23.1"))
  222. (interactive "@e\nP")
  223. (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
  224. (popup-menu (mouse-menu-major-mode-map) event prefix))
  225. (defun mouse-popup-menubar (event prefix)
  226. "Pop up a menu equivalent to the menu bar for keyboard EVENT with PREFIX.
  227. The contents are the items that would be in the menu bar whether or
  228. not it is actually displayed."
  229. (declare (obsolete mouse-menu-bar-map "23.1"))
  230. (interactive "@e \nP")
  231. (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
  232. (popup-menu (mouse-menu-bar-map) (unless (integerp event) event) prefix))
  233. (defun mouse-popup-menubar-stuff (event prefix)
  234. "Popup a menu like either `mouse-major-mode-menu' or `mouse-popup-menubar'.
  235. Use the former if the menu bar is showing, otherwise the latter."
  236. (declare (obsolete nil "23.1"))
  237. (interactive "@e\nP")
  238. (run-hooks 'activate-menubar-hook 'menu-bar-update-hook)
  239. (popup-menu
  240. (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
  241. (mouse-menu-bar-map)
  242. (mouse-menu-major-mode-map))
  243. event prefix))
  244. ;; Commands that operate on windows.
  245. (defun mouse-minibuffer-check (event)
  246. (let ((w (posn-window (event-start event))))
  247. (and (window-minibuffer-p w)
  248. (not (minibuffer-window-active-p w))
  249. (user-error "Minibuffer window is not active")))
  250. ;; Give temporary modes such as isearch a chance to turn off.
  251. (run-hooks 'mouse-leave-buffer-hook))
  252. (defun mouse-delete-window (click)
  253. "Delete the window you click on.
  254. Do nothing if the frame has just one window.
  255. This command must be bound to a mouse click."
  256. (interactive "e")
  257. (unless (one-window-p t)
  258. (mouse-minibuffer-check click)
  259. (delete-window (posn-window (event-start click)))))
  260. (defun mouse-select-window (click)
  261. "Select the window clicked on; don't move point."
  262. (interactive "e")
  263. (mouse-minibuffer-check click)
  264. (let ((oframe (selected-frame))
  265. (frame (window-frame (posn-window (event-start click)))))
  266. (select-window (posn-window (event-start click)))
  267. (raise-frame frame)
  268. (select-frame frame)
  269. (or (eq frame oframe)
  270. (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
  271. (define-obsolete-function-alias 'mouse-tear-off-window 'tear-off-window "24.4")
  272. (defun tear-off-window (click)
  273. "Delete the selected window, and create a new frame displaying its buffer."
  274. (interactive "e")
  275. (mouse-minibuffer-check click)
  276. (let* ((window (posn-window (event-start click)))
  277. (buf (window-buffer window))
  278. (frame (make-frame))) ;FIXME: Use pop-to-buffer.
  279. (select-frame frame)
  280. (switch-to-buffer buf)
  281. (delete-window window)))
  282. (defun mouse-delete-other-windows ()
  283. "Delete all windows except the one you click on."
  284. (interactive "@")
  285. (delete-other-windows))
  286. (defun mouse-split-window-vertically (click)
  287. "Select Emacs window mouse is on, then split it vertically in half.
  288. The window is split at the line clicked on.
  289. This command must be bound to a mouse click."
  290. (interactive "@e")
  291. (mouse-minibuffer-check click)
  292. (let ((start (event-start click)))
  293. (select-window (posn-window start))
  294. (let ((new-height (1+ (cdr (posn-col-row (event-end click)))))
  295. (first-line window-min-height)
  296. (last-line (- (window-height) window-min-height)))
  297. (if (< last-line first-line)
  298. (user-error "Window too short to split")
  299. ;; Bind `window-combination-resize' to nil so we are sure to get
  300. ;; the split right at the line clicked on.
  301. (let (window-combination-resize)
  302. (split-window-vertically
  303. (min (max new-height first-line) last-line)))))))
  304. (defun mouse-split-window-horizontally (click)
  305. "Select Emacs window mouse is on, then split it horizontally in half.
  306. The window is split at the column clicked on.
  307. This command must be bound to a mouse click."
  308. (interactive "@e")
  309. (mouse-minibuffer-check click)
  310. (let ((start (event-start click)))
  311. (select-window (posn-window start))
  312. (let ((new-width (1+ (car (posn-col-row (event-end click)))))
  313. (first-col window-min-width)
  314. (last-col (- (window-width) window-min-width)))
  315. (if (< last-col first-col)
  316. (user-error "Window too narrow to split")
  317. ;; Bind `window-combination-resize' to nil so we are sure to get
  318. ;; the split right at the column clicked on.
  319. (let (window-combination-resize)
  320. (split-window-horizontally
  321. (min (max new-width first-col) last-col)))))))
  322. (defun mouse-drag-line (start-event line)
  323. "Drag a mode line, header line, or vertical line with the mouse.
  324. START-EVENT is the starting mouse-event of the drag action. LINE
  325. must be one of the symbols `header', `mode', or `vertical'."
  326. ;; Give temporary modes such as isearch a chance to turn off.
  327. (run-hooks 'mouse-leave-buffer-hook)
  328. (let* ((echo-keystrokes 0)
  329. (start (event-start start-event))
  330. (window (posn-window start))
  331. (frame (window-frame window))
  332. ;; `position' records the x- or y-coordinate of the last
  333. ;; sampled position.
  334. (position (if (eq line 'vertical)
  335. (+ (window-pixel-left window)
  336. (car (posn-x-y start)))
  337. (+ (window-pixel-top window)
  338. (cdr (posn-x-y start)))))
  339. ;; `last-position' records the x- or y-coordinate of the
  340. ;; previously sampled position. The difference of `position'
  341. ;; and `last-position' determines the size change of WINDOW.
  342. (last-position position)
  343. (draggable t)
  344. posn-window growth dragged)
  345. ;; Decide on whether we are allowed to track at all and whose
  346. ;; window's edge we drag.
  347. (cond
  348. ((eq line 'header)
  349. (if (window-at-side-p window 'top)
  350. ;; We can't drag the header line of a topmost window.
  351. (setq draggable nil)
  352. ;; Drag bottom edge of window above the header line.
  353. (setq window (window-in-direction 'above window t))))
  354. ((eq line 'mode)
  355. (if (and (window-at-side-p window 'bottom)
  356. ;; Allow resizing the minibuffer window if it's on the
  357. ;; same frame as and immediately below `window', and it's
  358. ;; either active or `resize-mini-windows' is nil.
  359. (let ((minibuffer-window (minibuffer-window frame)))
  360. (not (and (eq (window-frame minibuffer-window) frame)
  361. (or (not resize-mini-windows)
  362. (eq minibuffer-window
  363. (active-minibuffer-window)))))))
  364. (setq draggable nil))))
  365. (let* ((exitfun nil)
  366. (move
  367. (lambda (event) (interactive "e")
  368. (cond
  369. ((not (consp event))
  370. nil)
  371. ((eq line 'vertical)
  372. ;; Drag right edge of `window'.
  373. (setq start (event-start event))
  374. (setq position (car (posn-x-y start)))
  375. ;; Set `posn-window' to the window where `event' was recorded.
  376. ;; This can be `window' or the window on the left or right of
  377. ;; `window'.
  378. (when (window-live-p (setq posn-window (posn-window start)))
  379. ;; Add left edge of `posn-window' to `position'.
  380. (setq position (+ (window-pixel-left posn-window) position))
  381. (unless (nth 1 start)
  382. ;; Add width of objects on the left of the text area to
  383. ;; `position'.
  384. (when (eq (window-current-scroll-bars posn-window) 'left)
  385. (setq position (+ (window-scroll-bar-width posn-window)
  386. position)))
  387. (setq position (+ (car (window-fringes posn-window))
  388. (or (car (window-margins posn-window)) 0)
  389. position))))
  390. ;; When the cursor overshoots after shrinking a window to its
  391. ;; minimum size and the dragging direction changes, have the
  392. ;; cursor first catch up with the window edge.
  393. (unless (or (zerop (setq growth (- position last-position)))
  394. (and (> growth 0)
  395. (< position (+ (window-pixel-left window)
  396. (window-pixel-width window))))
  397. (and (< growth 0)
  398. (> position (+ (window-pixel-left window)
  399. (window-pixel-width window)))))
  400. (setq dragged t)
  401. (adjust-window-trailing-edge window growth t t))
  402. (setq last-position position))
  403. (draggable
  404. ;; Drag bottom edge of `window'.
  405. (setq start (event-start event))
  406. ;; Set `posn-window' to the window where `event' was recorded.
  407. ;; This can be either `window' or the window above or below of
  408. ;; `window'.
  409. (setq posn-window (posn-window start))
  410. (setq position (cdr (posn-x-y start)))
  411. (when (window-live-p posn-window)
  412. ;; Add top edge of `posn-window' to `position'.
  413. (setq position (+ (window-pixel-top posn-window) position))
  414. ;; If necessary, add height of header line to `position'
  415. (when (memq (posn-area start)
  416. '(nil left-fringe right-fringe left-margin right-margin))
  417. (setq position (+ (window-header-line-height posn-window) position))))
  418. ;; When the cursor overshoots after shrinking a window to its
  419. ;; minimum size and the dragging direction changes, have the
  420. ;; cursor first catch up with the window edge.
  421. (unless (or (zerop (setq growth (- position last-position)))
  422. (and (> growth 0)
  423. (< position (+ (window-pixel-top window)
  424. (window-pixel-height window))))
  425. (and (< growth 0)
  426. (> position (+ (window-pixel-top window)
  427. (window-pixel-height window)))))
  428. (setq dragged t)
  429. (adjust-window-trailing-edge window growth nil t))
  430. (setq last-position position))))))
  431. ;; Start tracking. The special value 'dragging' signals the
  432. ;; display engine to freeze the mouse pointer shape for as long
  433. ;; as we drag.
  434. (setq track-mouse 'dragging)
  435. ;; Loop reading events and sampling the position of the mouse.
  436. (setq exitfun
  437. (set-transient-map
  438. (let ((map (make-sparse-keymap)))
  439. (define-key map [switch-frame] #'ignore)
  440. (define-key map [select-window] #'ignore)
  441. (define-key map [scroll-bar-movement] #'ignore)
  442. (define-key map [mouse-movement] move)
  443. ;; Swallow drag-mouse-1 events to avoid selecting some other window.
  444. (define-key map [drag-mouse-1]
  445. (lambda () (interactive) (funcall exitfun)))
  446. ;; For vertical line dragging swallow also a mouse-1
  447. ;; event (but only if we dragged at least once to allow mouse-1
  448. ;; clicks to get through).
  449. (when (eq line 'vertical)
  450. (define-key map [mouse-1]
  451. `(menu-item "" ,(lambda () (interactive) (funcall exitfun))
  452. :filter ,(lambda (cmd) (if dragged cmd)))))
  453. ;; Some of the events will of course end up looked up
  454. ;; with a mode-line, header-line or vertical-line prefix ...
  455. (define-key map [mode-line] map)
  456. (define-key map [header-line] map)
  457. (define-key map [vertical-line] map)
  458. ;; ... and some maybe even with a right- or bottom-divider
  459. ;; prefix.
  460. (define-key map [right-divider] map)
  461. (define-key map [bottom-divider] map)
  462. map)
  463. t (lambda () (setq track-mouse nil)))))))
  464. (defun mouse-drag-mode-line (start-event)
  465. "Change the height of a window by dragging on the mode line."
  466. (interactive "e")
  467. (mouse-drag-line start-event 'mode))
  468. (defun mouse-drag-header-line (start-event)
  469. "Change the height of a window by dragging on the header line."
  470. (interactive "e")
  471. (mouse-drag-line start-event 'header))
  472. (defun mouse-drag-vertical-line (start-event)
  473. "Change the width of a window by dragging on the vertical line."
  474. (interactive "e")
  475. (mouse-drag-line start-event 'vertical))
  476. (defun mouse-set-point (event &optional promote-to-region)
  477. "Move point to the position clicked on with the mouse.
  478. This should be bound to a mouse click event type.
  479. If PROMOTE-TO-REGION is non-nil and event is a multiple-click,
  480. select the corresponding element around point."
  481. (interactive "e\np")
  482. (mouse-minibuffer-check event)
  483. (if (and promote-to-region (> (event-click-count event) 1))
  484. (mouse-set-region event)
  485. ;; Use event-end in case called from mouse-drag-region.
  486. ;; If EVENT is a click, event-end and event-start give same value.
  487. (posn-set-point (event-end event))))
  488. (defvar mouse-last-region-beg nil)
  489. (defvar mouse-last-region-end nil)
  490. (defvar mouse-last-region-tick nil)
  491. (defun mouse-region-match ()
  492. "Return non-nil if there's an active region that was set with the mouse."
  493. (and (mark t) mark-active
  494. (eq mouse-last-region-beg (region-beginning))
  495. (eq mouse-last-region-end (region-end))
  496. (eq mouse-last-region-tick (buffer-modified-tick))))
  497. (defvar mouse--drag-start-event nil)
  498. (defun mouse-set-region (click)
  499. "Set the region to the text dragged over, and copy to kill ring.
  500. This should be bound to a mouse drag event.
  501. See the `mouse-drag-copy-region' variable to control whether this
  502. command alters the kill ring or not."
  503. (interactive "e")
  504. (mouse-minibuffer-check click)
  505. (select-window (posn-window (event-start click)))
  506. (let ((beg (posn-point (event-start click)))
  507. (end (posn-point (event-end click)))
  508. (click-count (event-click-count click)))
  509. (let ((drag-start (terminal-parameter nil 'mouse-drag-start)))
  510. (when drag-start
  511. ;; Drag events don't come with a click count, sadly, so we hack
  512. ;; our way around this problem by remembering the start-event in
  513. ;; `mouse-drag-start' and fetching the click-count from there.
  514. (when (and (<= click-count 1)
  515. (equal beg (posn-point (event-start drag-start))))
  516. (setq click-count (event-click-count drag-start)))
  517. ;; Occasionally we get spurious drag events where the user hasn't
  518. ;; dragged his mouse, but instead Emacs has dragged the text under the
  519. ;; user's mouse. Try to recover those cases (bug#17562).
  520. (when (and (equal (posn-x-y (event-start click))
  521. (posn-x-y (event-end click)))
  522. (not (eq (car drag-start) 'mouse-movement)))
  523. (setq end beg))
  524. (setf (terminal-parameter nil 'mouse-drag-start) nil)))
  525. (when (and (integerp beg) (integerp end))
  526. (let ((range (mouse-start-end beg end (1- click-count))))
  527. (if (< end beg)
  528. (setq end (nth 0 range) beg (nth 1 range))
  529. (setq beg (nth 0 range) end (nth 1 range)))))
  530. (and mouse-drag-copy-region (integerp beg) (integerp end)
  531. ;; Don't set this-command to `kill-region', so a following
  532. ;; C-w won't double the text in the kill ring. Ignore
  533. ;; `last-command' so we don't append to a preceding kill.
  534. (let (this-command last-command deactivate-mark)
  535. (copy-region-as-kill beg end)))
  536. (if (numberp beg) (goto-char beg))
  537. ;; On a text terminal, bounce the cursor.
  538. (or transient-mark-mode
  539. (window-system)
  540. (sit-for 1))
  541. (push-mark)
  542. (set-mark (point))
  543. (if (numberp end) (goto-char end))
  544. (mouse-set-region-1)))
  545. (defun mouse-set-region-1 ()
  546. ;; Set transient-mark-mode for a little while.
  547. (unless (eq (car-safe transient-mark-mode) 'only)
  548. (setq-local transient-mark-mode
  549. (cons 'only
  550. (unless (eq transient-mark-mode 'lambda)
  551. transient-mark-mode))))
  552. (setq mouse-last-region-beg (region-beginning))
  553. (setq mouse-last-region-end (region-end))
  554. (setq mouse-last-region-tick (buffer-modified-tick)))
  555. (defcustom mouse-scroll-delay 0.25
  556. "The pause between scroll steps caused by mouse drags, in seconds.
  557. If you drag the mouse beyond the edge of a window, Emacs scrolls the
  558. window to bring the text beyond that edge into view, with a delay of
  559. this many seconds between scroll steps. Scrolling stops when you move
  560. the mouse back into the window, or release the button.
  561. This variable's value may be non-integral.
  562. Setting this to zero causes Emacs to scroll as fast as it can."
  563. :type 'number
  564. :group 'mouse)
  565. (defcustom mouse-scroll-min-lines 1
  566. "The minimum number of lines scrolled by dragging mouse out of window.
  567. Moving the mouse out the top or bottom edge of the window begins
  568. scrolling repeatedly. The number of lines scrolled per repetition
  569. is normally equal to the number of lines beyond the window edge that
  570. the mouse has moved. However, it always scrolls at least the number
  571. of lines specified by this variable."
  572. :type 'integer
  573. :group 'mouse)
  574. (defun mouse-scroll-subr (window jump &optional overlay start)
  575. "Scroll the window WINDOW, JUMP lines at a time, until new input arrives.
  576. If OVERLAY is an overlay, let it stretch from START to the far edge of
  577. the newly visible text.
  578. Upon exit, point is at the far edge of the newly visible text."
  579. (cond
  580. ((and (> jump 0) (< jump mouse-scroll-min-lines))
  581. (setq jump mouse-scroll-min-lines))
  582. ((and (< jump 0) (< (- jump) mouse-scroll-min-lines))
  583. (setq jump (- mouse-scroll-min-lines))))
  584. (let ((opoint (point)))
  585. (while (progn
  586. (goto-char (window-start window))
  587. (if (not (zerop (vertical-motion jump window)))
  588. (progn
  589. (set-window-start window (point))
  590. (if (natnump jump)
  591. (if (window-end window)
  592. (progn
  593. (goto-char (window-end window))
  594. ;; window-end doesn't reflect the window's new
  595. ;; start position until the next redisplay.
  596. (vertical-motion (1- jump) window))
  597. (vertical-motion (- (window-height window) 2)))
  598. (goto-char (window-start window)))
  599. (if overlay
  600. (move-overlay overlay start (point)))
  601. ;; Now that we have scrolled WINDOW properly,
  602. ;; put point back where it was for the redisplay
  603. ;; so that we don't mess up the selected window.
  604. (or (eq window (selected-window))
  605. (goto-char opoint))
  606. (sit-for mouse-scroll-delay)))))
  607. (or (eq window (selected-window))
  608. (goto-char opoint))))
  609. (defvar mouse-selection-click-count 0)
  610. (defvar mouse-selection-click-count-buffer nil)
  611. (defun mouse-drag-region (start-event)
  612. "Set the region to the text that the mouse is dragged over.
  613. Highlight the drag area as you move the mouse.
  614. This must be bound to a button-down mouse event.
  615. In Transient Mark mode, the highlighting remains as long as the mark
  616. remains active. Otherwise, it remains until the next input event."
  617. (interactive "e")
  618. ;; Give temporary modes such as isearch a chance to turn off.
  619. (run-hooks 'mouse-leave-buffer-hook)
  620. (mouse-drag-track start-event))
  621. (defun mouse-posn-property (pos property)
  622. "Look for a property at click position.
  623. POS may be either a buffer position or a click position like
  624. those returned from `event-start'. If the click position is on
  625. a string, the text property PROPERTY is examined.
  626. If this is nil or the click is not on a string, then
  627. the corresponding buffer position is searched for PROPERTY.
  628. If PROPERTY is encountered in one of those places,
  629. its value is returned."
  630. (if (consp pos)
  631. (let ((w (posn-window pos)) (pt (posn-point pos))
  632. (str (posn-string pos)))
  633. (or (and str
  634. (get-text-property (cdr str) property (car str)))
  635. ;; Mouse clicks in the fringe come with a position in
  636. ;; (nth 5). This is useful but is not exactly where we clicked, so
  637. ;; don't look up that position's properties!
  638. (and pt (not (memq (posn-area pos) '(left-fringe right-fringe
  639. left-margin right-margin)))
  640. (get-char-property pt property w))))
  641. (get-char-property pos property)))
  642. (defun mouse-on-link-p (pos)
  643. "Return non-nil if POS is on a link in the current buffer.
  644. POS must be a buffer position in the current buffer or a mouse
  645. event location in the selected window (see `event-start').
  646. However, if `mouse-1-click-in-non-selected-windows' is non-nil,
  647. POS may be a mouse event location in any window.
  648. A clickable link is identified by one of the following methods:
  649. - If the character at POS has a non-nil `follow-link' text or
  650. overlay property, the value of that property determines what to do.
  651. - If there is a local key-binding or a keybinding at position POS
  652. for the `follow-link' event, the binding of that event determines
  653. what to do.
  654. The resulting value determine whether POS is inside a link:
  655. - If the value is `mouse-face', POS is inside a link if there
  656. is a non-nil `mouse-face' property at POS. Return t in this case.
  657. - If the value is a function, FUNC, POS is inside a link if
  658. the call \(FUNC POS) returns non-nil. Return the return value
  659. from that call. Arg is \(posn-point POS) if POS is a mouse event.
  660. - Otherwise, return the value itself.
  661. The return value is interpreted as follows:
  662. - If it is a string, the mouse-1 event is translated into the
  663. first character of the string, i.e. the action of the mouse-1
  664. click is the local or global binding of that character.
  665. - If it is a vector, the mouse-1 event is translated into the
  666. first element of that vector, i.e. the action of the mouse-1
  667. click is the local or global binding of that event.
  668. - Otherwise, the mouse-1 event is translated into a mouse-2 event
  669. at the same position."
  670. (let ((action
  671. (and (or (not (consp pos))
  672. mouse-1-click-in-non-selected-windows
  673. (eq (selected-window) (posn-window pos)))
  674. (or (mouse-posn-property pos 'follow-link)
  675. (let ((area (posn-area pos)))
  676. (when area
  677. (key-binding (vector area 'follow-link) nil t pos)))
  678. (key-binding [follow-link] nil t pos)))))
  679. (cond
  680. ((eq action 'mouse-face)
  681. (and (mouse-posn-property pos 'mouse-face) t))
  682. ((functionp action)
  683. ;; FIXME: This seems questionable if the click is not in a buffer.
  684. ;; Should we instead decide that `action' takes a `posn'?
  685. (if (consp pos)
  686. (with-current-buffer (window-buffer (posn-window pos))
  687. (funcall action (posn-point pos)))
  688. (funcall action pos)))
  689. (t action))))
  690. (defun mouse-fixup-help-message (msg)
  691. "Fix help message MSG for `mouse-1-click-follows-link'."
  692. (let (mp pos)
  693. (if (and mouse-1-click-follows-link
  694. (stringp msg)
  695. (string-match-p "\\`mouse-2" msg)
  696. (setq mp (mouse-pixel-position))
  697. (consp (setq pos (cdr mp)))
  698. (car pos) (>= (car pos) 0)
  699. (cdr pos) (>= (cdr pos) 0)
  700. (setq pos (posn-at-x-y (car pos) (cdr pos) (car mp)))
  701. (windowp (posn-window pos)))
  702. (with-current-buffer (window-buffer (posn-window pos))
  703. (if (mouse-on-link-p pos)
  704. (setq msg (concat
  705. (cond
  706. ((eq mouse-1-click-follows-link 'double) "double-")
  707. ((and (integerp mouse-1-click-follows-link)
  708. (< mouse-1-click-follows-link 0)) "Long ")
  709. (t ""))
  710. "mouse-1" (substring msg 7)))))))
  711. msg)
  712. (defun mouse-drag-track (start-event)
  713. "Track mouse drags by highlighting area between point and cursor.
  714. The region will be defined with mark and point."
  715. (mouse-minibuffer-check start-event)
  716. (setq mouse-selection-click-count-buffer (current-buffer))
  717. (deactivate-mark)
  718. (let* ((scroll-margin 0) ; Avoid margin scrolling (Bug#9541).
  719. ;; We've recorded what we needed from the current buffer and
  720. ;; window, now let's jump to the place of the event, where things
  721. ;; are happening.
  722. (_ (mouse-set-point start-event))
  723. (echo-keystrokes 0)
  724. (start-posn (event-start start-event))
  725. (start-point (posn-point start-posn))
  726. (start-window (posn-window start-posn))
  727. (bounds (window-edges start-window))
  728. (make-cursor-line-fully-visible nil)
  729. (top (nth 1 bounds))
  730. (bottom (if (window-minibuffer-p start-window)
  731. (nth 3 bounds)
  732. ;; Don't count the mode line.
  733. (1- (nth 3 bounds))))
  734. (click-count (1- (event-click-count start-event)))
  735. ;; Suppress automatic hscrolling, because that is a nuisance
  736. ;; when setting point near the right fringe (but see below).
  737. (auto-hscroll-mode-saved auto-hscroll-mode))
  738. (setq mouse-selection-click-count click-count)
  739. ;; In case the down click is in the middle of some intangible text,
  740. ;; use the end of that text, and put it in START-POINT.
  741. (if (< (point) start-point)
  742. (goto-char start-point))
  743. (setq start-point (point))
  744. ;; Activate the region, using `mouse-start-end' to determine where
  745. ;; to put point and mark (e.g., double-click will select a word).
  746. (setq-local transient-mark-mode
  747. (if (eq transient-mark-mode 'lambda)
  748. '(only)
  749. (cons 'only transient-mark-mode)))
  750. (let ((range (mouse-start-end start-point start-point click-count)))
  751. (push-mark (nth 0 range) t t)
  752. (goto-char (nth 1 range)))
  753. (setf (terminal-parameter nil 'mouse-drag-start) start-event)
  754. (setq track-mouse t)
  755. (setq auto-hscroll-mode nil)
  756. (set-transient-map
  757. (let ((map (make-sparse-keymap)))
  758. (define-key map [switch-frame] #'ignore)
  759. (define-key map [select-window] #'ignore)
  760. (define-key map [mouse-movement]
  761. (lambda (event) (interactive "e")
  762. (let* ((end (event-end event))
  763. (end-point (posn-point end)))
  764. (unless (eq end-point start-point)
  765. ;; As soon as the user moves, we can re-enable auto-hscroll.
  766. (setq auto-hscroll-mode auto-hscroll-mode-saved)
  767. ;; And remember that we have moved, so mouse-set-region can know
  768. ;; its event is really a drag event.
  769. (setcar start-event 'mouse-movement))
  770. (if (and (eq (posn-window end) start-window)
  771. (integer-or-marker-p end-point))
  772. (mouse--drag-set-mark-and-point start-point
  773. end-point click-count)
  774. (let ((mouse-row (cdr (cdr (mouse-position)))))
  775. (cond
  776. ((null mouse-row))
  777. ((< mouse-row top)
  778. (mouse-scroll-subr start-window (- mouse-row top)
  779. nil start-point))
  780. ((>= mouse-row bottom)
  781. (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
  782. nil start-point))))))))
  783. map)
  784. t (lambda ()
  785. (setq track-mouse nil)
  786. (setq auto-hscroll-mode auto-hscroll-mode-saved)
  787. (deactivate-mark)
  788. (pop-mark)))))
  789. (defun mouse--drag-set-mark-and-point (start click click-count)
  790. (let* ((range (mouse-start-end start click click-count))
  791. (beg (nth 0 range))
  792. (end (nth 1 range)))
  793. (cond ((eq (mark) beg)
  794. (goto-char end))
  795. ((eq (mark) end)
  796. (goto-char beg))
  797. ((< click (mark))
  798. (set-mark end)
  799. (goto-char beg))
  800. (t
  801. (set-mark beg)
  802. (goto-char end)))))
  803. ;; Commands to handle xterm-style multiple clicks.
  804. (defun mouse-skip-word (dir)
  805. "Skip over word, over whitespace, or over identical punctuation.
  806. If DIR is positive skip forward; if negative, skip backward."
  807. (let* ((char (following-char))
  808. (syntax (char-to-string (char-syntax char))))
  809. (cond ((string= syntax "w")
  810. ;; Here, we can't use skip-syntax-forward/backward because
  811. ;; they don't pay attention to word-separating-categories,
  812. ;; and thus they will skip over a true word boundary. So,
  813. ;; we simulate the original behavior by using forward-word.
  814. (if (< dir 0)
  815. (if (not (looking-at "\\<"))
  816. (forward-word -1))
  817. (if (or (looking-at "\\<") (not (looking-at "\\>")))
  818. (forward-word 1))))
  819. ((string= syntax " ")
  820. (if (< dir 0)
  821. (skip-syntax-backward syntax)
  822. (skip-syntax-forward syntax)))
  823. ((string= syntax "_")
  824. (if (< dir 0)
  825. (skip-syntax-backward "w_")
  826. (skip-syntax-forward "w_")))
  827. ((< dir 0)
  828. (while (and (not (bobp)) (= (preceding-char) char))
  829. (forward-char -1)))
  830. (t
  831. (while (and (not (eobp)) (= (following-char) char))
  832. (forward-char 1))))))
  833. (defun mouse-start-end (start end mode)
  834. "Return a list of region bounds based on START and END according to MODE.
  835. If MODE is 0 then set point to (min START END), mark to (max START END).
  836. If MODE is 1 then set point to start of word at (min START END),
  837. mark to end of word at (max START END).
  838. If MODE is 2 then do the same for lines."
  839. (if (> start end)
  840. (let ((temp start))
  841. (setq start end
  842. end temp)))
  843. (setq mode (mod mode 3))
  844. (cond ((= mode 0)
  845. (list start end))
  846. ((and (= mode 1)
  847. (= start end)
  848. (char-after start)
  849. (= (char-syntax (char-after start)) ?\())
  850. (list start
  851. (save-excursion
  852. (goto-char start)
  853. (forward-sexp 1)
  854. (point))))
  855. ((and (= mode 1)
  856. (= start end)
  857. (char-after start)
  858. (= (char-syntax (char-after start)) ?\)))
  859. (list (save-excursion
  860. (goto-char (1+ start))
  861. (backward-sexp 1)
  862. (point))
  863. (1+ start)))
  864. ((and (= mode 1)
  865. (= start end)
  866. (char-after start)
  867. (= (char-syntax (char-after start)) ?\"))
  868. (let ((open (or (eq start (point-min))
  869. (save-excursion
  870. (goto-char (- start 1))
  871. (looking-at "\\s(\\|\\s \\|\\s>")))))
  872. (if open
  873. (list start
  874. (save-excursion
  875. (condition-case nil
  876. (progn
  877. (goto-char start)
  878. (forward-sexp 1)
  879. (point))
  880. (error end))))
  881. (list (save-excursion
  882. (condition-case nil
  883. (progn
  884. (goto-char (1+ start))
  885. (backward-sexp 1)
  886. (point))
  887. (error end)))
  888. (1+ start)))))
  889. ((= mode 1)
  890. (list (save-excursion
  891. (goto-char start)
  892. (mouse-skip-word -1)
  893. (point))
  894. (save-excursion
  895. (goto-char end)
  896. (mouse-skip-word 1)
  897. (point))))
  898. ((= mode 2)
  899. (list (save-excursion
  900. (goto-char start)
  901. (line-beginning-position 1))
  902. (save-excursion
  903. (goto-char end)
  904. (forward-line 1)
  905. (point))))))
  906. ;; Subroutine: set the mark where CLICK happened,
  907. ;; but don't do anything else.
  908. (defun mouse-set-mark-fast (click)
  909. (mouse-minibuffer-check click)
  910. (let ((posn (event-start click)))
  911. (select-window (posn-window posn))
  912. (if (numberp (posn-point posn))
  913. (push-mark (posn-point posn) t t))))
  914. (defun mouse-undouble-last-event (events)
  915. (let* ((index (1- (length events)))
  916. (last (nthcdr index events))
  917. (event (car last))
  918. (basic (event-basic-type event))
  919. (old-modifiers (event-modifiers event))
  920. (modifiers (delq 'double (delq 'triple (copy-sequence old-modifiers))))
  921. (new
  922. (if (consp event)
  923. ;; Use reverse, not nreverse, since event-modifiers
  924. ;; does not copy the list it returns.
  925. (cons (event-convert-list (reverse (cons basic modifiers)))
  926. (cdr event))
  927. event)))
  928. (setcar last new)
  929. (if (and (not (equal modifiers old-modifiers))
  930. (key-binding (apply 'vector events)))
  931. t
  932. (setcar last event)
  933. nil)))
  934. ;; Momentarily show where the mark is, if highlighting doesn't show it.
  935. (defun mouse-set-mark (click)
  936. "Set mark at the position clicked on with the mouse.
  937. Display cursor at that position for a second.
  938. This must be bound to a mouse click."
  939. (interactive "e")
  940. (mouse-minibuffer-check click)
  941. (select-window (posn-window (event-start click)))
  942. ;; FIXME: Use save-excursion
  943. (let ((point-save (point)))
  944. (unwind-protect
  945. (progn (mouse-set-point click)
  946. (push-mark nil t t)
  947. (or transient-mark-mode
  948. (sit-for 1)))
  949. (goto-char point-save))))
  950. (defun mouse-kill (click)
  951. "Kill the region between point and the mouse click.
  952. The text is saved in the kill ring, as with \\[kill-region]."
  953. (interactive "e")
  954. (mouse-minibuffer-check click)
  955. (let* ((posn (event-start click))
  956. (click-posn (posn-point posn)))
  957. (select-window (posn-window posn))
  958. (if (numberp click-posn)
  959. (kill-region (min (point) click-posn)
  960. (max (point) click-posn)))))
  961. (defun mouse-yank-at-click (click arg)
  962. "Insert the last stretch of killed text at the position clicked on.
  963. Also move point to one end of the text thus inserted (normally the end),
  964. and set mark at the beginning.
  965. Prefix arguments are interpreted as with \\[yank].
  966. If `mouse-yank-at-point' is non-nil, insert at point
  967. regardless of where you click."
  968. (interactive "e\nP")
  969. ;; Give temporary modes such as isearch a chance to turn off.
  970. (run-hooks 'mouse-leave-buffer-hook)
  971. (when select-active-regions
  972. ;; Without this, confusing things happen upon e.g. inserting into
  973. ;; the middle of an active region.
  974. (deactivate-mark))
  975. (or mouse-yank-at-point (mouse-set-point click))
  976. (setq this-command 'yank)
  977. (setq mouse-selection-click-count 0)
  978. (yank arg))
  979. (defun mouse-yank-primary (click)
  980. "Insert the primary selection at the position clicked on.
  981. Move point to the end of the inserted text, and set mark at
  982. beginning. If `mouse-yank-at-point' is non-nil, insert at point
  983. regardless of where you click."
  984. (interactive "e")
  985. ;; Give temporary modes such as isearch a chance to turn off.
  986. (run-hooks 'mouse-leave-buffer-hook)
  987. ;; Without this, confusing things happen upon e.g. inserting into
  988. ;; the middle of an active region.
  989. (when select-active-regions
  990. (let (select-active-regions)
  991. (deactivate-mark)))
  992. (or mouse-yank-at-point (mouse-set-point click))
  993. (let ((primary (gui-get-primary-selection)))
  994. (push-mark (point))
  995. (insert-for-yank primary)))
  996. (defun mouse-kill-ring-save (click)
  997. "Copy the region between point and the mouse click in the kill ring.
  998. This does not delete the region; it acts like \\[kill-ring-save]."
  999. (interactive "e")
  1000. (mouse-set-mark-fast click)
  1001. (let (this-command last-command)
  1002. (kill-ring-save (point) (mark t))))
  1003. ;; This function used to delete the text between point and the mouse
  1004. ;; whenever it was equal to the front of the kill ring, but some
  1005. ;; people found that confusing.
  1006. ;; The position of the last invocation of `mouse-save-then-kill'.
  1007. (defvar mouse-save-then-kill-posn nil)
  1008. (defun mouse-save-then-kill-delete-region (beg end)
  1009. ;; We must make our own undo boundaries
  1010. ;; because they happen automatically only for the current buffer.
  1011. (undo-boundary)
  1012. (if (or (= beg end) (eq buffer-undo-list t))
  1013. ;; If we have no undo list in this buffer,
  1014. ;; just delete.
  1015. (delete-region beg end)
  1016. ;; Delete, but make the undo-list entry share with the kill ring.
  1017. ;; First, delete just one char, so in case buffer is being modified
  1018. ;; for the first time, the undo list records that fact.
  1019. (let (before-change-functions after-change-functions)
  1020. (delete-region beg
  1021. (+ beg (if (> end beg) 1 -1))))
  1022. (let ((buffer-undo-list buffer-undo-list))
  1023. ;; Undo that deletion--but don't change the undo list!
  1024. (let (before-change-functions after-change-functions)
  1025. (primitive-undo 1 buffer-undo-list))
  1026. ;; Now delete the rest of the specified region,
  1027. ;; but don't record it.
  1028. (setq buffer-undo-list t)
  1029. (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
  1030. (error "Lossage in mouse-save-then-kill-delete-region"))
  1031. (delete-region beg end))
  1032. (let ((tail buffer-undo-list))
  1033. ;; Search back in buffer-undo-list for the string
  1034. ;; that came from deleting one character.
  1035. (while (and tail (not (stringp (car (car tail)))))
  1036. (setq tail (cdr tail)))
  1037. ;; Replace it with an entry for the entire deleted text.
  1038. (and tail
  1039. (setcar tail (cons (car kill-ring) (min beg end))))))
  1040. (undo-boundary))
  1041. (defun mouse-save-then-kill (click)
  1042. "Set the region according to CLICK; the second time, kill it.
  1043. CLICK should be a mouse click event.
  1044. If the region is inactive, activate it temporarily. Set mark at
  1045. the original point, and move point to the position of CLICK.
  1046. If the region is already active, adjust it. Normally, do this by
  1047. moving point or mark, whichever is closer, to CLICK. But if you
  1048. have selected whole words or lines, move point or mark to the
  1049. word or line boundary closest to CLICK instead.
  1050. If `mouse-drag-copy-region' is non-nil, this command also saves the
  1051. new region to the kill ring (replacing the previous kill if the
  1052. previous region was just saved to the kill ring).
  1053. If this command is called a second consecutive time with the same
  1054. CLICK position, kill the region (or delete it
  1055. if `mouse-drag-copy-region' is non-nil)"
  1056. (interactive "e")
  1057. (mouse-minibuffer-check click)
  1058. (let* ((posn (event-start click))
  1059. (click-pt (posn-point posn))
  1060. (window (posn-window posn))
  1061. (buf (window-buffer window))
  1062. ;; Don't let a subsequent kill command append to this one.
  1063. (this-command this-command)
  1064. ;; Check if the user has multi-clicked to select words/lines.
  1065. (click-count
  1066. (if (and (eq mouse-selection-click-count-buffer buf)
  1067. (with-current-buffer buf (mark t)))
  1068. mouse-selection-click-count
  1069. 0)))
  1070. (cond
  1071. ((not (numberp click-pt)) nil)
  1072. ;; If the user clicked without moving point, kill the region.
  1073. ;; This also resets `mouse-selection-click-count'.
  1074. ((and (eq last-command 'mouse-save-then-kill)
  1075. (eq click-pt mouse-save-then-kill-posn)
  1076. (eq window (selected-window)))
  1077. (if mouse-drag-copy-region
  1078. ;; Region already saved in the previous click;
  1079. ;; don't make a duplicate entry, just delete.
  1080. (delete-region (mark t) (point))
  1081. (kill-region (mark t) (point)))
  1082. (setq mouse-selection-click-count 0)
  1083. (setq mouse-save-then-kill-posn nil))
  1084. ;; Otherwise, if there is a suitable region, adjust it by moving
  1085. ;; one end (whichever is closer) to CLICK-PT.
  1086. ((or (with-current-buffer buf (region-active-p))
  1087. (and (eq window (selected-window))
  1088. (mark t)
  1089. (or (and (eq last-command 'mouse-save-then-kill)
  1090. mouse-save-then-kill-posn)
  1091. (and (memq last-command '(mouse-drag-region
  1092. mouse-set-region))
  1093. (or mark-even-if-inactive
  1094. (not transient-mark-mode))))))
  1095. (select-window window)
  1096. (let* ((range (mouse-start-end click-pt click-pt click-count)))
  1097. (if (< (abs (- click-pt (mark t)))
  1098. (abs (- click-pt (point))))
  1099. (set-mark (car range))
  1100. (goto-char (nth 1 range)))
  1101. (setq deactivate-mark nil)
  1102. (mouse-set-region-1)
  1103. (when mouse-drag-copy-region
  1104. ;; Region already copied to kill-ring once, so replace.
  1105. (kill-new (filter-buffer-substring (mark t) (point)) t))
  1106. ;; Arrange for a repeated mouse-3 to kill the region.
  1107. (setq mouse-save-then-kill-posn click-pt)))
  1108. ;; Otherwise, set the mark where point is and move to CLICK-PT.
  1109. (t
  1110. (select-window window)
  1111. (mouse-set-mark-fast click)
  1112. (let ((before-scroll (with-current-buffer buf point-before-scroll)))
  1113. (if before-scroll (goto-char before-scroll)))
  1114. (exchange-point-and-mark)
  1115. (mouse-set-region-1)
  1116. (when mouse-drag-copy-region
  1117. (kill-new (filter-buffer-substring (mark t) (point))))
  1118. (setq mouse-save-then-kill-posn click-pt)))))
  1119. (global-set-key [M-mouse-1] 'mouse-start-secondary)
  1120. (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
  1121. (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
  1122. (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
  1123. (global-set-key [M-mouse-2] 'mouse-yank-secondary)
  1124. (defconst mouse-secondary-overlay
  1125. (let ((ol (make-overlay (point-min) (point-min))))
  1126. (delete-overlay ol)
  1127. (overlay-put ol 'face 'secondary-selection)
  1128. ol)
  1129. "An overlay which records the current secondary selection.
  1130. It is deleted when there is no secondary selection.")
  1131. (defvar mouse-secondary-click-count 0)
  1132. ;; A marker which records the specified first end for a secondary selection.
  1133. ;; May be nil.
  1134. (defvar mouse-secondary-start nil)
  1135. (defun mouse-start-secondary (click)
  1136. "Set one end of the secondary selection to the position clicked on.
  1137. Use \\[mouse-secondary-save-then-kill] to set the other end
  1138. and complete the secondary selection."
  1139. (interactive "e")
  1140. (mouse-minibuffer-check click)
  1141. (let ((posn (event-start click)))
  1142. (with-current-buffer (window-buffer (posn-window posn))
  1143. ;; Cancel any preexisting secondary selection.
  1144. (delete-overlay mouse-secondary-overlay)
  1145. (if (numberp (posn-point posn))
  1146. (progn
  1147. (or mouse-secondary-start
  1148. (setq mouse-secondary-start (make-marker)))
  1149. (move-marker mouse-secondary-start (posn-point posn)))))))
  1150. (defun mouse-set-secondary (click)
  1151. "Set the secondary selection to the text that the mouse is dragged over.
  1152. This must be bound to a mouse drag event."
  1153. (interactive "e")
  1154. (mouse-minibuffer-check click)
  1155. (let ((posn (event-start click))
  1156. beg
  1157. (end (event-end click)))
  1158. (with-current-buffer (window-buffer (posn-window posn))
  1159. (if (numberp (posn-point posn))
  1160. (setq beg (posn-point posn)))
  1161. (move-overlay mouse-secondary-overlay beg (posn-point end))
  1162. (gui-set-selection
  1163. 'SECONDARY
  1164. (buffer-substring (overlay-start mouse-secondary-overlay)
  1165. (overlay-end mouse-secondary-overlay))))))
  1166. (defun mouse-drag-secondary (start-event)
  1167. "Set the secondary selection to the text that the mouse is dragged over.
  1168. Highlight the drag area as you move the mouse.
  1169. This must be bound to a button-down mouse event.
  1170. The function returns a non-nil value if it creates a secondary selection."
  1171. (interactive "e")
  1172. (mouse-minibuffer-check start-event)
  1173. (let* ((echo-keystrokes 0)
  1174. (start-posn (event-start start-event))
  1175. (start-point (posn-point start-posn))
  1176. (start-window (posn-window start-posn))
  1177. (bounds (window-edges start-window))
  1178. (top (nth 1 bounds))
  1179. (bottom (if (window-minibuffer-p start-window)
  1180. (nth 3 bounds)
  1181. ;; Don't count the mode line.
  1182. (1- (nth 3 bounds))))
  1183. (click-count (1- (event-click-count start-event))))
  1184. (with-current-buffer (window-buffer start-window)
  1185. (setq mouse-secondary-click-count click-count)
  1186. (if (> (mod click-count 3) 0)
  1187. ;; Double or triple press: make an initial selection
  1188. ;; of one word or line.
  1189. (let ((range (mouse-start-end start-point start-point click-count)))
  1190. (set-marker mouse-secondary-start nil)
  1191. (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
  1192. (window-buffer start-window)))
  1193. ;; Single-press: cancel any preexisting secondary selection.
  1194. (or mouse-secondary-start
  1195. (setq mouse-secondary-start (make-marker)))
  1196. (set-marker mouse-secondary-start start-point)
  1197. (delete-overlay mouse-secondary-overlay))
  1198. ;; FIXME: Use mouse-drag-track!
  1199. (let (event end end-point)
  1200. (track-mouse
  1201. (while (progn
  1202. (setq event (read-event))
  1203. (or (mouse-movement-p event)
  1204. (memq (car-safe event) '(switch-frame select-window))))
  1205. (if (memq (car-safe event) '(switch-frame select-window))
  1206. nil
  1207. (setq end (event-end event)
  1208. end-point (posn-point end))
  1209. (cond
  1210. ;; Are we moving within the original window?
  1211. ((and (eq (posn-window end) start-window)
  1212. (integer-or-marker-p end-point))
  1213. (let ((range (mouse-start-end start-point end-point
  1214. click-count)))
  1215. (if (or (/= start-point end-point)
  1216. (null (marker-position mouse-secondary-start)))
  1217. (progn
  1218. (set-marker mouse-secondary-start nil)
  1219. (move-overlay mouse-secondary-overlay
  1220. (car range) (nth 1 range))))))
  1221. (t
  1222. (let ((mouse-row (cdr (cdr (mouse-position)))))
  1223. (cond
  1224. ((null mouse-row))
  1225. ((< mouse-row top)
  1226. (mouse-scroll-subr start-window (- mouse-row top)
  1227. mouse-secondary-overlay start-point))
  1228. ((>= mouse-row bottom)
  1229. (mouse-scroll-subr start-window (1+ (- mouse-row bottom))
  1230. mouse-secondary-overlay start-point)))))))))
  1231. (if (consp event)
  1232. (if (marker-position mouse-secondary-start)
  1233. (save-window-excursion
  1234. (delete-overlay mouse-secondary-overlay)
  1235. (gui-set-selection 'SECONDARY nil)
  1236. (select-window start-window)
  1237. (save-excursion
  1238. (goto-char mouse-secondary-start)
  1239. (sit-for 1)
  1240. nil))
  1241. (gui-set-selection
  1242. 'SECONDARY
  1243. (buffer-substring (overlay-start mouse-secondary-overlay)
  1244. (overlay-end mouse-secondary-overlay)))))))))
  1245. (defun mouse-yank-secondary (click)
  1246. "Insert the secondary selection at the position clicked on.
  1247. Move point to the end of the inserted text.
  1248. If `mouse-yank-at-point' is non-nil, insert at point
  1249. regardless of where you click."
  1250. (interactive "e")
  1251. ;; Give temporary modes such as isearch a chance to turn off.
  1252. (run-hooks 'mouse-leave-buffer-hook)
  1253. (or mouse-yank-at-point (mouse-set-point click))
  1254. (let ((secondary (gui-get-selection 'SECONDARY)))
  1255. (if secondary
  1256. (insert-for-yank secondary)
  1257. (error "No secondary selection"))))
  1258. (defun mouse-kill-secondary ()
  1259. "Kill the text in the secondary selection.
  1260. This is intended more as a keyboard command than as a mouse command
  1261. but it can work as either one.
  1262. The current buffer (in case of keyboard use), or the buffer clicked on,
  1263. must be the one that the secondary selection is in. This requirement
  1264. is to prevent accidents."
  1265. (interactive)
  1266. (let* ((keys (this-command-keys))
  1267. (click (elt keys (1- (length keys)))))
  1268. (or (eq (overlay-buffer mouse-secondary-overlay)
  1269. (if (listp click)
  1270. (window-buffer (posn-window (event-start click)))
  1271. (current-buffer)))
  1272. (error "Select or click on the buffer where the secondary selection is")))
  1273. (let (this-command)
  1274. (with-current-buffer (overlay-buffer mouse-secondary-overlay)
  1275. (kill-region (overlay-start mouse-secondary-overlay)
  1276. (overlay-end mouse-secondary-overlay))))
  1277. (delete-overlay mouse-secondary-overlay))
  1278. (defun mouse-secondary-save-then-kill (click)
  1279. "Set the secondary selection and save it to the kill ring.
  1280. The second time, kill it. CLICK should be a mouse click event.
  1281. If you have not called `mouse-start-secondary' in the clicked
  1282. buffer, activate the secondary selection and set it between point
  1283. and the click position CLICK.
  1284. Otherwise, adjust the bounds of the secondary selection.
  1285. Normally, do this by moving its beginning or end, whichever is
  1286. closer, to CLICK. But if you have selected whole words or lines,
  1287. adjust to the word or line boundary closest to CLICK instead.
  1288. If this command is called a second consecutive time with the same
  1289. CLICK position, kill the secondary selection."
  1290. (interactive "e")
  1291. (mouse-minibuffer-check click)
  1292. (let* ((posn (event-start click))
  1293. (click-pt (posn-point posn))
  1294. (window (posn-window posn))
  1295. (buf (window-buffer window))
  1296. ;; Don't let a subsequent kill command append to this one.
  1297. (this-command this-command)
  1298. ;; Check if the user has multi-clicked to select words/lines.
  1299. (click-count
  1300. (if (eq (overlay-buffer mouse-secondary-overlay) buf)
  1301. mouse-secondary-click-count
  1302. 0))
  1303. (beg (overlay-start mouse-secondary-overlay))
  1304. (end (overlay-end mouse-secondary-overlay)))
  1305. (cond
  1306. ((not (numberp click-pt)) nil)
  1307. ;; If the secondary selection is not active in BUF, activate it.
  1308. ((not (eq buf (or (overlay-buffer mouse-secondary-overlay)
  1309. (if mouse-secondary-start
  1310. (marker-buffer mouse-secondary-start)))))
  1311. (select-window window)
  1312. (setq mouse-secondary-start (make-marker))
  1313. (move-marker mouse-secondary-start (point))
  1314. (move-overlay mouse-secondary-overlay (point) click-pt buf)
  1315. (kill-ring-save (point) click-pt))
  1316. ;; If the user clicked without moving point, delete the secondary
  1317. ;; selection. This also resets `mouse-secondary-click-count'.
  1318. ((and (eq last-command 'mouse-secondary-save-then-kill)
  1319. (eq click-pt mouse-save-then-kill-posn)
  1320. (eq window (selected-window)))
  1321. (mouse-save-then-kill-delete-region beg end)
  1322. (delete-overlay mouse-secondary-overlay)
  1323. (setq mouse-secondary-click-count 0)
  1324. (setq mouse-save-then-kill-posn nil))
  1325. ;; Otherwise, if there is a suitable secondary selection overlay,
  1326. ;; adjust it by moving one end (whichever is closer) to CLICK-PT.
  1327. ((and beg (eq buf (overlay-buffer mouse-secondary-overlay)))
  1328. (let* ((range (mouse-start-end click-pt click-pt click-count)))
  1329. (if (< (abs (- click-pt beg))
  1330. (abs (- click-pt end)))
  1331. (move-overlay mouse-secondary-overlay (car range) end)
  1332. (move-overlay mouse-secondary-overlay beg (nth 1 range))))
  1333. (setq deactivate-mark nil)
  1334. (if (eq last-command 'mouse-secondary-save-then-kill)
  1335. ;; If the front of the kill ring comes from an immediately
  1336. ;; previous use of this command, replace the entry.
  1337. (kill-new
  1338. (buffer-substring (overlay-start mouse-secondary-overlay)
  1339. (overlay-end mouse-secondary-overlay))
  1340. t)
  1341. (let (deactivate-mark)
  1342. (copy-region-as-kill (overlay-start mouse-secondary-overlay)
  1343. (overlay-end mouse-secondary-overlay))))
  1344. (setq mouse-save-then-kill-posn click-pt))
  1345. ;; Otherwise, set the secondary selection overlay.
  1346. (t
  1347. (select-window window)
  1348. (if mouse-secondary-start
  1349. ;; All we have is one end of a selection, so put the other
  1350. ;; end here.
  1351. (let ((start (+ 0 mouse-secondary-start)))
  1352. (kill-ring-save start click-pt)
  1353. (move-overlay mouse-secondary-overlay start click-pt)))
  1354. (setq mouse-save-then-kill-posn click-pt))))
  1355. ;; Finally, set the window system's secondary selection.
  1356. (let (str)
  1357. (and (overlay-buffer mouse-secondary-overlay)
  1358. (setq str (buffer-substring (overlay-start mouse-secondary-overlay)
  1359. (overlay-end mouse-secondary-overlay)))
  1360. (> (length str) 0)
  1361. (gui-set-selection 'SECONDARY str))))
  1362. (defcustom mouse-buffer-menu-maxlen 20
  1363. "Number of buffers in one pane (submenu) of the buffer menu.
  1364. If we have lots of buffers, divide them into groups of
  1365. `mouse-buffer-menu-maxlen' and make a pane (or submenu) for each one."
  1366. :type 'integer
  1367. :group 'mouse)
  1368. (defcustom mouse-buffer-menu-mode-mult 4
  1369. "Group the buffers by the major mode groups on \\[mouse-buffer-menu]?
  1370. This number which determines (in a hairy way) whether \\[mouse-buffer-menu]
  1371. will split the buffer menu by the major modes (see
  1372. `mouse-buffer-menu-mode-groups') or just by menu length.
  1373. Set to 1 (or even 0!) if you want to group by major mode always, and to
  1374. a large number if you prefer a mixed multitude. The default is 4."
  1375. :type 'integer
  1376. :group 'mouse
  1377. :version "20.3")
  1378. (defvar mouse-buffer-menu-mode-groups
  1379. (mapcar (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
  1380. '(("Info\\|Help\\|Apropos\\|Man" . "Help")
  1381. ("\\bVM\\b\\|\\bMH\\b\\|Message\\|Mail\\|Group\\|Score\\|Summary\\|Article"
  1382. . "Mail/News")
  1383. ("\\<C\\>" . "C")
  1384. ("ObjC" . "C")
  1385. ("Text" . "Text")
  1386. ("Outline" . "Text")
  1387. ("\\(HT\\|SG\\|X\\|XHT\\)ML" . "SGML")
  1388. ("log\\|diff\\|vc\\|cvs\\|Annotate" . "Version Control") ; "Change Management"?
  1389. ("Threads\\|Memory\\|Disassembly\\|Breakpoints\\|Frames\\|Locals\\|Registers\\|Inferior I/O\\|Debugger"
  1390. . "GDB")
  1391. ("Lisp" . "Lisp")))
  1392. "How to group various major modes together in \\[mouse-buffer-menu].
  1393. Each element has the form (REGEXP . GROUPNAME).
  1394. If the major mode's name string matches REGEXP, use GROUPNAME instead.")
  1395. (defun mouse-buffer-menu (event)
  1396. "Pop up a menu of buffers for selection with the mouse.
  1397. This switches buffers in the window that you clicked on,
  1398. and selects that window."
  1399. (interactive "e")
  1400. (mouse-minibuffer-check event)
  1401. (let ((buf (x-popup-menu event (mouse-buffer-menu-map)))
  1402. (window (posn-window (event-start event))))
  1403. (when buf
  1404. (select-window
  1405. (if (framep window) (frame-selected-window window)
  1406. window))
  1407. (switch-to-buffer buf))))
  1408. (defun mouse-buffer-menu-map ()
  1409. ;; Make an alist of elements that look like (MENU-ITEM . BUFFER).
  1410. (let ((buffers (buffer-list)) split-by-major-mode sum-of-squares)
  1411. (dolist (buf buffers)
  1412. ;; Divide all buffers into buckets for various major modes.
  1413. ;; Each bucket looks like (MODE NAMESTRING BUFFERS...).
  1414. (with-current-buffer buf
  1415. (let* ((adjusted-major-mode major-mode) elt)
  1416. (dolist (group mouse-buffer-menu-mode-groups)
  1417. (when (string-match (car group) (format-mode-line mode-name))
  1418. (setq adjusted-major-mode (cdr group))))
  1419. (setq elt (assoc adjusted-major-mode split-by-major-mode))
  1420. (unless elt
  1421. (setq elt (list adjusted-major-mode
  1422. (if (stringp adjusted-major-mode)
  1423. adjusted-major-mode
  1424. (format-mode-line mode-name nil nil buf)))
  1425. split-by-major-mode (cons elt split-by-major-mode)))
  1426. (or (memq buf (cdr (cdr elt)))
  1427. (setcdr (cdr elt) (cons buf (cdr (cdr elt))))))))
  1428. ;; Compute the sum of squares of sizes of the major-mode buckets.
  1429. (let ((tail split-by-major-mode))
  1430. (setq sum-of-squares 0)
  1431. (while tail
  1432. (setq sum-of-squares
  1433. (+ sum-of-squares
  1434. (let ((len (length (cdr (cdr (car tail)))))) (* len len))))
  1435. (setq tail (cdr tail))))
  1436. (if (< (* sum-of-squares mouse-buffer-menu-mode-mult)
  1437. (* (length buffers) (length buffers)))
  1438. ;; Subdividing by major modes really helps, so let's do it.
  1439. (let (subdivided-menus (buffers-left (length buffers)))
  1440. ;; Sort the list to put the most popular major modes first.
  1441. (setq split-by-major-mode
  1442. (sort split-by-major-mode
  1443. (function (lambda (elt1 elt2)
  1444. (> (length elt1) (length elt2))))))
  1445. ;; Make a separate submenu for each major mode
  1446. ;; that has more than one buffer,
  1447. ;; unless all the remaining buffers are less than 1/10 of them.
  1448. (while (and split-by-major-mode
  1449. (and (> (length (car split-by-major-mode)) 3)
  1450. (> (* buffers-left 10) (length buffers))))
  1451. (let ((this-mode-list (mouse-buffer-menu-alist
  1452. (cdr (cdr (car split-by-major-mode))))))
  1453. (and this-mode-list
  1454. (setq subdivided-menus
  1455. (cons (cons
  1456. (nth 1 (car split-by-major-mode))
  1457. this-mode-list)
  1458. subdivided-menus))))
  1459. (setq buffers-left
  1460. (- buffers-left (length (cdr (car split-by-major-mode)))))
  1461. (setq split-by-major-mode (cdr split-by-major-mode)))
  1462. ;; If any major modes are left over,
  1463. ;; make a single submenu for them.
  1464. (if split-by-major-mode
  1465. (let ((others-list
  1466. (mouse-buffer-menu-alist
  1467. ;; we don't need split-by-major-mode any more,
  1468. ;; so we can ditch it with nconc.
  1469. (apply 'nconc (mapcar 'cddr split-by-major-mode)))))
  1470. (and others-list
  1471. (setq subdivided-menus
  1472. (cons (cons "Others" others-list)
  1473. subdivided-menus)))))
  1474. (cons "Buffer Menu" (nreverse subdivided-menus)))
  1475. (cons "Buffer Menu"
  1476. (mouse-buffer-menu-split "Select Buffer"
  1477. (mouse-buffer-menu-alist buffers))))))
  1478. (defun mouse-buffer-menu-alist (buffers)
  1479. (let (tail
  1480. (maxlen 0)
  1481. head)
  1482. (setq buffers
  1483. (sort buffers
  1484. (function (lambda (elt1 elt2)
  1485. (string< (buffer-name elt1) (buffer-name elt2))))))
  1486. (setq tail buffers)
  1487. (while tail
  1488. (or (eq ?\s (aref (buffer-name (car tail)) 0))
  1489. (setq maxlen
  1490. (max maxlen
  1491. (length (buffer-name (car tail))))))
  1492. (setq tail (cdr tail)))
  1493. (setq tail buffers)
  1494. (while tail
  1495. (let ((elt (car tail)))
  1496. (if (/= (aref (buffer-name elt) 0) ?\s)
  1497. (setq head
  1498. (cons
  1499. (cons
  1500. (format
  1501. (format "%%-%ds %%s%%s %%s" maxlen)
  1502. (buffer-name elt)
  1503. (if (buffer-modified-p elt) "*" " ")
  1504. (with-current-buffer elt
  1505. (if buffer-read-only "%" " "))
  1506. (or (buffer-file-name elt)
  1507. (with-current-buffer elt
  1508. (if list-buffers-directory
  1509. (expand-file-name
  1510. list-buffers-directory)))
  1511. ""))
  1512. elt)
  1513. head))))
  1514. (setq tail (cdr tail)))
  1515. ;; Compensate for the reversal that the above loop does.
  1516. (nreverse head)))
  1517. (defun mouse-buffer-menu-split (title alist)
  1518. ;; If we have lots of buffers, divide them into groups of 20
  1519. ;; and make a pane (or submenu) for each one.
  1520. (if (> (length alist) (/ (* mouse-buffer-menu-maxlen 3) 2))
  1521. (let ((alist alist) sublists next
  1522. (i 1))
  1523. (while alist
  1524. ;; Pull off the next mouse-buffer-menu-maxlen buffers
  1525. ;; and make them the next element of sublist.
  1526. (setq next (nthcdr mouse-buffer-menu-maxlen alist))
  1527. (if next
  1528. (setcdr (nthcdr (1- mouse-buffer-menu-maxlen) alist)
  1529. nil))
  1530. (setq sublists (cons (cons (format "Buffers %d" i) alist)
  1531. sublists))
  1532. (setq i (1+ i))
  1533. (setq alist next))
  1534. (nreverse sublists))
  1535. ;; Few buffers--put them all in one pane.
  1536. (list (cons title alist))))
  1537. (define-obsolete-function-alias
  1538. 'mouse-choose-completion 'choose-completion "23.2")
  1539. ;; Font selection.
  1540. (defun font-menu-add-default ()
  1541. (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
  1542. (font-alist x-fixed-font-alist)
  1543. (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
  1544. (if (assoc "Default" elt)
  1545. (delete (assoc "Default" elt) elt))
  1546. (setcdr elt
  1547. (cons (list "Default" default)
  1548. (cdr elt)))))
  1549. (defvar x-fixed-font-alist
  1550. (list
  1551. (purecopy "Font Menu")
  1552. (cons
  1553. (purecopy "Misc")
  1554. (mapcar
  1555. (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
  1556. ;; For these, we specify the pixel height and width.
  1557. '(("fixed" "fixed")
  1558. ("6x10" "-misc-fixed-medium-r-normal--10-*-*-*-c-60-iso8859-1" "6x10")
  1559. ("6x12"
  1560. "-misc-fixed-medium-r-semicondensed--12-*-*-*-c-60-iso8859-1" "6x12")
  1561. ("6x13"
  1562. "-misc-fixed-medium-r-semicondensed--13-*-*-*-c-60-iso8859-1" "6x13")
  1563. ("7x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-70-iso8859-1" "7x13")
  1564. ("7x14" "-misc-fixed-medium-r-normal--14-*-*-*-c-70-iso8859-1" "7x14")
  1565. ("8x13" "-misc-fixed-medium-r-normal--13-*-*-*-c-80-iso8859-1" "8x13")
  1566. ("9x15" "-misc-fixed-medium-r-normal--15-*-*-*-c-90-iso8859-1" "9x15")
  1567. ("10x20" "-misc-fixed-medium-r-normal--20-*-*-*-c-100-iso8859-1" "10x20")
  1568. ("11x18" "-misc-fixed-medium-r-normal--18-*-*-*-c-110-iso8859-1" "11x18")
  1569. ("12x24" "-misc-fixed-medium-r-normal--24-*-*-*-c-120-iso8859-1" "12x24")
  1570. ("")
  1571. ("clean 5x8"
  1572. "-schumacher-clean-medium-r-normal--8-*-*-*-c-50-iso8859-1")
  1573. ("clean 6x8"
  1574. "-schumacher-clean-medium-r-normal--8-*-*-*-c-60-iso8859-1")
  1575. ("clean 8x8"
  1576. "-schumacher-clean-medium-r-normal--8-*-*-*-c-80-iso8859-1")
  1577. ("clean 8x10"
  1578. "-schumacher-clean-medium-r-normal--10-*-*-*-c-80-iso8859-1")
  1579. ("clean 8x14"
  1580. "-schumacher-clean-medium-r-normal--14-*-*-*-c-80-iso8859-1")
  1581. ("clean 8x16"
  1582. "-schumacher-clean-medium-r-normal--16-*-*-*-c-80-iso8859-1")
  1583. ("")
  1584. ("sony 8x16" "-sony-fixed-medium-r-normal--16-*-*-*-c-80-iso8859-1")
  1585. ;; We don't seem to have these; who knows what they are.
  1586. ;; ("fg-18" "fg-18")
  1587. ;; ("fg-25" "fg-25")
  1588. ("lucidasanstypewriter-12" "-b&h-lucidatypewriter-medium-r-normal-sans-*-120-*-*-*-*-iso8859-1")
  1589. ("lucidasanstypewriter-bold-14" "-b&h-lucidatypewriter-bold-r-normal-sans-*-140-*-*-*-*-iso8859-1")
  1590. ("lucidasanstypewriter-bold-24"
  1591. "-b&h-lucidatypewriter-bold-r-normal-sans-*-240-*-*-*-*-iso8859-1")
  1592. ;; ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
  1593. ;; ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
  1594. )))
  1595. (cons
  1596. (purecopy "Courier")
  1597. (mapcar
  1598. (lambda (arg) (cons (purecopy (car arg)) (purecopy (cdr arg))))
  1599. ;; For these, we specify the point height.
  1600. '(("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
  1601. ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
  1602. ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
  1603. ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
  1604. ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
  1605. ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
  1606. ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
  1607. ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
  1608. ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
  1609. ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
  1610. ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
  1611. ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
  1612. ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
  1613. ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
  1614. ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
  1615. ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
  1616. ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
  1617. ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
  1618. ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
  1619. ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
  1620. ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
  1621. ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
  1622. ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
  1623. ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1")
  1624. ))))
  1625. "X fonts suitable for use in Emacs.")
  1626. (declare-function generate-fontset-menu "fontset" ())
  1627. (defun mouse-select-font ()
  1628. "Prompt for a font name, using `x-popup-menu', and return it."
  1629. (interactive)
  1630. (unless (display-multi-font-p)
  1631. (error "Cannot change fonts on this display"))
  1632. (car
  1633. (x-popup-menu
  1634. (if (listp last-nonmenu-event)
  1635. last-nonmenu-event
  1636. (list '(0 0) (selected-window)))
  1637. (append x-fixed-font-alist
  1638. (list (generate-fontset-menu))))))
  1639. (declare-function text-scale-mode "face-remap")
  1640. (defun mouse-set-font (&rest fonts)
  1641. "Set the default font for the selected frame.
  1642. The argument FONTS is a list of font names; the first valid font
  1643. in this list is used.
  1644. When called interactively, pop up a menu and allow the user to
  1645. choose a font."
  1646. (interactive
  1647. (progn (unless (display-multi-font-p)
  1648. (error "Cannot change fonts on this display"))
  1649. (x-popup-menu
  1650. (if (listp last-nonmenu-event)
  1651. last-nonmenu-event
  1652. (list '(0 0) (selected-window)))
  1653. ;; Append list of fontsets currently defined.
  1654. (append x-fixed-font-alist (list (generate-fontset-menu))))))
  1655. (if fonts
  1656. (let (font)
  1657. (while fonts
  1658. (condition-case nil
  1659. (progn
  1660. (set-frame-font (car fonts))
  1661. (setq font (car fonts))
  1662. (setq fonts nil))
  1663. (error
  1664. (setq fonts (cdr fonts)))))
  1665. (if (null font)
  1666. (error "Font not found")))))
  1667. (defvar mouse-appearance-menu-map nil)
  1668. (declare-function x-select-font "xfns.c" (&optional frame ignored)) ; USE_GTK
  1669. (declare-function buffer-face-mode-invoke "face-remap"
  1670. (face arg &optional interactive))
  1671. (declare-function font-face-attributes "font.c" (font &optional frame))
  1672. (defvar w32-use-w32-font-dialog)
  1673. (defvar w32-fixed-font-alist)
  1674. (defun mouse-appearance-menu (event)
  1675. "Show a menu for changing the default face in the current buffer."
  1676. (interactive "@e")
  1677. (require 'face-remap)
  1678. (when (display-multi-font-p)
  1679. (with-selected-window (car (event-start event))
  1680. (if mouse-appearance-menu-map
  1681. nil ; regenerate new fonts
  1682. ;; Initialize mouse-appearance-menu-map
  1683. (setq mouse-appearance-menu-map
  1684. (make-sparse-keymap "Change Default Buffer Face"))
  1685. (define-key mouse-appearance-menu-map [face-remap-reset-base]
  1686. '(menu-item "Reset to Default" face-remap-reset-base))
  1687. (define-key mouse-appearance-menu-map [text-scale-decrease]
  1688. '(menu-item "Decrease Buffer Text Size" text-scale-decrease))
  1689. (define-key mouse-appearance-menu-map [text-scale-increase]
  1690. '(menu-item "Increase Buffer Text Size" text-scale-increase))
  1691. ;; Font selector
  1692. (if (and (functionp 'x-select-font)
  1693. (or (not (boundp 'w32-use-w32-font-dialog))
  1694. w32-use-w32-font-dialog))
  1695. (define-key mouse-appearance-menu-map [x-select-font]
  1696. '(menu-item "Change Buffer Font..." x-select-font))
  1697. ;; If the select-font is unavailable, construct a menu.
  1698. (let ((font-submenu (make-sparse-keymap "Change Text Font"))
  1699. (font-alist (cdr (append
  1700. (if (eq system-type 'windows-nt)
  1701. w32-fixed-font-alist
  1702. x-fixed-font-alist)
  1703. (list (generate-fontset-menu))))))
  1704. (dolist (family font-alist)
  1705. (let* ((submenu-name (car family))
  1706. (submenu-map (make-sparse-keymap submenu-name)))
  1707. (dolist (font (cdr family))
  1708. (let ((font-name (car font))
  1709. font-symbol)
  1710. (if (string= font-name "")
  1711. (define-key submenu-map [space]
  1712. '("--"))
  1713. (setq font-symbol (intern (cadr font)))
  1714. (define-key submenu-map (vector font-symbol)
  1715. (list 'menu-item (car font) font-symbol)))))
  1716. (define-key font-submenu (vector (intern submenu-name))
  1717. (list 'menu-item submenu-name submenu-map))))
  1718. (define-key mouse-appearance-menu-map [font-submenu]
  1719. (list 'menu-item "Change Text Font" font-submenu)))))
  1720. (let ((choice (x-popup-menu event mouse-appearance-menu-map)))
  1721. (setq choice (nth (1- (length choice)) choice))
  1722. (cond ((eq choice 'text-scale-increase)
  1723. (text-scale-increase 1))
  1724. ((eq choice 'text-scale-decrease)
  1725. (text-scale-increase -1))
  1726. ((eq choice 'face-remap-reset-base)
  1727. (text-scale-mode 0)
  1728. (buffer-face-mode 0))
  1729. (choice
  1730. ;; Either choice == 'x-select-font, or choice is a
  1731. ;; symbol whose name is a font.
  1732. (let ((font (if (eq choice 'x-select-font)
  1733. (x-select-font)
  1734. (symbol-name choice))))
  1735. (buffer-face-mode-invoke
  1736. (if (fontp font 'font-spec)
  1737. (list :font font)
  1738. (font-face-attributes font))
  1739. t (called-interactively-p 'interactive)))))))))
  1740. ;;; Bindings for mouse commands.
  1741. (global-set-key [down-mouse-1] 'mouse-drag-region)
  1742. (global-set-key [mouse-1] 'mouse-set-point)
  1743. (global-set-key [drag-mouse-1] 'mouse-set-region)
  1744. (defun mouse--strip-first-event (_prompt)
  1745. (substring (this-single-command-raw-keys) 1))
  1746. (define-key function-key-map [left-fringe mouse-1] 'mouse--strip-first-event)
  1747. (define-key function-key-map [right-fringe mouse-1] 'mouse--strip-first-event)
  1748. (global-set-key [mouse-2] 'mouse-yank-primary)
  1749. ;; Allow yanking also when the corresponding cursor is "in the fringe".
  1750. (define-key function-key-map [right-fringe mouse-2] 'mouse--strip-first-event)
  1751. (define-key function-key-map [left-fringe mouse-2] 'mouse--strip-first-event)
  1752. (global-set-key [mouse-3] 'mouse-save-then-kill)
  1753. (define-key function-key-map [right-fringe mouse-3] 'mouse--strip-first-event)
  1754. (define-key function-key-map [left-fringe mouse-3] 'mouse--strip-first-event)
  1755. ;; By binding these to down-going events, we let the user use the up-going
  1756. ;; event to make the selection, saving a click.
  1757. (global-set-key [C-down-mouse-1] 'mouse-buffer-menu)
  1758. (if (not (eq system-type 'ms-dos))
  1759. (global-set-key [S-down-mouse-1] 'mouse-appearance-menu))
  1760. ;; C-down-mouse-2 is bound in facemenu.el.
  1761. (global-set-key [C-down-mouse-3]
  1762. `(menu-item ,(purecopy "Menu Bar") ignore
  1763. :filter (lambda (_)
  1764. (if (zerop (or (frame-parameter nil 'menu-bar-lines) 0))
  1765. (mouse-menu-bar-map)
  1766. (mouse-menu-major-mode-map)))))
  1767. ;; Binding mouse-1 to mouse-select-window when on mode-, header-, or
  1768. ;; vertical-line prevents Emacs from signaling an error when the mouse
  1769. ;; button is released after dragging these lines, on non-toolkit
  1770. ;; versions.
  1771. (global-set-key [header-line down-mouse-1] 'mouse-drag-header-line)
  1772. (global-set-key [header-line mouse-1] 'mouse-select-window)
  1773. ;; (global-set-key [mode-line drag-mouse-1] 'mouse-select-window)
  1774. (global-set-key [mode-line down-mouse-1] 'mouse-drag-mode-line)
  1775. (global-set-key [mode-line mouse-1] 'mouse-select-window)
  1776. (global-set-key [mode-line mouse-2] 'mouse-delete-other-windows)
  1777. (global-set-key [mode-line mouse-3] 'mouse-delete-window)
  1778. (global-set-key [mode-line C-mouse-2] 'mouse-split-window-horizontally)
  1779. (global-set-key [vertical-scroll-bar C-mouse-2] 'mouse-split-window-vertically)
  1780. (global-set-key [horizontal-scroll-bar C-mouse-2] 'mouse-split-window-horizontally)
  1781. (global-set-key [vertical-line down-mouse-1] 'mouse-drag-vertical-line)
  1782. (global-set-key [vertical-line mouse-1] 'mouse-select-window)
  1783. (global-set-key [vertical-line C-mouse-2] 'mouse-split-window-vertically)
  1784. (global-set-key [right-divider down-mouse-1] 'mouse-drag-vertical-line)
  1785. (global-set-key [right-divider mouse-1] 'ignore)
  1786. (global-set-key [right-divider C-mouse-2] 'mouse-split-window-vertically)
  1787. (global-set-key [bottom-divider down-mouse-1] 'mouse-drag-mode-line)
  1788. (global-set-key [bottom-divider mouse-1] 'ignore)
  1789. (global-set-key [bottom-divider C-mouse-2] 'mouse-split-window-horizontally)
  1790. (provide 'mouse)
  1791. ;;; mouse.el ends here