Skip to content

Commit 0113aa9

Browse files
committed
Add a command to toggle negation for an expression
1 parent 498a3cd commit 0113aa9

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### New features
66

77
* New interactive command `clojure-cycle-when`.
8+
* New interactive command `clojure-cycle-not`.
89

910
## 5.6.1 (2016-12-21)
1011

clojure-mode.el

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ Out-of-the box clojure-mode understands lein, boot and gradle."
225225
(define-key map (kbd "i") #'clojure-cycle-if)
226226
(define-key map (kbd "C-w") #'clojure-cycle-when)
227227
(define-key map (kbd "w") #'clojure-cycle-when)
228+
(define-key map (kbd "C-o") #'clojure-cycle-not)
229+
(define-key map (kbd "o") #'clojure-cycle-not)
228230
(define-key map (kbd "n i") #'clojure-insert-ns-form)
229231
(define-key map (kbd "n h") #'clojure-insert-ns-form-at-point)
230232
(define-key map (kbd "n u") #'clojure-update-ns)
@@ -249,6 +251,7 @@ Out-of-the box clojure-mode understands lein, boot and gradle."
249251
["Cycle privacy" clojure-cycle-privacy]
250252
["Cycle if, if-not" clojure-cycle-if]
251253
["Cycle when, when-not" clojure-cycle-when]
254+
["Cycle not" clojure-cycle-not]
252255
("ns forms"
253256
["Insert ns form at the top" clojure-insert-ns-form]
254257
["Insert ns form here" clojure-insert-ns-form-at-point]
@@ -2097,6 +2100,7 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-cycle-if"
20972100
(forward-sexp 2)
20982101
(transpose-sexps 1)))))
20992102

2103+
;; TODO: Remove code duplication with `clojure--goto-if'.
21002104
(defun clojure--goto-when ()
21012105
"Find the first surrounding when or when-not expression."
21022106
(when (in-string-p)
@@ -2122,6 +2126,22 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-cycle-if"
21222126
(forward-char 5)
21232127
(insert "-not")))))
21242128

2129+
(defun clojure-cycle-not ()
2130+
"Add or remove a not form around the current form."
2131+
(interactive)
2132+
(save-excursion
2133+
(condition-case nil
2134+
(backward-up-list)
2135+
(scan-error (user-error "`clojure-cycle-not' must be invoked inside a list")))
2136+
(if (looking-back "(not ")
2137+
(progn
2138+
(delete-char -5)
2139+
(forward-sexp)
2140+
(delete-char 1))
2141+
(insert "(not ")
2142+
(forward-sexp)
2143+
(insert ")"))))
2144+
21252145
;;; let related stuff
21262146

21272147
(defvar clojure--let-regexp

test/clojure-mode-cycling-test.el

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@
143143
(search-forward "bbb))")
144144
(clojure-cycle-when))
145145

146+
(def-refactor-test test-cycle-not-add
147+
"(ala bala portokala)"
148+
"(not (ala bala portokala))"
149+
(beginning-of-buffer)
150+
(search-forward "bala")
151+
(clojure-cycle-not))
152+
153+
(def-refactor-test test-cycle-not-remove
154+
"(not (ala bala portokala))"
155+
"(ala bala portokala)"
156+
(beginning-of-buffer)
157+
(search-forward "bala")
158+
(clojure-cycle-not))
146159

147160
(provide 'clojure-mode-cycling-test)
148161

0 commit comments

Comments
 (0)