Skip to content

Commit 498a3cd

Browse files
committed
Add a command to toggle between when and when-not
1 parent cc3a4d6 commit 498a3cd

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## master (unreleased)
44

5+
### New features
6+
7+
* New interactive command `clojure-cycle-when`.
8+
59
## 5.6.1 (2016-12-21)
610

711
### Bugs fixed

clojure-mode.el

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ Out-of-the box clojure-mode understands lein, boot and gradle."
223223
(define-key map (kbd "#") #'clojure-convert-collection-to-set)
224224
(define-key map (kbd "C-i") #'clojure-cycle-if)
225225
(define-key map (kbd "i") #'clojure-cycle-if)
226+
(define-key map (kbd "C-w") #'clojure-cycle-when)
227+
(define-key map (kbd "w") #'clojure-cycle-when)
226228
(define-key map (kbd "n i") #'clojure-insert-ns-form)
227229
(define-key map (kbd "n h") #'clojure-insert-ns-form-at-point)
228230
(define-key map (kbd "n u") #'clojure-update-ns)
@@ -246,6 +248,7 @@ Out-of-the box clojure-mode understands lein, boot and gradle."
246248
["Align expression" clojure-align]
247249
["Cycle privacy" clojure-cycle-privacy]
248250
["Cycle if, if-not" clojure-cycle-if]
251+
["Cycle when, when-not" clojure-cycle-when]
249252
("ns forms"
250253
["Insert ns form at the top" clojure-insert-ns-form]
251254
["Insert ns form here" clojure-insert-ns-form-at-point]
@@ -2094,6 +2097,31 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-cycle-if"
20942097
(forward-sexp 2)
20952098
(transpose-sexps 1)))))
20962099

2100+
(defun clojure--goto-when ()
2101+
"Find the first surrounding when or when-not expression."
2102+
(when (in-string-p)
2103+
(while (or (not (looking-at "("))
2104+
(in-string-p))
2105+
(backward-char)))
2106+
(while (not (looking-at "\\((when \\)\\|\\((when-not \\)"))
2107+
(condition-case nil
2108+
(backward-up-list)
2109+
(scan-error (user-error "No when or when-not found")))))
2110+
2111+
;;;###autoload
2112+
(defun clojure-cycle-when ()
2113+
"Change a surrounding when to when-not, or vice-versa."
2114+
(interactive)
2115+
(save-excursion
2116+
(clojure--goto-when)
2117+
(cond
2118+
((looking-at "(when-not")
2119+
(forward-char 9)
2120+
(delete-char -4))
2121+
((looking-at "(when")
2122+
(forward-char 5)
2123+
(insert "-not")))))
2124+
20972125
;;; let related stuff
20982126

20992127
(defvar clojure--let-regexp

test/clojure-mode-cycling-test.el

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,37 @@
113113
(search-forward "BBB))")
114114
(clojure-cycle-if))
115115

116+
(def-refactor-test test-cycle-when-inner-when
117+
"(when this
118+
(when that
119+
(aaa)
120+
(bbb))
121+
(ccc))"
122+
"(when this
123+
(when-not that
124+
(aaa)
125+
(bbb))
126+
(ccc))"
127+
(beginning-of-buffer)
128+
(search-forward "bbb)")
129+
(clojure-cycle-when))
130+
131+
(def-refactor-test test-cycle-when-outer-when
132+
"(when-not this
133+
(when that
134+
(aaa)
135+
(bbb))
136+
(ccc))"
137+
"(when this
138+
(when that
139+
(aaa)
140+
(bbb))
141+
(ccc))"
142+
(beginning-of-buffer)
143+
(search-forward "bbb))")
144+
(clojure-cycle-when))
145+
146+
116147
(provide 'clojure-mode-cycling-test)
117148

118149
;;; clojure-mode-cycling-test.el ends here

0 commit comments

Comments
 (0)