diff --git a/CHANGELOG.md b/CHANGELOG.md index f783d11d..30988889 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ * Font-lock Lein's `defproject` as a keyword. +### Bugs fixed + +* [#586](https://github.com/clojure-emacs/clojure-mode/issues/586): Fix infinite loop when opening file containing `comment` with `clojure-toplevel-inside-comment-form` set to `t`. + ## 5.16.0 (2022-12-14) ### Changes diff --git a/clojure-mode.el b/clojure-mode.el index a5f75316..320eca1c 100644 --- a/clojure-mode.el +++ b/clojure-mode.el @@ -2264,8 +2264,11 @@ position before the current position." (while (< (point) position) (clojure-forward-logical-sexp 1) (clojure-backward-logical-sexp 1) - (push (point) sexp-positions) - (clojure-forward-logical-sexp 1)) + ;; Needed to prevent infinite recursion when there's only 1 form in buffer. + (if (eq (point) (car sexp-positions)) + (goto-char position) + (push (point) sexp-positions) + (clojure-forward-logical-sexp 1))) (scan-error nil)) sexp-positions))) diff --git a/test/clojure-mode-sexp-test.el b/test/clojure-mode-sexp-test.el index de4bea7c..f82552a0 100644 --- a/test/clojure-mode-sexp-test.el +++ b/test/clojure-mode-sexp-test.el @@ -169,6 +169,24 @@ (goto-char (point-max)) (expect (clojure-find-ns) :to-equal expected))))))) +(describe "clojure-sexp-starts-until-position" + (it "should return starting points for forms after POINT until POSITION" + (with-clojure-buffer "(run 1) (def b 2) (slurp \"file\")\n" + (goto-char (point-min)) + (expect (not (cl-set-difference '(19 9 1) + (clojure-sexp-starts-until-position (point-max))))))) + + (it "should return starting point for a single form in buffer after POINT" + (with-clojure-buffer "comment\n" + (goto-char (point-min)) + (expect (not (cl-set-difference '(1) + (clojure-sexp-starts-until-position (point-max))))))) + + (it "should return nil if POSITION is behind POINT" + (with-clojure-buffer "(run 1) (def b 2)\n" + (goto-char (point-max)) + (expect (not (clojure-sexp-starts-until-position (- (point-max) 1))))))) + (provide 'clojure-mode-sexp-test) ;;; clojure-mode-sexp-test.el ends here