Subject: Adding a few context-menu-mode commands

From: Philip Kaludercic <[email-masked]>
Date: Mon, 03 Jan 2022 08:34:18 +0000

Tags: patch

Hi,

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?

In GNU Emacs 29.0.50 (build 5, x86_64-pc-linux-gnu, GTK+ Version 3.24.30, cairo version 1.16.0)
 of 2021-12-25 built on viero
Repository revision: f9d518bc2f333e03d5e7c4e935b08fbe19f33c5b
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
>From 17cd2634df3f71155efa5b9e534850f473b5b05e Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
Date: Fri, 31 Dec 2021 12:37:50 +0100
Subject: [PATCH 1/3] Implement context-menu-mode support for man pages

* man.el (Man-at-mouse): Add new command
(Man-context-menu): Add new function
---
 lisp/man.el | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/lisp/man.el b/lisp/man.el
index fff31baa5f..2f956525d3 100644
--- a/lisp/man.el
+++ b/lisp/man.el
@@ -1976,6 +1976,29 @@ Man-bookmark-jump
     (bookmark-default-handler
      `("" (buffer . ,buf) . ,(bookmark-get-bookmark-record bookmark)))))
 
+;;; Mouse support
+(defun Man-at-mouse (e)
+  "Open man manual at point."
+  (interactive "e")
+  (save-excursion
+    (mouse-set-point e)
+    (man (Man-default-man-entry))))
+
+(defun Man-context-menu (menu click)
+  "Populate MENU with commands that open a man page at point."
+  (save-excursion
+    (mouse-set-point click)
+    (when (save-excursion
+            (skip-syntax-backward "^ ")
+            (and (looking-at
+                  "[[:space:]]*\\([[:alnum:]_-]+([[:alnum:]]+)\\)")
+                  (match-string 1)))
+      (define-key-after menu [man-separator] menu-bar-separator)
+      (define-key-after menu [man-at-mouse]
+    '(menu-item "Open man page" man-at-mouse
+            :help "Open man page around mouse click"))))
+  menu)
+
 
 ;; Init the man package variables, if not already done.
 (Man-init-defvars)
-- 
2.34.0

>From d08f6ef980b1e6464af112f692cfdaed1a6d7256 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
Date: Fri, 31 Dec 2021 12:45:52 +0100
Subject: [PATCH 2/3] 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 | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el
index 7d126cb558..5d99fb5288 100644
--- a/lisp/hi-lock.el
+++ b/lisp/hi-lock.el
@@ -854,6 +854,25 @@ hi-lock-unload-function
   ;; continue standard unloading
   nil)
 
+;;; Mouse support
+(defun hi-lock-symbol-at-mouse (event)
+  "Highlight symbol at mouse click EVENT."
+  (interactive "e")
+  (save-excursion
+    (mouse-set-point event)
+    (highlight-symbol-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)
+    (when (symbol-at-point)
+      (define-key-after menu [highlight-search-separator] menu-bar-separator)
+      (define-key-after menu [highlight-search-mouse]
+        '(menu-item "Highlight Symbol" highlight-symbol-at-mouse
+                    :help "Highlight symbol at point"))))
+  menu)
+
 (provide 'hi-lock)
 
 ;;; hi-lock.el ends here
-- 
2.34.0

>From c5fbec3e04c5e4ef641ff8f99d9c2a58f3c8a770 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <[email-masked]>
Date: Fri, 31 Dec 2021 15:21:13 +0100
Subject: [PATCH 3/3] Add command to invoke a search engine

* mouse.el (context-menu-online-search): Add new function
(eww-search-prefix): Declare variable from eww.el
(mouse-online-search-at-point): Add new command
---
 lisp/mouse.el | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/lisp/mouse.el b/lisp/mouse.el
index 11fdd3f639..edf4504bdd 100644
--- a/lisp/mouse.el
+++ b/lisp/mouse.el
@@ -523,6 +523,16 @@ context-menu-ffap
                     :help "Find file or URL from text around mouse click"))))
   menu)
 
+(defun context-menu-online-search (menu click)
+  "Populate MENU with command to search online."
+  (save-excursion
+    (mouse-set-point click)
+    (define-key-after menu [online-search-separator] menu-bar-separator)
+    (define-key-after menu [online-search-at-mouse]
+      '(menu-item "Online search" mouse-online-search-at-point
+                  :help "Search for region or word online")))
+  menu)
+
 (defvar context-menu-entry
   `(menu-item ,(purecopy "Context Menu") ,(make-sparse-keymap)
               :filter ,(lambda (_) (context-menu-map)))
@@ -3209,6 +3219,26 @@ mouse-drag-and-drop-region
         (with-current-buffer (window-buffer window)
           (setq cursor-type (nth 3 state)))))))
 
+(defvar eww-search-prefix)
+(defun mouse-online-search-at-point (event)
+  "Query an online search engine at EVENT.
+If a region is active, the entire region will be sent, otherwise
+the symbol at point will be used.  This command uses EWW's
+default search engine, as configured by `eww-search-prefix'."
+  (interactive "e")
+  (require 'eww)
+  (let ((query (if (use-region-p)
+                   (buffer-substring (region-beginning)
+                                     (region-end))
+                 (save-excursion
+                   (mouse-set-point event)
+                   (thing-at-point 'symbol)))))
+    (unless query
+      (user-error "Nothing to search for"))
+    (browse-url (concat
+                 eww-search-prefix
+                 (mapconcat #'url-hexify-string (split-string query) "+")))))
+
 
 ;;; Bindings for mouse commands.
 
-- 
2.34.0

-- 
	Philip Kaludercic