mouse.el 77 KB

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