Fork me on GitHub

ah brilliant didn't know about the , trick @dpsutton


Am I the only one occasionally capturing a value in the REPL? Did someone make emacs helpers for this? (I am aware of btw, not really what I am after) In other words, how do I go from:

(defn test-fn [a b]
  (let [c [a b]]
    ;; <- cursor there
    [(+ a b) c]))
(defn test-fn [a b]
  (let [c [a b]]
    (def a a)
    (def b b)
    (def c c)
    [(+ a b) c]))
Using a bit of elisp? I could type for instance “my-emacs-def-insert-cmd” “a” ENTER “b” ENTER “c” ENTER ENTER and have this generated? That would be a welcome addition to my particular workflow. Anyone doing this?


@nha I think @dpsutton has something like that and I probably have the same. Not at the keyboard now, will check and write later.

❤️ 4

let me dig it up. you have to run it while stepping through with the debugger


(defun cider-debug-create-local-let (start end)
  "During debugging, grab the locally bound vars and create a let
  binding. Place this let binding in the kill ring for future use."
  (interactive "r")
  (if cider--debug-mode-response
      (nrepl-dbind-response cider--debug-mode-response (locals)
        (let* ((code (buffer-substring-no-properties start end))
               (bindings (apply #'append locals))
               (formatted-bindings (mapconcat 'identity bindings " ")))
          (kill-new (format "(let [%s]\n %s)" formatted-bindings code))
          (message "copied let form to kill ring")))
    (message "No debugging information found.")))
while in the debugger, get to a stack frame that includes the vars you want to capture. then invoke this function. it grabs the local state from the debugger and formats them into a let binding

😎 4

oh and do it while highlighting code


with a region, it will capture that region as the body of the let and the locals in your debugger state as the let bindings above it


(kill-new (format "(let [%s]\n %s)" formatted-bindings code)) is the formatting


Oh I see. Interesting, I don’t use the debugger though (I probably should, although I am quite sure some of our params are too big for it)


I suspect what I am after is simpler (ie. does not require any of cider’s functionality) - but my elisp is still fairly weak


depends on how smart you want it. you could create an interactive function that would ask for a list of variable names and then insert a bunch of (def [name] [name]) inside a do block


trivial example:

(defun ask-for-variable-names (names)
  (interactive "sEnter the variable names: ")
  (let ((bindings (mapcar (lambda (name)
                            (format "(def %s %s)" name name))
                          (s-split " " names))))
    (kill-new (format "(do %s)"
                      (s-join "\n" bindings)))))

❤️ 4