March 6, 2009

Small addition to sh-mode

Below is piece of code, that could be useful for all, who write shell scripts in Emacs. It provides sh-check-syntax function, that performs syntax checking (currently only for sh/bash/zsh) and navigation between errors, if they found. It's better to bind this function to some key (i use C-c l)
(defun sh-check-finish-hook (buf msg)
"Function, that is executed at the end of sh check"
(when (not (string-match "finished" msg))
(next-error 1 t)))

(define-compilation-mode sh-check-mode "SH"
"Mode for check sh source code."
(set (make-local-variable 'compilation-disable-input) t)
(set (make-local-variable 'compilation-scroll-output) nil)
(set (make-local-variable 'compilation-finish-functions)
(list 'sh-check-finish-hook))
)

(defun sh-check-syntax ()
"Check syntax of current file"
(interactive)
(when (string-match "^\\(ba\\|z\\)sh" (symbol-name sh-shell))
(save-some-buffers t)
(compilation-start (concat (symbol-name sh-shell) " -n " (buffer-file-name))
'sh-check-mode))
)

1 comments:

123 123 said...
This comment has been removed by a blog administrator.