JavaScript
Formatting
(defun format-json () (interactive) (save-excursion (shell-command-on-region (point-min) (point-max) "python -m json.tool" t ; output buffer (current) t ; replace region? )))
Node.js Set-up
For REPLs and such, because actually writing server-side code in Javascript is insane.
(setenv "NODE_PATH" "~/node_modules") (setq inferior-js-program-command "env NODE_NO_READLINE=1 node")
Mustache/handlebarl js support:
(add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode)) (add-to-list 'auto-mode-alist '("\\.hbs\\'" . web-mode))
Js-Mode
Tern
key | command |
---|---|
M-. | jump to def |
M-, | return from jump |
C-c C-r | Rename variable |
C-c C-c | Find type |
C-c C-d | Find documentation |
(add-to-list 'load-path (concat vendor-dir "tern/emacs")) (autoload 'tern-mode "tern.el" nil t) (require-package 'company-tern) (add-to-list 'company-backends 'company-tern)
Javascript editing modes
(require-package 'js-comint) (require-package 'js2-mode) (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode)) (add-hook 'js-mode-hook 'js2-minor-mode) (setq js2-highlight-level 3) (defvar toby/js-prettify-alist '()) (add-to-list 'toby/js-prettify-alist '("function" . ?𝒇)) (add-to-list 'toby/js-prettify-alist '("test" . ?∀)) (add-to-list 'toby/js-prettify-alist '("QUnit.test" . ?∀)) (add-to-list 'toby/js-prettify-alist '("assert" . ?∃)) (add-to-list 'toby/js-prettify-alist '("===" . ?≣)) (add-to-list 'toby/js-prettify-alist '("==" . ?≈)) (defun toby/js-init () (glasses-mode t) (tern-mode t) (flycheck-mode t) (smartparens-strict-mode) (prettify-symbols-mode) (setq mode-name "Js")) (eval-after-load "js-mode" '(progn (add-hook 'js-mode-hook 'toby/js-init) (setq js--prettify-symbols-alist (append toby/js-prettify-alist js--prettify-symbols-alist)) (require 'js-comint))) (eval-after-load "js2-mode" '(progn (add-hook 'js2-mode-hook 'toby/js-init) (require 'js-comint))) (defun jslint-thisfile () (interactive) (compile (format "jsl -process %s" (buffer-file-name)))) ;; Ember handlebar templates (add-to-list 'auto-mode-alist '("\\.hbs$" . web-mode))
Js-beautify
npm install -g js-beautify
(require-package 'web-beautify) (eval-after-load 'js2-mode '(define-key js2-mode-map (kbd "C-c b") 'web-beautify-js)) (eval-after-load 'js '(define-key js-mode-map (kbd "C-c b") 'web-beautify-js)) (eval-after-load 'json-mode '(define-key json-mode-map (kbd "C-c b") 'web-beautify-js))
(provide 'javascript-init)