Skip to content

How to run custom clojure function in REPL on startup? #3794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
edenworky opened this issue Mar 24, 2025 · 2 comments
Open

How to run custom clojure function in REPL on startup? #3794

edenworky opened this issue Mar 24, 2025 · 2 comments

Comments

@edenworky
Copy link

edenworky commented Mar 24, 2025

Is your feature request related to a problem? Please describe.

I want to run custom logic inside the REPL when it starts up, and binding to cider-connected-hook runs outside of the REPL even when using cider-interactive-eval. E.g., I have a hook defined like:

(use-package cider
  :ensure t
  :hook ((cider-connected . (lambda () (cider-interactive-eval "(go)")))))

Which does run the function, but at a different time and place than when I run (go) in the REPL. Specifically, I want to print a startup message in the REPL, but using this I get all printed logs in the nrepl-server buffer and don't show up in the REPL (even though running cider-interactive-eval manually afterwards does show up in the REPL buffer). Also, I'm doing some monkeypatching to print-method which works well when I run it after cider-nrepl is up but when run with the hook as shown above CIDER's own work with print-method happens after my custom function is run and overrides it.

Describe the solution you'd like

There's multiple ways of solving this, but I think the cleanest one would be to have some var cider-repl-startup-sexp which would default to nil and when it exists then the first REPL prompt after startup should yank that variable and run it, or else an appropriate hook (cider-repl-mode-hook?) and some function to do this (cider-interactive-eval I suppose, but I couldn't get it to work for this and it only shows the result in the REPL and not the command). Or else to do smth convoluted like storing the startup command as a macro and hooking that to idle REPL buffers or something, but overall this feels like something that should be simple to do and documented somewhere.

How would this be best approached? Is this already solvable with CIDER? I'd love to put a PR together but I'm not sure where to start.

@alexander-yakushev
Copy link
Member

Try this one:

(add-hook 'cider-connected-hook (lambda ()
                                    (insert "(go)")
                                    (cider-repl-return)))

Would that work? You may also need to ensure that you are looking at the REPL buffer at the moment that the hook is run.

Copy link

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed soon if no further activity occurs. Thank you for your contribution and understanding!

@github-actions github-actions bot added the stale label May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants