-
Notifications
You must be signed in to change notification settings - Fork 3
/
node-repl.el
426 lines (371 loc) · 16.1 KB
/
node-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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
;;; nodejs-repl.el --- Run Node.js REPL
;; Copyright (C) 2012-2017 Takeshi Arabiki
;; Author: Takeshi Arabiki
;; Version: 0.1.1
;; 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 following 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 the following line to your .emacs:
;;
;; (require 'nodejs-repl)
;;
;; Type M-x nodejs-repl to run Node.js REPL.
;; See also `comint-mode' to check key bindings.
;;
;; You can define key bindings to send JavaScript codes to REPL like below:
;;
;; (add-hook 'js-mode-hook
;; (lambda ()
;; (define-key js-mode-map (kbd "C-x C-e") 'nodejs-repl-send-last-sexp)
;; (define-key js-mode-map (kbd "C-c C-r") 'nodejs-repl-send-region)
;; (define-key js-mode-map (kbd "C-c C-l") 'nodejs-repl-load-file)
;; (define-key js-mode-map (kbd "C-c C-z") 'nodejs-repl-switch-to-repl)))
;;
(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.1.1"
"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-arguments '()
"Command line parameters forwarded to `nodejs-repl-command'."
:group 'nodejs-repl
:type '(repeat string))
(defcustom nodejs-repl-prompt "> "
"Node.js prompt used in `nodejs-repl-mode'."
:group 'nodejs-repl
:type 'string)
(defcustom nodejs-repl-input-ignoredups t
"If non-nil, don't add input matching the last on the input ring.
See also `comint-input-ignoredups'"
:group 'nodejs-repl
:type 'boolean)
(defcustom nodejs-repl-process-echoes t
"If non-nil, Node.js does not echo any input.
See also `comint-process-echoes'"
:group 'nodejs-repl
:type 'boolean)
(defvar nodejs-repl-nodejs-version)
(defvar nodejs-repl--nodejs-version-re
"^v\\([0-9]+\\(?:\\.[0-9]+\\)*\\)\\(?:\\.x\\)*\\(?:-\\w+\\)?[\r\n]*$")
(defvar nodejs-repl-mode-hook nil
"Functions runafter `nodejs-repl' is started.")
(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-format
(concat
"process.stdout.columns = %d;"
"require('repl').start('%s', null, null, true, false, "
"require('repl')['REPL_MODE_' + '%s'.toUpperCase()])"))
(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 "[ \t\r\n]*\\'" "" 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 completion candidates sending TAB to Node.js process."
(let ((ret (if (version< nodejs-repl-nodejs-version "7.0.0")
(nodejs-repl--send-string (concat token "\t"))
(nodejs-repl--send-string (concat token "\t"))
(nodejs-repl--send-string "\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 "[ \t\r\n]*\\'" "" ret))
;; don't split by whitespaces because the prompt might have 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
(replace-regexp-in-string " *$" "" (mapconcat 'identity candidates " "))
"[ \t\r\n]+"))
)
(setq ret (replace-regexp-in-string nodejs-repl-extra-espace-sequence-re "" ret))
(let ((candidate-token (nodejs-repl--get-last-token ret)))
(setq candidates (if (or (null candidate-token) (equal candidate-token token))
nil
(list candidate-token))))))
candidates))
(defun nodejs-repl--get-or-create-process ()
(let ((proc (get-process nodejs-repl-process-name)))
(unless (processp proc)
(save-excursion (nodejs-repl))
(setq proc (get-process nodejs-repl-process-name)))
proc))
(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 "")))))
(defun nodejs-repl--clear-cache (string)
"Clear caches when outputting the result."
(setq nodejs-repl-cache-token "")
(setq nodejs-repl-cache-candidates ()))
(defun nodejs-repl--remove-duplicated-prompt (string)
;; `.load` command of Node.js repl outputs a duplicated prompt
(let ((beg (or comint-last-output-start
(point-min-marker)))
(end (process-mark (get-buffer-process (current-buffer)))))
(save-excursion
(goto-char beg)
(when (re-search-forward (concat nodejs-repl-prompt nodejs-repl-prompt) end t)
(replace-match nodejs-repl-prompt)))))
;;;--------------------------
;;; Public functions
;;;--------------------------
(defun nodejs-repl-quit-or-cancel ()
"Send ^C to Node.js process."
(interactive)
(process-send-string (get-process nodejs-repl-process-name) "\x03"))
(defun nodejs-repl-clear-line ()
"Send ^U to Node.js process."
(nodejs-repl--send-string "\x15"))
;;;###autoload
(defun nodejs-repl-send-region (start end)
"Send the current region to the `nodejs-repl-process'"
(interactive "r")
(let ((proc (nodejs-repl--get-or-create-process)))
(comint-send-region proc start end)
(comint-send-string proc "\n")))
;;;###autoload
(defun nodejs-repl-send-buffer ()
"Send the current buffer to the `nodejs-repl-process'"
(interactive)
(nodejs-repl-send-region (point-min) (point-max)))
;;;###autoload
(defun nodejs-repl-load-file (file)
"Load the file to the `nodejs-repl-process'"
(interactive (list (read-file-name "Load file: " nil nil 'lambda)))
(let ((proc (nodejs-repl--get-or-create-process)))
(comint-send-string proc (format ".load %s\n" file))))
;;;###autoload
(defun nodejs-repl-send-last-sexp ()
"Send the expression before point to the `nodejs-repl-process'"
(interactive)
(nodejs-repl-send-region (save-excursion (backward-sexp)
(point))
(point)))
;;;###autoload
(defun nodejs-repl-switch-to-repl ()
"If there is a `nodejs-repl-process' running switch to it,
otherwise spawn one."
(interactive)
(pop-to-buffer
(process-buffer (nodejs-repl--get-or-create-process))))
(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 completion 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))
(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--remove-duplicated-prompt 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))
;;;###autoload
(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))
(setq nodejs-repl-nodejs-version
;; "v7.3.0" => "7.3.0", "v7.x-dev" => "7"
(replace-regexp-in-string nodejs-repl--nodejs-version-re "\\1" (shell-command-to-string "node --version")))
(let* ((repl-mode (or (getenv "NODE_REPL_MODE") "magic"))
(nodejs-repl-code (format nodejs-repl-code-format
(window-width) nodejs-repl-prompt repl-mode )))
(pop-to-buffer
(apply 'make-comint nodejs-repl-process-name nodejs-repl-command nil
`(,@nodejs-repl-arguments "-e" ,nodejs-repl-code)))
(nodejs-repl-mode)))
(provide 'nodejs-repl)
;;; nodejs-repl.el ends here