This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-06-03
Channels
- # announcements (12)
- # beginners (44)
- # boot (27)
- # calva (73)
- # cider (1)
- # clj-kondo (9)
- # cljdoc (9)
- # cljs-dev (15)
- # cljsrn (6)
- # clojure (90)
- # clojure-dev (5)
- # clojure-europe (4)
- # clojure-ireland (3)
- # clojure-italy (22)
- # clojure-mexico (2)
- # clojure-nl (8)
- # clojure-uk (32)
- # clojurescript (12)
- # core-async (2)
- # cursive (16)
- # data-science (10)
- # datascript (3)
- # datomic (44)
- # emacs (17)
- # events (4)
- # graalvm (1)
- # hoplon (5)
- # jackdaw (17)
- # keechma (11)
- # nrepl (7)
- # off-topic (24)
- # re-frame (19)
- # reitit (4)
- # rewrite-clj (2)
- # robots (9)
- # shadow-cljs (20)
- # sql (12)
- # testing (4)
- # tools-deps (23)
- # vim (55)
Anyone know of a command / package for normalising whitespace in Clojure code or lisps in general? (foo bar )
=>`(foo bar)`
That doesn't do the job of removing whitespace before closing delimiters though, or inserting spaces between forms eg. ([a][b])
=> ([a] [b])
cljfmt (via cider-format
) requires a running REPL process and doesn't affect inter-symbol whitespace
Yeah, neither parinfer-fix
or whitespace-cleanup
from whitespace
package clean inside forms.
I use delete-horizontal-space
a lot though, often with multiple cursors. Maybe a simple macro can do the job?
Here it is bound to M-\
.
My Emacs regexp skills are failing me, but I believe you just need to ignore the first set of empty spaces (indentation), delete-horizontal-space
on everything else and enter a single space or move to the end of the whitespace, one character back and delete-horizontal-space
with C-u 1 M-\
.
hmm, it looks like fixup-whitespace
does the job for a particular point, the problem now is to iterate it through the form and handle edge cases, eg. not affecting whitespace in strings/comments
Actually I mixed two ways to solve the problem. With regexup substitution you only need to target the second group of two or more whitespaces and set them to one.
Nice! Wasn't aware of fixup-whitespace
. I may even bind it to something.
C-u C-M-|
perl -pe 's/(\S)(\s)\s*/$1$2/g'
RET
with a region
hehehe
(defun fixup-defun-whitespace ()
"Normalise all whitespace between forms in defun, and group closing parens.
Does not affect strings/comments"
(interactive)
(let* ((bnds (bounds-of-thing-at-point 'defun))
(beg (car bnds)) (end (cdr bnds)))
(save-excursion
(goto-char end)
(while (> (point) beg)
(search-backward-regexp "[([{ \t\r\n]" beg)
(unless (or (nth 8 (syntax-ppss)) ;; in a string or comment
(nth 4 (syntax-ppss)))
(let ((s (buffer-substring (point-at-bol) (point))))
(cond
;; indentation, skip
((string-match-p "^[ \t]*\\'" s)
(forward-line 0))
;; closing paren on separate line
((string-match-p "^[ \t]*[])}]" s)
(forward-line -1)
(end-of-line)
;; don't join if prev line is a comment
(unless (nth 8 (syntax-ppss))
(delete-char 1)
(fixup-whitespace)
(forward-line 1)))
(t
(fixup-whitespace))))))
(goto-char beg)
(indent-sexp)
(when (derived-mode-p 'clojure-mode)
(call-interactively 'clojure-align)))))