bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#50552: 28.0.50; Add context-menu-occur


From: Philip Kaludercic
Subject: bug#50552: 28.0.50; Add context-menu-occur
Date: Sun, 12 Sep 2021 19:09:25 +0000

Juri Linkov <juri@linkov.net> writes:

>> the below patch adds the commands occur-word-at-mouse and
>> occur-symbol-at-mouse, and a function for context-menu-mode to occur
>> words or symbols where the context menu was invoked.
>>
>> Would there be any interest in adding such a functionality?
>
> Thanks.  There is a new function 'thing-at-mouse' with a new arg 'click'
> added today that you can use instead of 'thing-at-point'.
> You can see an example in lisp/net/dictionary.el.

I see, but I wonder why dictionary.el directly manipulates
context-menu-functions with add-hook.

>> diff --git a/lisp/mouse.el b/lisp/mouse.el
>> index 7d3ed9a0e4..3590e27e3e 100644
>> --- a/lisp/mouse.el
>> +++ b/lisp/mouse.el
>>[...]
>> +(defun context-menu-occur (menu)
>
> The default set of context menus in mouse.el contains only menus
> created from the existing non-context menus.  All new context menus
> should be added to their respective packages.  So please move
> context-menu-occur to replace.el.
>
> Another example prog-context-menu in lisp/progmodes/prog-mode.el also
> demonstrates how context menus should belong to the package where they
> are used.  And prog-context-menu is similar to context-menu-occur,
> so it should have the name prefix of the package: occur-context-menu.

Ok, no problem. This should do it:

>From 77c080476a6e64ab9ae5717e75c2358d3e8d20a7 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk@posteo.net>
Date: Sun, 12 Sep 2021 21:02:46 +0200
Subject: [PATCH] Add occur-related context-menu operations

* replace.el (occur-word-at-mouse): Add new command
(occur-symbol-at-mouse): Add new command
(occur-context-menu): Add new function
---
 lisp/replace.el | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/lisp/replace.el b/lisp/replace.el
index 69bdfe1331..e8ca3fac80 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -2367,6 +2367,31 @@ occur-context-lines
      ;; And the second element is the list of context after-lines.
      (if (> nlines 0) after-lines))))
 
+(defun occur-word-at-mouse (event)
+  "Display an occur buffer for the word at EVENT."
+  (interactive "e")
+  (occur (thing-at-mouse event 'word t)))
+
+(defun occur-symbol-at-mouse (event)
+  "Display an occur buffer for the symbol at EVENT."
+  (interactive "e")
+  (occur (thing-at-mouse event 'symbol t)))
+
+(defun occur-context-menu (menu click)
+  "Populate MENU with occur commands for CLICK.
+To be added to `context-menu-functions'."
+  (let ((word (thing-at-mouse click 'word))
+        (sym (thing-at-mouse click 'symbol)))
+    (when (or word sym)
+      (define-key-after menu [occur-separator] menu-bar-separator)
+      (when word
+        (define-key-after menu [occur-word-at-mouse]
+          '(menu-item "Occur Word" occur-word-at-mouse)))
+      (when sym
+        (define-key-after menu [occur-symbol-at-mouse]
+          '(menu-item "Occur Symbol" occur-symbol-at-mouse)))))
+  menu)
+
 
 ;; It would be nice to use \\[...], but there is no reasonable way
 ;; to make that display both SPC and Y.
-- 
2.30.2

To keep in line with prog-context-menu and context-menu-dictionary, I
also removed the modification of context-menu-functions's :type.

-- 
        Philip Kaludercic

reply via email to

[Prev in Thread] Current Thread [Next in Thread]