From a4162d9e9204735ecd6ac864ed467e87362ace32 Mon Sep 17 00:00:00 2001 From: Visuwesh Date: Mon, 23 Dec 2024 12:41:13 +0530 Subject: [PATCH] org-mouse: Make it possible to turn on and off the feature Introduce new minor-modes `org-mouse-minor-mode' and `org-mouse-agenda-minor-mode' to make it easy to turn on/off the mouse support. * lisp/org-mouse.el (org-mode-hook, org-agenda-mode-hook): Remove the lambda which makes it unnecessarily difficult to reverse the effect of loading this file. (org-mouse--old-mouse-map): New variable to hold the original definition of org-mouse-map. (org-mouse-minor-mode-map, org-mouse-minor-mode) (org-mouse-agenda-minor-mode-map, org-mouse-agenda-minor-mode): Add new minor-modes to easily toggle the mouse features on and off per buffer. --- lisp/org-mouse.el | 120 ++++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 52 deletions(-) diff --git a/lisp/org-mouse.el b/lisp/org-mouse.el index 7f85653f2..eb4355d2f 100644 --- a/lisp/org-mouse.el +++ b/lisp/org-mouse.el @@ -866,48 +866,59 @@ (defun org-mouse-down-mouse (event) ;; events to `mouse-drag-region'. (put 'org-mouse-down-mouse 'ignored-mouse-command t) -(add-hook 'org-mode-hook - (lambda () - (setq org-mouse-context-menu-function #'org-mouse-context-menu) - - (when (memq 'context-menu org-mouse-features) - (org-defkey org-mouse-map [mouse-3] nil) - (org-defkey org-mode-map [mouse-3] #'org-mouse-show-context-menu)) - (org-defkey org-mode-map [down-mouse-1] #'org-mouse-down-mouse) - (when (memq 'context-menu org-mouse-features) - (org-defkey org-mouse-map [C-drag-mouse-1] #'org-mouse-move-tree) - (org-defkey org-mouse-map [C-down-mouse-1] #'org-mouse-move-tree-start)) - (when (memq 'yank-link org-mouse-features) - (org-defkey org-mode-map [S-mouse-2] #'org-mouse-yank-link) - (org-defkey org-mode-map [drag-mouse-3] #'org-mouse-yank-link)) - (when (memq 'move-tree org-mouse-features) - (org-defkey org-mouse-map [drag-mouse-3] #'org-mouse-move-tree) - (org-defkey org-mouse-map [down-mouse-3] #'org-mouse-move-tree-start)) - - (when (memq 'activate-stars org-mouse-features) - (font-lock-add-keywords - nil +(defvar org-mouse-minor-mode-map + (let ((map (make-sparse-keymap))) + (org-defkey map [mouse-3] #'org-mouse-show-context-menu) + (org-defkey map [down-mouse-1] #'org-mouse-down-mouse) + map) + "Keymap for `org-mouse-minor-mode'.") + +(defvar-local org-mouse--old-mouse-map (copy-keymap org-mouse-map)) +(define-minor-mode org-mouse-minor-mode + "Minor mode to enable better mouse support in `org-mode'." + :global nil + (let ((fl-fun (if org-mouse-minor-mode + (lambda (mode kw) + (font-lock-add-keywords mode kw t)) + #'font-lock-remove-keywords))) + (setq org-mouse-context-menu-function + (and org-mouse-minor-mode #'org-mouse-context-menu)) + (when org-mouse-minor-mode + ;; Without `copy-keymap', later `org-defkey' calls modify the + ;; value of keymap `org-mouse--old-mouse-map' holds. + (setq org-mouse--old-mouse-map (copy-keymap org-mouse-map))) + (setq org-mouse-map (copy-keymap org-mouse--old-mouse-map)) + (org-defkey org-mouse-minor-mode-map [S-mouse-2] nil) + (org-defkey org-mouse-minor-mode-map [drag-mouse-3] nil) + (when org-mouse-minor-mode + (when (memq 'context-menu org-mouse-features) + (org-defkey org-mouse-map [mouse-3] nil) + (org-defkey org-mouse-map [C-drag-mouse-1] #'org-mouse-move-tree) + (org-defkey org-mouse-map [C-down-mouse-1] #'org-mouse-move-tree-start)) + (when (memq 'yank-link org-mouse-features) + (org-defkey org-mouse-minor-mode-map [S-mouse-2] #'org-mouse-yank-link) + (org-defkey org-mouse-minor-mode-map [drag-mouse-3] #'org-mouse-yank-link)) + (when (memq 'move-tree org-mouse-features) + (org-defkey org-mouse-map [drag-mouse-3] #'org-mouse-move-tree) + (org-defkey org-mouse-map [down-mouse-3] #'org-mouse-move-tree-start))) + (when (memq 'activate-stars org-mouse-features) + (funcall fl-fun nil `((,org-outline-regexp 0 `(face org-link mouse-face highlight keymap ,org-mouse-map) - 'prepend)) - t)) - - (when (memq 'activate-bullets org-mouse-features) - (font-lock-add-keywords - nil + 'prepend)))) + (when (memq 'activate-bullets org-mouse-features) + (funcall fl-fun nil `(("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +" (1 `(face org-link keymap ,org-mouse-map mouse-face highlight) - 'prepend))) - t)) - - (when (memq 'activate-checkboxes org-mouse-features) - (font-lock-add-keywords - nil + 'prepend))))) + (when (memq 'activate-checkboxes org-mouse-features) + (funcall fl-fun nil `(("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)" - (1 `(face nil keymap ,org-mouse-map mouse-face highlight) prepend))) - t)) - - (advice-add 'org-open-at-point :around #'org--mouse-open-at-point))) + (1 `(face nil keymap ,org-mouse-map mouse-face highlight) prepend))))) + (if org-mouse-minor-mode + (advice-add 'org-open-at-point :around #'org--mouse-open-at-point) + (advice-remove 'org-open-at-point #'org--mouse-open-at-point)) + (font-lock-flush))) (defun org--mouse-open-at-point (orig-fun &rest args) (let ((context (org-context))) @@ -923,7 +934,6 @@ (defun org-mouse-move-tree-start (_event) (interactive "e") (message "Same line: promote/demote, (***):move before, (text): make a child")) - (defun org-mouse-make-marker (position) (with-current-buffer (window-buffer (posn-window position)) (copy-marker (posn-point position)))) @@ -1095,20 +1105,26 @@ (defun org-mouse-get-gesture (event) (if (< (car startxy) (car endxy)) :right :left))) - ; (setq org-agenda-mode-hook nil) -(defvar org-agenda-mode-map) -(add-hook 'org-agenda-mode-hook - (lambda () - (setq org-mouse-context-menu-function #'org-mouse-agenda-context-menu) - (org-defkey org-agenda-mode-map [mouse-3] #'org-mouse-show-context-menu) - (org-defkey org-agenda-mode-map [down-mouse-3] #'org-mouse-move-tree-start) - (org-defkey org-agenda-mode-map [C-mouse-4] #'org-agenda-earlier) - (org-defkey org-agenda-mode-map [C-mouse-5] #'org-agenda-later) - (org-defkey org-agenda-mode-map [drag-mouse-3] - (lambda (event) (interactive "e") - (cl-case (org-mouse-get-gesture event) - (:left (org-agenda-earlier 1)) - (:right (org-agenda-later 1))))))) +(defvar org-mouse-agenda-minor-mode-map + (let ((map (make-sparse-keymap))) + (org-defkey map [mouse-3] #'org-mouse-show-context-menu) + (org-defkey map [down-mouse-3] #'org-mouse-move-tree-start) + (org-defkey map [C-mouse-4] #'org-agenda-earlier) + (org-defkey map [C-mouse-5] #'org-agenda-later) + (org-defkey map [drag-mouse-3] + (lambda (event) (interactive "e") + (cl-case (org-mouse-get-gesture event) + (:left (org-agenda-earlier 1)) + (:right (org-agenda-later 1))))) + map) + "Keymap for `org-mouse-agenda-minor-mode'.") + +(define-minor-mode org-mouse-agenda-minor-mode + "Minor mode to enable better mouse support in `org-agenda-mode'." + :global nil + (setq org-mouse-context-menu-function + (and org-mouse-agenda-minor-mode + #'org-mouse-agenda-context-menu))) (provide 'org-mouse) -- 2.45.2