elisp: Finding Overlays
37.9.3 Searching for Overlays
-----------------------------
-- Function: overlays-at pos &optional sorted
This function returns a list of all the overlays that cover the
character at position POS in the current buffer. If SORTED is
non-‘nil’, the list is in decreasing order of priority, otherwise
it is in no particular order. An overlay contains position POS if
it begins at or before POS, and ends after POS.
To illustrate usage, here is a Lisp function that returns a list of
the overlays that specify property PROP for the character at point:
(defun find-overlays-specifying (prop)
(let ((overlays (overlays-at (point)))
found)
(while overlays
(let ((overlay (car overlays)))
(if (overlay-get overlay prop)
(setq found (cons overlay found))))
(setq overlays (cdr overlays)))
found))
-- Function: overlays-in beg end
This function returns a list of the overlays that overlap the
region BEG through END. An overlay overlaps with a region if it
contains one or more characters in the region; empty overlays
(empty overlay Managing Overlays.) overlap if they are at
BEG, strictly between BEG and END, or at END when END denotes the
position at the end of the buffer.
-- Function: next-overlay-change pos
This function returns the buffer position of the next beginning or
end of an overlay, after POS. If there is none, it returns
‘(point-max)’.
-- Function: previous-overlay-change pos
This function returns the buffer position of the previous beginning
or end of an overlay, before POS. If there is none, it returns
‘(point-min)’.
As an example, here’s a simplified (and inefficient) version of the
primitive function ‘next-single-char-property-change’ (Property
Search). It searches forward from position POS for the next position
where the value of a given property ‘prop’, as obtained from either
overlays or text properties, changes.
(defun next-single-char-property-change (position prop)
(save-excursion
(goto-char position)
(let ((propval (get-char-property (point) prop)))
(while (and (not (eobp))
(eq (get-char-property (point) prop) propval))
(goto-char (min (next-overlay-change (point))
(next-single-property-change (point) prop)))))
(point)))