public
Last active

Emacs Func: Use ~/bin/jslint4java-1.4.4.jar to automatically JSLint the Javascript file

  • Download Gist
Emacs-JSLint
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
;;; C-x c calls jslint and outputs to the *compilation* buffer.
(setq jslint-wrapper (concat "java -jar " (substitute-in-file-name "$HOME") "/bin/jslint4java-1.4.4.jar "))
(require 'compile)
(add-hook 'javascript-mode-hook
(lambda ()
(set (make-local-variable 'compilation-read-command) nil)
(set (make-local-variable 'compile-command)
(concat jslint-wrapper buffer-file-name))
(local-set-key (kbd "C-x c") 'compile)))
 
;;;auto check on save of a javascript file
(defun jslint-hook ()
(let* ((filename (buffer-file-name))
(suffix (downcase (file-name-extension filename))))
(if (and filename (string= suffix "js"))
(call-interactively 'compile))))
(add-hook 'after-save-hook 'jslint-hook)
 
(defun notify-compilation-result(buffer msg)
"Notify that the compilation is finished,
close the *compilation* buffer if the compilation is successful,
and set the focus back to Emacs frame"
(if (string-match "^finished" msg)
(progn
(delete-windows-on buffer)
(tooltip-show "\n Compilation Successful :-) \n "))
(tooltip-show "\n Compilation Failed :-( \n "))
(setq current-frame (car (car (cdr (current-frame-configuration)))))
(select-frame-set-input-focus current-frame))
 
(add-to-list 'compilation-finish-functions 'notify-compilation-result)

Could you invoke it via M-x compile instead? I designed the output to be roughly compiler like so you could jump to the errors easily.

Hey, happygiraffe.

I can't quit understand you. What's your requirement?

Sorry, I was wondering if, instead of invoking this function into a separate buffer, you could use the standard M-x compile functionality. Then you automatically get output in a *compilation* buffer as well as navigation via C-x backquote You have to set the compile-command variable, something like this.

(add-hook 'javascript-mode
  (lambda ()
    (set (make-local-variable 'compile-command)
     (concat "java -jar "
         jslint4java
         " "
         buffer-file-name))))

(the above also needs:)

(setq jslint4java "/opt/misc/jslint4java-1.4.4/jslint4java-1.4.4.jar")

Thanks happygiraffe. I rewrote it based on your comment, :)

Also check out https://github.com/purcell/flymake-jslint, which uses flymake to show jslint hints directly in the javascript buffer.

-Steve

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.