Skip to content

Commit

Permalink
racket-run vs. racket-run-module-at-point
Browse files Browse the repository at this point in the history
The command named `racket-run` now runs the `main` submodule, if any,
else the file module. This behavior is like command-line racket, and,
like Dr Racket in its default mode. (Note that if a main submodule is
found and run, the REPL prompt will be "file.rkt/main>". This is
intentional. When the "magic" happens, you will see that it happened.)

The command named `racket-run-module-at-point` is what `racket-run`
used to be: It runs a specific module around point. C-c C-c is still
bound to this.

How the back end implements this: If a dynamic-requiring `(submod
"file.rkt" main)` fails, we always retry with "file.rkt". This
automatically handles the racket-run case. And it is N/A for the
racket-run-at-module case because it will only try to run `main` if
such a submodule exists in the source file.

This fixes issue #430 -- at least if you use the new `racket-run`
command, either directly or via `racket-run-and-switch-to-repl` bound
to F5 by default. If you have the default keybindings and C-c C-c to
do a racket-run-module-at-point, the hidden `main` submodule created
by lindenmayer/turtle will not run adn therefore the tree will not
print.
  • Loading branch information
greghendershott committed Mar 23, 2020
1 parent c0abae2 commit 5226366
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 32 deletions.
1 change: 1 addition & 0 deletions doc/generate.el
Expand Up @@ -64,6 +64,7 @@
racket-xp-documentation
"Run"
racket-run
racket-run-module-at-point
racket-repl
racket-repl-describe
racket-repl-documentation
Expand Down
22 changes: 17 additions & 5 deletions doc/racket-mode.texi
Expand Up @@ -99,6 +99,7 @@ Explore
Run
* racket-run::
* racket-run-module-at-point::
* racket-repl::
* racket-repl-describe::
* racket-repl-documentation::
Expand Down Expand Up @@ -1183,6 +1184,7 @@ definitions in submodules.

@menu
* racket-run::
* racket-run-module-at-point::
* racket-repl::
* racket-repl-describe::
* racket-repl-documentation::
Expand All @@ -1198,19 +1200,19 @@ definitions in submodules.
@node racket-run
@subsection racket-run

@kbd{C-c C-k} or @kbd{C-c C-c}
@kbd{M-x} @code{racket-run} @kbd{RET}

Save the buffer in REPL and run your program.

Save and evaluate the buffer in REPL.
Runs the @code{main} submodule, if any, otherwise the file's module.
See also @ref{racket-run-module-at-point}.

With one C-u prefix, uses errortrace for improved stack traces.
Otherwise follows the @ref{racket-error-context} setting.

With two C-u prefixes, instruments code for step debugging. See
@ref{racket-debug-mode} and the variable @ref{racket-debuggable-files}.

If point is within a Racket module form, the REPL ``enters'' that
submodule (uses its language info and namespace).

When you run again, the file is evaluated from scratch --- the
custodian releases resources like threads and the evaluation
environment is reset to the contents of the file. In other words,
Expand Down Expand Up @@ -1239,6 +1241,16 @@ To visit these locations, move point there and press RET or mouse
click. Or, use the standard @code{next-error} and @code{previous-error}
commands.

@node racket-run-module-at-point
@subsection racket-run-module-at-point

@kbd{C-c C-k} or @kbd{C-c C-c}

Save the buffer and run the moudule at point.

Like @ref{racket-run} but runs the module around point -- either the
file module, or, a submodule nested at ny depth.

@node racket-repl
@subsection racket-repl

Expand Down
2 changes: 1 addition & 1 deletion racket-mode.el
Expand Up @@ -47,7 +47,7 @@
(defvar racket-mode-map
(racket--easy-keymap-define
'((("C-c C-c"
"C-c C-k") racket-run)
"C-c C-k") racket-run-module-at-point)
("C-c C-z" racket-repl)
("<f5>" racket-run-and-switch-to-repl)
("M-C-<f5>" racket-racket)
Expand Down
21 changes: 17 additions & 4 deletions racket-repl.el
Expand Up @@ -235,17 +235,17 @@ buffer to run command-line Racket."

;;;###autoload
(defun racket-run (&optional prefix)
"Save and evaluate the buffer in REPL.
"Save the buffer in REPL and run your program.
Runs the `main` submodule, if any, otherwise the file's module.
See also `racket-run-module-at-point'.
With one C-u prefix, uses errortrace for improved stack traces.
Otherwise follows the `racket-error-context' setting.
With two C-u prefixes, instruments code for step debugging. See
`racket-debug-mode' and the variable `racket-debuggable-files'.
If point is within a Racket module form, the REPL \"enters\" that
submodule (uses its language info and namespace).
When you run again, the file is evaluated from scratch --- the
custodian releases resources like threads and the evaluation
environment is reset to the contents of the file. In other words,
Expand All @@ -268,6 +268,19 @@ include:
To visit these locations, move point there and press RET or mouse
click. Or, use the standard `next-error' and `previous-error'
commands."
(interactive "P")
(racket--repl-run (list (racket--buffer-file-name) 'main)
(pcase prefix
(`(4) 'high)
(`(16) 'debug)
(_ racket-error-context))))

;;;###autoload
(defun racket-run-module-at-point (&optional prefix)
"Save the buffer and run the moudule at point.
Like `racket-run' but runs the module around point -- either the
file module, or, a submodule nested at ny depth."
(interactive "P")
(racket--repl-run (racket--what-to-run)
(pcase prefix
Expand Down
30 changes: 15 additions & 15 deletions racket/fresh-line.rkt
Expand Up @@ -5,24 +5,24 @@

;; Borrowed from xrepl

(define last-output-port #f)
(define last-error-port #f)
(define last-output-port (make-parameter #f))
(define last-error-port (make-parameter #f))

(define (maybe-new-output-ports)
(define-syntax-rule (maybe last cur)
(unless (eq? last cur)
(when (and last
(not (port-closed? last)))
(flush-output last)) ;just in case
(set! last cur)
(flush-output last)
(port-count-lines! last)))
(maybe last-output-port (current-output-port))
(maybe last-error-port (current-error-port)))
(define (maybe last cur)
(unless (eq? (last) (cur))
(when (and (last)
(not (port-closed? (last))))
(flush-output (last))) ;just in case
(last (cur))
(flush-output (last))
(port-count-lines! (last))))
(maybe last-output-port current-output-port)
(maybe last-error-port current-error-port))

(define (fresh-line [stderr? #f])
(maybe-new-output-ports)
(define port (if stderr? last-error-port last-output-port))
(define port (if stderr? (last-error-port) (last-output-port)))
(flush-output port)
(define-values [line col pos] (port-next-location port))
(unless (eq? col 0) (newline)))
Expand All @@ -33,5 +33,5 @@
;; will think that it's still right after the printout; call this
;; function in such cases to adjust the column to 0.
(maybe-new-output-ports)
(define-values [line col pos] (port-next-location last-output-port))
(set-port-next-location! last-output-port line 0 pos))
(define-values [line col pos] (port-next-location (last-output-port)))
(set-port-next-location! (last-output-port) line 0 pos))
21 changes: 14 additions & 7 deletions racket/repl.rkt
Expand Up @@ -211,7 +211,7 @@
context-level
cmd-line-args
debug-files
ready-thunk) cfg)
ready-thunk) cfg)
(define-values (dir file mod-path) (maybe-mod->dir/file/rmp maybe-mod))
;; Always set current-directory and current-load-relative-directory
;; to match the source file.
Expand Down Expand Up @@ -272,14 +272,21 @@
(with-expanded-syntax-caching-evaluator maybe-mod
(when (and maybe-mod mod-path)
(parameterize ([current-module-name-resolver module-name-resolver-for-run])
;; When exn:fail? during module load, re-run with "empty"
;; module. Note: Unlikely now that we're using
;; dynamic-require/some-namespace.
;; When exn:fail during module load, re-run.
(define (load-exn-handler exn)
(display-exn exn)
(define new-mod
(match mod-path
[`(submod ,(== file) main)
(log-racket-mode-debug "~v not found, retry as ~v"
mod-path (build-path dir file))
(->mod/existing (build-path dir file))]
;; Else display exn and retry as "empty" REPL.
[_
(display-exn exn)
#f]))
(channel-put (current-repl-msg-chan)
(struct-copy run-config cfg [maybe-mod #f]))
(sync never-evt))
(struct-copy run-config cfg [maybe-mod new-mod]))
(sync never-evt)) ;mananger thread will shutdown custodian
(with-handlers ([exn? load-exn-handler])
(maybe-configure-runtime mod-path) ;FIRST: see #281
(current-namespace
Expand Down

0 comments on commit 5226366

Please sign in to comment.