Function: text-property-search-forward

text-property-search-forward is an autoloaded, interactive and byte-compiled function defined in text-property-search.el.gz.

Signature

(text-property-search-forward PROPERTY &optional VALUE PREDICATE NOT-CURRENT)

Documentation

Search for next region of text where PREDICATE returns non-nil for PROPERTY.

PREDICATE is used to decide whether the value of PROPERTY at a given buffer position should be considered as a match for VALUE. VALUE defaults to nil if omitted.

If PREDICATE is a function, it will be called with two arguments: VALUE and the value of PROPERTY at some buffer position. The function should return non-nil if these two values are to be considered a match.

Two special values of PREDICATE can also be used: If PREDICATE is t, that means the value of PROPERTY must equal VALUE to be considered a match. If PREDICATE is nil (which is the default), the value of PROPERTY will match if it is not equal to VALUE. Furthermore, a nil PREDICATE means that the match region ends where the value changes. For instance, this means that if you loop with

  (while (setq prop (text-property-search-forward 'face))
    ...)

you will get all the distinct regions with non-nil face values in the buffer, and the prop object will have the details about the match. See the manual for more details and examples about how VALUE and PREDICATE interact.

If NOT-CURRENT is non-nil, current buffer position is not examined for matches: the function will search for the first region that doesn't include point and has a value of PROPERTY that matches VALUE.

If no matches can be found, return nil and don't move point. If found, move point to the end of the region and return a prop-match object describing the match. To access the details of the match, use prop-match-beginning and prop-match-end for the buffer positions that limit the region, and prop-match-value for the value of PROPERTY in the region.

Other relevant functions are documented in the text-properties group.

View in manual

Probably introduced at or before Emacs version 27.1.

Key Bindings

Shortdoc

;; text-properties
(text-property-search-forward 'face nil t)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/text-property-search.el.gz
(defun text-property-search-forward (property &optional value predicate
                                              not-current)
  "Search for next region of text where PREDICATE returns non-nil for PROPERTY.
PREDICATE is used to decide whether the value of PROPERTY at a given
buffer position should be considered as a match for VALUE.
VALUE defaults to nil if omitted.

If PREDICATE is a function, it will be called with two arguments:
VALUE and the value of PROPERTY at some buffer position.  The function
should return non-nil if these two values are to be considered a match.

Two special values of PREDICATE can also be used:
If PREDICATE is t, that means the value of PROPERTY must `equal' VALUE
to be considered a match.
If PREDICATE is nil (which is the default), the value of PROPERTY will
match if it is not `equal' to VALUE.  Furthermore, a nil PREDICATE
means that the match region ends where the value changes.  For
instance, this means that if you loop with

  (while (setq prop (text-property-search-forward \\='face))
    ...)

you will get all the distinct regions with non-nil `face' values in
the buffer, and the `prop' object will have the details about the
match.  See the manual for more details and examples about how
VALUE and PREDICATE interact.

If NOT-CURRENT is non-nil, current buffer position is not examined for
matches: the function will search for the first region that doesn't
include point and has a value of PROPERTY that matches VALUE.

If no matches can be found, return nil and don't move point.
If found, move point to the end of the region and return a
`prop-match' object describing the match.  To access the details
of the match, use `prop-match-beginning' and `prop-match-end' for
the buffer positions that limit the region, and `prop-match-value'
for the value of PROPERTY in the region."
  (interactive
   (list
    (let ((string (completing-read "Search for property: " obarray)))
      (when (> (length string) 0)
        (intern string obarray)))))
  (cond
   ;; No matches at the end of the buffer.
   ((eobp)
    nil)
   ;; We're standing in the property we're looking for, so find the
   ;; end.
   ((and (text-property--match-p value (get-text-property (point) property)
                                 predicate)
         (not not-current))
    (text-property--find-end-forward (point) property value predicate))
   (t
    (let ((origin (point))
          (ended nil)
          pos)
      ;; Find the next candidate.
      (while (not ended)
        (setq pos (next-single-property-change (point) property))
        (if (not pos)
            (progn
              (goto-char origin)
              (setq ended t))
          (goto-char pos)
          (if (text-property--match-p value (get-text-property (point) property)
                                      predicate)
              (setq ended
                    (text-property--find-end-forward
                     (point) property value predicate))
            ;; Skip past this section of non-matches.
            (setq pos (next-single-property-change (point) property))
            (unless pos
              (goto-char origin)
              (setq ended t)))))
      (and (not (eq ended t))
           ended)))))