Skip to content

Commit

Permalink
Fix: Ensure REPL configure from edit buffer happens on first run
Browse files Browse the repository at this point in the history
This requires waiting to do it until the repl buffer is created and
sufficiently initialized -- but before issuing any command to start a
repl session.

While we're add it, change this from a hook to a direct function call.
There's nothing "hooky" about this, we just need to avoid a mutual
require, and we can do that the usual way with declare-function and
autoload.
  • Loading branch information
greghendershott committed Nov 13, 2023
1 parent dfd9c23 commit a9fb2c8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
30 changes: 12 additions & 18 deletions racket-hash-lang.el
Original file line number Diff line number Diff line change
Expand Up @@ -637,31 +637,27 @@ When the lang lexer token is...
:lighter " #lang"
:keymap racket-hash-lang-repl-mode-map)

(defun racket--hash-lang-configure-repl-buffer-from-edit-buffer ()
"Update the `racket-repl-mode' buffer associated with the current edit buffer.
(defun racket--configure-repl-buffer-from-edit-buffer (edit-buffer repl-buffer)
"Configure REPL-BUFFER from EDIT-BUFFER.
A value for the hook `racket--repl-configure-buffer-hook'.
To be called when a `racket-mode' or `racket-hash-lang-mode' edit
buffer is `current-buffer'.
To be called upon each run command. EDIT-BUFFER is the buffer
where the run command was issued, REPL-BUFFER is the
`racket-repl-mode' buffer to be used.
It is possible for multiple edit buffers to \"take turns\" using
the same `racket-repl-mode' buffer, for successive `racket-run'
commands. Even if various edit buffers all use
`racket-hash-lang-mode', the hash-lang for each may differ, e.g.
one buffer is \"#lang racket\" while another is \"#lang
rhombus\"."
;;;(message "racket--hash-lang-configure-repl called from buffer %s" (buffer-name))
(let ((hl (eq major-mode 'racket-hash-lang-mode))
(edit-buffer (current-buffer)))
;; FIXME: On the very first run, the before-run hook is called
;; before the REPL buffer exists so none of the following happens.
(with-racket-repl-buffer
;;;(message "%S" (list 'racket--configure-repl-buffer-from-edit-buffer edit-buffer repl-buffer))
(let ((hash-lang-p (with-current-buffer edit-buffer (eq major-mode 'racket-hash-lang-mode))))
(with-current-buffer repl-buffer
;; Clean up from previous hash-lang use of REPL, if any
(racket--hash-lang-delete)

;; Maybe create hash-lang object, synchronously.
(when hl
(when hash-lang-p
(setq-local
racket--hash-lang-id
(racket--cmd/await
Expand Down Expand Up @@ -693,14 +689,12 @@ rhombus\"."
;; nav
(setq-local forward-sexp-function
(with-current-buffer edit-buffer forward-sexp-function))
(racket-hash-lang-repl-mode (if hl 1 -1)) ;keybindings
(if hl
(racket-hash-lang-repl-mode (if hash-lang-p 1 -1)) ;keybindings
(if hash-lang-p
(add-hook 'after-change-functions #'racket--hash-lang-after-change-hook t t)
(remove-hook 'after-change-functions #'racket--hash-lang-after-change-hook t))
(setq-local racket-repl-submit-function
(if hl #'racket-hash-lang-submit nil)))))
(add-hook 'racket--repl-before-run-hook
#'racket--hash-lang-configure-repl-buffer-from-edit-buffer)
(if hash-lang-p #'racket-hash-lang-submit nil)))))

(defun racket--hash-lang-repl-on-stop-back-end ()
(dolist (buf (buffer-list))
Expand Down
33 changes: 23 additions & 10 deletions racket-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ Mode's REPL as intended, then consider using a plain Emacs
(interactive "P")
(racket-call-racket-repl-buffer-name-function)
(racket--repl-ensure-buffer-and-session
nil
(lambda (repl-buffer)
(racket--repl-refresh-namespace-symbols)
(unless noselect
Expand Down Expand Up @@ -719,6 +720,10 @@ for end user customization is `racket-after-run-hook'.
Here \"after\" means that the run has completed and e.g. the REPL
is waiting at another prompt.")

;; Don't (require 'racket-hash-lang). Mutual dependency. Instead:
(declare-function racket--configure-repl-buffer-from-edit-buffer "racket-hash-lang" (edit-buf repl-buf))
(autoload 'racket--configure-repl-buffer-from-edit-buffer "racket-hash-lang")

(defun racket--repl-run (&optional what extra-submods context-level callback)
"Do an initial or subsequent run.
Expand Down Expand Up @@ -771,11 +776,6 @@ be nil which is equivalent to #\\='ignore."
(message "")
(racket-start-back-end)))

(racket--repl-delete-prompt-mark 'abandon)
(with-racket-repl-buffer ;if it already exists
(set-marker racket--repl-run-mark (point)))
(run-hooks 'racket--repl-before-run-hook
'racket-before-run-hook)
(pcase-let*
((context-level (or context-level racket-error-context))
(what (or what (racket--what-to-run)))
Expand All @@ -797,15 +797,18 @@ be nil which is equivalent to #\\='ignore."
context-level
racket-user-command-line-arguments
debug-files))
(buf (current-buffer))
(edit-buffer (current-buffer))
(after (lambda (_ignore)
(with-current-buffer buf
(with-current-buffer edit-buffer
(run-hooks 'racket--repl-after-run-hook
'racket-after-run-hook)
(when callback
(funcall callback))))))
(racket--repl-ensure-buffer-and-session
(lambda (_repl-buffer)
edit-buffer
(lambda (repl-buffer)
(run-hooks 'racket--repl-before-run-hook
'racket-before-run-hook)
(racket--cmd/async (racket--repl-session-id) cmd after)))))

(defun racket--write-contents ()
Expand All @@ -819,13 +822,18 @@ be nil which is equivalent to #\\='ignore."
(set-window-buffer nil (current-buffer))
(car (window-text-pixel-size nil (line-beginning-position) (point))))))

(defun racket--repl-ensure-buffer-and-session (continue)
(defun racket--repl-ensure-buffer-and-session (edit-buffer continue)
"Ensure a `racket-repl-mode' buffer exists with a live session.
Create the buffer if necessary, enabling `racket-repl-mode'.
Start the session if necessary.
When EDIT-BUFFER is not nil, use it to call
`racket--configure-repl-buffer-from-edit-buffer' after the repl
buffer is fully initialized (and if the repl session isn't
started, before starting it).
Calls CONTINUE with one argument, the repl buffer.
This displays the buffer but does not change the selected window."
Expand All @@ -837,7 +845,10 @@ This displays the buffer but does not change the selected window."
(display-buffer repl-buf)
(with-current-buffer repl-buf
(if racket--repl-session-id
(funcall continue repl-buf)
(progn
(when edit-buffer
(racket--configure-repl-buffer-from-edit-buffer edit-buffer repl-buf))
(funcall continue repl-buf))
(setq racket--repl-session-id (cl-incf racket--repl-next-session-id))
(when noninteractive
(princ (format "{racket--repl-start}: picked next session id %S\n"
Expand All @@ -847,6 +858,8 @@ This displays the buffer but does not change the selected window."
(setq racket--repl-run-mark (point-marker))
(setq racket--repl-output-mark (point-marker))
(set-marker-insertion-type racket--repl-output-mark nil)
(when edit-buffer
(racket--configure-repl-buffer-from-edit-buffer edit-buffer repl-buf))
(unless (racket--cmd-open-p)
(racket--repl-insert-output 'message "Starting back end..."))
(racket--cmd/async nil
Expand Down

0 comments on commit a9fb2c8

Please sign in to comment.