diff --git a/doc/generate.el b/doc/generate.el index 4e482ae5..fc86e26f 100644 --- a/doc/generate.el +++ b/doc/generate.el @@ -204,6 +204,7 @@ racket-images-keep-last racket-images-system-viewer racket-pretty-print + racket-repl-command-file "Other variables" racket-indent-curly-as-sequence racket-indent-sequence-depth diff --git a/doc/racket-mode.texi b/doc/racket-mode.texi index 2b0bce48..a8f49063 100644 --- a/doc/racket-mode.texi +++ b/doc/racket-mode.texi @@ -226,6 +226,7 @@ REPL variables * racket-images-keep-last:: * racket-images-system-viewer:: * racket-pretty-print:: +* racket-repl-command-file:: Other variables @@ -997,7 +998,7 @@ Major mode for editing Racket source files. @item @kbd{C-c C-t} @tab @ref{racket-test} @item @kbd{C-c C-z} -@tab @ref{racket-repl} +@tab @code{racket-edit-switch-to-repl} @item @kbd{C-c C-k} @tab @ref{racket-run-module-at-point} @item @kbd{C-c C-c} @@ -1618,7 +1619,7 @@ can contribute more colors; see the customization variable @item @kbd{C-c C-t} @tab @ref{racket-test} @item @kbd{C-c C-z} -@tab @ref{racket-repl} +@tab @code{racket-edit-switch-to-repl} @item @kbd{C-c C-k} @tab @ref{racket-run-module-at-point} @item @kbd{C-c C-c} @@ -2321,26 +2322,22 @@ simply the outermost, file module. @node racket-repl @subsection racket-repl -@kbd{C-c C-z} +@kbd{M-x} @code{racket-repl} Show a Racket REPL buffer in some window. -@strong{IMPORTANT} - The main, intended use of Racket Mode's REPL is that you -@code{find-file} some specific .rkt file, then run it using +@code{find-file} some specific file, then run it using a command like @ref{racket-run} or @ref{racket-run-module-at-point}. The resulting REPL will correspond to those definitions and match your expectations. -If you really want to start a REPL for no file in particular, -then you could use this @ref{racket-repl} command. But the resulting -REPL will have a minimal ``#lang racket/base'' namespace. You -could enter ``(require racket)'' if you want the equivalent of -``#lang racket''. You could also ``(require racket/enter)'' if -you want things like ``enter!''. But in some sense you'd be -``using it wrong''. If you actually don't want to use Racket -Mode's REPL as intended, then consider using a plain Emacs -@code{shell} buffer to run command-line Racket. +Therefore this @ref{racket-repl} command -- which is intended as a +convenience for people who want a way to ``just get a quick +REPL'' -- is actually implemented as just running the file named +in the customization variable @ref{racket-repl-command-file}. By +default when that file doesn't exist, it is created to contain +just ``#lang racket/base''. You may edit the file to use a +different lang, require other modules, or whatever. @node racket-repl-describe @subsection racket-repl-describe @@ -3304,6 +3301,7 @@ classic @ref{racket-mode} for rackety module languages: * racket-images-keep-last:: * racket-images-system-viewer:: * racket-pretty-print:: +* racket-repl-command-file:: @end menu @node racket-repl-buffer-name-function @@ -3401,6 +3399,11 @@ The image viewer program to use for @code{racket-view-image}. Use pretty-print instead of print in REPL@? +@node racket-repl-command-file +@subsection racket-repl-command-file + +Name of the file used by @ref{racket-repl}. + @node Other variables @section Other variables diff --git a/racket-custom.el b/racket-custom.el index 03a63971..7636e4ea 100644 --- a/racket-custom.el +++ b/racket-custom.el @@ -392,7 +392,6 @@ will use this to decide whether to submit your input, yet." :safe #'booleanp :group 'racket-repl) - (defcustom racket-before-run-hook nil "Normal hook done before various Racket Mode run commands. @@ -423,6 +422,14 @@ from the variable `racket-repl-buffer-name'." :risky t :group 'racket-repl) +(defcustom racket-repl-command-file + (expand-file-name "repl.rkt" + (locate-user-emacs-file (file-name-as-directory "racket-mode"))) + "Name of the file used by `racket-repl'." + :tag "REPL Command File" + :type 'file + :group 'racket-repl) + ;;; Other (defgroup racket-other nil diff --git a/racket-hash-lang.el b/racket-hash-lang.el index 0bae3aee..690006ad 100644 --- a/racket-hash-lang.el +++ b/racket-hash-lang.el @@ -19,7 +19,7 @@ (racket--easy-keymap-define `((("C-c C-c" "C-c C-k") ,#'racket-run-module-at-point) - ("C-c C-z" ,#'racket-repl) + ("C-c C-z" ,#'racket-edit-switch-to-repl) ("" ,#'racket-run-and-switch-to-repl) ("M-C-" ,#'racket-racket) ("C-" ,#'racket-test) diff --git a/racket-mode.el b/racket-mode.el index 05ec57c0..5b1bc8e9 100644 --- a/racket-mode.el +++ b/racket-mode.el @@ -44,7 +44,7 @@ (racket--easy-keymap-define '((("C-c C-c" "C-c C-k") racket-run-module-at-point) - ("C-c C-z" racket-repl) + ("C-c C-z" racket-edit-switch-to-repl) ("" racket-run-and-switch-to-repl) ("M-C-" racket-racket) ("C-" racket-test) diff --git a/racket-repl.el b/racket-repl.el index 36abf729..f1653030 100644 --- a/racket-repl.el +++ b/racket-repl.el @@ -508,30 +508,37 @@ even when the module language doesn't provide any binding for (defun racket-repl (&optional noselect) "Show a Racket REPL buffer in some window. -*IMPORTANT* - The main, intended use of Racket Mode's REPL is that you -`find-file' some specific .rkt file, then run it using +`find-file' some specific file, then run it using a command like `racket-run' or `racket-run-module-at-point'. The resulting REPL will correspond to those definitions and match your expectations. -If you really want to start a REPL for no file in particular, -then you could use this `racket-repl' command. But the resulting -REPL will have a minimal \"#lang racket/base\" namespace. You -could enter \"(require racket)\" if you want the equivalent of -\"#lang racket\". You could also \"(require racket/enter)\" if -you want things like \"enter!\". But in some sense you'd be -\"using it wrong\". If you actually don't want to use Racket -Mode's REPL as intended, then consider using a plain Emacs -`shell' buffer to run command-line Racket." +Therefore this `racket-repl' command -- which is intended as a +convenience for people who want a way to \"just get a quick +REPL\" -- is actually implemented as just running the file named +in the customization variable `racket-repl-command-file'. By +default when that file doesn't exist, it is created to contain +just \"#lang racket/base\". You may edit the file to use a +different lang, require other modules, or whatever." (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 - (select-window (get-buffer-window repl-buffer t)))))) + ;; Create file if it doesn't exist + (unless (file-exists-p racket-repl-command-file) + (let ((dir (file-name-directory racket-repl-command-file))) + (unless (file-exists-p dir) + (make-directory dir t))) + (write-region ";; Used by M-x racket-repl; you may edit\n#lang racket/base\n" + nil racket-repl-command-file)) + ;; Visit the file without selecting it, and run it. + (let ((racket-repl-buffer-name-function #'racket-repl-buffer-name-unique)) + (with-current-buffer (find-file-noselect racket-repl-command-file) + (racket--repl-run + (list racket-repl-command-file) + nil + nil + (lambda () + (display-buffer racket-repl-buffer-name) + (unless noselect + (select-window (get-buffer-window racket-repl-buffer-name t)))))))) ;;; Run @@ -649,9 +656,7 @@ the variable `racket-before-run-hook'." (`(4) 'high) (`(16) 'debug) (_ racket-error-context)) - (lambda () - (display-buffer racket-repl-buffer-name) - (select-window (get-buffer-window racket-repl-buffer-name t))))) + #'racket-edit-switch-to-repl)) (defun racket-test (&optional prefix) "Run the \"test\" submodule. @@ -916,13 +921,21 @@ This displays the buffer but does not change the selected window." (lambda (_id) (funcall continue repl-buf))))))) -;;; Misc +;;; Switch between associcated edit and REPL buffers + +(defun racket-edit-switch-to-repl () + "Select REPL buffer associated with the edit buffer." + (interactive) + (racket--assert-edit-mode) + (when-let (repl-buf (get-buffer racket-repl-buffer-name)) + (when (buffer-live-p repl-buf) + (display-buffer repl-buf) + (select-window (get-buffer-window repl-buf t))))) (defun racket-repl-file-name () "Return the file running in the REPL, or nil. -The result can be nil if the REPL is not started, or if it is -running no particular file." +The result can be nil if the REPL is not started." (when (racket--repl-session-id) (racket--cmd/await (racket--repl-session-id) `(path)))) @@ -935,12 +948,9 @@ running no particular file." (and buf-file repl-file (string-equal buf-file repl-file))))) (defun racket-repl-switch-to-edit () - "Switch to the window for the buffer of the file running in the REPL. - -If no buffer is visting the file, `find-file' it in `other-window'. + "Select edit buffer of the file running in the REPL. -If the REPL is running no file -- if the prompt is `>` -- use the -most recent `racket-mode' buffer, if any." +If no buffer is visting the file, `find-file' it in `other-window'." (interactive) (pcase (racket-repl-file-name) ((and (pred stringp) path)