2017-05-28 12:32:48 +02:00
|
|
|
;;; Sh
|
2013-10-07 20:01:45 +02:00
|
|
|
|
2017-06-23 15:44:16 +02:00
|
|
|
(defvaralias 'sh-basic-offset 'tab-width)
|
2014-10-30 18:18:08 +01:00
|
|
|
(defvaralias 'sh-indentation 'sh-basic-offset)
|
|
|
|
|
2013-10-07 20:01:45 +02:00
|
|
|
(setq sh-indent-comment t)
|
2014-10-31 17:12:39 +01:00
|
|
|
|
2017-06-17 13:17:50 +02:00
|
|
|
(setq-default sh-shell-file "/bin/sh")
|
2014-03-10 12:28:25 +01:00
|
|
|
;; (setq-default sh-shell 'sh)
|
|
|
|
|
2017-06-16 21:01:02 +02:00
|
|
|
;;; Faces
|
|
|
|
(set-face-foreground 'sh-heredoc "#00bfff")
|
|
|
|
(set-face-bold 'sh-heredoc nil)
|
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(defun ambrevar/sh-set-compiler ()
|
2014-03-10 12:28:25 +01:00
|
|
|
"Set shell interpreter.
|
|
|
|
Set `sh-shell', `sh-shell-file' and `compile-command' according to the following rules:
|
|
|
|
- Look at shabang.
|
|
|
|
- If file has no name, use default value of sh-shell-file.
|
|
|
|
- Check extension or file name.
|
|
|
|
- If none of the above yields a result, use default value of
|
|
|
|
sh-shell-file.
|
|
|
|
The advantages of this function over the vanilla code are:
|
|
|
|
- You can change default value of sh-shell-file in sh-mode-hook
|
|
|
|
and it will be used subsequently.
|
|
|
|
- Zsh is supported
|
|
|
|
- compile-command is set.
|
|
|
|
- Once sh-shell is set, sh-shell-file is changed accordingly. In
|
|
|
|
default Emacs, sh-shell-file is always the same."
|
|
|
|
(interactive)
|
|
|
|
(sh-set-shell
|
|
|
|
(cond ((save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(looking-at "#![ \t]?\\([^ \t\n]*/bin/env[ \t]\\)?\\([^ \t\n]+\\)"))
|
|
|
|
(match-string 2))
|
|
|
|
((not buffer-file-name) sh-shell-file)
|
|
|
|
;; Checks that use `buffer-file-name' follow.
|
|
|
|
((string-match "\\.m?spec\\'" buffer-file-name) "rpm")
|
|
|
|
((string-match "[.]bash\\>" buffer-file-name) "bash")
|
|
|
|
((string-match "[.]csh\\>" buffer-file-name) "csh")
|
|
|
|
((string-match "[.]ksh\\>" buffer-file-name) "ksh")
|
|
|
|
((string-match "[.]sh\\>" buffer-file-name) "sh")
|
|
|
|
((string-match "[.]zsh\\>" buffer-file-name) "zsh")
|
|
|
|
((equal (file-name-nondirectory buffer-file-name) ".profile") "sh")
|
|
|
|
(t sh-shell-file))
|
|
|
|
nil nil)
|
2017-05-28 12:32:48 +02:00
|
|
|
;; Universal version:
|
|
|
|
;; (setq sh-shell-file (executable-find (symbol-name sh-shell)))
|
|
|
|
;; Convenient version:
|
2014-03-10 12:28:25 +01:00
|
|
|
(setq sh-shell-file (concat "/bin/" (symbol-name sh-shell)))
|
2017-05-28 12:03:28 +02:00
|
|
|
;; Sometimes with `git merge` it seems that the `buffer-file-name' is not a
|
|
|
|
;; string. We safe-guard that case.
|
2015-09-10 09:57:58 +02:00
|
|
|
(when (stringp buffer-file-name)
|
2017-05-12 08:34:17 +02:00
|
|
|
(setq compile-command (concat sh-shell-file " " (shell-quote-argument buffer-file-name)))))
|
2013-10-07 20:01:45 +02:00
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(defun ambrevar/sh-set-indent-rules ()
|
2017-05-28 12:32:48 +02:00
|
|
|
(setq sh-indent-for-case-label 0
|
|
|
|
sh-indent-for-case-alt '+))
|
2017-05-28 12:03:28 +02:00
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(defun ambrevar/sh-set-prompt ()
|
2017-05-28 12:03:28 +02:00
|
|
|
(set (make-local-variable 'defun-prompt-regexp)
|
|
|
|
(concat "^\\(function[ \t]\\|[[:alnum:]_]+[ \t]+()[ \t]+\\)")))
|
|
|
|
|
2017-06-16 16:29:52 +02:00
|
|
|
;;; Hooks
|
2018-02-15 13:28:49 +01:00
|
|
|
(dolist (fun '(ambrevar/sh-set-indent-rules ambrevar/sh-set-prompt ambrevar/sh-set-compiler))
|
2017-06-16 16:29:52 +02:00
|
|
|
(add-hook 'sh-mode-hook fun))
|
2014-02-26 17:22:42 +01:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2014-02-12 17:48:29 +01:00
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(define-skeleton ambrevar/sh-commands-or-die
|
2016-10-12 09:13:45 +02:00
|
|
|
"Insert a loop that exits if any of the commands is not found in path."
|
|
|
|
"Command names: "
|
|
|
|
> "for i " @ str "; do" \n
|
|
|
|
> "if ! command -v \"$i\" >/dev/null 2>&1; then" \n
|
|
|
|
> "echo >&2 \"'$i' not found\"" \n
|
2014-03-10 00:20:13 +01:00
|
|
|
> "exit 1" \n
|
2016-10-12 09:13:45 +02:00
|
|
|
"fi" > \n
|
|
|
|
"done" > \n \n)
|
2014-03-10 00:20:13 +01:00
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(define-skeleton ambrevar/sh-ifcommand
|
2014-03-10 00:20:13 +01:00
|
|
|
"Insert a test to check if command is found in path."
|
|
|
|
"Command name: "
|
|
|
|
> "if command -v " @ str " >/dev/null 2>&1; then" \n
|
|
|
|
> @ _ \n
|
|
|
|
"fi" > \n)
|
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(define-skeleton ambrevar/sh-while-getopts
|
2014-02-13 18:08:25 +01:00
|
|
|
"Insert a getops prototype."
|
2014-02-13 18:55:06 +01:00
|
|
|
"optstring: "
|
2014-10-31 13:24:45 +01:00
|
|
|
> "usage() {" \n
|
2016-06-11 09:11:12 +02:00
|
|
|
> "cat<<EOF" \n
|
2014-10-31 13:24:45 +01:00
|
|
|
"Usage: ${1##*/} [OPTIONS] FILES
|
2014-02-13 18:08:25 +01:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-h: Show this help.
|
|
|
|
|
|
|
|
EOF
|
2016-06-11 09:11:12 +02:00
|
|
|
}" > \n
|
2014-02-13 18:55:06 +01:00
|
|
|
\n
|
|
|
|
> "while getopts :" str " OPT; do" \n
|
|
|
|
> "case $OPT in" \n
|
|
|
|
'(setq v1 (append (vconcat str) nil))
|
2016-06-11 09:11:12 +02:00
|
|
|
((prog1 (if v1 (char-to-string (car v1)))
|
|
|
|
(if (eq (nth 1 v1) ?:)
|
|
|
|
(setq v1 (nthcdr 2 v1)
|
|
|
|
v2 "\"$OPTARG\"")
|
|
|
|
(setq v1 (cdr v1)
|
|
|
|
v2 nil)))
|
|
|
|
> str ")" \n
|
|
|
|
> _ v2 " ;;" \n)
|
2014-10-31 18:07:20 +01:00
|
|
|
> "\\?)" \n
|
2014-03-10 00:20:13 +01:00
|
|
|
> "usage \"$0\"" \n
|
2014-02-13 18:55:06 +01:00
|
|
|
"exit 1 ;;" > \n
|
|
|
|
"esac" > \n
|
2016-06-11 09:11:12 +02:00
|
|
|
"done" > \n
|
|
|
|
\n
|
2014-02-13 18:55:06 +01:00
|
|
|
"shift $(($OPTIND - 1))" \n
|
2014-10-31 13:24:45 +01:00
|
|
|
"if [ $# -eq 0 ]; then" \n
|
2016-06-11 09:11:12 +02:00
|
|
|
> "usage \"$0\"" \n
|
2014-10-31 13:24:45 +01:00
|
|
|
"exit 1" \n
|
|
|
|
"fi" > \n)
|
2014-02-13 18:08:25 +01:00
|
|
|
|
2018-02-15 13:28:49 +01:00
|
|
|
(define-skeleton ambrevar/sh-while-read
|
2014-02-13 18:40:02 +01:00
|
|
|
"Insert a while read loop."
|
|
|
|
nil
|
|
|
|
> "while IFS= read -r i; do" \n
|
|
|
|
> @ _ \n
|
2016-06-11 09:11:12 +02:00
|
|
|
"done <<EOF" > \n
|
|
|
|
"EOF" > \n)
|
2014-02-13 18:08:25 +01:00
|
|
|
|
2017-06-18 18:50:31 +02:00
|
|
|
(provide 'init-sh)
|