Skip to content

Commit 2cccb5d

Browse files
committed
Introduce inf-clojure-reload
It will evaluate (require 'ns :reload) or (require 'ns :reload-all) at the REPL, depending on the arguments passed in. This works only from source buffers at the moment (like all the other namespace commands).
1 parent 630471b commit 2cccb5d

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* [#125](https://github.com/clojure-emacs/inf-clojure/pull/125): Avoid throwing an error for frequent operations like completion.
1414
* [#130](https://github.com/clojure-emacs/inf-clojure/pull/130): Support loading directory locals in our buffers.
1515
* [#129](https://github.com/clojure-emacs/inf-clojure/pull/129): Improve the completion bounds detection (now with keywords).
16+
* [#132](https://github.com/clojure-emacs/inf-clojure/pull/132): Introduce inf-clojure-reload.
1617

1718
### Bugs Fixed
1819

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Clojure(Script) development:
2323
* ElDoc
2424
* Apropos
2525
* Macroexpansion
26+
* Require `:reload`/`:reload-all`
2627
* Support connecting to socket REPLs
2728
* Support for Lumo
2829
* Support for Planck

inf-clojure.el

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ mode. Default is whitespace followed by 0 or 1 single-letter colon-keyword
131131
(define-key map "\C-c\C-c" #'inf-clojure-eval-defun) ; SLIME/CIDER style
132132
(define-key map "\C-c\C-b" #'inf-clojure-eval-buffer)
133133
(define-key map "\C-c\C-r" #'inf-clojure-eval-region)
134+
(define-key map "\C-c\M-r" #'inf-clojure-reload)
134135
(define-key map "\C-c\C-n" #'inf-clojure-eval-form-and-next)
135136
(define-key map "\C-c\C-z" #'inf-clojure-switch-to-repl)
136137
(define-key map "\C-c\C-i" #'inf-clojure-show-ns-vars)
@@ -152,6 +153,7 @@ mode. Default is whitespace followed by 0 or 1 single-letter colon-keyword
152153
["Eval buffer" inf-clojure-eval-buffer t]
153154
"--"
154155
["Load file..." inf-clojure-load-file t]
156+
["Reload file... " inf-clojure-reload t]
155157
"--"
156158
["Switch to REPL" inf-clojure-switch-to-repl t]
157159
["Set REPL ns" inf-clojure-set-ns t]
@@ -372,6 +374,48 @@ If you are using REPL types, it will pickup the most appropriate
372374
(`planck inf-clojure-load-form-planck)
373375
(_ inf-clojure-load-form)))
374376

377+
(defcustom inf-clojure-reload-form "(require '\"%s\" :reload)"
378+
"Format-string for building a Clojure expression to reload a file.
379+
Reload forces loading of all the identified libs even if they are
380+
already loaded.
381+
This format string should use `%s' to substitute a namespace and
382+
should result in a Clojure form that will be sent to the inferior
383+
Clojure to load that file."
384+
:type 'string
385+
:safe #'stringp
386+
:package-version '(inf-clojure . "2.2.0"))
387+
388+
;; :reload forces loading of all the identified libs even if they are
389+
;; already loaded
390+
;; :reload-all implies :reload and also forces loading of all libs that the
391+
;; identified libs directly or indirectly load via require or use
392+
393+
(defun inf-clojure-reload-form (proc)
394+
"Return the form to query the Inf-Clojure PROC for reloading a namespace.
395+
If you are using REPL types, it will pickup the most appropriate
396+
`inf-clojure-reload-form` variant."
397+
(inf-clojure--set-repl-type proc)
398+
inf-clojure-reload-form)
399+
400+
(defcustom inf-clojure-reload-all-form "(require '\"%s\" :reload-all)"
401+
"Format-string for building a Clojure expression to :reload-all a file.
402+
Reload-all implies :reload and also forces loading of all libs
403+
that the identified libs directly or indirectly load via require
404+
or use.
405+
This format string should use `%s' to substitute a namespace and
406+
should result in a Clojure form that will be sent to the inferior
407+
Clojure to load that file."
408+
:type 'string
409+
:safe #'stringp
410+
:package-version '(inf-clojure . "2.2.0"))
411+
412+
(defun inf-clojure-reload-all-form (proc)
413+
"Return the form to query the Inf-Clojure PROC for :reload-all of a namespace.
414+
If you are using REPL types, it will pickup the most appropriate
415+
`inf-clojure-reload-all-form` variant."
416+
(inf-clojure--set-repl-type proc)
417+
inf-clojure-reload-all-form)
418+
375419
(defcustom inf-clojure-prompt "^[^=> \n]+=> *"
376420
"Regexp to recognize prompts in the Inferior Clojure mode."
377421
:type 'regexp)
@@ -702,6 +746,28 @@ is present it will be used instead of the current file."
702746
(when switch-to-repl
703747
(inf-clojure-switch-to-repl t))))
704748

749+
(defun inf-clojure-reload (arg)
750+
"Send a query to the inferior Clojure for reloading the namespace.
751+
See variable `inf-clojure-reload-form' and
752+
`inf-clojure-reload-all-form'.
753+
754+
The prefix argument ARG can change the behavior of the command:
755+
756+
- C-u M-x `inf-clojure-reload': prompts for a namespace name.
757+
- M-- M-x `inf-clojure-reload': executes (require ... :reload-all).
758+
- M-- C-u M-x `inf-clojure-reload': reloads all AND prompts."
759+
(interactive "P")
760+
(let* ((proc (inf-clojure-proc))
761+
(invertp (or (equal arg "-") (equal arg '(-4))))
762+
(promptp (or (equal arg '(4)) (equal arg '(-4))))
763+
(ns (if promptp
764+
(car (inf-clojure-symprompt "Namespace" (clojure-find-ns)))
765+
(clojure-find-ns)))
766+
(form (if (not invertp)
767+
(inf-clojure-reload-form proc)
768+
(inf-clojure-reload-all-form proc))))
769+
(inf-clojure--send-string proc (format form ns))))
770+
705771
(defun inf-clojure-connected-p ()
706772
"Return t if inferior Clojure is currently connected, nil otherwise."
707773
(not (null inf-clojure-buffer)))

0 commit comments

Comments
 (0)