Skip to content

Commit

Permalink
doc and bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen Bin committed Nov 30, 2017
1 parent eb47441 commit 83e932e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 46 deletions.
7 changes: 6 additions & 1 deletion README.org
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* js-comint.el (v1.1.0)
* js-comint.el (v1.1.1)
Run a JavaScript interpreter in an inferior process window

The first release, [[http://js-comint-el.sourceforge.net/][js-comint 0.0.1, is hosted on sourceforge]] but it has not been updated for five years.
Expand Down Expand Up @@ -36,6 +36,11 @@ Add js-comint to your Cask file:
(depends-on "js-comint")
#+END_SRC

** Windows
On window, you may need below setup:
#+begin_src elisp
(setq inferior-js-program-command "C:/Program Files/nodejs/node.exe")
#+end_src
* Usage
After installation, do `M-x run-js` to create a comint buffer with the JavaScript interpreter.

Expand Down
103 changes: 58 additions & 45 deletions js-comint.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; js-comint.el --- Run a JavaScript interpreter in an inferior process window.
;;; js-comint.el --- JavaScript interpreter in window.

;;; Copyright (C) 2008 Paul Huff
;;; Copyright (C) 2015 Stefano Mazzucco
Expand All @@ -7,23 +7,33 @@
;;; Author: Paul Huff <[email protected]>, Stefano Mazzucco <MY FIRST NAME - AT - CURSO - DOT - RE>
;;; Maintainer: Chen Bin <chenbin.sh AT gmail DOT com>
;;; Created: 15 Feb 2014
;;; Version: 1.1.0
;;; URL: https://github.com/js-comint/js-comint
;;; Package-Requires: ()
;;; Version: 1.1.1
;;; URL: https://github.com/redguardtoo/js-comint
;;; Package-Requires: ((emacs "24.3"))
;;; Keywords: javascript, node, inferior-mode, convenience

;; 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 file is NOT part of GNU Emacs.

;; 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.
;;; License:

;; 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, or
;; at your option any later version.

;; js-comint.el 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/>.
;; along with GNU Emacs; see the file COPYING, or type `C-h C-c'. If
;; not, write to the Free Software Foundation at this address:

;; Free Software Foundation
;; 51 Franklin Street, Fifth Floor
;; Boston, MA 02110-1301
;; USA

;;; Commentary:

Expand All @@ -39,14 +49,17 @@
;; Optionally, set the `js-comint-program-command' string
;; and the `js-comint-program-arguments' list to the executable that runs
;; the JS interpreter and the arguments to pass to it respectively.
;; Do: `M-x js-comint-repl'
;; For example, on windows you might need below setup:
;; (setq inferior-js-program-command "C:/Program Files/nodejs/node.exe")
;;
;; After setup, do: `M-x js-comint-repl'
;; Away you go.
;; `node_modules' is *automatically* searched and appended into environment
;; variable `NODE_PATH'. So 3rd party javascript is usable out of box.
;; variable `NODE_PATH'. So 3rd party javascript is usable out of box.

;; If you have nvm, you can select the versions of node.js installed and run
;; them. This is done thanks to nvm.el.
;; Please note nvm.el is optional. So you need *manually* install it.
;; Please note nvm.el is optional. So you need *manually* install it.
;; To enable nvm support, run `js-do-use-nvm'.
;; The first time you start the JS interpreter with `js-comint-repl', you will
;;be asked to select a version of Node.js
Expand All @@ -65,8 +78,7 @@
;; (add-hook 'js2-mode-hook
;; (lambda ()
;; (local-set-key (kbd "C-x C-e") 'js-send-last-sexp)
;; (local-set-key (kbd "C-c b") 'js-send-buffer)
;; ))
;; (local-set-key (kbd "C-c b") 'js-send-buffer)))

;;; Code:

Expand All @@ -83,8 +95,7 @@
:group 'js-comint)

(defvar js-comint-module-paths '()
"List of modules paths which could be used by NodeJS
to search modules.")
"List of modules paths which could be used by NodeJS to search modules.")

(defvar js-comint-drop-regexp
"\\(\x1b\\[[0-9]+[GJK]\\|^[ \t]*undefined[\r\n]+\\)"
Expand Down Expand Up @@ -160,6 +171,7 @@ Return a string representing the node version."
"/node"))))

(defun js-comint-guess-load-file-cmd (filename)
"Create Node file loading command for FILENAME."
(concat "require(\"" filename "\")\n"))

(defun js-comint-quit-or-cancel ()
Expand All @@ -168,16 +180,19 @@ Return a string representing the node version."
(process-send-string (js-comint-get-process) "\x03"))

(defun js-comint--path-sep ()
"Separator of file path."
(if (eq system-type 'windows-nt) ";" ":"))

(defun js-comint--suggest-module-path ()
"Find node_modules."
(let* ((dir (locate-dominating-file default-directory
"node_modules")))
(if dir (concat (file-name-as-directory dir)
"node_modules")
default-directory)))

(defun js-comint-get-process ()
"Get repl process."
(and js-comint-buffer
(get-process js-comint-buffer)))

Expand Down Expand Up @@ -208,7 +223,7 @@ Return a string representing the node version."

;;;###autoload
(defun js-comint-save-setup ()
"Save current setup to `.dir-locals.el'"
"Save current setup to \".dir-locals.el\"."
(interactive)
(let* (sexp
(root (read-directory-name "Where to create .dir-locals.el: "
Expand All @@ -226,6 +241,7 @@ Return a string representing the node version."
(message "Nothing to save. `js-comint-module-paths' is empty.")))))

(defun js-comint-setup-module-paths ()
"Setup node_modules path."
(let* ((paths (mapconcat 'identity
js-comint-module-paths
(js-comint--path-sep)))
Expand All @@ -242,18 +258,19 @@ Return a string representing the node version."

;;;###autoload
(defun js-comint-reset-repl ()
"Kill existing REPL process if possible. Create a new
Javascript REPL process. The environment variable `NODE_PATH'
is setup by `js-comint-module-paths' before the process starts."
"Kill existing REPL process if possible.
Create a new Javascript REPL process.
The environment variable `NODE_PATH' is setup by `js-comint-module-paths'
before the process starts."
(interactive)
(when (js-comint-get-process)
(process-send-string (js-comint-get-process) ".exit\n")
;; wait the process to be killed
(sit-for 1))
(js-comint-start-or-switch-to-repl))

(defun js-cominit-filter-output (string)
"Filter extra escape sequences from output."
(defun js-comint-filter-output (string)
"Filter extra escape sequences from STRING."
(let ((beg (or comint-last-output-start
(point-min-marker)))
(end (process-mark (get-buffer-process (current-buffer)))))
Expand All @@ -264,9 +281,11 @@ is setup by `js-comint-module-paths' before the process starts."
(replace-match "")))))

(defun js-comint-get-buffer-name ()
"Get repl buffer name."
(format "*%s*" js-comint-buffer))

(defun js-comint-get-buffer ()
"Get rpl buffer."
(and js-comint-buffer
(get-buffer (js-comint-get-buffer-name))))

Expand All @@ -288,7 +307,10 @@ is setup by `js-comint-module-paths' before the process starts."
(defalias 'js-clear 'js-comint-clear)


;;;###autoload
(defun js-comint-start-or-switch-to-repl ()
"Start a new repl or switch to existing repl."
(interactive)
(setenv "NODE_NO_READLINE" "1")
(js-comint-setup-module-paths)
(let* ((repl-mode (or (getenv "NODE_REPL_MODE") "magic"))
Expand All @@ -301,9 +323,8 @@ is setup by `js-comint-module-paths' before the process starts."

;;;###autoload
(defun js-comint-repl (cmd)
"Run an Javascript process. The environment variable `NODE_PATH'
is setup by `js-comint-module-paths' before the process
starts."
"Start a Javascript process by running CMD.
The environment variable \"NODE_PATH\" is setup by `js-comint-module-paths'."
(interactive
(list
;; You can select node version here
Expand All @@ -323,8 +344,8 @@ starts."
(js-comint-start-or-switch-to-repl))
(defalias 'run-js 'js-comint-repl)

;;;###autoload
(defun js-comint-send-string (str)
"Send STR to repl."
(comint-send-string (js-comint-get-process)
(concat str "\n")))

Expand All @@ -344,7 +365,7 @@ If no region selected, you could manually input javascript expression."

;;;###autoload
(defun js-comint-send-last-sexp ()
"Send the previous sexp to the inferior Javascript process. `evil-mode' friendly."
"Send the previous sexp to the inferior Javascript process."
(interactive)
(let* ((b (save-excursion
(backward-sexp)
Expand Down Expand Up @@ -373,25 +394,17 @@ If no region selected, you could manually input javascript expression."
(defalias 'js-send-buffer 'js-comint-send-buffer)

;;;###autoload
(defun js-comint-load-file (filename)
"Load a file in the javascript interpreter."
(defun js-comint-load-file (file)
"Load FILE into the javascript interpreter."
(interactive "f")
(let ((filename (expand-file-name filename)))
(let ((file (expand-file-name file)))
(js-comint-repl js-comint-program-command)
(comint-send-string (js-comint-get-process) (js-comint-guess-load-file-cmd filename))))
(comint-send-string (js-comint-get-process) (js-comint-guess-load-file-cmd file))))

;;;###autoload
(defalias 'js-load-file 'js-comint-load-file)

;;;###autoload
(defun js-comint-switch-to-repl (eob-p)
"Switch to the javascript process buffer.
With argument, position cursor at end of buffer."
(interactive "P")
(if (js-comint-get-buffer)
(pop-to-buffer (js-comint-get-buffer))
(error "No current process buffer. See variable `js-comint-buffer'")))
(defalias 'switch-to-js 'js-comint-switch-repl)
(defalias 'switch-to-js 'js-comint-start-or-switch-to-repl)

(defvar js-comint-mode-map
(let ((map (make-sparse-keymap)))
Expand All @@ -408,7 +421,7 @@ With argument, position cursor at end of buffer."
(setq comint-process-echoes t)
;; Ignore duplicates
(setq comint-input-ignoredups t)
(add-hook 'comint-output-filter-functions 'js-cominit-filter-output nil t)
(add-hook 'comint-output-filter-functions 'js-comint-filter-output nil t)
(use-local-map js-comint-mode-map)
(ansi-color-for-comint-mode-on))

Expand Down

0 comments on commit 83e932e

Please sign in to comment.