Background
I wrote a hack for Emacs that allows me to submit a Clojure form from the editor buffer to the REPL buffer. It works fine, except if the two buffers are in different namespaces, the copied text usually doesn't make sense or, even worse, it might make sense, but it has a different meaning in the editor buffer.
I want to convert the text so that it makes sense in the REPL buffer.
Solution in Common Lisp
In Common Lisp, I could do this using the following function:
;; Common Lisp (defun translate-text-between-packages (text from-package to-package) (let* ((*package* from-package) (form (read-from-string text)) (*package* to-package)) (with-output-to-string (*standard-output*) (pprint form))))
And an example of use:
;; Common Lisp (make-package 'editor-package) (make-package 'repl-package) (defvar repl-package::a) (translate-text-between-packages "(+ repl-package::ab)" (find-package 'editor-package) (find-package 'repl-package)) ;; => "(+ A EDITOR-PACKAGE::B)"
The qualifications of the package name in the input line and output line are different - exactly what is needed to solve the problem of copying and pasting text between packages.
(By the way, there is material on how to run the translation code in the general Lisp process and move the material between the Emacs world and the general Lisp world, but I am fine with this and I donβt really want to get here.)
Not a solution in Clojure
Here's a direct translation to Clojure:
;; Clojure (defn translate-text-between-namespaces [text from-ns to-ns] (let [*ns* from-ns form (read-string text) *ns* to-ns] (with-out-str (clojure.pprint/pprint form))))
And an example of use:
;; Clojure (create-ns 'editor-ns) (create-ns 'repl-ns) (translate-text-between-namespaces "(+ repl-ns/ab)" (find-ns 'editor-ns) (find-ns 'repl-ns)) ;; => "(+ repl-ns/ab)"
So the translation function in Clojure did nothing. This is because the characters and packages / namespaces in Common Lisp and Clojure work differently.
In Common Lisp, characters refer to a package, and the definition of a package of characters occurs while reading.
In Clojure, for good reason, characters do not belong in the namespace, and the definition of the character namespace occurs during the evaluation.
Could this be done in Clojure?
So finally, my question is: can I convert Clojure code from one namespace to another?