unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: Juri Linkov <juri@linkov.net>
Cc: 50552@debbugs.gnu.org
Subject: bug#50552: 28.0.50; Add context-menu-occur
Date: Tue, 14 Sep 2021 20:45:44 +0000	[thread overview]
Message-ID: <87lf3yj37r.fsf@posteo.net> (raw)
In-Reply-To: <87ilz5d9a1.fsf@posteo.net>

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


Sorry for the delay,

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.
>
> The reasoning was the following: by default, dictionary.el is not loaded,
> so its context menu item is not used.  But when the user wants to use
> dictionary.el and explicitly loads it, then automatically provide also
> its context menu item.
>
> This reasoning can't be applied to occur, because occur in replace.el
> is pre-loaded.
>
>> +(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)))
>
> Thanks, this is almost perfect.  What remains to do is to create
> a regexp that matches only words/symbols.  You can see how
> 'isearch-occur' converts a string to a word/symbol regexp by using
> '(funcall isearch-regexp-function isearch-string)' before calling 'occur'.

Ok, I've tried it here:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-occur-related-context-menu-operations1.patch --]
[-- Type: text/x-diff, Size: 2305 bytes --]

From 78a0be097333b1619c5d25b4b96facb703fb8ceb 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 | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/lisp/replace.el b/lisp/replace.el
index 69bdfe1331..d652f37e4d 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -2367,6 +2367,43 @@ 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")
+  (let ((word (thing-at-mouse event 'word t)))
+    (occur (cond
+            ((functionp isearch-regexp-function)
+             (funcall isearch-regexp-function word))
+            (isearch-regexp-function (word-search-regexp word))
+            (isearch-regexp word)
+            (t (regexp-quote word))))))
+
+(defun occur-symbol-at-mouse (event)
+  "Display an occur buffer for the symbol at EVENT."
+  (interactive "e")
+  (let ((symbol (thing-at-mouse event 'symbol t)))
+    (occur (cond
+            ((functionp isearch-regexp-function)
+             (funcall isearch-regexp-function symbol))
+            (isearch-regexp-function (word-search-regexp symbol))
+            (isearch-regexp symbol)
+            (t (regexp-quote symbol))))))
+
+(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)
+
 \f
 ;; It would be nice to use \\[...], but there is no reasonable way
 ;; to make that display both SPC and Y.
-- 
2.30.2


[-- Attachment #3: Type: text/plain, Size: 64 bytes --]


And here another version with word/symbol-start/end matching:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #4: 0001-Add-occur-related-context-menu-operations.patch --]
[-- Type: text/x-diff, Size: 2402 bytes --]

From 3cde46258915dd7f56942fd1decba397d30f809b 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 | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/lisp/replace.el b/lisp/replace.el
index 69bdfe1331..8c50a6ad4c 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -2367,6 +2367,50 @@ 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")
+  (let ((word (thing-at-mouse event 'word t)))
+    (occur (concat
+            "\\<"
+            (cond
+             ((functionp isearch-regexp-function)
+              (funcall isearch-regexp-function word))
+             (isearch-regexp-function (word-search-regexp word))
+             (isearch-regexp word)
+             (t (regexp-quote word)))
+            "\\>"))))
+
+(defun occur-symbol-at-mouse (event)
+  "Display an occur buffer for the symbol at EVENT."
+  (interactive "e")
+  (let ((symbol (thing-at-mouse event 'symbol t)))
+    (occur
+     (concat
+      "\\_<"
+      (cond
+       ((functionp isearch-regexp-function)
+        (funcall isearch-regexp-function symbol))
+       (isearch-regexp-function (word-search-regexp symbol))
+       (isearch-regexp symbol)
+       (t (regexp-quote symbol)))
+      "\\_>"))))
+
+(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)
+
 \f
 ;; It would be nice to use \\[...], but there is no reasonable way
 ;; to make that display both SPC and Y.
-- 
2.30.2


[-- Attachment #5: Type: text/plain, Size: 316 bytes --]


>> To keep in line with prog-context-menu and context-menu-dictionary, I
>> also removed the modification of context-menu-functions's :type.
>
> I guess this is the right thing since the user still can add
> 'occur-context-menu' as a function name to 'context-menu-functions'.

Makes sense.

-- 
	Philip Kaludercic

  reply	other threads:[~2021-09-14 20:45 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-12 16:54 bug#50552: 28.0.50; Add context-menu-occur Philip Kaludercic
2021-09-12 17:59 ` Juri Linkov
2021-09-12 19:09   ` Philip Kaludercic
2021-09-13  8:28     ` Juri Linkov
2021-09-14 20:45       ` Philip Kaludercic [this message]
2021-09-15  7:14         ` Juri Linkov
2021-09-28 20:00       ` Philip Kaludercic
2021-09-29  7:19         ` Juri Linkov
2021-09-29 10:00           ` Philip Kaludercic
2021-09-29 17:31             ` Juri Linkov
2021-10-02 19:29               ` Juri Linkov
2021-10-01 16:28         ` Philip Kaludercic
2021-10-02 19:25           ` Juri Linkov

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.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87lf3yj37r.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=50552@debbugs.gnu.org \
    --cc=juri@linkov.net \
    /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.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).