Skip to content

Commit 20910ba

Browse files
yuhan0bbatsov
authored andcommitted
Add commands for toggling #_ ignore forms
1 parent f26379b commit 20910ba

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

clojure-mode.el

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,6 +2822,57 @@ Assumes cursor is at beginning of function."
28222822
(clojure--add-arity-reify-internal)))
28232823
(indent-region beg end-marker))))
28242824

2825+
2826+
;;; Toggle Ignore forms
2827+
2828+
(defun clojure--toggle-ignore-next-sexp (&optional n)
2829+
"Insert or delete N `#_' ignore macros at the current point.
2830+
Point must be directly before a sexp or the #_ characters.
2831+
When acting on a top level form, insert #_ on a new line
2832+
preceding the form to prevent indentation changes."
2833+
(let ((rgx (rx-to-string `(repeat ,(or n 1) (seq "#_" (* (in "\r\n" blank)))))))
2834+
(backward-prefix-chars)
2835+
(skip-chars-backward "#_ \r\n")
2836+
(skip-chars-forward " \r\n")
2837+
(if (looking-at rgx)
2838+
(delete-region (point) (match-end 0))
2839+
(dotimes (_ (or n 1)) (insert-before-markers "#_"))
2840+
(when (zerop (car (syntax-ppss)))
2841+
(insert-before-markers "\n")))))
2842+
2843+
(defun clojure-toggle-ignore (&optional n)
2844+
"Toggle the #_ ignore reader form for the sexp at point.
2845+
With numeric argument, toggle N number of #_ forms at the same point.
2846+
2847+
e.g. with N = 2:
2848+
|a b c => #_#_a b c"
2849+
(interactive "p")
2850+
(save-excursion
2851+
(ignore-errors
2852+
(goto-char (or (nth 8 (syntax-ppss)) ;; beginning of string
2853+
(beginning-of-thing 'sexp))))
2854+
(clojure--toggle-ignore-next-sexp n)))
2855+
2856+
(defun clojure-toggle-ignore-surrounding-form (&optional arg)
2857+
"Toggle the #_ ignore reader form for the surrounding form at point.
2858+
With optional ARG, move up by ARG surrounding forms first.
2859+
With universal argument \\[universal-argument], act on the \"top-level\" form."
2860+
(interactive "P")
2861+
(save-excursion
2862+
(if (consp arg)
2863+
(clojure-toggle-ignore-defun)
2864+
(condition-case nil
2865+
(backward-up-list arg t t)
2866+
(scan-error nil)))
2867+
(clojure--toggle-ignore-next-sexp)))
2868+
2869+
(defun clojure-toggle-ignore-defun ()
2870+
"Toggle the #_ ignore reader form for the \"top-level\" form at point."
2871+
(interactive)
2872+
(save-excursion
2873+
(beginning-of-defun)
2874+
(clojure--toggle-ignore-next-sexp)))
2875+
28252876

28262877
;;; ClojureScript
28272878
(defconst clojurescript-font-lock-keywords

0 commit comments

Comments
 (0)