Function: clojure--find-indent-spec-backtracking

clojure--find-indent-spec-backtracking is a byte-compiled function defined in clojure-mode.el.

Signature

(clojure--find-indent-spec-backtracking)

Documentation

Return the indent spec that applies to the sexp at point.

Walk up the sexp tree (up to clojure-max-backtracking levels) to find a parent form with an indent spec, then use the current position within that parent to index into its spec.

For a list spec like (1 ((:defn)) nil), position 0 yields 1, position 1 yields ((:defn)), and position 2+ yields nil. A sub-spec wrapped in a list like ((:defn)) means "this position holds a list of forms, each indented with :defn style".

Source Code

;; Defined in ~/.emacs.d/elpa/clojure-mode-20260325.811/clojure-mode.el
(defun clojure--find-indent-spec-backtracking ()
  "Return the indent spec that applies to the sexp at point.
Walk up the sexp tree (up to `clojure-max-backtracking' levels)
to find a parent form with an indent spec, then use the current
position within that parent to index into its spec.

For a list spec like (1 ((:defn)) nil), position 0 yields 1,
position 1 yields ((:defn)), and position 2+ yields nil.  A
sub-spec wrapped in a list like ((:defn)) means \"this position
holds a list of forms, each indented with :defn style\"."
  (when (and (>= clojure-max-backtracking clojure--current-backtracking-depth)
             (not (looking-at "^")))
    (let ((clojure--current-backtracking-depth (1+ clojure--current-backtracking-depth))
          (pos 0))
      ;; Count how far we are from the start of the sexp.
      (while (ignore-errors (clojure-backward-logical-sexp 1)
                            (not (or (bobp)
                                     (eq (char-before) ?\n))))
        (cl-incf pos))
      (let* ((function (thing-at-point 'symbol))
             (method (or (when function ;; Is there a spec here?
                           (clojure--get-indent-method function))
                         (ignore-errors
                           ;; Otherwise look higher up.
                           (pcase (syntax-ppss)
                             (`(,(pred (< 0)) ,start . ,_)
                              (goto-char start)
                              (clojure--find-indent-spec-backtracking)))))))
        (when (numberp method)
          (setq method (list method)))
        (pcase method
          ((pred functionp)
           (when (= pos 0)
             method))
          ((pred sequencep)
           (pcase (length method)
             (`0 nil)
             (`1 (let ((head (elt method 0)))
                   (when (or (= pos 0) (sequencep head))
                     head)))
             (l (if (>= pos l)
                    (elt method (1- l))
                  (elt method pos)))))
          ((or `defun `:defn)
           (when (= pos 0)
             :defn))
          (_
           (message "Invalid indent spec for `%s': %s" function method)
           nil))))))