Skip to content

Commit

Permalink
Use envrc-mode-hook to add back ends; closes #706
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
greghendershott committed Apr 9, 2024
1 parent fd3e3dd commit a90f66c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
40 changes: 33 additions & 7 deletions racket-back-end.el
Expand Up @@ -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.
Expand Down Expand Up @@ -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))))
Expand Down
7 changes: 4 additions & 3 deletions racket-repl-buffer-name.el
Expand Up @@ -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)
Expand All @@ -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))))))

Expand Down

0 comments on commit a90f66c

Please sign in to comment.