Skip to content

Commit

Permalink
Add racket-shell-or-terminal-function; closes greghendershott#641
Browse files Browse the repository at this point in the history
  • Loading branch information
greghendershott committed Nov 2, 2022
1 parent 8077ab7 commit a77b50f
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 32 deletions.
11 changes: 9 additions & 2 deletions doc/generate.el
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@
"Browsing file URLs with anchors"
racket-browse-url-using-temporary-file
"Configuring back ends"
racket-add-back-end)
racket-add-back-end
"Running racket and raco commands in a shell or terminal"
racket-shell
racket-term
racket-ansi-term
racket-vterm)
"Configuration functions to include in the Reference.")

(defun racket-generate--configuration-functions ()
Expand Down Expand Up @@ -198,7 +203,9 @@
"Experimental debugger variables"
racket-debuggable-files
"Showing information"
racket-show-functions)
racket-show-functions
"Running racket and raco commands in a shell or terminal"
racket-shell-or-terminal-function)
"Variables to include in the Reference.")

(defun racket-generate--variables ()
Expand Down
94 changes: 92 additions & 2 deletions doc/racket-mode.texi
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Variables
* Other variables::
* Experimental debugger variables::
* Showing information::
* Running racket and raco commands in a shell or terminal::
General variables
Expand Down Expand Up @@ -224,12 +225,17 @@ Experimental debugger variables
Showing information
* racket-show-functions::
Running racket and raco commands in a shell or terminal
* racket-shell-or-terminal-function::
Configuration functions
* Showing information: Showing informationx.
* Associating edit buffers with REPL buffers::
* Browsing file URLs with anchors::
* Configuring back ends::
* Running racket and raco commands in a shell or terminal: Running racket and raco commands in a shell or terminalx.
Showing information
Expand All @@ -252,6 +258,13 @@ Browsing file URLs with anchors
Configuring back ends
* racket-add-back-end::
Running racket and raco commands in a shell or terminal
* racket-shell::
* racket-term::
* racket-ansi-term::
* racket-vterm::
Faces
* All::
Expand Down Expand Up @@ -2127,7 +2140,10 @@ would like to see documentation for all identifiers named

@kbd{<C-M-f5>}

Do ``racket <file>'' in a shell buffer.
Use command-line racket to run the file.

Uses a shell or terminal buffer as specified by the configuration
variable @ref{racket-shell-or-terminal-function}.

@node racket-profile
@subsection racket-profile
Expand Down Expand Up @@ -2363,7 +2379,10 @@ See also:

@kbd{M-x} @code{racket-raco-test}

Do ``raco test -x <file>'' in a shell buffer to run the ``test'' submodule.
Use command-line raco test to run the ``test'' submodule.

Uses a shell or terminal buffer as specified by the configuration
variable @ref{racket-shell-or-terminal-function}.

@node Eval
@section Eval
Expand Down Expand Up @@ -2643,6 +2662,7 @@ Delete the ``compiled'' directories made by @ref{racket-mode-start-faster}.
* Other variables::
* Experimental debugger variables::
* Showing information::
* Running racket and raco commands in a shell or terminal::
@end menu

@node General variables
Expand Down Expand Up @@ -3077,6 +3097,25 @@ A function that shows STR in a fixed location may of course
ignore POS. Examples: @ref{racket-show-echo-area} and
@ref{racket-show-header-line}

@node Running racket and raco commands in a shell or terminal
@section Running racket and raco commands in a shell or terminal

@menu
* racket-shell-or-terminal-function::
@end menu

@node racket-shell-or-terminal-function
@subsection racket-shell-or-terminal-function

How @ref{racket-racket} and @ref{racket-raco-test} run commands.

The function should accept a command string, not including a
newline, get or create a suitable buffer, send the command, and
send a newline or enter.

Predefined choices include @ref{racket-shell}, @ref{racket-term},
@ref{racket-ansi-term}, and @ref{racket-vterm}.

@node Configuration functions
@chapter Configuration functions

Expand All @@ -3085,6 +3124,7 @@ ignore POS. Examples: @ref{racket-show-echo-area} and
* Associating edit buffers with REPL buffers::
* Browsing file URLs with anchors::
* Configuring back ends::
* Running racket and raco commands in a shell or terminal: Running racket and raco commands in a shell or terminalx.
@end menu

@node Showing informationx
Expand Down Expand Up @@ -3442,6 +3482,56 @@ are a few examples.
:racket-program "xvfb-run racket")
@end lisp

@node Running racket and raco commands in a shell or terminalx
@section Running racket and raco commands in a shell or terminal

@menu
* racket-shell::
* racket-term::
* racket-ansi-term::
* racket-vterm::
@end menu

@node racket-shell
@subsection racket-shell

@code{(racket-shell cmd)}


Run CMD using @code{shell}.

A value for the variable @ref{racket-shell-or-terminal-function}.

@node racket-term
@subsection racket-term

@code{(racket-term cmd)}


Run CMD using @code{term}.

A value for the variable @ref{racket-shell-or-terminal-function}.

@node racket-ansi-term
@subsection racket-ansi-term

@code{(racket-ansi-term cmd)}


Run CMD using @code{ansi-term}.

A value for the variable @ref{racket-shell-or-terminal-function}.

@node racket-vterm
@subsection racket-vterm

@code{(racket-vterm cmd)}


Run CMD using @code{vterm}, if that package is installed.

A value for the variable @ref{racket-shell-or-terminal-function}.

@node Faces
@chapter Faces

Expand Down
20 changes: 19 additions & 1 deletion racket-custom.el
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
;; In other words defcustom of racket-foo-bar has a :tag "Foo Bar".

(require 'rx)
(require 'cl-lib)
(require 'sh-script) ;for sh-heredoc face
(require 'comint) ;for comint-simple-send in racket-shell-or-terminal

(defgroup racket nil
"Modes for the Racket language."
Expand Down Expand Up @@ -91,6 +91,24 @@ and `racket-repl-documentation' should look for the search page.
:safe (lambda (val) (or (stringp val) (eq val 'local)))
:group 'racket)

(defcustom racket-shell-or-terminal-function 'racket-shell
"How `racket-racket' and `racket-raco-test' run commands.
The function should accept a command string, not including a
newline, get or create a suitable buffer, send the command, and
send a newline or enter.
Predefined choices include `racket-shell', `racket-term',
`racket-ansi-term', and `racket-vterm'."
:tag "Shell or Terminal"
:type 'functionp
:options '(racket-shell
racket-term
racket-ansi-term
racket-vterm)
:safe #'functionp
:group 'racket)

;;; Xp Mode

(defgroup racket-xp nil
Expand Down
27 changes: 0 additions & 27 deletions racket-edit.el
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,6 @@
(require 'hideshow)
(require 'xref)

(defun racket-racket ()
"Do \"racket <file>\" in a shell buffer."
(interactive)
(racket--shell (concat (shell-quote-argument racket-program)
" "
(shell-quote-argument (racket--buffer-file-name)))))

(defun racket-raco-test ()
"Do \"raco test -x <file>\" in a shell buffer to run the \"test\" submodule."
(interactive)
(racket--shell (concat (shell-quote-argument racket-program)
" -l raco test -x "
(shell-quote-argument (racket--buffer-file-name)))))

(defun racket--shell (cmd)
(racket--save-if-changed)
(let ((w (selected-window)))
(pcase (get-buffer-window "*shell*" t)
(`() (other-window -1))
(win (select-window win)))
(with-temp-message cmd
(shell)
(pop-to-buffer-same-window "*shell*")
(comint-send-string "*shell*" (concat cmd "\n"))
(select-window w)
(sit-for 3))))

;;; code folding

;;;###autoload
Expand Down
1 change: 1 addition & 0 deletions racket-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
(require 'racket-imenu)
(require 'racket-profile)
(require 'racket-logger)
(require 'racket-shell)
(require 'racket-stepper)
(require 'racket-repl)
(require 'racket-repl-buffer-name)
Expand Down
89 changes: 89 additions & 0 deletions racket-shell.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
;;; racket-shell.el -*- lexical-binding: t -*-

;; Copyright (c) 2022 by Greg Hendershott.
;; Portions Copyright (C) 1985-1986, 1999-2013 Free Software Foundation, Inc.

;; Author: Greg Hendershott
;; URL: https://github.com/greghendershott/racket-mode

;; SPDX-License-Identifier: GPL-3.0-or-later

(require 'racket-custom)
(require 'racket-util)
(require 'shell)
(require 'term)

(defun racket-racket ()
"Use command-line racket to run the file.
Uses a shell or terminal buffer as specified by the configuration
variable `racket-shell-or-terminal-function'."
(interactive)
(racket--shell-or-terminal
(concat (shell-quote-argument (racket--buffer-file-name)))))

(defun racket-raco-test ()
"Use command-line raco test to run the \"test\" submodule.
Uses a shell or terminal buffer as specified by the configuration
variable `racket-shell-or-terminal-function'."
(interactive)
(racket--shell-or-terminal
(concat "-l raco test -x "
(shell-quote-argument (racket--buffer-file-name)))))

(defun racket--shell-or-terminal (args)
(racket--save-if-changed)
(let* ((exe (shell-quote-argument
(if (file-name-absolute-p racket-program)
(expand-file-name racket-program) ;handle e.g. ~/
racket-program)))
(cmd (concat exe " " args))
(win (selected-window)))
(funcall racket-shell-or-terminal-function cmd)
(select-window win)))

(defun racket-shell (cmd)
"Run CMD using `shell'.
A value for the variable `racket-shell-or-terminal-function'."
(let ((buf (shell)))
(comint-simple-send buf cmd)))

(defun racket-term (cmd)
"Run CMD using `term'.
A value for the variable `racket-shell-or-terminal-function'."
(let ((buf (term (or explicit-shell-file-name
(getenv "ESHELL")
(getenv "SHELL")
"/bin/sh"))))
(term-simple-send buf cmd)))

(defun racket-ansi-term (cmd)
"Run CMD using `ansi-term'.
A value for the variable `racket-shell-or-terminal-function'."
(let ((buf (ansi-term (or explicit-shell-file-name
(getenv "ESHELL")
(getenv "SHELL")
"/bin/sh"))))
(term-simple-send buf cmd)))

(declare-function vterm "ext:vterm")
(declare-function vterm-send-return "ext:vterm")
(declare-function vterm-send-string "ext:vterm")

(defun racket-vterm (cmd)
"Run CMD using `vterm', if that package is installed.
A value for the variable `racket-shell-or-terminal-function'."
(unless (require 'vterm nil 'noerror)
(error "Package 'vterm' is not available"))
(vterm)
(vterm-send-string cmd)
(vterm-send-return))

(provide 'racket-shell)

;; racket-shell.el ends here

0 comments on commit a77b50f

Please sign in to comment.