mouse.el 77 KB

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