branch: elpa/racket-mode
commit 0abcedb2c3b16263f95ffbdc1b0b018366fe9865
Author: Greg Hendershott <g...@greghendershott.com>
Commit: Greg Hendershott <g...@greghendershott.com>

    Fix: Ensure REPL configure from edit buffer happens on first run
    
    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.
---
 racket-hash-lang.el | 30 ++++++++++++------------------
 racket-repl.el      | 32 +++++++++++++++++++++++---------
 2 files changed, 35 insertions(+), 27 deletions(-)

diff --git a/racket-hash-lang.el b/racket-hash-lang.el
index f4a2f1b831..01a082b7e9 100644
--- a/racket-hash-lang.el
+++ b/racket-hash-lang.el
@@ -637,13 +637,12 @@ 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'
@@ -651,17 +650,14 @@ 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
@@ -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))
diff --git a/racket-repl.el b/racket-repl.el
index a0631261df..79d4dfdd7f 100644
--- a/racket-repl.el
+++ b/racket-repl.el
@@ -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
@@ -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.
 
@@ -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)))
@@ -797,15 +797,19 @@ 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
+     edit-buffer
      (lambda (_repl-buffer)
+       (with-current-buffer edit-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 ()
@@ -819,13 +823,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."
@@ -837,7 +846,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"
@@ -847,6 +859,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

Reply via email to