diff options
| author | Stefan Monnier | 2002-07-16 13:27:05 +0000 |
|---|---|---|
| committer | Stefan Monnier | 2002-07-16 13:27:05 +0000 |
| commit | 640201f82eed0d24c475e8719f2518cfc6ad2fb0 (patch) | |
| tree | 1968fbe5c2733ac8051cf5537f8aa90adcb401b9 | |
| parent | 979a8abc6981aab3f6c963ed050f97f3b593b065 (diff) | |
| download | emacs-640201f82eed0d24c475e8719f2518cfc6ad2fb0.tar.gz emacs-640201f82eed0d24c475e8719f2518cfc6ad2fb0.zip | |
(mouse-sel-mode): Use define-minor-mode.
Fold mouse-sel-bindings into it.
(mouse-sel-bound-events): Turn it into an alist.
(mouse-insert-selection): Delegate to mouse-yank-at-click if
mouse-sel-default-bindings asks for it.
| -rw-r--r-- | lisp/mouse-sel.el | 165 |
1 files changed, 63 insertions, 102 deletions
diff --git a/lisp/mouse-sel.el b/lisp/mouse-sel.el index 004b6cbf718..87a75a54c6f 100644 --- a/lisp/mouse-sel.el +++ b/lisp/mouse-sel.el | |||
| @@ -93,8 +93,9 @@ | |||
| 93 | ;; | 93 | ;; |
| 94 | ;; Mouse sets selection, and pastes from kill-ring | 94 | ;; Mouse sets selection, and pastes from kill-ring |
| 95 | ;; mouse-1 mouse-select | 95 | ;; mouse-1 mouse-select |
| 96 | ;; mouse-2 mouse-yank-at-click | 96 | ;; mouse-2 mouse-insert-selection |
| 97 | ;; mouse-3 mouse-extend | 97 | ;; mouse-3 mouse-extend |
| 98 | ;; In this mode, mouse-insert-selection just calls mouse-yank-at-click. | ||
| 98 | ;; | 99 | ;; |
| 99 | ;; Selection/kill-ring interaction is retained | 100 | ;; Selection/kill-ring interaction is retained |
| 100 | ;; interprogram-cut-function = x-select-text | 101 | ;; interprogram-cut-function = x-select-text |
| @@ -149,18 +150,6 @@ | |||
| 149 | "Mouse selection enhancement." | 150 | "Mouse selection enhancement." |
| 150 | :group 'mouse) | 151 | :group 'mouse) |
| 151 | 152 | ||
| 152 | (defcustom mouse-sel-mode nil | ||
| 153 | "Toggle Mouse Sel mode. | ||
| 154 | When Mouse Sel mode is enabled, mouse selection is enhanced in various ways. | ||
| 155 | Setting this variable directly does not take effect; | ||
| 156 | use either \\[customize] or the function `mouse-sel-mode'." | ||
| 157 | :set (lambda (symbol value) | ||
| 158 | (mouse-sel-mode (or value 0))) | ||
| 159 | :initialize 'custom-initialize-default | ||
| 160 | :type 'boolean | ||
| 161 | :group 'mouse-sel | ||
| 162 | :require 'mouse-sel) | ||
| 163 | |||
| 164 | (defcustom mouse-sel-leave-point-near-mouse t | 153 | (defcustom mouse-sel-leave-point-near-mouse t |
| 165 | "*Leave point near last mouse position. | 154 | "*Leave point near last mouse position. |
| 166 | If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end | 155 | If non-nil, \\[mouse-select] and \\[mouse-extend] will leave point at the end |
| @@ -181,10 +170,38 @@ If nil, point will always be placed at the beginning of the region." | |||
| 181 | (other :tag "default bindings" t)) | 170 | (other :tag "default bindings" t)) |
| 182 | :group 'mouse-sel) | 171 | :group 'mouse-sel) |
| 183 | 172 | ||
| 173 | ;;=== Key bindings ======================================================== | ||
| 174 | |||
| 175 | (defconst mouse-sel-bound-events | ||
| 176 | '(;; Primary selection bindings. | ||
| 177 | ;; | ||
| 178 | ;; Bind keys to `ignore' instead of unsetting them because modes may | ||
| 179 | ;; bind `down-mouse-1', for instance, without binding `mouse-1'. | ||
| 180 | ;; If we unset `mouse-1', this leads to a bitch_at_user when the | ||
| 181 | ;; mouse goes up because no matching binding is found for that. | ||
| 182 | ([mouse-1] . ignore) | ||
| 183 | ([drag-mouse-1] . ignore) | ||
| 184 | ([mouse-3] . ignore) | ||
| 185 | ([down-mouse-1] . mouse-select) | ||
| 186 | ([down-mouse-3] . mouse-extend) | ||
| 187 | ([mouse-2] . mouse-insert-selection) | ||
| 188 | ;; Secondary selection bindings. | ||
| 189 | ([M-mouse-1] . ignore) | ||
| 190 | ([M-drag-mouse-1] . ignore) | ||
| 191 | ([M-mouse-3] . ignore) | ||
| 192 | ([M-down-mouse-1] . mouse-select-secondary) | ||
| 193 | ([M-mouse-2] . mouse-insert-secondary) | ||
| 194 | ([M-down-mouse-3] . mouse-extend-secondary)) | ||
| 195 | "An alist of events that `mouse-sel-mode' binds.") | ||
| 196 | |||
| 184 | ;;=== User Command ======================================================== | 197 | ;;=== User Command ======================================================== |
| 185 | 198 | ||
| 199 | (defvar mouse-sel-original-bindings nil) | ||
| 200 | (defvar mouse-sel-original-interprogram-cut-function nil) | ||
| 201 | (defvar mouse-sel-original-interprogram-cut-function nil) | ||
| 202 | |||
| 186 | ;;;###autoload | 203 | ;;;###autoload |
| 187 | (defun mouse-sel-mode (&optional arg) | 204 | (define-minor-mode mouse-sel-mode |
| 188 | "Toggle Mouse Sel mode. | 205 | "Toggle Mouse Sel mode. |
| 189 | With prefix ARG, turn Mouse Sel mode on if and only if ARG is positive. | 206 | With prefix ARG, turn Mouse Sel mode on if and only if ARG is positive. |
| 190 | Returns the new status of Mouse Sel mode (non-nil means on). | 207 | Returns the new status of Mouse Sel mode (non-nil means on). |
| @@ -203,10 +220,10 @@ Triple-clicking selects lines. | |||
| 203 | Quad-clicking selects paragraphs. | 220 | Quad-clicking selects paragraphs. |
| 204 | 221 | ||
| 205 | - Selecting sets the region & X primary selection, but does NOT affect | 222 | - Selecting sets the region & X primary selection, but does NOT affect |
| 206 | the kill-ring, nor do the kill-ring function change the X selection. | 223 | the `kill-ring', nor do the kill-ring functions change the X selection. |
| 207 | Because the mouse handlers set the primary selection directly, | 224 | Because the mouse handlers set the primary selection directly, |
| 208 | mouse-sel sets the variables interprogram-cut-function and | 225 | mouse-sel sets the variables `interprogram-cut-function' and |
| 209 | interprogram-paste-function to nil. | 226 | `interprogram-paste-function' to nil. |
| 210 | 227 | ||
| 211 | - Clicking mouse-2 inserts the contents of the primary selection at | 228 | - Clicking mouse-2 inserts the contents of the primary selection at |
| 212 | the mouse position (or point, if `mouse-yank-at-point' is non-nil). | 229 | the mouse position (or point, if `mouse-yank-at-point' is non-nil). |
| @@ -219,93 +236,35 @@ to the kill ring. Pressing mouse-1 or mouse-3 kills it. | |||
| 219 | - M-mouse-1, M-mouse-2 & M-mouse-3 work similarly to mouse-1, mouse-2 | 236 | - M-mouse-1, M-mouse-2 & M-mouse-3 work similarly to mouse-1, mouse-2 |
| 220 | & mouse-3, but operate on the X secondary selection rather than the | 237 | & mouse-3, but operate on the X secondary selection rather than the |
| 221 | primary selection and region." | 238 | primary selection and region." |
| 222 | (interactive "P") | 239 | :global t |
| 223 | (let ((on-p (if arg | 240 | (if mouse-sel-mode |
| 224 | (> (prefix-numeric-value arg) 0) | 241 | (progn |
| 225 | (not mouse-sel-mode)))) | ||
| 226 | (if on-p | ||
| 227 | (add-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook) | 242 | (add-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook) |
| 228 | (remove-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook)) | 243 | (when mouse-sel-default-bindings |
| 229 | (mouse-sel-bindings on-p) | 244 | ;; Save original bindings and replace them with new ones. |
| 230 | (setq mouse-sel-mode on-p))) | 245 | (setq mouse-sel-original-bindings |
| 246 | (mapcar (lambda (binding) | ||
| 247 | (let ((event (car binding))) | ||
| 248 | (prog1 (cons event (lookup-key global-map event)) | ||
| 249 | (global-set-key event (cdr binding))))) | ||
| 250 | mouse-sel-bound-events)) | ||
| 251 | ;; Update interprogram functions. | ||
| 252 | (setq mouse-sel-original-interprogram-cut-function | ||
| 253 | interprogram-cut-function | ||
| 254 | mouse-sel-original-interprogram-paste-function | ||
| 255 | interprogram-paste-function) | ||
| 256 | (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste) | ||
| 257 | (setq interprogram-cut-function nil | ||
| 258 | interprogram-paste-function nil)))) | ||
| 231 | 259 | ||
| 232 | ;;=== Key bindings ======================================================== | ||
| 233 | |||
| 234 | (defconst mouse-sel-bound-events | ||
| 235 | '([down-mouse-1] [mouse-1] [drag-mouse-1] | ||
| 236 | [mouse-2] | ||
| 237 | [down-mouse-3] [mouse-3] | ||
| 238 | [M-mouse-2] | ||
| 239 | [M-down-mouse-1] [M-mouse-1] [M-drag-mouse-1] | ||
| 240 | [M-down-mouse-3] [M-mouse-3]) | ||
| 241 | "A list of events that mouse-sel binds.") | ||
| 242 | |||
| 243 | (defun mouse-sel-bindings (bind) | ||
| 244 | (cond | ||
| 245 | |||
| 246 | ;; Default mouse-sel bindings | ||
| 247 | ((and bind mouse-sel-default-bindings) | ||
| 248 | |||
| 249 | ;; Save original bindings | ||
| 250 | (setq mouse-sel-original-bindings nil) | ||
| 251 | (mapc (function | ||
| 252 | (lambda (event) | ||
| 253 | (setq mouse-sel-original-bindings | ||
| 254 | (cons (cons event (lookup-key global-map event)) | ||
| 255 | mouse-sel-original-bindings)))) | ||
| 256 | mouse-sel-bound-events) | ||
| 257 | (setq mouse-sel-original-interprogram-cut-function | ||
| 258 | interprogram-cut-function | ||
| 259 | mouse-sel-original-interprogram-paste-function | ||
| 260 | interprogram-paste-function) | ||
| 261 | |||
| 262 | ;; Primary selection bindings. | ||
| 263 | ;; | ||
| 264 | ;; Bind keys to `ignore' instead of unsetting them because | ||
| 265 | ;; modes may bind `down-mouse-1', for instance, without | ||
| 266 | ;; binding other `up-mouse-1' or `mouse-1'. If we unset | ||
| 267 | ;; `mouse-1', this leads to a bitch_at_user when the mouse | ||
| 268 | ;; goes up because no matching binding is found for that. | ||
| 269 | (global-set-key [mouse-1] 'ignore) | ||
| 270 | (global-set-key [drag-mouse-1] 'ignore) | ||
| 271 | (global-set-key [mouse-3] 'ignore) | ||
| 272 | (global-set-key [down-mouse-1] 'mouse-select) | ||
| 273 | (unless (eq mouse-sel-default-bindings 'interprogram-cut-paste) | ||
| 274 | (global-set-key [mouse-2] 'mouse-insert-selection) | ||
| 275 | (setq interprogram-cut-function nil | ||
| 276 | interprogram-paste-function nil)) | ||
| 277 | (global-set-key [down-mouse-3] 'mouse-extend) | ||
| 278 | |||
| 279 | ;; Secondary selection bindings. | ||
| 280 | (global-set-key [M-mouse-1] 'ignore) | ||
| 281 | (global-set-key [M-drag-mouse-1] 'ignore) | ||
| 282 | (global-set-key [M-mouse-3] 'ignore) | ||
| 283 | (global-set-key [M-down-mouse-1] 'mouse-select-secondary) | ||
| 284 | (global-set-key [M-mouse-2] 'mouse-insert-secondary) | ||
| 285 | (global-set-key [M-down-mouse-3] 'mouse-extend-secondary)) | ||
| 286 | |||
| 287 | ((not bind) | ||
| 288 | ;; Restore original bindings | 260 | ;; Restore original bindings |
| 289 | (mapc (function | 261 | (remove-hook 'x-lost-selection-hooks 'mouse-sel-lost-selection-hook) |
| 290 | (lambda (binding) | 262 | (dolist (binding mouse-sel-original-bindings) |
| 291 | (if (cdr binding) | 263 | (global-set-key (car binding) (cdr binding))) |
| 292 | (global-set-key (car binding) (cdr binding)) | 264 | (setq interprogram-cut-function |
| 293 | (global-unset-key (car binding))))) | ||
| 294 | mouse-sel-original-bindings) | ||
| 295 | (setq interprogram-cut-function | ||
| 296 | mouse-sel-original-interprogram-cut-function | 265 | mouse-sel-original-interprogram-cut-function |
| 297 | interprogram-paste-function | 266 | interprogram-paste-function |
| 298 | mouse-sel-original-interprogram-paste-function)) | 267 | mouse-sel-original-interprogram-paste-function))) |
| 299 | |||
| 300 | )) | ||
| 301 | |||
| 302 | ;;=== Command Variable ==================================================== | ||
| 303 | |||
| 304 | ;; This has to come after the function `mouse-sel-mode' and its callee. | ||
| 305 | ;; An alternative is to put the option `mouse-sel-mode' here and remove its | ||
| 306 | ;; `:initialize' keyword. | ||
| 307 | (when mouse-sel-mode | ||
| 308 | (mouse-sel-mode t)) | ||
| 309 | 268 | ||
| 310 | ;;=== Internal Variables/Constants ======================================== | 269 | ;;=== Internal Variables/Constants ======================================== |
| 311 | 270 | ||
| @@ -711,11 +670,13 @@ See documentation for mouse-select-internal for more details." | |||
| 711 | 670 | ||
| 712 | ;;=== Paste =============================================================== | 671 | ;;=== Paste =============================================================== |
| 713 | 672 | ||
| 714 | (defun mouse-insert-selection (event) | 673 | (defun mouse-insert-selection (event arg) |
| 715 | "Insert the contents of the PRIMARY selection at mouse click. | 674 | "Insert the contents of the PRIMARY selection at mouse click. |
| 716 | If `mouse-yank-at-point' is non-nil, insert at point instead." | 675 | If `mouse-yank-at-point' is non-nil, insert at point instead." |
| 717 | (interactive "e") | 676 | (interactive "e\nP") |
| 718 | (mouse-insert-selection-internal 'PRIMARY event)) | 677 | (if (eq mouse-sel-default-bindings 'interprogram-cut-paste) |
| 678 | (mouse-yank-at-click event arg) | ||
| 679 | (mouse-insert-selection-internal 'PRIMARY event))) | ||
| 719 | 680 | ||
| 720 | (defun mouse-insert-secondary (event) | 681 | (defun mouse-insert-secondary (event) |
| 721 | "Insert the contents of the SECONDARY selection at mouse click. | 682 | "Insert the contents of the SECONDARY selection at mouse click. |