Function: clojure-remove-unused-keys
clojure-remove-unused-keys is an interactive and byte-compiled
function defined in clojure-mode.el.
Signature
(clojure-remove-unused-keys)
Documentation
Remove what the top-level form at point destructures from maps but doesn't use.
That's the names of :keys, :strs and :syms, :as names and names bound
to keys, in the arglists of fn, defn, defmethod, letfn, reify and the
like, and in the bindings of let, loop, for, doseq and the like. The
:or defaults of the names removed go too, and a map with nothing left
becomes _.
A name stays when a symbol with that name appears anywhere in its scope, even one that shadows it, and so does one starting with _, which marks it unused on purpose. A macro that uses a local without its name appearing in the code, through &env, isn't something this can tell.
Key Bindings
Source Code
;; Defined in ~/.emacs.d/elpa/clojure-mode-20261103.1226/clojure-mode.el
(defun clojure-remove-unused-keys ()
"Remove what the top-level form at point destructures from maps but doesn't use.
That's the names of :keys, :strs and :syms, :as names and names bound
to keys, in the arglists of fn, defn, defmethod, letfn, reify and the
like, and in the bindings of let, loop, for, doseq and the like. The
:or defaults of the names removed go too, and a map with nothing left
becomes _.
A name stays when a symbol with that name appears anywhere in its scope,
even one that shadows it, and so does one starting with _, which marks
it unused on purpose. A macro that uses a local without its name
appearing in the code, through &env, isn't something this can tell."
(interactive)
(let ((start (copy-marker (or (clojure--top-level-form-start)
(user-error "No form at point"))))
(tick nil)
names)
(unless (save-excursion
(goto-char start)
(when-let* ((form (ignore-errors (clojure--read-form (point-max))))
(state (parse-partial-sexp start (clojure--form-end form))))
(and (zerop (car state)) (not (nth 3 state)))))
(user-error "The form at point doesn't parse"))
(save-excursion
;; Removing a name can leave another one unused, like one that only
;; the :or default of the first used.
(while (not (eql tick (buffer-chars-modified-tick)))
(setq tick (buffer-chars-modified-tick))
(setq names (append names
(clojure--remove-unused-keys-once
(progn (goto-char start)
(clojure--read-form (point-max))))))))
(set-marker start nil)
(if names
(message "Removed %s" (string-join (delete-dups names) ", "))
(message "Nothing unused to remove"))))