Skip to content

Commit

Permalink
racket-repl: Re-implement as just running a file; closes #689
Browse files Browse the repository at this point in the history
Narrowly, this fixes the kind of problem as in #689 where a setting
like racket-pretty-print only takes effect via a run.

Broadly, this makes this command follow the the main intended use case
-- a REPL is associated with running a file. Now the command is just a
shortcut for running a file with a certain name (and creating that
file if it doesn't exist).

Also, this fits better in a hash-lang world. If say someone mostly
works with #lang rhombus, and wants "give me a quick REPL" to use
that, now they can instead of getting #lang racket/base.

Finally, this lets the doc string explain that the REPL is always
just about running some file. If a user wanted to have "several init
files", well, they're really talking about running several source
files frequently, which of course they can already do. And if they
want a quick key binding for each one, that's an easy customization
nearly any end user can do for themselves.

TL;DR: Years ago when some folks asked for a racket-repl command, this
is probably how I should have done it: A trivial variation of
racket-run.
  • Loading branch information
greghendershott committed Dec 11, 2023
1 parent e8cbeff commit 65b8eb1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 32 deletions.
1 change: 1 addition & 0 deletions doc/generate.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 15 additions & 12 deletions doc/racket-mode.texi
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ REPL variables
* racket-images-keep-last::
* racket-images-system-viewer::
* racket-pretty-print::
* racket-repl-command-file::
Other variables
Expand Down Expand Up @@ -2325,22 +2326,18 @@ simply the outermost, file module.

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion racket-custom.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
46 changes: 27 additions & 19 deletions racket-repl.el
Original file line number Diff line number Diff line change
Expand Up @@ -508,30 +508,38 @@ 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.
(with-current-buffer (find-file-noselect racket-repl-command-file)
(let ((name (format "*Racket REPL <%s>*" racket-repl-command-file)))
(setq-local racket-repl-buffer-name name))
(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

Expand Down

0 comments on commit 65b8eb1

Please sign in to comment.