Lawrence D'Oliveiro
2024-03-29 00:41:10 UTC
Emacs has this feature called “overlays”, which let you set custom
display attributes for portions of a text buffer. This can include
completely hiding the text. That can be handy if you want to look at
two parts of a source file while temporarily ignoring some irrelevant
details in-between.
Here is a command that hides a selected region of text, replacing it
with a distinctive marker symbol, and also attaching a distinctive
category identifier for the overlay:
(defun hide_text (beg end)
(interactive "r")
(let
(
(olay (make-overlay beg end))
)
(overlay-put olay 'category 'collapsar)
(overlay-put olay
'display #("🐧\n" 0 1 (face '(:foreground "turquoise" :background "yellow")))
)
(overlay-put olay 'evaporate t)
(deactivate-mark)
) ; let
) ; hide_text
Here is a function that invokes a specified callback for all overlays
of that category that overlap a specified position:
(defun foreach_hidden_text_at (act pos)
(dolist (olay (overlays-at pos))
(when (eq (overlay-get olay 'category) 'collapsar)
(funcall act olay)
) ; when
) ; dolist
) ; foreach_hidden_text_at
And here is a command that uses foreach_hidden_text_at to reveal all
hidden text at the current position:
(defun reveal_text ()
(interactive)
(let
(
(revealed-something)
)
(foreach_hidden_text_at
(lambda (olay)
(delete-overlay olay)
(setq revealed-something t)
) ; lambda
(point)
) ; foreach_hidden_text_at
(unless revealed-something
(ding)
) ; unless
) ; let
) ; reveal_text
display attributes for portions of a text buffer. This can include
completely hiding the text. That can be handy if you want to look at
two parts of a source file while temporarily ignoring some irrelevant
details in-between.
Here is a command that hides a selected region of text, replacing it
with a distinctive marker symbol, and also attaching a distinctive
category identifier for the overlay:
(defun hide_text (beg end)
(interactive "r")
(let
(
(olay (make-overlay beg end))
)
(overlay-put olay 'category 'collapsar)
(overlay-put olay
'display #("🐧\n" 0 1 (face '(:foreground "turquoise" :background "yellow")))
)
(overlay-put olay 'evaporate t)
(deactivate-mark)
) ; let
) ; hide_text
Here is a function that invokes a specified callback for all overlays
of that category that overlap a specified position:
(defun foreach_hidden_text_at (act pos)
(dolist (olay (overlays-at pos))
(when (eq (overlay-get olay 'category) 'collapsar)
(funcall act olay)
) ; when
) ; dolist
) ; foreach_hidden_text_at
And here is a command that uses foreach_hidden_text_at to reveal all
hidden text at the current position:
(defun reveal_text ()
(interactive)
(let
(
(revealed-something)
)
(foreach_hidden_text_at
(lambda (olay)
(delete-overlay olay)
(setq revealed-something t)
) ; lambda
(point)
) ; foreach_hidden_text_at
(unless revealed-something
(ding)
) ; unless
) ; let
) ; reveal_text