init-javascript.org 4.4 KB

This files contains my various javascript settings.

js2 mode

js syntax


  (use-package js2-mode

    :config
    ;; Disable js2 mode's syntax error highlighting by default...
    (setq-default js2-mode-show-parse-errors nil
                  js2-mode-show-strict-warnings nil)
    ;; ... but enable it if flycheck can't handle javascript
    (autoload 'flycheck-get-checker-for-buffer "flycheck")

    (defun sanityinc/disable-js2-checks-if-flycheck-active ()
      (unless (flycheck-get-checker-for-buffer)
        (set (make-local-variable 'js2-mode-show-parse-errors) t)
        (set (make-local-variable 'js2-mode-show-strict-warnings) t)))
    (add-hook 'js2-mode-hook 'sanityinc/disable-js2-checks-if-flycheck-active)

    (add-hook 'js2-mode-hook (lambda () (setq mode-name "JS2")))

    ;; (defvar preferred-javascript-indent-level 2)

    (setq-default
     js2-basic-offset 2
     js2-bounce-indent-p nil)
    (js2-imenu-extras-setup)

    :mode
    ("\\.js?\\'" . js2-mode))


js2-mode-hook


  (defun my-js-minify-function ()
    "Minifying my js files."
    (interactive)
    (when (eq major-mode 'js2-mode)
      (async-shell-command (concat (format "closure --js  %s --js_output_file "
                                           (buffer-file-name))
                                   (s-replace ".js" ".min.js" buffer-file-name)) "*js minifying*")))

  (add-hook 'js2-mode-hook '(lambda ()
                              ;; I have abbrev turned on for all prog and text modes
                              ;; (abbrev-mode 1)
                              ;; (skewer-mode)
                              (ggtags-mode 1)
                              ;;(push '("function" . ?𝆑) prettify-symbols-alist)
                              (push '(">=" . ?≥) prettify-symbols-alist)
                              (push '("<=" . ?≤) prettify-symbols-alist)
                              (add-hook 'after-save-hook 'my-js-minify-function nil t)))

  (add-to-list 'display-buffer-alist (cons "\\\*js minifying\\\*" (cons #'display-buffer-no-window nil)))

COMMENT prefeered javascript

Need to first remove from list if present, since elpa adds entries too, which may be in an arbitrary order


(eval-when-compile (require 'cl))

(setq auto-mode-alist (cons `("\\.js\\(\\.erb\\)?\\'" . ,preferred-javascript-mode)
                            (loop for entry in auto-mode-alist
                                  unless (eq preferred-javascript-mode (cdr entry))
                                  collect entry)))

inferior js



(setq inferior-js-program-command "js")

(defvar inferior-js-minor-mode-map (make-sparse-keymap))
(define-key inferior-js-minor-mode-map "\C-x\C-e" 'js-send-last-sexp)
(define-key inferior-js-minor-mode-map "\C-\M-x" 'js-send-last-sexp-and-go)
(define-key inferior-js-minor-mode-map "\C-cb" 'js-send-buffer)
(define-key inferior-js-minor-mode-map "\C-c\C-b" 'js-send-buffer-and-go)
(define-key inferior-js-minor-mode-map "\C-cl" 'js-load-file-and-go)

(define-minor-mode inferior-js-keys-mode
  "Bindings for communicating with an inferior js interpreter."
  nil " InfJS" inferior-js-minor-mode-map)

(dolist (hook '(js2-mode-hook js-mode-hook))
  (add-hook hook 'inferior-js-keys-mode))

COMMENT Things I don't use

COMMENT Coffeescript


(use-package coffee-mode
  :defer t
  :config (setq coffee-js-mode preferred-javascript-mode
                coffee-tab-width preferred-javascript-indent-level))

(when (fboundp 'coffee-mode)
  (add-to-list 'auto-mode-alist '("\\.coffee\\.erb\\'" . coffee-mode)))

COMMENT js-mode



(setq-default js-indent-level preferred-javascript-indent-level)

(add-to-list 'interpreter-mode-alist (cons "node" preferred-javascript-mode))

Javascript nests {} and () a lot, so I find this helpful

provide this file


(provide 'init-javascript)