I use hide-show to collapse certain parts of my text and I use the code below to show the number of hidden lines.
However, when a file is large enough (e.g. C++ or LaTeX) and I collapse all regions (thus creating tens of overlays), Emacs becomes really slow to the point of being unusable. Even moving the marker from one line to another takes half a second or so.
Is there a way of resolving this?
(defun display-code-line-counts (ov)
(overlay-put ov 'display
(format "...%d..."
(count-lines (overlay-start ov)
(overlay-end ov))
))
(overlay-put ov 'face '(:foreground "red" :box (:line-width 1 :style none)))
)
(setq hs-set-up-overlay 'display-code-line-counts)
EDIT: Turns out the reason emacs becomes very slow is because of the linum minor mode which creates thousands of (hidden) overlays that are collapsed with hide-show. Is there a way to fix this? Or a better line number mode?
As you said in your title, overlays can make Emacs slow.
One thing you can do, which can help sometimes, is to recenter the set of overlays around the current curson position (point
). You do that using function
overlay-recenter
.
As the Elisp manual, node Managing Overlays tells you:
This function recenters the overlays of the current buffer around position
POS
. That makes overlay lookup faster for positions nearPOS
, but slower for positions far away fromPOS
.A loop that scans the buffer forwards, creating overlays, can run faster if you do
(overlay-recenter (point-max))
first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With