redguardtoo / js-comint

js-comint will send the code from Emacs into node.js or rhino
GNU General Public License v3.0
72 stars 24 forks source link

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.

Insert below code to "~/.emacs.d/init.el",

+BEGIN_SRC elisp

(add-to-list 'load-path "~/mylisp/") (require 'js-comint)

+END_SRC

** Using [[http://www.emacswiki.org/emacs/ELPA][ELPA]] Insert below code to "~/.emacs.d/init.el",

+BEGIN_SRC elisp

(require 'package) (package-initialize) (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/")) (package-install 'js-comint) (require 'js-comint)

+END_SRC

** Using [[https://github.com/cask/cask][Cask]] Add js-comint to your Cask file:

+BEGIN_SRC elisp

(depends-on "js-comint")

+END_SRC

** Windows On window, you may need below setup:

+begin_src elisp

(setq js-comint-program-command "C:/Program Files/nodejs/node.exe")

+end_src

Please note the directory =node_modules= is automatically searched and appended into environment variable `NODE_PATH'. So you can use third party javascript without setup. For example, after =npm install --save moment=, run below command in js-comint shell,

+begin_src javascript

require('moment')().format('YYYY-MM-DD');

+end_src

You can =M-x js-clear= before =M-x js-send-buffer= to get clean output.

In order to get cleaner output when using NodeJS, I suggest add below setup into =.emacs=,

+begin_src elisp

(defun inferior-js-mode-hook-setup () (add-hook 'comint-output-filter-functions 'js-comint-process-output)) (add-hook 'inferior-js-mode-hook 'inferior-js-mode-hook-setup t)

+end_src

E.g., the default is:

+BEGIN_SRC elisp

;; You can also customize `js-comint-drop-regexp' to filter output (setq js-comint-program-command "node") (setq js-comint-program-arguments '("--interactive"))

+END_SRC

Note that in the example above, the version of node that is picked up will be the first found in `exec-path'.

But you could use Rhino or SpiderMonkey or whatever you want. E.g. to set up the Rhino JAR downloaded from https://github.com/mozilla/rhino, do

+BEGIN_SRC elisp

(setq js-comint-program-command "java") (setq js-comint-program-arguments '("-jar" "/absolute/path/to/rhino/js.jar"))

+END_SRC

If you have nvm, you can select the versions of node.js installed and run them. This is done thanks to =nvm.el=. =nvm.el= is optional. So you need manually install it.

To enable nvm support, run

+BEGIN_SRC elisp

(js-do-use-nvm)

+END_SRC

The first time you start the JS interpreter with run-js, you will be asked to select a version of node.js. If you want to change version of node js, run ~(js-select-node-version)~.

You can add the following couple of lines to your .emacs/init file to take advantage of key bindings for sending things to the JavaScript REPL:

+BEGIN_SRC elisp

; Remap Elisp's eval-last-sexp (C-x C-e) to eval JavaScript (define-key js-mode-map [remap eval-last-sexp] #'js-comint-send-last-sexp)) (define-key js-mode-map (kbd "C-c b") 'js-send-buffer))

+END_SRC