unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: Augusto Stoffel <arstoffel@gmail.com>
Cc: 62250@debbugs.gnu.org
Subject: bug#62250: 29.0.60; Allow context menu from text properties to not override everything
Date: Sun, 02 Apr 2023 19:35:15 +0300	[thread overview]
Message-ID: <86a5zqcke4.fsf@mail.linkov.net> (raw)
In-Reply-To: <86fsa0r60v.fsf@mail.linkov.net> (Juri Linkov's message of "Sun,  19 Mar 2023 19:43:28 +0200")

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

>> Just for the record, this would be useful to create a
>> context menu for images.
>
> The patch below will allow using the text property like
>
>   'context-menu-functions '(image-context-menu)

Here is a complete patch that uses the new text property
for create a context menu for images:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: context-menu-functions.patch --]
[-- Type: text/x-diff, Size: 4238 bytes --]

diff --git a/lisp/iimage.el b/lisp/iimage.el
index b4c175a7b63..8235c4a6fdb 100644
--- a/lisp/iimage.el
+++ b/lisp/iimage.el
@@ -134,6 +134,7 @@ iimage-mode-buffer
                                     :max-width (- (nth 2 edges) (nth 0 edges))
 				    :max-height (- (nth 3 edges) (nth 1 edges)))
                      keymap ,image-map
+                     context-menu-functions (image-context-menu)
                      modification-hooks
                      (iimage-modification-hook)))
                 (remove-list-of-text-properties
diff --git a/lisp/image.el b/lisp/image.el
index 2372fd1ce09..11bf46c80cc 100644
--- a/lisp/image.el
+++ b/lisp/image.el
@@ -188,6 +188,29 @@ image-map
   "C-<wheel-up>"   #'image-mouse-increase-size
   "C-<mouse-4>"    #'image-mouse-increase-size)
 
+(defun image-context-menu (menu click)
+  "Populate MENU with image-related commands at CLICK."
+  (when (mouse-posn-property (event-start click) 'display)
+    (define-key menu [image-separator] menu-bar-separator)
+    (let ((easy-menu (make-sparse-keymap "Image")))
+      (easy-menu-define nil easy-menu nil
+        '("Image"
+          ["Zoom In" image-increase-size
+           :help "Enlarge the image"]
+          ["Zoom Out" image-decrease-size
+           :help "Shrink the image"]
+          ["Rotate Clockwise" image-rotate
+           :help "Rotate the image"]
+          ["Flip horizontally" image-flip-horizontally
+           :help "Flip horizontally"]
+          ["Flip vertically" image-flip-vertically
+           :help "Flip vertically"]))
+      (dolist (item (reverse (lookup-key easy-menu [menu-bar image])))
+        (when (consp item)
+          (define-key menu (vector (car item)) (cdr item))))))
+
+  menu)
+
 (defun image-load-path-for-library (library image &optional path no-error)
   "Return a suitable search path for images used by LIBRARY.
 
@@ -615,6 +638,7 @@ put-image
       (overlay-put overlay 'put-image t)
       (overlay-put overlay 'before-string string)
       (overlay-put overlay 'keymap image-map)
+      (overlay-put overlay 'context-menu-functions '(image-context-menu))
       overlay)))
 
 
@@ -665,7 +689,9 @@ insert-image
 				      image)
                                    rear-nonsticky t
 				   inhibit-isearch ,inhibit-isearch
-                                   keymap ,image-map))))
+                                   keymap ,image-map
+                                   context-menu-functions
+                                   (image-context-menu)))))
 
 
 ;;;###autoload
@@ -702,7 +728,9 @@ insert-sliced-image
 	  (add-text-properties start (point)
 			       `(display ,(list (list 'slice x y dx dy) image)
 					 rear-nonsticky (display)
-                                         keymap ,image-map))
+                                         keymap ,image-map
+                                         context-menu-functions
+                                         (image-context-menu)))
 	  (setq x (+ x dx))))
       (setq x 0.0
 	    y (+ y dy))
diff --git a/lisp/mouse.el b/lisp/mouse.el
index 6b9accd6758..2cd2840bad9 100644
--- a/lisp/mouse.el
+++ b/lisp/mouse.el
@@ -368,9 +368,10 @@ context-menu-map
 the function `context-menu-filter-function'."
   (let* ((menu (make-sparse-keymap (propertize "Context Menu" 'hide t)))
          (click (or click last-input-event))
-         (window (posn-window (event-start click)))
-         (fun (mouse-posn-property (event-start click)
-                                   'context-menu-function)))
+         (start (event-start click))
+         (window (posn-window start))
+         (fun (mouse-posn-property start 'context-menu-function))
+         (funs (mouse-posn-property start 'context-menu-functions)))
 
     (unless (eq (selected-window) window)
       (select-window window))
@@ -380,7 +381,9 @@ context-menu-map
       (run-hook-wrapped 'context-menu-functions
                         (lambda (fun)
                           (setq menu (funcall fun menu click))
-                          nil)))
+                          nil))
+      (dolist (fun funs)
+        (setq menu (funcall fun menu click))))
 
     ;; Remove duplicate separators as well as ones at the beginning or
     ;; end of the menu.

  parent reply	other threads:[~2023-04-02 16:35 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-18  7:51 bug#62250: 29.0.60; Allow context menu from text properties to not override everything Augusto Stoffel
2023-03-18  8:31 ` Eli Zaretskii
2023-03-18 11:19   ` Augusto Stoffel
2023-03-18 11:24     ` Eli Zaretskii
2023-03-18 11:29       ` Augusto Stoffel
2023-03-18 11:33         ` Eli Zaretskii
2023-03-18 18:10     ` Juri Linkov
2023-03-18 18:44       ` Augusto Stoffel
2023-03-19 17:40         ` Juri Linkov
2023-03-19 18:10           ` Augusto Stoffel
2023-03-20 18:29             ` Juri Linkov
2023-03-18 18:08 ` Juri Linkov
2023-03-18 18:39   ` Augusto Stoffel
2023-03-19 17:43     ` Juri Linkov
2023-03-19 18:21       ` Augusto Stoffel
2023-03-20 18:34         ` Juri Linkov
2023-04-02 16:35       ` Juri Linkov [this message]
2024-03-05 16:51         ` 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=86a5zqcke4.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=62250@debbugs.gnu.org \
    --cc=arstoffel@gmail.com \
    /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).