Previous: Overlay Properties, Up: Overlays

38.9.3 Searching for Overlays

— Function: overlays-at pos

This function returns a list of all the overlays that cover the character at position pos in the current buffer. The list 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. “Overlap” means that at least one character is contained within the overlay and also contained within the specified region; however, empty overlays are included in the result if they are located 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 (see 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)))