Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
4.4 KiB

5 years ago
  1. ;;; disable-mouse.el --- Disable mouse commands globally
  2. ;; Copyright (C) 2016 Steve Purcell
  3. ;; Author: Steve Purcell <steve@sanityinc.com>
  4. ;; URL: https://github.com/purcell/disable-mouse
  5. ;; Package-Commit: 81639930bcaeedadbcc19728e91719afcac84613
  6. ;; Package-Version: 0.3
  7. ;; Package-X-Original-Version: 0
  8. ;; Keywords: mouse
  9. ;; This program is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; Provides `disable-mouse-mode' and `global-disable-mouse-mode', pair
  21. ;; of minor modes which suppress all mouse events by intercepting them
  22. ;; and running a customisable handler command (`ignore' by default).
  23. ;;; Code:
  24. (defgroup disable-mouse nil
  25. "Disable mouse commands globally."
  26. :prefix "disable-mouse-"
  27. :group 'mouse)
  28. (defcustom disable-mouse-command 'ignore
  29. "The command to run when a mouse action is attempted."
  30. :group 'disable-mouse
  31. :type 'function)
  32. (defcustom disable-mouse-mode-lighter " NoMouse"
  33. "Mode-line lighter for `disable-mouse-mode'."
  34. :group 'disable-mouse
  35. :type 'string)
  36. (defcustom global-disable-mouse-mode-lighter " NoMouse!"
  37. "Mode-line lighter for `global-disable-mouse-mode'."
  38. :group 'disable-mouse
  39. :type 'string)
  40. (defconst disable-mouse--bindings-modifier-combos
  41. '("C-" "M-" "S-" "C-M-" "C-S-" "M-S-" "M-C-S-"))
  42. (defconst disable-mouse--bindings-targets '("mode-line" "bottom-divider" "vertical-line"))
  43. (defconst disable-mouse--multipliers '("double" "triple"))
  44. (defconst disable-mouse--bindings
  45. '("mouse-1" "mouse-2" "mouse-3"
  46. "up-mouse-1" "up-mouse-2" "up-mouse-3"
  47. "down-mouse-1" "down-mouse-2" "down-mouse-3"
  48. "drag-mouse-1" "drag-mouse-2" "drag-mouse-3"
  49. "mouse-4" "mouse-5"
  50. "up-mouse-4" "up-mouse-5"
  51. "down-mouse-4" "down-mouse-5"
  52. "drag-mouse-4" "drag-mouse-5"
  53. "wheel-up" "wheel-down" "wheel-left" "wheel-right"
  54. ))
  55. (defun disable-mouse--all-bindings (include-targets)
  56. "Return an extensive list of mouse-related keybindings.
  57. When INCLUDE-TARGETS is non-nil, also return bindings that target
  58. the elements in `disable-mouse--bindings-targets'."
  59. (let ((bindings))
  60. (dolist (target (append '(nil)
  61. (when include-targets
  62. disable-mouse--bindings-targets)))
  63. (dolist (mod (append '(nil) disable-mouse--bindings-modifier-combos))
  64. (dolist (mult (append '(nil) disable-mouse--multipliers))
  65. (dolist (binding disable-mouse--bindings)
  66. (push (read-kbd-macro
  67. (concat (when target (concat "<" target "> "))
  68. mod
  69. "<"
  70. (when mult (concat mult "-"))
  71. binding
  72. ">"))
  73. bindings)))))
  74. bindings))
  75. (defun disable-mouse--handle ()
  76. "Handle when a disabled mouse event is fired."
  77. (interactive)
  78. (call-interactively disable-mouse-command))
  79. (defvar disable-mouse-mode-map
  80. (let ((map (make-sparse-keymap)))
  81. (dolist (binding (disable-mouse--all-bindings nil))
  82. (define-key map binding 'disable-mouse--handle))
  83. map)
  84. "Map containing no-op bindings for all mouse events.")
  85. (defvar global-disable-mouse-mode-map
  86. (let ((map (make-sparse-keymap)))
  87. (dolist (binding (disable-mouse--all-bindings t))
  88. (define-key map binding 'disable-mouse--handle))
  89. map)
  90. "Map containing no-op bindings for all mouse events.")
  91. ;;;###autoload
  92. (define-minor-mode disable-mouse-mode
  93. "Disable the mouse in the current buffer.
  94. You can still use the mouse to click into other buffers or
  95. interact with GUI elements such as divider lines."
  96. nil
  97. :lighter disable-mouse-mode-lighter)
  98. ;;;###autoload
  99. (define-minor-mode global-disable-mouse-mode
  100. "Disable the mouse globally.
  101. Interact with GUI elements such as divider lines will also be prevented."
  102. nil
  103. :lighter global-disable-mouse-mode-lighter
  104. :global t)
  105. (provide 'disable-mouse)
  106. ;;; disable-mouse.el ends here