I've been trying to add the ability to unfold with mouse by clicking on ellipsis in my json-mode
, but after considerable time spent experimenting and looking through the Elisp manual, I can't figure out how to bind a function to clicking on the overlay I add when I fold.
Currently, my folding function looks like this:
(defun json-mode-hide-region (beg end)
"Hides region from BEG to END with an overlay."
(let ((overlay (make-overlay beg end))
(map (make-sparse-keymap)))
(define-key map [follow-link] (kbd "mouse-2"))
(define-key map (kbd "mouse-2")
(lambda (&rest args)
(interactive)
(delete-overlay overlay)))
;; FIXME: show hidden content in isearch before it's finished
(overlay-put overlay 'invisible t)
(overlay-put overlay 'isearch-open-invisible #'delete-overlay)
(overlay-put overlay 'display json-mode-fold-ellipsis)
(overlay-put overlay 'mouse-face 'highlight)
(overlay-put overlay 'help-echo "mouse-2: unfold")
(overlay-put overlay 'keymap map)
(overlay-put overlay 'evaporate t)))
It's worth noting that I use lexical-binding
to have proper lexical scoping and closures in my code, the entirety of which is in this repo. I've tried moving out the function that handles mouse-2 to a separate function, and even throwing errors in it, but it seems to never get called. It seems that documentation about making clickable buttons and links is mostly related to text properties, and overlays which don't make the original text invisible.
Does anyone know how to turn the display
text into a link that calls a function when clicked (which deletes the overlay in my case)?
isearch
? The mere fact that the code sample I put up hasisearch
-related code isn't relevant to the actual question at all.