2013-10-07 20:01:45 +02:00
|
|
|
;;==============================================================================
|
|
|
|
;; Shell
|
|
|
|
;;==============================================================================
|
|
|
|
|
|
|
|
;; Allow comment indentation.
|
|
|
|
(setq sh-indent-comment t)
|
|
|
|
|
2014-02-26 18:04:00 +01:00
|
|
|
(add-hook-and-eval
|
2013-10-07 20:01:45 +02:00
|
|
|
'sh-mode-hook
|
|
|
|
(lambda ()
|
2014-03-09 15:03:38 +01:00
|
|
|
(set (make-local-variable 'defun-prompt-regexp)
|
|
|
|
(concat "^\\(function[ \t]\\|[[:alnum:]_]+[ \t]+()[ \t]+\\)"))
|
|
|
|
(set (make-local-variable 'compile-command) (concat (symbol-name sh-shell) " " buffer-file-name))) )
|
2014-02-26 17:22:42 +01:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2014-02-12 17:48:29 +01:00
|
|
|
|
2014-02-13 18:08:25 +01:00
|
|
|
(define-skeleton sh-check
|
|
|
|
"Insert a function checking for presence in PATH."
|
|
|
|
nil
|
|
|
|
"check()
|
|
|
|
{
|
|
|
|
for i ; do
|
|
|
|
if ! command -v $i >/dev/null 2>&1; then
|
|
|
|
echo \"'$i' not found in PATH. Exiting.\" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
")
|
|
|
|
|
2014-03-10 00:20:13 +01:00
|
|
|
(define-skeleton sh-command
|
|
|
|
"Insert a line that executes if command is found in path."
|
|
|
|
"Command name: "
|
|
|
|
> "command -v " @ str " >/dev/null 2>&1 && " @ _)
|
|
|
|
|
|
|
|
(define-skeleton sh-command-or-die
|
|
|
|
"Insert a line that quits if command is not found in path."
|
|
|
|
"Command name: "
|
|
|
|
> "if ! command -v " @ str " >/dev/null 2>&1; then" \n
|
|
|
|
> "echo '" str " not found in PATH. Exiting.' >&2" \n
|
|
|
|
> "exit 1" \n
|
|
|
|
"fi" > \n)
|
|
|
|
|
|
|
|
(define-skeleton sh-for
|
|
|
|
"Insert a for loop. See `sh-feature'. This overrides vanilla function."
|
|
|
|
"Index variable: "
|
|
|
|
> "for " str | "i"
|
|
|
|
'(setq v1 (skeleton-read "Index values: " ""))
|
|
|
|
(unless (string= v1 "")
|
|
|
|
(concat " in " v1))
|
|
|
|
"; do" \n
|
|
|
|
> _ \n
|
|
|
|
"done" > \n)
|
|
|
|
|
|
|
|
(define-skeleton sh-ifcommand
|
|
|
|
"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)
|
|
|
|
|
|
|
|
(define-skeleton sh-redirect-to-null
|
|
|
|
"Insert a null redirection."
|
|
|
|
nil
|
|
|
|
">/dev/null 2>&1")
|
|
|
|
|
2014-02-13 18:55:06 +01:00
|
|
|
(define-skeleton 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-03-10 00:20:13 +01:00
|
|
|
> "usage ()
|
2014-02-13 18:08:25 +01:00
|
|
|
{
|
|
|
|
cat<<EOF
|
|
|
|
Usage: ${1##*/} [OPTIONS] FILES
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
-h: Show this help.
|
|
|
|
|
|
|
|
EOF
|
2014-02-13 18:55:06 +01:00
|
|
|
}" \n
|
|
|
|
\n
|
|
|
|
> "while getopts :" str " OPT; do" \n
|
|
|
|
> "case $OPT in" \n
|
|
|
|
'(setq v1 (append (vconcat str) nil))
|
|
|
|
( (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)
|
|
|
|
> "?)" \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
|
2014-02-23 14:00:16 +01:00
|
|
|
"done" > \n \n
|
2014-02-13 18:55:06 +01:00
|
|
|
"shift $(($OPTIND - 1))" \n
|
|
|
|
"if [ $# -eq 0 ]; then
|
2014-03-10 00:20:13 +01:00
|
|
|
usage \"$0\"
|
2014-02-13 18:08:25 +01:00
|
|
|
exit 1
|
2014-02-13 18:55:06 +01:00
|
|
|
fi" \n)
|
2014-02-13 18:08:25 +01:00
|
|
|
|
2014-02-13 18:40:02 +01:00
|
|
|
(define-skeleton sh-while-read
|
|
|
|
"Insert a while read loop."
|
|
|
|
nil
|
|
|
|
> "while IFS= read -r i; do" \n
|
|
|
|
> @ _ \n
|
|
|
|
> "done <<EOF" \n
|
|
|
|
> "EOF" \n)
|
2014-02-13 18:08:25 +01:00
|
|
|
|
2014-02-23 14:00:16 +01:00
|
|
|
(provide 'mode-sh)
|