Function: text-property-search-backward

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

Signature

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

Documentation

Search for the previous region of text whose PROPERTY matches VALUE.

Like text-property-search-forward, which see, but searches backward, and if a matching region is found, place point at the start of the region.

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

Probably introduced at or before Emacs version 27.1.

Key Bindings

Shortdoc

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

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/text-property-search.el.gz
(defun text-property-search-backward (property &optional value predicate
                                               not-current)
  "Search for the previous region of text whose PROPERTY matches VALUE.

Like `text-property-search-forward', which see, but searches backward,
and if a matching region is found, place point at the start of the region."
  (interactive
   (list
    (let ((string (completing-read "Search for property: " obarray)))
      (when (> (length string) 0)
        (intern string obarray)))))
  (cond
   ;; We're at the start of the buffer; no previous matches.
   ((bobp)
    nil)
   ;; We're standing in the property we're looking for, so find the
   ;; end.
   ((text-property--match-p
     value (get-text-property (1- (point)) property)
     predicate)
    (let ((origin (point))
          (match (text-property--find-end-backward
                  (1- (point)) property value predicate)))
      ;; When we want to ignore the current element, then repeat the
      ;; search if we haven't moved out of it yet.
      (if (and not-current
               (equal (get-text-property (point) property)
                      (get-text-property origin property)))
          (text-property-search-backward property value predicate)
        match)))
   (t
    (let ((origin (point))
          (ended nil)
          pos)
      (forward-char -1)
      ;; Find the previous candidate.
      (while (not ended)
        (setq pos (previous-single-property-change (point) property))
        (if (not pos)
            (progn
              (goto-char origin)
              (setq ended t))
          (goto-char (1- pos))
          (if (text-property--match-p value (get-text-property (point) property)
                                      predicate)
              (setq ended
                    (text-property--find-end-backward
                     (point) property value predicate))
            ;; Skip past this section of non-matches.
            (setq pos (previous-single-property-change (point) property))
            (unless pos
              (goto-char origin)
              (setq ended t)))))
      (and (not (eq ended t))
           ended)))))