Subject: Re: bug#52973: Adding a few context-menu-mode commands

From: Philip Kaludercic <[email-masked]>
Date: Tue, 04 Jan 2022 20:09:27 +0000

Juri Linkov <[email-masked]> writes:

>> I am attaching a few patches adding a few features for
>> context-menu-mode, that I mentioned and explained here:
>> https://ruzkuku.com/texts/emacs-mouse.html.
>>
>> Would it be OK to add these to Emacs itself?
>
> Thanks, it would be beneficial to enrich the packages with the
> context-menus.  Man-context-menu is a good addition to the package
> man.el, and hi-lock-context-menu to hi-lock.el.

Here an extension on my previous hi-lock commit, adding the ability to
remove highlighting:

>From 76addfdb26b172cf1cea56f868f9f3e3afec1a20 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
Date: Fri, 31 Dec 2021 12:45:52 +0100
Subject: [PATCH] Implement context-menu-mode support for hi-lock

* hi-lock.el (hi-lock-symbol-at-mouse): Add new command
(hi-lock-context-menu): Add new function
---
 lisp/hi-lock.el | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el
index 7d126cb558..0ed2def24c 100644
--- a/lisp/hi-lock.el
+++ b/lisp/hi-lock.el
@@ -854,6 +854,40 @@ hi-lock-unload-function
   ;; continue standard unloading
   nil)
 
+;;; Mouse support
+(defun hi-lock-symbol-at-mouse (click)
+  "Highlight symbol at mouse CLICK."
+  (interactive "e")
+  (save-excursion
+    (mouse-set-point click)
+    (highlight-symbol-at-point)))
+
+(defun hi-lock-unhighlight-at-mouse (click)
+  "Remove highlighting from all regular expressions at CLICK."
+  (interactive "e")
+  (save-excursion
+    (mouse-set-point click)
+    (unless (mapc #'hi-lock-unface-buffer (hi-lock--regexps-at-point))
+      (message "Nothing highlighted at point."))))
+
+(defun hi-lock-context-menu (menu click)
+  "Populate MENU with a menu item to highlight symbol at CLICK."
+  (save-excursion
+    (mouse-set-point click)
+    (let ((sym (thing-at-point 'symbol))
+          (rap  (hi-lock--regexps-at-point)))
+      (when (or sym rap)
+        (define-key-after menu [hi-lick-separator] menu-bar-separator))
+      (when sym
+        (define-key-after menu [hi-lock-add]
+          '(menu-item "Highlight Symbol" highlight-symbol-at-mouse
+                      :help "Highlight symbol at point")))
+      (when rap
+        (define-key-after menu [hi-lock-remove]
+          '(menu-item "Unhighlight Symbol" hi-lock-unhighlight-at-mouse
+                      :help "Remove highlight at point")))))
+  menu)
+
 (provide 'hi-lock)
 
 ;;; hi-lock.el ends here
-- 
2.34.0

-- 
	Philip Kaludercic