Function: treesit-outline-search

treesit-outline-search is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit-outline-search &optional BOUND MOVE BACKWARD LOOKING-AT)

Documentation

Search for the next outline heading in the syntax tree.

For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in outline-search-function.

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit-outline-search (&optional bound move backward looking-at)
  "Search for the next outline heading in the syntax tree.
For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in
`outline-search-function'."
  (treesit--pre-redisplay)
  (if looking-at
      (when (treesit-outline--at-point) (pos-bol))

    (let* ((bob-pos
            ;; `treesit-navigate-thing' can't find a thing at bobp,
            ;; so use `looking-at' to match at bobp.
            (and (bobp) (treesit-outline-search bound move backward t) (point)))
           (pos
            ;; When function wants to find the current outline, point
            ;; is at the beginning of the current line.  When it wants
            ;; to find the next outline, point is at the second column.
            (unless bob-pos
              (if (eq (point) (pos-bol))
                  (if (bobp) (point) (1- (point)))
                (pos-eol))))
           (pred (unless bob-pos
                   (if treesit-aggregated-outline-predicate
                       (alist-get (treesit-language-at pos)
                                  treesit-aggregated-outline-predicate)
                     treesit-outline-predicate)))
           (found (or bob-pos
                      (treesit-navigate-thing pos (if backward -1 1) 'beg pred)))
           (closest (when (and treesit-aggregated-outline-predicate (not bob-pos))
                      (if backward
                          (previous-single-char-property-change pos 'treesit-parser)
                        (next-single-char-property-change pos 'treesit-parser)))))

      ;; Handle multi-language modes.
      (while (and closest
                  (not (eq closest (if backward (point-min) (point-max))))
                  (or
                   ;; Possibly was inside the local parser, and when can't find
                   ;; more matches inside it then need to go over the closest
                   ;; parser boundary to the primary parser, and search again.
                   (not found)
                   ;; Possibly skipped the local parser, either while navigating
                   ;; inside the primary parser, or inside a local parser
                   ;; interspersed by ranges of other local parsers, e.g.
                   ;; <html><script>|</script><style/><script/></html>
                   (if backward (> closest found) (< closest found))))
        (goto-char (if backward
                       (max (point-min) (1- closest))
                     (min (point-max) (1+ closest))))
        (setq pos (if (eq (point) (pos-bol))
                      (if (bobp) (point) (1- (point)))
                    (pos-eol))
              pred (alist-get (treesit-language-at pos)
                              treesit-aggregated-outline-predicate)
              found (treesit-navigate-thing pos (if backward -1 1) 'beg pred)
              closest (if backward
                          (previous-single-char-property-change pos 'treesit-parser)
                        (next-single-char-property-change pos 'treesit-parser))))

      (if found
          (if (or (not bound) (if backward (>= found bound) (<= found bound)))
              (progn
                (goto-char found)
                (goto-char (pos-bol))
                (set-match-data (list (point) (pos-eol)))
                t)
            (when move (goto-char bound))
            nil)
        (when move (goto-char (or bound (if backward (point-min) (point-max)))))
        nil))))