Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 163 lines (126 sloc) 3.5 kB
#!/bin/sh
# copy source-file and Indent using Emacs
ERROR()
{
echo error: $*
exit 1
}
USAGE()
{
[ -z "$1" ] || echo error: $*
echo usage: $(basename "$0") '<options...>'
echo ' --help -h This message'
echo ' --verbose -v More messages'
echo ' --copy source-file destination-file -c src dst Copy indented and tabified source to destination'
echo ' --in-place source-files... -i files... In place tabify and indent of each file'
echo ' --overwrite -o For --copy to overwrite destination'
echo ' --no-indent -n Only remove extraneous whitespace, no re-indentation'
exit 1
}
EmacsIndentAndTabity()
{
local verbose overwrite indent source destination out
verbose="$1"; shift
overwrite="$1"; shift
indent="$1"; shift
source=$(realpath "$1"); shift
destination=$(realpath "$1"); shift
out='cat > /dev/null'
[ -f "${source}" ] || USAGE source file: ${source} does not exist
[ -d "${destination}" ] && USAGE destination: ${destination} cannot be a directory
[ -f "${destination}" -a X"${overwrite}" != X"yes" ] && USAGE file: ${destination} already exists
if [ X"${verbose}" = X"yes" ]
then
echo "indent/tabify ${source} -> ${destination}"
out='cat'
fi
# use emacs to indent and tabify file
# and change DOS style EOL (CRLF) to Unix style (LF)
emacs --batch --execute='
(progn
(setq c-default-style "bsd")
(setq backup-inhibited t)
(setq auto-save-default nil)
(setq indent-tabs-mode nil)
(setq tabify-regexp "^\t* [ \t]+")
(find-file "'"${source}"'")
(set-buffer-file-coding-system '"'"'unix)
(goto-line 1)
(while (search-forward "\r" nil t)
(replace-match "" nil t))
(goto-line 1)
(delete-trailing-whitespace)
(untabify (point-min) (point-max))
(when (string-equal "yes" "'"${indent}"'")
(indent-region (point-min) (point-max)))
(write-file "'"${destination}"'" nil)
(kill-emacs 0))
' 2>&1 | eval ${out}
}
# main program
# ------------
verbose=no
overwrite=no
InPlace=no
copy=no
indent=yes
debug=no
getopt=/usr/local/bin/getopt
[ -x "${getopt}" ] || getopt=getopt
args=$(${getopt} -o hvoicnd --long=help,verbose,overwrite,in-place,copy,no-indent,debug -- "$@") || exit 1
# replace the arguments with the parsed values
eval set -- "${args}"
while :
do
case "$1" in
-v|--verbose)
verbose=yes
shift
;;
-o|--overwrite)
overwrite=yes
shift
;;
-i|--in-place)
InPlace=yes
overwrite=yes
shift
;;
-c|--copy)
copy=yes
shift
;;
-n|--no-indent)
indent=no
shift
;;
-d|--debug)
debug=yes
shift
;;
--)
shift
break
;;
-h|--help)
USAGE
;;
*)
USAGE invalid option: $1
;;
esac
done
[ X"${debug}" = X"yes" ] && set -x
[ X"${InPlace}" = X"${copy}" ] && USAGE must have only one option set
if [ X"${copy}" = X"yes" ]
then
[ -z "$1" ] && USAGE missing source file
[ -z "$2" ] && USAGE missing destination file
EmacsIndentAndTabity "${verbose}" "${overwrite}" "${indent}" "$1" "$2"
elif [ X"${InPlace}" = X"yes" ]
then
for source in "$@"
do
EmacsIndentAndTabity "${verbose}" "${overwrite}" "${indent}" "${source}" "${source}"
done
fi
Jump to Line
Something went wrong with that request. Please try again.