From a90f66ce38d4aa7e1c08e2b0d89d5b521e9f2e79 Mon Sep 17 00:00:00 2001 From: Greg Hendershott Date: Tue, 9 Apr 2024 16:03:35 -0400 Subject: [PATCH] Use envrc-mode-hook to add back ends; closes #706 Also improve racket-repl-buffer-name-project to be per back end as well as per project. In other words, if a project subdir happens to use an envrc with a distinct back end, that has to be a distinct REPL. --- racket-back-end.el | 40 +++++++++++++++++++++++++++++++------- racket-repl-buffer-name.el | 7 ++++--- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/racket-back-end.el b/racket-back-end.el index a2001b1e..fdd06c1c 100644 --- a/racket-back-end.el +++ b/racket-back-end.el @@ -257,6 +257,32 @@ are a few examples. (unless no-refresh-watches-p (racket--back-end-refresh-watches))) +(require 'envrc nil t) + +(defun racket-back-end-envrc-mode-hook () + "A value for `envrc-mode-hook'. + +Automatically configures a distinct back end for the directory of +the dominating envrc file, if any. As a result, each such back +end uses appropriate `exec-path' and `process-environment' +values." + (when (featurep 'envrc) + (when-let ((env-dir (and (eq 'on envrc--status) + (envrc--find-env-dir)))) + (unless (cl-find env-dir + racket-back-end-configurations + :test + (lambda (dir back-end) + (equal dir (plist-get back-end :directory)))) + (when-let ((path-to-racket (executable-find (if racket--winp "Racket.exe" "racket")))) + (message "racket-back-end-envrc-mode-hook configuring back end for %S to use %S" + env-dir path-to-racket) + (racket-add-back-end env-dir :racket-program path-to-racket)))))) + +;; Avoid user needing to use dir-locals.el to manually create a +;; distinct back end. +(add-hook 'envrc-mode-hook #'racket-back-end-envrc-mode-hook) + (defun racket-back-end-name (&optional back-end) "Return the \"name\" of a back end. @@ -443,13 +469,13 @@ a possibly slow remote connection." (defun racket--back-end-args->command (back-end racket-command-args) "Given RACKET-COMMAND-ARGS, prepend path to racket for BACK-END." (if (racket--back-end-local-p back-end) - `(,(or (plist-get back-end :racket-program) - (executable-find racket-program) - (user-error - "Cannot find Racket executable\nracket-program: %S\nexec-path: %S" - racket-program - exec-path)) - ,@racket-command-args) + (cons (or (executable-find (or (plist-get back-end :racket-program) + racket-program)) + (user-error + "Cannot find Racket executable\nracket-program: %S\nexec-path: %S" + racket-program + exec-path)) + racket-command-args) (pcase-let ((`(,host ,user ,port ,_name) (racket--file-name->host+user+port+name (plist-get back-end :directory)))) diff --git a/racket-repl-buffer-name.el b/racket-repl-buffer-name.el index 2e234d73..c025e4f6 100644 --- a/racket-repl-buffer-name.el +++ b/racket-repl-buffer-name.el @@ -26,7 +26,7 @@ customization." ;;;###autoload (defun racket-repl-buffer-name-shared () - "All `racket-mode' edit buffers share one `racket-repl-mode' buffer per back end. + "Share one `racket-repl-mode' buffer per back end. A value for the variable `racket-repl-buffer-name-function'." (interactive) @@ -45,14 +45,15 @@ A value for the variable `racket-repl-buffer-name-function'." ;;;###autoload (defun racket-repl-buffer-name-project () - "All `racket-mode' buffers in a project share a `racket-repl-mode' buffer. + "Share a `racket-repl-mode' buffer per back end and per project. A value for the variable `racket-repl-buffer-name-function'. The \"project\" is determined by `racket-project-root'." (interactive) (setq-local racket-repl-buffer-name - (format "*Racket REPL <%s>*" + (format "*Racket REPL <%s %s>*" + (racket-back-end-name) (racket--file-name-sans-remote-method (racket-project-root (racket--buffer-file-name))))))