emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Visuwesh <visuweshm@gmail.com>
To: emacs-orgmode@gnu.org
Subject: [PATCH] Turn org-mouse features into minor-modes
Date: Thu, 26 Dec 2024 18:44:18 +0530	[thread overview]
Message-ID: <87seqad1sl.fsf@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 432 bytes --]

[ Resending it as it seems like it never reached the mailing list.  ]

To make loading org-mouse less annoying for all, I was thinking of
introducing minor-modes like in the attached patch.  If this approach is
acceptable, I will get to calling out the change in NEWS and updating
the manual if required.  We could also add a `add-hook' call to turn on
the minor-mode automatically to preserve the old behaviour if required.
WDYT?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-mouse-Make-it-possible-to-turn-on-and-off-the-fe.patch --]
[-- Type: text/x-diff, Size: 7894 bytes --]

From a4162d9e9204735ecd6ac864ed467e87362ace32 Mon Sep 17 00:00:00 2001
From: Visuwesh <visuweshm@gmail.com>
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


             reply	other threads:[~2024-12-26 13:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-26 13:14 Visuwesh [this message]
2024-12-27  8:25 ` [PATCH] Turn org-mouse features into minor-modes Ihor Radchenko
2024-12-27  8:35   ` Visuwesh
2024-12-27  8:40     ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87seqad1sl.fsf@gmail.com \
    --to=visuweshm@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).