Fork me on GitHub
#emacs
<
2021-06-29
>
furiel13:06:37

Hello, does someone know an efficient soultion for this usecase? I often need to develop a function and try to execute it with a given set of parameters in an interactive manner. I approach this like: I edit the function, then ctrl-c-ctrl-c to reevaluate the function. A few lines below I have a function call prepared with proper parameters (generally in a comment block). So I go down to the end of the prepared function call, and evaluate it there. Do you have a nice trick to do this in a faster way? My approach takes a lot of moving around into the function definition, then to the end of the example call. I experimented with a `do` statement, that defines a function and calls it at the same time, because then ctrl-c+ctrl+c would do both, but do messes up the indentation, and I feel there should be a better way to do it.

dpsutton13:06:33

I wrote a simple “insert register in repl” function that I use all the time. Just save some text to a register, eval the function, then send the form to the repl

furiel13:06:03

That sounds neat. Can you share the snippet?

dpsutton14:06:15

ha yeah. sorry. was making coffee and feeding the dog

dpsutton14:06:54

it added it outside of the thread. But use C-x C-r C-s <register-key> (x Resgister Save), and then you can send to the repl with the handy repl sending map at C-c C-j x and then the register key

dpsutton14:06:01

i do it all the time in inf-clojure

furiel14:06:35

Thanks for sharing! Trying out now.

pithyless15:06:38

@thoneyvazul I use a different approach. I'll wrap the runner-code in a deftest:

(comment
  (deftest foo
    (setup-and-run-my-code)))
Eval and run cider-test-run-test. Then, when I'm changing the original code, just eval the form (e.g. cider-eval-defun-at-point) followed by cider-test-rerun-test. That last one does the trick: CIDER will remember which test was run last, so you don't need to jump around the buffer anymore (sometimes I'll even be modifying code in a different namespace). On top of that, it's an actual deftest, so you can use is assertions to test some expected behaviors and optionally have CIDER popup the error buffers, etc.

dpsutton15:06:16

oh that's clever

furiel16:06:07

great idea thanks @U05476190

pithyless16:06:50

Happy to help, it's something I've previously struggled with as well. Although I may just have to take @U11BV7MTK's compliment and frame it somewhere. ;)

timvisher18:06:33

I like the pattern of having the function I'm working on in a file and the exercising code be in my repl. 1. Edit the code 2. Whack C-c C-c C-c C-z M-p RET C-z Or 1. Edit the code 2. Eval the form (`C-c C-c`) 3. Switch to the repl (`C-c C-z`) 4. Bring up the previous form and execute it (`M-p RET`) 5. Return to the function I'm working on (`C-c C-z`) If you're not using the current keyboard macro for anything else that can be collapsed down to 1. Edit the code 2. Whack <f4> :)

timvisher18:06:26

Going the clojure.test route though is intriguing. I think it would be ideal to combine this with the with-test feature of clojure.test so that you can freely edit both the invocation and the implementation at the same time with a single C-c C-c or C-M-x. Unfortunately I can't seem to get CIDER to recognize with-test as something that defines after a quick try. Can anyone make CIDER recognize this as a test to run?

(require '[clojure.test :refer [with-test is]])

(with-test
  (defn +'
    "Adds two numbers"
    [a b]
    (+ a b))
  (is (= 3 (+' 1 2))))
Note clojure.test does this just fine:
user> (require '[clojure.test :refer [with-test is]])

(with-test
  (defn +'
    "Adds two numbers"
    [a b]
    (+ a b))
  (is (= 3 (+' 1 2))))

nil#'user/+'
user> (clojure.test/run-tests 'user)

Testing user

Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
{:test 1, :pass 1, :fail 0, :error 0, :type :summary}

timvisher18:06:47

My initial attempt was to customize the cider-test-defining-forms to add with-test but doing that doesn't change the behavior I'm seeing at all. ¯\(ツ)

timvisher18:06:20

Oooooo this is super nice. Yeah after running the cider-test-run-test function through edebug the trick is adding defn to the cider-test-defining-forms. With that I can whack C-c C-c C-C C-t C-t and rerun the exercising invocation. And again if my keyboard macro slot wasn't busy with something else I could bind that to <f4>. That's actually really nice. Great idea @U05476190! :)

pithyless18:06:37

OT: I'm an emacs evil-mode user and C-c C-c C-C C-t C-t looks absolutely terrifying (compared to say , t a), but whatever works! ;)

timvisher19:06:46

LOL. /me crosses self against modal editing. xD

timvisher19:06:13

The result's the same in either case. :)

metal 2
timvisher19:06:44

The key sequence isn't nearly as bad as it looks if you use your 6th finger.

pithyless19:06:00

@U096U98TC are you referring to a USB foot pedal? :P

timvisher19:06:47

No although I've considered it! Just my palms. http://ergoemacs.org/emacs/emacs_pinky.html

timvisher19:06:01

So basically to type that sequence I just rest my left palm down slightly and then rapidly type c c c t t. There's probably a better sequence but I'm not vimgolfing here. :)

timvisher19:06:54

Oh yeah for instance another prefix for test map is indeed , so it could be C-c C-c C-c , t but then I need to lift my palm during the sequence so that's actually worse.

timvisher19:06:53

This is completely irrelevant to you as an evil user but anything that makes me break the ctrl key sequence is a big no no. It makes emacs actually feel clunky to use. It's remarkable how similar to vim it is once you're pressing ctrl with your palm and meta with your thumb.

timvisher19:06:27

(Similar to vim in terms of raw bindings vs. edit function composition which, IMO, is vim's real super power)

Jakub Šťastný01:07:24

@U05476190 exactly! Wouldn't have made the switch (from NeoVim) without EVIL. EVIL rocks.

pithyless15:06:38

@thoneyvazul I use a different approach. I'll wrap the runner-code in a deftest:

(comment
  (deftest foo
    (setup-and-run-my-code)))
Eval and run cider-test-run-test. Then, when I'm changing the original code, just eval the form (e.g. cider-eval-defun-at-point) followed by cider-test-rerun-test. That last one does the trick: CIDER will remember which test was run last, so you don't need to jump around the buffer anymore (sometimes I'll even be modifying code in a different namespace). On top of that, it's an actual deftest, so you can use is assertions to test some expected behaviors and optionally have CIDER popup the error buffers, etc.