Skip to content

Commit e0138ef

Browse files
committed
Extract clojure--goto-form to deduplicate goto-if and goto-when
Replace the nearly identical clojure--goto-if and clojure--goto-when with a shared clojure--goto-form helper parameterized by form name.
1 parent 331db3f commit e0138ef

1 file changed

Lines changed: 10 additions & 20 deletions

File tree

clojure-mode.el

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,16 +2760,18 @@ by unwrapping it an wrapping it between COLL-OPEN and COLL-CLOSE."
27602760
"Check whether the point is currently in a comment."
27612761
(nth 4 (syntax-ppss)))
27622762

2763-
(defun clojure--goto-if ()
2764-
"Find the first surrounding if or if-not expression."
2763+
(defun clojure--goto-form (name)
2764+
"Find the first surrounding form matching NAME or NAME-not.
2765+
NAME is a string like \"if\" or \"when\"."
27652766
(when (clojure--in-string-p)
27662767
(while (or (not (looking-at "("))
27672768
(clojure--in-string-p))
27682769
(backward-char)))
2769-
(while (not (looking-at "\\((if \\)\\|\\((if-not \\)"))
2770-
(condition-case nil
2771-
(backward-up-list)
2772-
(scan-error (user-error "No if or if-not found")))))
2770+
(let ((regexp (concat "\\((" name " \\)\\|\\((" name "-not \\)")))
2771+
(while (not (looking-at regexp))
2772+
(condition-case nil
2773+
(backward-up-list)
2774+
(scan-error (user-error "No %s or %s-not found" name name))))))
27732775

27742776
;;;###autoload
27752777
(defun clojure-cycle-if ()
@@ -2778,7 +2780,7 @@ by unwrapping it an wrapping it between COLL-OPEN and COLL-CLOSE."
27782780
See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-cycle-if"
27792781
(interactive)
27802782
(save-excursion
2781-
(clojure--goto-if)
2783+
(clojure--goto-form "if")
27822784
(cond
27832785
((looking-at "(if-not")
27842786
(forward-char 3)
@@ -2791,24 +2793,12 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-cycle-if"
27912793
(forward-sexp 2)
27922794
(transpose-sexps 1)))))
27932795

2794-
;; TODO: Remove code duplication with `clojure--goto-if'.
2795-
(defun clojure--goto-when ()
2796-
"Find the first surrounding when or when-not expression."
2797-
(when (clojure--in-string-p)
2798-
(while (or (not (looking-at "("))
2799-
(clojure--in-string-p))
2800-
(backward-char)))
2801-
(while (not (looking-at "\\((when \\)\\|\\((when-not \\)"))
2802-
(condition-case nil
2803-
(backward-up-list)
2804-
(scan-error (user-error "No when or when-not found")))))
2805-
28062796
;;;###autoload
28072797
(defun clojure-cycle-when ()
28082798
"Change a surrounding when to when-not, or vice-versa."
28092799
(interactive)
28102800
(save-excursion
2811-
(clojure--goto-when)
2801+
(clojure--goto-form "when")
28122802
(cond
28132803
((looking-at "(when-not")
28142804
(forward-char 9)

0 commit comments

Comments
 (0)