Skip to content

Instantly share code, notes, and snippets.

@uchida
Created January 18, 2014 18:42
Show Gist options
  • Select an option

  • Save uchida/8494461 to your computer and use it in GitHub Desktop.

Select an option

Save uchida/8494461 to your computer and use it in GitHub Desktop.
emacs lisp script to tidy shellscript
#!/usr/bin/env emacs --script
(defadvice message (before message-disable activate)
"disable message function to trash stderr message"
(ad-set-args 0 nil))
(defun insert-standard-input ()
"insert contents from standard input"
(condition-case nil
(let (line)
(while (setq line (read-from-minibuffer ""))
(insert line "\n")))
(error nil)))
(defun insert-files (files)
"insert contents from given files"
(dolist (file files)
(insert-file-contents-literally file)
(goto-char (point-max))))
(defun tidy-by-alist (alist)
"tidy by rules in given alist"
(dolist (as alist)
(let ((key (car as)) (value (cdr as)))
(goto-char (point-min))
(while (re-search-forward key nil t)
(replace-match value)))))
(setq shell-tidyrule-alist
'(("\\([^;]\\);\\([^;]\\)" . "\\1\n\\2")
(" +$" . "")
("^ *function *" . "")
("\\(\\w+()\\){" . "\\1 {")
("\n *{" . " {")
("\n *then" . "; then")
("\n *do[^n][^e]" . "; done")
(" *\\(|+\\) *" . " \\1 ")
(" *\\(&+\\) *" . " \\1 ")
("\\S-\\\\$" . " \\\\")))
;;; main
(with-temp-buffer
(cond (argv (insert-files argv))
(t (insert-standard-input)))
(setq default-major-mode 'sh-mode)
(set-buffer-major-mode (current-buffer))
(untabify (point-min) (point-max))
(tidy-by-alist shell-tidyrule-alist)
(indent-region (point-min) (point-max))
(princ (buffer-string)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment