-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodejs-repl.el
328 lines (285 loc) · 12.5 KB
/
nodejs-repl.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
;;; nodejs-repl.el --- Run Node.js REPL
;; Copyright (C) 2012-2013 Takeshi Arabiki
;; Author: Takeshi Arabiki
;; Version: See `nodejs-repl-version'
;; This program 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 3 of the License, or
;; (at your option) any later version.
;; This program 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.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This program is derived from comint-mode and provides the below features.
;;
;; * TAB completion, same as Node.js REPL
;; * file name completion in string
;; * incremental history search
;;
;;
;; Put this file in your Emacs lisp path (e.g. ~/.emacs.d/site-lisp)
;; and add to the following lines to your .emacs:
;;
;; (require 'nodejs-repl)
;;
;; In order to run Node.js REPL, type M-x nodejs-repl.
;; See also `comint-mode' to check key bindings, typing M-x help f comint-mode.
;;
(require 'cc-mode)
(require 'comint)
(require 'ansi-color)
(defgroup nodejs-repl nil
"Run Node.js REPL and communicate the process."
:group 'processes)
(defconst nodejs-repl-version "0.0.2"
"Node.js mode Version.")
(defcustom nodejs-repl-command "node"
"Node.js command used in `nodejs-repl-mode'."
:group 'nodejs-repl
:type 'string)
(defcustom nodejs-repl-prompt "> "
"Node.js prompt used in `nodejs-repl-mode'."
:group 'nodejs-repl
:type 'string)
(defvar nodejs-repl-process-name "nodejs"
"process name of Node.js REPL.")
(defvar nodejs-repl-temp-buffer-name "*nodejs-repl-command-output*")
(defvar nodejs-repl-mode-syntax-table
(let ((st (make-syntax-table)))
(c-populate-syntax-table st)
(modify-syntax-entry ?$ "_" st)
st))
(defvar nodejs-repl-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "TAB") 'comint-dynamic-complete)
(define-key map (kbd "C-c C-c") 'nodejs-repl-quit-or-cancel)
map))
;; process.stdout.columns should be set.
;; Node.js 0.8 and 0.10 uses this value as the maximum number of columns,
;; but process.stdout.columns in Emacs is infinity because Emacs returns 0 as winsize.ws_col.
;; The completion candidates won't be displayed if process.stdout.columns is infinity.
;; see also `handleGroup` function in readline.js
(defvar nodejs-repl-code
(concat
"process.stdout.columns = %d;"
"require('repl').start('%s', null, null, true, false)"))
(defvar nodejs-repl-input-ignoredups t
"If non-nil, don't add input matching the last on the input ring.
See also `comint-input-ignoredups'")
(defvar nodejs-repl-process-echoes t
"If non-nil, Node.js does not echo any input.
See also `comint-process-echoes'")
(defvar nodejs-repl-extra-espace-sequence-re "\\(\x1b\\[[0-9]+[GJK]\\)")
(defvar nodejs-repl-ansi-color-sequence-re "\\(\x1b\\[[0-9]+m\\)")
;;; if send string like "a; Ma\t", return a; Math\x1b[1G> a; Math\x1b[0K\x1b[10G
(defvar nodejs-repl-prompt-re-format
(concat
"\x1b\\[1G"
"\\("
"\x1b\\[0J%s.*\x1b\\[[0-9]+G" ; for Node.js 0.8
"\\|"
"%s.*\x1b\\[0K\x1b\\[[0-9]+G" ; for Node.js 0.4 or 0.6
"\\)"
"$"))
(defvar nodejs-repl-prompt-re
(format nodejs-repl-prompt-re-format nodejs-repl-prompt nodejs-repl-prompt))
;;; not support Unicode characters
(defvar nodejs-repl-require-re
(concat
"\\(?:^\\|\\s-\\|[-+*/%&|><!;{}()[]\\|\\]\\)" ; delimiter
"require\\s-*(\\s-*"
"\\("
"\"[^\"\\]*\\(?:\\\\.[^\"\\]*\\)*" ; double quote
"\\|"
"'[^'\\]*\\(?:\\\\.[^'\\]*\\)*" ; single quote
"\\)"
"$"))
(defvar nodejs-repl-cache-token "")
(defvar nodejs-repl-cache-candidates ())
;;;--------------------------
;;; Private functions
;;;--------------------------
(defun nodejs-repl--in-string-p (&optional pos)
"Return non-nil if point is inside string"
(nth 3 (syntax-ppss pos)))
(defun nodejs-repl--extract-require-argument (string)
(if (string-match nodejs-repl-require-re string)
(match-string 1 string)))
(defun nodejs-repl--get-last-token (string)
"Return the last token in the string."
(if (string-match "\\([._$]\\|\\w\\)+$" string)
(match-string 0 string)))
;;; TODO:
;;; * the case that a command is sent while another command is being prossesed
;;; * the case that incomplete commands are sent like "1 +\n"
;;; * support commands which output a string without CR-LF like process.stdout.write("a")
;;; while being processed
(defun nodejs-repl--send-string (string)
"Send string to Node.js process and return the output."
(with-temp-buffer
(let* ((proc (get-process nodejs-repl-process-name))
(orig-marker (marker-position (process-mark proc)))
(orig-filter (process-filter proc))
(orig-buf (process-buffer proc)))
(unwind-protect
(progn
(set-process-buffer proc (current-buffer))
(set-process-filter proc 'nodejs-repl--insert-and-update-status)
(set-marker (process-mark proc) (point-min))
(process-send-string proc string)
(nodejs-repl--wait-for-process proc string 0.01))
(set-process-buffer proc orig-buf)
(set-process-filter proc orig-filter)
(set-marker (process-mark proc) orig-marker orig-buf))
(buffer-string))))
(defun nodejs-repl--wait-for-process (proc string interval)
"Wait for Node.js process to output all results."
(process-put proc 'last-line "")
(process-put proc 'running-p t)
;; trim trailing whitespaces
(setq string (replace-regexp-in-string "\\s-*$" "" string))
;; TODO: write unit test for the case that the process returns 'foo' when string is 'foo\t'
(while (or (process-get proc 'running-p)
(not
(let ((last-line (process-get proc 'last-line)))
(or (string-match-p nodejs-repl-prompt-re last-line)
(string-match-p "^\x1b[[0-9]+D$" last-line) ; for Node.js 0.8
(string= last-line string)))))
(process-put proc 'running-p nil)
(accept-process-output proc interval)))
(defun nodejs-repl--insert-and-update-status (proc string)
"Insert the output string and update the process status (properties)
when receive the output string"
(process-put proc 'running-p t)
(with-current-buffer (process-buffer proc)
(insert string)
(goto-char (point-max))
(process-put proc 'last-line (buffer-substring (point-at-bol) (point)))))
(defun nodejs-repl--get-candidates-from-process (token)
"Get copmletion candidates sending TAB to Node.js process."
(let ((ret (nodejs-repl--send-string (concat token "\t")))
candidates)
(nodejs-repl-clear-line)
(when (not (equal ret token))
(if (string-match-p "\n" ret)
(progn
;; remove extra substrings
(setq ret (replace-regexp-in-string "\r" "" ret))
;; remove LF
(setq ret (replace-regexp-in-string "\n\\{2,\\}" "\n" ret))
;; trim trailing whitespaces
(setq ret (replace-regexp-in-string "\\s-*$" "" ret))
;; don't split by whitespaces because the prompt may has whitespaces!!
(setq candidates (split-string ret "\n"))
;; remove the first element (input) and the last element (prompt)
(setq candidates (reverse (cdr (reverse (cdr candidates)))))
;; split by whitespaces
;; '("encodeURI encodeURIComponent") -> '("encodeURI" "encodeURIComponent")
(setq candidates (split-string (mapconcat 'identity candidates " ") "\\s-+")))
(setq ret (replace-regexp-in-string nodejs-repl-extra-espace-sequence-re "" ret))
(setq candidates (list (nodejs-repl--get-last-token ret)))))
candidates))
;;;--------------------------
;;; Public functions
;;;--------------------------
(defun nodejs-repl-quit-or-cancel ()
"Send ^C to Node.js process."
(interactive)
(process-send-string (get-process "node") "\x03"))
(defun nodejs-repl-clear-line ()
"Send ^U to Node.js process."
(nodejs-repl--send-string "\x15"))
(defun nodejs-repl-execute (command &optional buf)
"Execute a command and output the result to the temporary buffer."
(let ((ret (nodejs-repl--send-string (concat command "\n"))))
(with-current-buffer (get-buffer-create nodejs-repl-temp-buffer-name)
(erase-buffer)
(setq ret (replace-regexp-in-string nodejs-repl-ansi-color-sequence-re "" ret))
;; delete inputs
(setq ret (replace-regexp-in-string "\\(\\w\\|\\W\\)+\r\r\n" "" ret))
(setq ret (replace-regexp-in-string "\r" "" ret))
(insert ret)
;; delete last line (prompt)
(goto-char (point-max))
(delete-region (point-at-bol) (point)))))
(defun nodejs-repl-complete-from-process ()
"Dynamically complete tokens at the point."
(when (comint-after-pmark-p)
(let* ((input (buffer-substring (comint-line-beginning-position) (point)))
require-arg
token
candidates
ret)
(if (nodejs-repl--in-string-p)
(progn
(setq require-arg (nodejs-repl--extract-require-argument input))
(if (and require-arg
(or (= (length require-arg) 1)
(not (string-match-p "[./]" (substring require-arg 1 2)))))
(setq token (concat "require(" require-arg))
(setq ret (comint-dynamic-complete-as-filename))))
(setq token (nodejs-repl--get-last-token input)))
(when token
(setq candidates (nodejs-repl-get-candidates token))
;; TODO: write unit test
(setq token (replace-regexp-in-string "^require(['\"]" "" token))
(setq ret (comint-dynamic-simple-complete token candidates)))
(if (eq ret 'sole)
(delete-char -1))
ret)))
(defun nodejs-repl-get-candidates (token)
"Get copmletion candidates."
(let (candidates)
(if (and (not (equal nodejs-repl-cache-token ""))
(string-match-p (concat "^" nodejs-repl-cache-token) token)
(not (string-match-p (concat "^" nodejs-repl-cache-token ".*?[.(/'\"]") token)))
(setq candidates nodejs-repl-cache-candidates)
(if (equal token "require(") ; a bug occurs when press TAB after "require(" in node 0.6
(setq candidates nil)
(setq candidates (nodejs-repl--get-candidates-from-process token)))
(setq nodejs-repl-cache-token token)
(setq nodejs-repl-cache-candidates candidates))
candidates))
;;; a function belong to comint-output-filter-functions must have one argument
(defun nodejs-repl-filter-escape-sequnces (string)
"Filter extra escape sequences from output."
(let ((beg (or comint-last-output-start
(point-min-marker)))
(end (process-mark (get-buffer-process (current-buffer)))))
(save-excursion
(goto-char beg)
;; remove ansi escape sequences used in readline.js
(while (re-search-forward nodejs-repl-extra-espace-sequence-re end t)
(replace-match "")))))
;;; a function belong to comint-output-filter-functions must have one argument
(defun nodejs-repl-clear-cache (string)
"Clear caches when outputting the result."
(setq nodejs-repl-cache-token "")
(setq nodejs-repl-cache-candidates ()))
(define-derived-mode nodejs-repl-mode comint-mode "Node.js REPL"
"Major mode for Node.js REPL"
:syntax-table nodejs-repl-mode-syntax-table
(set (make-local-variable 'font-lock-defaults) '(nil nil t))
(add-hook 'comint-output-filter-functions 'nodejs-repl-filter-escape-sequnces nil t)
(add-hook 'comint-output-filter-functions 'nodejs-repl-clear-cache nil t)
(setq comint-input-ignoredups nodejs-repl-input-ignoredups)
(setq comint-process-echoes nodejs-repl-process-echoes)
;; delq seems to change global variables if called this phase
(set (make-local-variable 'comint-dynamic-complete-functions)
(delete 'comint-dynamic-complete-filename comint-dynamic-complete-functions))
(add-hook 'comint-dynamic-complete-functions 'nodejs-repl-complete-from-process nil t)
(ansi-color-for-comint-mode-on))
(defun nodejs-repl ()
"Run Node.js REPL."
(interactive)
(setq nodejs-repl-prompt-re
(format nodejs-repl-prompt-re-format nodejs-repl-prompt nodejs-repl-prompt))
(switch-to-buffer-other-window
(apply 'make-comint nodejs-repl-process-name nodejs-repl-command nil
`("-e" ,(format nodejs-repl-code (window-width) nodejs-repl-prompt))))
(nodejs-repl-mode))
(provide 'nodejs-repl)