GNU bug report logs - #50552
28.0.50; Add context-menu-occur

Previous Next

Package: emacs;

Reported by: Philip Kaludercic <philipk <at> posteo.net>

Date: Sun, 12 Sep 2021 16:55:01 UTC

Severity: normal

Tags: fixed

Fixed in version 28.0.50

Done: Juri Linkov <juri <at> linkov.net>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 50552 in the body.
You can then email your comments to 50552 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Sun, 12 Sep 2021 16:55:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Philip Kaludercic <philipk <at> posteo.net>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 12 Sep 2021 16:55:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Philip Kaludercic <philipk <at> posteo.net>
To: bug-gnu-emacs <at> gnu.org
Subject: 28.0.50; Add context-menu-occur
Date: Sun, 12 Sep 2021 16:54:30 +0000
[Message part 1 (text/plain, inline)]
Hi,

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?

[0001-Add-occur-menu-for-context-mode-menu.patch (text/x-diff, inline)]
From cad330f31eb7f7b127a970296f323524d31e7fee Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk <at> posteo.net>
Date: Sat, 11 Sep 2021 22:41:32 +0200
Subject: [PATCH] Add occur menu for context-mode-menu

* lisp/mouse.el (context-menu-functions): Register context-menu-occur
(context-menu-occur): Add backend function
* lisp/replace.el (occur-word-at-mouse): Add command
(occur-symbol-at-mouse): Add command
---
 lisp/mouse.el   | 17 +++++++++++++++++
 lisp/replace.el | 14 ++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/lisp/mouse.el b/lisp/mouse.el
index 7d3ed9a0e4..3590e27e3e 100644
--- a/lisp/mouse.el
+++ b/lisp/mouse.el
@@ -296,6 +296,7 @@ context-menu-functions
                   (function-item context-menu-buffers)
                   (function-item context-menu-vc)
                   (function-item context-menu-ffap)
+                  (function-item context-menu-occur)
                   (function :tag "Custom function")))
   :version "28.1")
 
@@ -465,6 +466,22 @@ context-menu-ffap
                     :help "Find file or URL guessed from text around mouse click"))))
   menu)
 
+(defun context-menu-occur (menu)
+  "Occur at point menu."
+  (save-excursion
+    (mouse-set-point last-input-event)
+    (let ((word (thing-at-point 'word))
+          (sym (thing-at-point '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)
+
 (defvar context-menu-entry
   `(menu-item ,(purecopy "Context Menu") ignore
               :filter (lambda (_) (context-menu-map))))
diff --git a/lisp/replace.el b/lisp/replace.el
index 69bdfe1331..f234a933bd 100644
--- a/lisp/replace.el
+++ b/lisp/replace.el
@@ -2367,6 +2367,20 @@ 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")
+  (save-excursion
+    (mouse-set-point event)
+    (occur (regexp-quote (thing-at-point 'word)))))
+
+(defun occur-symbol-at-mouse (event)
+  "Display an occur buffer for the symbol at EVENT."
+  (interactive "e")
+  (save-excursion
+    (mouse-set-point event)
+    (occur (regexp-quote (thing-at-point 'symbol)))))
+
 
 ;; It would be nice to use \\[...], but there is no reasonable way
 ;; to make that display both SPC and Y.
-- 
2.30.2

[Message part 3 (text/plain, inline)]
In GNU Emacs 28.0.50 (build 3, x86_64-pc-linux-gnu, X toolkit, cairo version 1.16.0, Xaw scroll bars)
 of 2021-09-11 built on icterid
Repository revision: 6d31d5b4b4649a25c6f1c8857c2371e14b7e3805
Repository branch: feature/rcirc-update
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Debian GNU/Linux 11 (bullseye)

Configured using:
 'configure LDFLAGS=-flto 'CFLAGS=-O2 -march=native -mtune=native -pipe'
 --with-native-compilation PKG_CONFIG_PATH='

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
JSON LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NATIVE_COMP
NOTIFY INOTIFY PDUMPER PNG SECCOMP SOUND THREADS TIFF
TOOLKIT_SCROLL_BARS X11 XDBE XIM XPM LUCID ZLIB

Important settings:
  value of $EMACSLOADPATH: 
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  fido-mode: t
  global-git-commit-mode: t
  magit-auto-revert-mode: t
  shell-dirtrack-mode: t
  TeX-PDF-mode: t
  paredit-mode: t
  company-mode: t
  flymake-mode: t
  flyspell-mode: t
  outline-minor-mode: t
  repeat-mode: t
  rcirc-track-minor-mode: t
  display-time-mode: t
  winner-mode: t
  windmove-mode: t
  electric-pair-mode: t
  recentf-mode: t
  save-place-mode: t
  savehist-mode: t
  show-paren-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tab-bar-mode: t
  file-name-shadow-mode: t
  context-menu-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  temp-buffer-resize-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t

Load-path shadows:
/home/philip/.config/emacs/elpa/transient-0.3.6/transient hides /home/philip/Code/src/emacs/lisp/transient
~/.config/emacs/site-lisp/autoload hides /home/philip/Code/src/emacs/lisp/emacs-lisp/autoload

Features:
(shadow emacsbug mhtml-mode js skeleton autoinsert ibuf-ext loadhist
pcmpl-unix tar-mode rect dictionary dictionary-connection finder
modus-vivendi-theme magit-patch vc-dir edebug delsel ffap cvs-status
pcvs pcvs-defs pcvs-parse pcvs-info icomplete slime-tests ert ewoc slime
arc-mode archive-mode hyperspec sh-script executable org-element
avl-tree ol-eww ol-rmail ol-mhe ol-irc ol-info ol-gnus ol-docview
doc-view image-mode exif ol-bibtex bibtex ol-bbdb ol-w3m org ob
ob-tangle ob-ref ob-lob ob-table ob-exp org-macro org-footnote org-src
ob-comint org-pcomplete org-list org-faces org-entities org-version
ob-emacs-lisp ob-core ob-eval org-table ol org-keys org-compat advice
org-macs org-loaddefs crdt css-mode smie sgml-mode eww xdg url-queue
mm-url url-http finder-inf ibuffer ibuffer-loaddefs enriched facemenu
mailalias bbdb-pgp nnselect gnus-search flow-fill mm-archive qp
goto-addr bbdb-message autocrypt-message smiley gnus-cite mail-extr
gnus-async gnus-bcklg sort gnus-ml tabify man make-mode thai-util
thai-word insert-kaomoji url-about url-auth url-cache url-dired url-gw
vc-annotate magit-extras face-remap magit-submodule magit-obsolete
magit-blame magit-stash magit-reflog magit-bisect magit-push magit-pull
magit-fetch magit-clone magit-remote magit-commit magit-sequence
magit-notes magit-worktree magit-tag magit-merge magit-branch
magit-reset magit-files magit-refs magit-status magit magit-repos
magit-apply magit-wip magit-log which-func magit-diff git-commit
magit-core magit-autorevert autorevert filenotify magit-margin
magit-transient magit-process with-editor term ehelp eshell esh-cmd
esh-ext esh-opt esh-proc esh-io esh-arg esh-module esh-groups esh-util
server magit-mode transient format-spec magit-git magit-section cl-print
debug backtrace flymake-cc macrostep-c cmacexp macrostep apropos
eieio-opt speedbar ezimage dframe shortdoc imenu cus-edit cus-start
disp-table whitespace magit-utils dash tex-buf latex latex-flymake
tex-ispell tex-style grep markdown-mode find-dired avy log-edit add-log
smerge-mode cc-mode cc-fonts cc-guess cc-menus cc-cmds cc-styles
cc-align cc-engine cc-vars cc-defs pp autocrypt-gnus autocrypt nndraft
nnmh epa-file gnutls network-stream nsm nnmaildir nnfolder bbdb-gnus
bbdb-mua bbdb-com nnnil gnus-agent gnus-srvr gnus-score score-mode
nnvirtual gnus-msg gnus-art mm-uu mml2015 mm-view mml-smime smime dig
nntp gnus-cache gnus-sum shr kinsoku svg dom gnus-group gnus-undo
gnus-start gnus-dbus gnus-cloud nnimap nnmail mail-source utf7 netrc
nnoo gnus-spec gnus-int gnus-range message rmc puny rfc822 mml mml-sec
epa epg rfc6068 epg-config mm-decode mm-bodies mm-encode mailabbrev
gmm-utils mailheader gnus-win shell pcomplete shell-command+ time-stamp
help-at-pt jka-compr mule-util cal-move pulse color find-func char-fold
misearch multi-isearch bug-reference tex-info tex dbus xml crm texmathp
texinfo texinfo-loaddefs dired-aux vc-backup log-view pcvs-util diff
vc-fossil vc-mtn vc-hg vc-git diff-mode vc-bzr vc-src vc-sccs vc-svn
vc-cvs vc-rcs vc vc-dispatcher paredit company-oddmuse company-keywords
company-etags etags fileloop generator xref project company-gtags
company-dabbrev-code company-dabbrev company-files company-capf
company-cmake company-xcode company-clang company-semantic company-eclim
company-template company-bbdb company checkdoc flymake-proc flymake
thingatpt flyspell ispell noutline outline easy-mmode repeat gnus-dired
dired-x dired dired-loaddefs rcirc parse-time iso8601 time bbdb derived
bbdb-site timezone sendmail gnus nnheader gnus-util rmail rmail-loaddefs
time-date mail-utils hippie-exp winner windmove elec-pair recentf
tree-widget wid-edit saveplace savehist paren modus-operandi-theme
modus-themes pcase edmacro kmacro holidays hol-loaddefs cal-menu
calendar cal-loaddefs cus-load setup load compile text-property-search
comint ansi-color autoload lisp-mnt mail-parse rfc2231 rfc2047 rfc2045
mm-util ietf-drums mail-prsvr tex-site geiser-impl help-fns radix-tree
geiser-custom geiser-base ring slime-autoloads info package browse-url
url url-proxy url-privacy url-expand url-methods url-history url-cookie
url-domsuf url-util mailcap url-handlers url-parse auth-source eieio
eieio-core eieio-loaddefs password-cache json map url-vars comp
comp-cstr warnings subr-x rx cl-seq cl-macs cl-extra help-mode seq
byte-opt gv bytecomp byte-compile cconv cl-loaddefs cl-lib iso-transl
tooltip eldoc electric uniquify ediff-hook vc-hooks lisp-float-type
mwheel term/x-win x-win term/common-win x-dnd tool-bar dnd fontset image
regexp-opt fringe tabulated-list replace newcomment text-mode elisp-mode
lisp-mode prog-mode register page tab-bar menu-bar rfn-eshadow isearch
easymenu timer select scroll-bar mouse jit-lock font-lock syntax
font-core term/tty-colors frame minibuffer cl-generic cham georgian
utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao korean
japanese eucjp-ms cp51932 hebrew greek romanian slovak czech european
ethiopic indian cyrillic chinese composite charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice button loaddefs faces cus-face macroexp files
window text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget hashtable-print-readable backquote threads dbusbind
inotify dynamic-setting system-font-setting font-render-setting cairo
x-toolkit x multi-tty make-network-process native-compile emacs)

Memory information:
((conses 16 1882584 1950104)
 (symbols 48 66780 234)
 (strings 32 353422 201509)
 (string-bytes 1 10945494)
 (vectors 16 131046)
 (vector-slots 8 3232159 1772668)
 (floats 8 1207 6466)
 (intervals 56 80220 44224)
 (buffers 992 116))

-- 
	Philip K.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Sun, 12 Sep 2021 18:18:01 GMT) Full text and rfc822 format available.

Message #8 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Philip Kaludercic <philipk <at> posteo.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Sun, 12 Sep 2021 20:59:06 +0300
> 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.

> 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.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Sun, 12 Sep 2021 19:10:01 GMT) Full text and rfc822 format available.

Message #11 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Philip Kaludercic <philipk <at> posteo.net>
To: Juri Linkov <juri <at> linkov.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Sun, 12 Sep 2021 19:09:25 +0000
[Message part 1 (text/plain, inline)]
Juri Linkov <juri <at> 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:

[0001-Add-occur-related-context-menu-operations.patch (text/x-diff, inline)]
From 77c080476a6e64ab9ae5717e75c2358d3e8d20a7 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk <at> 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

[Message part 3 (text/plain, inline)]
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

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Mon, 13 Sep 2021 08:39:02 GMT) Full text and rfc822 format available.

Message #14 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Philip Kaludercic <philipk <at> posteo.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Mon, 13 Sep 2021 11:28:24 +0300
>>> 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'.

> 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'.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Tue, 14 Sep 2021 20:46:02 GMT) Full text and rfc822 format available.

Message #17 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Philip Kaludercic <philipk <at> posteo.net>
To: Juri Linkov <juri <at> linkov.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Tue, 14 Sep 2021 20:45:44 +0000
[Message part 1 (text/plain, inline)]
Sorry for the delay,

Juri Linkov <juri <at> 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:

[0001-Add-occur-related-context-menu-operations1.patch (text/x-diff, inline)]
From 78a0be097333b1619c5d25b4b96facb703fb8ceb Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk <at> 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)
+
 
 ;; It would be nice to use \\[...], but there is no reasonable way
 ;; to make that display both SPC and Y.
-- 
2.30.2

[Message part 3 (text/plain, inline)]
And here another version with word/symbol-start/end matching:

[0001-Add-occur-related-context-menu-operations.patch (text/x-diff, inline)]
From 3cde46258915dd7f56942fd1decba397d30f809b Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk <at> 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)
+
 
 ;; It would be nice to use \\[...], but there is no reasonable way
 ;; to make that display both SPC and Y.
-- 
2.30.2

[Message part 5 (text/plain, inline)]
>> 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

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Wed, 15 Sep 2021 07:16:01 GMT) Full text and rfc822 format available.

Message #20 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Philip Kaludercic <philipk <at> posteo.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Wed, 15 Sep 2021 10:14:36 +0300
tags 50552 fixed
close 50552 28.0.50
thanks

>> 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'.
>
> And here another version with word/symbol-start/end matching:

Your second patch was better, so I pushed it, but without isearch-regexp-function.
I tested that it works fine, thanks for it.  If you have more suggestions,
please send more patches.




Added tag(s) fixed. Request was from Juri Linkov <juri <at> linkov.net> to control <at> debbugs.gnu.org. (Wed, 15 Sep 2021 07:16:02 GMT) Full text and rfc822 format available.

bug marked as fixed in version 28.0.50, send any further explanations to 50552 <at> debbugs.gnu.org and Philip Kaludercic <philipk <at> posteo.net> Request was from Juri Linkov <juri <at> linkov.net> to control <at> debbugs.gnu.org. (Wed, 15 Sep 2021 07:16:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Tue, 28 Sep 2021 20:02:01 GMT) Full text and rfc822 format available.

Message #27 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Philip Kaludercic <philipk <at> posteo.net>
To: Juri Linkov <juri <at> linkov.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Tue, 28 Sep 2021 20:00:57 +0000
Juri Linkov <juri <at> linkov.net> writes:

>> 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.

On this topic, I am not sure how good it is to do this
automatically. One effect seems to be that context-menu-dictionary
inserts itself at an unintended place in the menu (in my case as the
first item, even though I'd prefer it to be further down). Might it make
more sense to autoload context-menu-dictionary and let the user manually
add it to context-menu-functions?

-- 
	Philip Kaludercic




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Wed, 29 Sep 2021 07:21:02 GMT) Full text and rfc822 format available.

Message #30 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Philip Kaludercic <philipk <at> posteo.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Wed, 29 Sep 2021 10:19:29 +0300
>>> 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.
>
> On this topic, I am not sure how good it is to do this
> automatically. One effect seems to be that context-menu-dictionary
> inserts itself at an unintended place in the menu (in my case as the
> first item, even though I'd prefer it to be further down). Might it make
> more sense to autoload context-menu-dictionary and let the user manually
> add it to context-menu-functions?

Then we need to decide what is the preferred way for the users
to add the dictionary menu item to the context menu.

1. One possible way is allow the user to customize context-menu-functions,
   and manually paste context-menu-dictionary to the entry field:

  INS DEL Choice: Value Menu Custom function: context-menu-dictionary

2. To allow using 'add-hook' in the user's init file with e.g.

  (add-hook 'context-menu-functions 'context-menu-dictionary 1)

In either case, 'context-menu-dictionary' should be autoloaded
like you suggested.  So now I've changed dictionary.el according to this,
thanks for the suggestion.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Wed, 29 Sep 2021 10:01:02 GMT) Full text and rfc822 format available.

Message #33 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Philip Kaludercic <philipk <at> posteo.net>
To: Juri Linkov <juri <at> linkov.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Wed, 29 Sep 2021 10:00:11 +0000
Juri Linkov <juri <at> linkov.net> writes:

>>>> 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.
>>
>> On this topic, I am not sure how good it is to do this
>> automatically. One effect seems to be that context-menu-dictionary
>> inserts itself at an unintended place in the menu (in my case as the
>> first item, even though I'd prefer it to be further down). Might it make
>> more sense to autoload context-menu-dictionary and let the user manually
>> add it to context-menu-functions?
>
> Then we need to decide what is the preferred way for the users
> to add the dictionary menu item to the context menu.
>
> 1. One possible way is allow the user to customize context-menu-functions,
>    and manually paste context-menu-dictionary to the entry field:
>
>   INS DEL Choice: Value Menu Custom function: context-menu-dictionary

Why not use the new custom-add-choice function (or something along these
lines) in dictionary.el?

> 2. To allow using 'add-hook' in the user's init file with e.g.
>
>   (add-hook 'context-menu-functions 'context-menu-dictionary 1)

In my case I am setting context-menu-functions manually, without
add-hook. I am not sure if that breaks anything?

> In either case, 'context-menu-dictionary' should be autoloaded
> like you suggested.  So now I've changed dictionary.el according to this,
> thanks for the suggestion.

Great, thank you.

-- 
	Philip Kaludercic




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Wed, 29 Sep 2021 17:34:01 GMT) Full text and rfc822 format available.

Message #36 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Philip Kaludercic <philipk <at> posteo.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Wed, 29 Sep 2021 20:31:18 +0300
> Why not use the new custom-add-choice function (or something along these
> lines) in dictionary.el?

I tried this:

  (custom-add-choice 'context-menu-functions
                     '(function-item :tag "Dictionary menu" context-menu-dictionary))

But it fails with the error:

  (error "Not a choice type: (repeat (choice (function-item ...")

>> 2. To allow using 'add-hook' in the user's init file with e.g.
>>
>>   (add-hook 'context-menu-functions 'context-menu-dictionary 1)
>
> In my case I am setting context-menu-functions manually, without
> add-hook. I am not sure if that breaks anything?

This is a valid way to configure it, it should not break.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Fri, 01 Oct 2021 16:29:02 GMT) Full text and rfc822 format available.

Message #39 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Philip Kaludercic <philipk <at> posteo.net>
To: Juri Linkov <juri <at> linkov.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Fri, 01 Oct 2021 16:28:02 +0000
[Message part 1 (text/plain, inline)]
Philip Kaludercic <philipk <at> posteo.net> writes:

> Juri Linkov <juri <at> linkov.net> writes:
>
>>> 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.
>
> On this topic, I am not sure how good it is to do this
> automatically. One effect seems to be that context-menu-dictionary
> inserts itself at an unintended place in the menu (in my case as the
> first item, even though I'd prefer it to be further down).

It turns out the issue here is that context-menu-dictionary uses
define-key instead of define-key-after. Here is a patch to fix it:

[0001-Insert-dictionary-context-menu-at-right-position.patch (text/x-diff, inline)]
From 375390ff1e8e6fff4a9675f7430721025b31cffe Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk <at> posteo.net>
Date: Fri, 1 Oct 2021 18:27:37 +0200
Subject: [PATCH] Insert dictionary context menu at right position

* dictionary.el (context-menu-dictionary): Use define-key-after
  instead of define-key (bug#50552)
---
 lisp/net/dictionary.el | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lisp/net/dictionary.el b/lisp/net/dictionary.el
index 86447c2c35..258025fc51 100644
--- a/lisp/net/dictionary.el
+++ b/lisp/net/dictionary.el
@@ -1382,8 +1382,8 @@ context-menu-dictionary
 the context menu will contain an item that searches
 the word at mouse click."
   (when (thing-at-mouse click 'word)
-    (define-key menu [dictionary-separator] menu-bar-separator)
-    (define-key menu [dictionary-search-word-at-mouse]
+    (define-key-after menu [dictionary-separator] menu-bar-separator)
+    (define-key-after menu [dictionary-search-word-at-mouse]
       '(menu-item "Dictionary Search" dictionary-search-word-at-mouse
                   :help "Search the word at mouse click in dictionary")))
   menu)
-- 
2.30.2

[Message part 3 (text/plain, inline)]
-- 
	Philip Kaludercic

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Sat, 02 Oct 2021 19:28:02 GMT) Full text and rfc822 format available.

Message #42 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Philip Kaludercic <philipk <at> posteo.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Sat, 02 Oct 2021 22:25:10 +0300
>>> 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.
>>
>> On this topic, I am not sure how good it is to do this
>> automatically. One effect seems to be that context-menu-dictionary
>> inserts itself at an unintended place in the menu (in my case as the
>> first item, even though I'd prefer it to be further down).
>
> It turns out the issue here is that context-menu-dictionary uses
> define-key instead of define-key-after. Here is a patch to fix it:

To make the context menus in Emacs more in line with other apps
the design principles were the following:

1. When a context function can detect context with 100% reliability
   that usually achieved by using e.g. text properties on short stretches
   of the buffer like links in eww and Info manuals, or file names in Dired,
   then corresponding menu items are placed at the top of the context menus,
   like link-related items are placed first in web browsers, e.g. "Open link",
   "Save link", etc.

2. After such items comes Undo/Redo and the region-related submenu.

3. When the context around the mouse click is not 100% bound to the action,
   then it's placed further down from the above items.  So such items as
   "Look up in Manual" and "Find Definition" are placed below since
   they are activated on every word in the buffer, even on such words
   that are not valid program symbols.

4. The last items (major/minor mode menus) replicate the order of items
   from the menu-bar.

So according to this design, you are right that context-menu-dictionary
should be more below.  And it should be placed to the position 3
that has the special marker named 'middle-separator'.  Now fixed, thanks
for the suggestion.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50552; Package emacs. (Sat, 02 Oct 2021 19:30:02 GMT) Full text and rfc822 format available.

Message #45 received at 50552 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Philip Kaludercic <philipk <at> posteo.net>
Cc: 50552 <at> debbugs.gnu.org
Subject: Re: bug#50552: 28.0.50; Add context-menu-occur
Date: Sat, 02 Oct 2021 22:29:18 +0300
>> Why not use the new custom-add-choice function (or something along these
>> lines) in dictionary.el?
>
> I tried this:
>
>   (custom-add-choice 'context-menu-functions
>                      '(function-item :tag "Dictionary menu" context-menu-dictionary))
>
> But it fails with the error:
>
>   (error "Not a choice type: (repeat (choice (function-item ...")

Maybe it makes sense to implement a new function 'custom-add-repeat-choice'
that is like 'custom-add-choice' but appends to the choices inside 'repeat'?




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 31 Oct 2021 11:24:08 GMT) Full text and rfc822 format available.

This bug report was last modified 2 years and 170 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.