From 1a25e95babf60678fad11b03d36d6b1f1ab90cc8 Mon Sep 17 00:00:00 2001 From: Vadim Rodionov Date: Thu, 8 Jun 2023 13:33:23 +0600 Subject: [PATCH 1/3] Fix infinite loop in clojure-sexp-starts-until-position --- clojure-mode.el | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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))) From da4657588fbc3b53cfff13d1432f75733e80f4a4 Mon Sep 17 00:00:00 2001 From: Vadim Rodionov Date: Thu, 8 Jun 2023 13:33:40 +0600 Subject: [PATCH 2/3] Add tests for clojure-sexp-starts-until-position --- test/clojure-mode-sexp-test.el | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 From 267afc92c4f804cac5f49e86056a915bc7a6b597 Mon Sep 17 00:00:00 2001 From: Vadim Rodionov Date: Thu, 8 Jun 2023 13:33:52 +0600 Subject: [PATCH 3/3] Mention issue #586 in changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) 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