Discussion:
Hiding/Showing Text
(too old to reply)
Lawrence D'Oliveiro
2024-03-29 00:41:10 UTC
Permalink
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
Janis Papanagnou
2024-03-29 09:54:57 UTC
Permalink
Post by Lawrence D'Oliveiro
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.
[...]
I was astonished to read that there's a necessity to add such
functions to Emacs. Would have expected that it's there already
as Emacs being one of the prominent and widely used editors.

If I inspect https://en.wikipedia.org/wiki/Code_folding the table
shows three (of four) "Yes" for folding support, albeit marked
with footnotes (which might indicate some restriction/workaround?).

Since I'm not using that editor I'm just curious what deficiency
the posted functions solve or add to the existing folding features
in Emacs as seen in Wikipedia. (From the code I cannot derive that,
so I'm asking.)

Janis
Johanne Fairchild
2024-03-29 11:30:26 UTC
Permalink
Post by Lawrence D'Oliveiro
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.
That's one of the fundamental objectives of literate programming. By
the way, comp.programming.literate has been restored very recently.
Lawrence D'Oliveiro
2024-04-08 00:03:59 UTC
Permalink
Post by Johanne Fairchild
That's one of the fundamental objectives of literate programming.
As far as I’m aware, “literate programming” is only a way of presenting
code, not for letting you mess around with it. Jupyter notebooks not only
let you present code, they also encourage experimentation. And they let
the code itself produce rich output, like images, audio, video and even
interactive widgets.
Johanne Fairchild
2024-04-08 10:59:11 UTC
Permalink
Post by Lawrence D'Oliveiro
Post by Johanne Fairchild
That's one of the fundamental objectives of literate programming.
As far as I’m aware, “literate programming” is only a way of presenting
code, not for letting you mess around with it.
Literate programming allows you take entire chunks of error verification
out of the way by replacing them with a chunk tag. (Now you look at the
relevant part of your code without having to look at irrelevant parts.)
When you want to compile your program, the literate programming tool
replaces the chunk tag with the code to which it refers.
Lawrence D'Oliveiro
2024-04-09 00:45:39 UTC
Permalink
Post by Johanne Fairchild
Post by Lawrence D'Oliveiro
Post by Johanne Fairchild
That's one of the fundamental objectives of literate programming.
As far as I’m aware, “literate programming” is only a way of presenting
code, not for letting you mess around with it.
Literate programming allows you take entire chunks of error verification
out of the way by replacing them with a chunk tag.
Interesting, but maybe not very useful nowadays.

Axel Reichert
2024-04-08 14:45:15 UTC
Permalink
Post by Lawrence D'Oliveiro
As far as I’m aware, “literate programming” is only a way of presenting
code, not for letting you mess around with it. Jupyter notebooks not only
let you present code, they also encourage experimentation. And they let
the code itself produce rich output, like images, audio, video and even
interactive widgets.
Have a look at org-babel in Emacs.

Axel
Loading...