forked from Flusspferd/flusspferd
/
flusspferd.el
196 lines (169 loc) · 6.99 KB
/
flusspferd.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
;;; flusspferd.el --- Interactive interface to the Flusspferd REPL
;; Copyright (C) 2008, 2009 Flusspferd contributors (see "CONTRIBUTORS" or
;; http://flusspferd.org/contributors.txt)
;;
;; Author: Flusspferd contributors (see "CONTRIBUTORS" or
;; http://flusspferd.org/contributors.txt)
;; Homepage: http://flusspferd.org
;;
;; This file is NOT part of GNU Emacs.
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
;;; Commentary:
;;
;; ...
;;; Code:
(require 'comint)
(require 'easymenu)
;; Options
(defgroup flusspferd nil
"Flusspferd (Javascript interpreter) REPL"
:version "0.9pre"
:link '(url-link "http://flusspferd.org")
:prefix "flusspferd-"
:group 'external)
(defcustom flusspferd-executable "flusspferd"
"Flusspferd executable."
:group 'flusspferd
:type '(string))
(defcustom flusspferd-options ""
"Options for the Flusspferd executable."
:group 'flusspferd
:type '(string))
(defcustom flusspferd-buffer-name "*Flusspferd*"
"Name of the Flusspferd buffer."
:group 'flusspferd
:type '(string))
(defun flusspferd-is-running-p ()
"Check if Flusspferd is already running."
(comint-check-proc flusspferd-buffer-name))
(defun flusspferd-switch-to ()
"Switch to Flusspferd buffer."
(interactive)
(switch-to-buffer flusspferd-buffer-name))
(defmacro flusspferd-defun-and-go (name arglist docstring &rest body)
"Create two function with name NAME and NAME-and-go.
The NAME-and-go version calls (switch-to-buffer flusspferd-buffer-name) when done."
(let ((name-and-go (intern (concat (symbol-name name) "-and-go")))
(new-docstring
(concat docstring " Switches to Flusspferd buffer when finished.")))
`(progn
(defun ,name ,arglist ,docstring ,@body)
(defun ,name-and-go ,arglist ,new-docstring
,@body (list 'flusspferd-switch-to)))))
(flusspferd-defun-and-go flusspferd-eval-region (start end)
"Evaluates the region between START and END with flusspferd."
(interactive "r")
(unless (flusspferd-is-running-p)
(flusspferd))
(comint-send-region flusspferd-buffer-name start end)
(comint-send-string flusspferd-buffer-name "\n"))
(flusspferd-defun-and-go flusspferd-eval-last-defun ()
"Evaluate the function definition as determined by c-mark-function/js2-mark-defun."
(interactive)
(save-excursion
(if (functionp 'js2-mark-defun)
(js2-mark-defun)
(c-mark-function))
(flusspferd-eval-region (point) (mark))))
(flusspferd-defun-and-go flusspferd-eval-last-sexp ()
"Evaluate last sexp."
(interactive)
(flusspferd-eval-region
(save-excursion (backward-sexp) (point))
(point)))
(flusspferd-defun-and-go flusspferd-eval-buffer ()
"Evaluate buffer with Flusspferd."
(interactive)
(flusspferd-eval-region (point-min) (point-max)))
(flusspferd-defun-and-go flusspferd-eval-line ()
"Evaluate current line with Flusspferd."
(interactive)
(flusspferd-eval-region
(save-excursion (move-beginning-of-line nil) (point))
(save-excursion (move-end-of-line nil) (point))))
(defvar flusspferd-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-x\C-e" 'flusspferd-eval-last-sexp)
(define-key map "\C-c\C-c" 'flusspferd-eval-buffer)
(define-key map "\C-c\C-r" 'flusspferd-eval-region)
map))
(defun flusspferd-helper-mark-active ()
"Check if there is an active region."
(if (functionp 'use-region-p)
(use-region-p)
(and transient-mark-mode mark-active)))
(easy-menu-define flusspferd-minor-mode-menu flusspferd-minor-mode-map
"Flusspferd menu"
'("Flusspferd"
["Evaluate Last S-Expression" flusspferd-eval-last-sexp t]
["Evaluate Region" flusspferd-eval-region
:active (flusspferd-helper-mark-active)]
["Evaluate Buffer" flusspferd-eval-buffer t]
["Evaluate Line" flusspferd-eval-line t :active (not (and (eolp) (bolp)))]
["Evaluate Last Function Definition" flusspferd-eval-last-defun t]
"--"
["Go to Flusspferd buffer" flusspferd-switch-to
:visible (flusspferd-is-running-p)
:active (not (string= (buffer-name) flusspferd-buffer-name))]
["Start Flusspferd Shell" flusspferd
:visible (not (flusspferd-is-running-p))]))
;;;###autoload
(define-minor-mode flusspferd-minor-mode
"Flusspferd minor mode. Provides keymap to evaluate Javascript code with Flusspferd."
:init-value nil
:lighter "Flusspferd"
:keymap flusspferd-minor-mode-map
(easy-menu-add flusspferd-minor-mode-menu))
(defvar inferior-flusspferd-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-a" 'comint-bol)
map))
(defun inferior-flusspferd-mode-quit (&optional arg)
"Quit inferior flusspferd mode."
(interactive "P")
(when (and (or arg (y-or-n-p "Really quit Flusspferd? "))
(flusspferd-is-running-p))
(delete-process flusspferd-buffer-name)
(kill-buffer flusspferd-buffer-name)))
(easy-menu-define inferior-flusspferd-mode-menu inferior-flusspferd-mode-map
"Inferior Flusspferd Mode menu."
'("Inferior Flusspferd"
["Quit Flusspferd" inferior-flusspferd-mode-quit t]))
(define-derived-mode inferior-flusspferd-mode comint-mode "Inferior Flusspferd"
"Embeds Flusspferd as a Javascript REPL into Emacs."
:group 'flusspferd
(setq comint-prompt-regexp "^[>\\?] ")
(setq comint-use-prompt-regexp t)
(setq comint-process-echoes t)
(easy-menu-add inferior-flusspferd-mode-menu))
;;;###autoload
(defun flusspferd ()
"Flusspferd (Javascript interpreter) REPL."
(interactive)
(unless (flusspferd-is-running-p)
(let ((flusspferd-buffer
(apply 'make-comint-in-buffer "Flusspferd" flusspferd-buffer-name
flusspferd-executable nil
(split-string flusspferd-options))))
(save-excursion
(set-buffer flusspferd-buffer)
(inferior-flusspferd-mode))))
(when (interactive-p)
(switch-to-buffer flusspferd-buffer-name)))
(provide 'flusspferd)
;;; flusspferd.el ends here