forked from greghendershott/racket-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathracket-eval.el
93 lines (81 loc) · 3.31 KB
/
racket-eval.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
;;; racket-eval.el
;; Copyright (c) 2013-2015 by Greg Hendershott.
;; Portions Copyright (C) 1985-1986, 1999-2013 Free Software Foundation, Inc.
;; Author: Greg Hendershott
;; URL: https://github.com/greghendershott/racket-mode
;; License:
;; This is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version. This is distributed in the hope that it will be
;; useful, but without any warranty; without even the implied warranty
;; of merchantability or fitness for a particular purpose. See the GNU
;; General Public License for more details. See
;; http://www.gnu.org/licenses/ for details.
(require 'racket-repl)
(defun racket--eval (expression)
"Eval EXPRESSION in the *Racket REPL* buffer, allow Racket output to be displayed, and show the window. Intended for use by things like ,run command."
(racket-repl)
(racket--repl-forget-errors)
(comint-send-string (racket--get-repl-buffer-process) expression)
(racket--repl-show-and-move-to-end))
(defun racket--eval/buffer (expression)
"Eval EXPRESSION in the *Racket REPL* buffer, but redirect the
resulting output to a *Racket REPL Redirected Output* buffer, and
return that buffer's name."
(racket-repl-ensure-buffer-and-process)
;; Important: Leading space in buffer name disables undo for it.
;; That in turn means that racket--eval/buffer in the midst of a
;; command won't cause an undo-boundary to be inserted.
(let ((output-buffer-name " *Racket REPL Redirected Output*"))
(with-current-buffer (get-buffer-create output-buffer-name)
(erase-buffer)
(comint-redirect-send-command-to-process
expression
output-buffer-name
(racket--get-repl-buffer-process)
nil ;echo?
t) ;no-display?
;; Wait for the process to complete
(with-current-buffer (process-buffer (racket--get-repl-buffer-process))
(while (null comint-redirect-completed)
(accept-process-output nil 1)))
output-buffer-name)))
(defun racket--eval/string (expression)
"Call `racket--eval/buffer' and return the output as a string."
(let ((output-buffer (racket--eval/buffer expression)))
(with-current-buffer output-buffer
(goto-char (point-min))
;; Skip past the expression, if it was echoed
(and (looking-at expression)
(forward-line))
;; Skip past the Typed Racket type display, if any
(and (looking-at "^- : ")
(forward-line))
(buffer-substring (point) (point-max)))))
(defun racket--eval/sexpr (expression)
"Call `racket--eval/string' and `read' the result to return a sexpr."
(eval (read (racket--eval/string expression))))
(defun racket--shell (cmd)
(let ((w (selected-window)))
(save-buffer)
(let ((rw (get-buffer-window "*shell*")))
(if rw
(select-window rw)
(other-window -1)))
(message (concat cmd "..."))
(shell)
(pop-to-buffer-same-window "*shell*")
(comint-send-string "*shell*" (concat cmd "\n"))
(select-window w)
(sit-for 3)
(message nil)))
(provide 'racket-eval)
;; Local Variables:
;; coding: utf-8
;; comment-column: 40
;; indent-tabs-mode: nil
;; require-final-newline: t
;; show-trailing-whitespace: t
;; End:
;; racket-eval.el ends here