Skip to content

Instantly share code, notes, and snippets.

@wasamasa
Last active May 13, 2021 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wasamasa/5d3b8b51263b90669fdb5501e18f434c to your computer and use it in GitHub Desktop.
Save wasamasa/5d3b8b51263b90669fdb5501e18f434c to your computer and use it in GitHub Desktop.
(defun my-image-map-key-handler (posn)
(let* ((area (symbol-name (posn-area posn)))
(color (capitalize (cadr (split-string area "-")))))
(message "%s" color)))
(defun my-image-map-key-filter (_map)
(let ((e last-input-event))
(when (mouse-event-p e)
;; implicitly returns nil if not all conditions were satisfied,
;; thereby allowing other keys to pass through
(let ((posn (event-start e)))
(when (posn-area posn)
;; only call handler on mouse-up, not mouse-down
(when (and (consp e) (eq (car e) 'mouse-1))
(my-image-map-key-handler posn))
'ignore)))))
(defun my-image-map-demo ()
(interactive)
(let ((buf (get-buffer-create "*image-map*")))
(with-current-buffer buf
(let* ((svg "<svg width=\"150\" height=\"150\"><g><rect x=\"25\" y=\"25\" width=\"50\" height=\"50\" fill=\"red\"></rect><rect x=\"75\" y=\"25\" width=\"50\" height=\"50\" fill=\"green\"></rect><rect x=\"25\" y=\"75\" width=\"50\" height=\"50\" fill=\"blue\"></rect><rect x=\"75\" y=\"75\" width=\"50\" height=\"50\" fill=\"yellow\"></rect></g></svg>")
(image-map '(((rect . ((25 . 25) . (75 . 75)))
area-red
(help-echo "red" pointer hourglass))
((rect . ((75 . 25) . (125 . 75)))
area-green
(help-echo "green" pointer vdrag))
((rect . ((25 . 75) . (75 . 125)))
area-blue
(help-echo "blue" pointer hdrag))
((rect . ((75 . 75) . (125 . 125)))
area-yellow
(help-echo "yellow" pointer hand))))
(keymap (let ((map (make-sparse-keymap)))
(define-key map [t]
`(menu-item "" nil :filter my-image-map-key-filter))
map))
(image (propertize " "
'display `(image :type svg
:data ,svg
:map ,image-map)
'keymap keymap))
buffer-read-only)
(erase-buffer)
(insert image)))
(display-buffer buf)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment