From 99b9d8aa0b5ad14a015fdf7f7bfdc22eb804dde9 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. * etc/ORG-NEWS: Announce the breaking change. Link: https://list.orgmode.org/87seqad1sl.fsf@gmail.com --- etc/ORG-NEWS | 15 ++++++ lisp/org-mouse.el | 120 ++++++++++++++++++++++++++-------------------- 2 files changed, 83 insertions(+), 52 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index c9bb192de..99d5b6b58 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -18,6 +18,21 @@ Please send Org bug reports to mailto:emacs-orgmode@gnu.org. # require user action for most Org mode users. # Sorted from most important to least important. +*** ~org-mouse~ is no longer activated by default by anymore + +To follow Emacs [[info:elisp#Coding Conventions][coding conventions]] and to avoid confusion of users who +accidentally get ~org-mouse~ autoloaded due to help completion, the +library does not modify ~org-mode-map~ and other variables during +loading anymore. + +To enable the ~org-mouse~ features, the new minor-modes +~org-mouse-minor-mode~ and ~org-mouse-agenda-minor-mode~ should be +enabled. + +To turn on the features in all ~org-mode~ and ~org-agenda-mode~ +buffers, you can use the globalized versions +~global-org-mouse-minor-mode~ and ~global-org-mouse-agenda-minor-mode~. + *** You may need to update =org-protocol= bookmarklets for browsers In Firefox-133, and Firefox-128.5 ESR the previously suggested diff --git a/lisp/org-mouse.el b/lisp/org-mouse.el index 928d18d66..33f253fc4 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