public
Last active

beautify some js code in emacs

  • Download Gist
js-beautify.el
Emacs Lisp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
;;; js-beautify.el -- beautify some js code
 
;;TODO(ssm): don't hard code all this java crap in here
(defun js-beautify ()
"Beautify a region of javascript using the code from jsbeautify.org"
(interactive)
(let ((orig-point (point)))
(unless (mark)
(mark-defun))
(shell-command-on-region (point)
(mark)
"java -jar ~/lib/java/js.jar ~/projects/js-beautify/beautify-cl.js -i 4 -p -d ~/projects/js-beautify" nil t)
(goto-char orig-point)))
 
(provide 'js-beautify)
;;; js-beautify.el ends here

Thanks for putting this out there. I added to this a bit (checked in also):

;;; js-beautify.el -- beautify some js code
 (defcustom rhino-jar "~/software/rhino1_7R"
       "Location of the rhino jar e.g. rhino1_7R2"
       :type '(string)
       :group 'data)
 (defcustom beautify-dir "~/software/js-beautify"
       "Location of the js-beautify directory e.g. ~/software/js-beautify"
       :type '(string)
       :group 'data)
 (defun js-beautify-region ()
  "Beautify a region of javascript using the code from jsbeautify.org. If you don't have
   a region selected, the code will go from the cursor to the end of the line."
  (interactive)
  (let ((orig-point (point)))
    (setq endm 0)
    (if mark-active
        (setq endm (mark))
      (setq endm (point-at-eol))
    )
    (setq command (concatenate 'string "java -jar " rhino-jar "/js.jar " beautify-dir  "/beautify-cl.js -i 4 -p -d " beautify-dir ))
    (shell-command-on-region (point)
                             endm
                             command  nil t)
    (goto-char orig-point)))
 (defun js-beautify-buffer ()
  "Beautify the buffer using the code from jsbeautify.org"
  (interactive)
  (let ((orig-point (point)))
    (setq command (concatenate 'string "java -jar " rhino-jar "/js.jar " beautify-dir  "/beautify-cl.js -i 4 -p -d " beautify-dir ))
    (shell-command-on-region (point-min) (point-max) command  nil t)
    (goto-char orig-point)))

 (provide 'js-beautify)

Thanks! I just updated to use the python version.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.