Skip to content

Commit e450d1e

Browse files
yuhan0bbatsov
authored andcommitted
Add undef-all option when loading files/buffers
1 parent b4c3bdc commit e450d1e

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

cider-eval.el

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,12 +1329,15 @@ buffer, else display in a popup buffer."
13291329
(cider-pprint-eval-defun-to-comment)
13301330
(cider--pprint-eval-form (cider-defun-at-point 'bounds))))
13311331

1332-
(defun cider-eval-ns-form ()
1333-
"Evaluate the current buffer's namespace form."
1334-
(interactive)
1332+
(defun cider-eval-ns-form (&optional undef-all)
1333+
"Evaluate the current buffer's namespace form.
1334+
When UNDEF-ALL is non-nil, unmap all symbols and aliases first."
1335+
(interactive "p")
13351336
(when (clojure-find-ns)
13361337
(save-excursion
13371338
(goto-char (match-beginning 0))
1339+
(when undef-all
1340+
(cider-undef-all (match-string 0)))
13381341
(cider-eval-defun-at-point))))
13391342

13401343
(defun cider-read-and-eval (&optional value)
@@ -1431,13 +1434,15 @@ passing arguments."
14311434
(widen)
14321435
(substring-no-properties (buffer-string)))))
14331436

1434-
(defun cider-load-buffer (&optional buffer callback)
1437+
(defun cider-load-buffer (&optional buffer callback undef-all)
14351438
"Load (eval) BUFFER's file in nREPL.
14361439
If no buffer is provided the command acts on the current buffer. If the
14371440
buffer is for a cljc file, and both a Clojure and ClojureScript REPL exists
14381441
for the project, it is evaluated in both REPLs.
1439-
Optional argument CALLBACK will override the default ‘cider-load-file-handler’."
1440-
(interactive)
1442+
Optional argument CALLBACK will override the default ‘cider-load-file-handler’.
1443+
When UNDEF-ALL is non-nil or called with \\[universal-argument], removes
1444+
all ns aliases and var mappings from the namespace before reloading it."
1445+
(interactive (list (current-buffer) nil (equal current-prefix-arg '(4))))
14411446
(setq buffer (or buffer (current-buffer)))
14421447
;; When cider-load-buffer or cider-load-file are called in programs the
14431448
;; current context might not match the buffer's context. We use the caller
@@ -1455,6 +1460,8 @@ Optional argument CALLBACK will override the default ‘cider-load-file-handler
14551460
(y-or-n-p (format "Save file %s? " buffer-file-name))))
14561461
(save-buffer))
14571462
(remove-overlays nil nil 'cider-temporary t)
1463+
(when undef-all
1464+
(cider-undef-all (cider-current-ns)))
14581465
(cider--clear-compilation-highlights)
14591466
(cider--quit-error-window)
14601467
(let ((filename (buffer-file-name buffer))
@@ -1471,25 +1478,30 @@ Optional argument CALLBACK will override the default ‘cider-load-file-handler
14711478
callback)))
14721479
(message "Loading %s..." filename))))))
14731480

1474-
(defun cider-load-file (filename)
1481+
(defun cider-load-file (filename &optional undef-all)
14751482
"Load (eval) the Clojure file FILENAME in nREPL.
14761483
If the file is a cljc file, and both a Clojure and ClojureScript REPL
14771484
exists for the project, it is evaluated in both REPLs. The heavy lifting
1478-
is done by `cider-load-buffer'."
1485+
is done by `cider-load-buffer'.
1486+
When UNDEF-ALL is non-nil or called with \\[universal-argument], removes
1487+
all ns aliases and var mappings from the namespace before reloading it."
14791488
(interactive (list
14801489
(read-file-name "Load file: " nil nil nil
14811490
(when (buffer-file-name)
14821491
(file-name-nondirectory
1483-
(buffer-file-name))))))
1492+
(buffer-file-name))))
1493+
(equal current-prefix-arg '(4))))
14841494
(if-let* ((buffer (find-buffer-visiting filename)))
1485-
(cider-load-buffer buffer)
1486-
(cider-load-buffer (find-file-noselect filename))))
1495+
(cider-load-buffer buffer nil undef-all)
1496+
(cider-load-buffer (find-file-noselect filename) nil undef-all)))
14871497

1488-
(defun cider-load-all-files (directory)
1498+
(defun cider-load-all-files (directory undef-all)
14891499
"Load all files in DIRECTORY (recursively).
1490-
Useful when the running nREPL on remote host."
1491-
(interactive "DLoad files beneath directory: ")
1492-
(mapcar #'cider-load-file
1500+
Useful when the running nREPL on remote host.
1501+
When UNDEF-ALL is non-nil or called with \\[universal-argument], removes
1502+
all ns aliases and var mappings from the namespaces being reloaded"
1503+
(interactive "DLoad files beneath directory: \nP")
1504+
(mapcar (lambda (file) (cider-load-file file undef-all))
14931505
(directory-files-recursively directory "\\.clj[cs]?$")))
14941506

14951507
(defalias 'cider-eval-file #'cider-load-file

0 commit comments

Comments
 (0)