-
Notifications
You must be signed in to change notification settings - Fork 2
/
IndentAndTabify
executable file
·155 lines (127 loc) · 3.35 KB
/
IndentAndTabify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/sh
# copy source-file and Indent using Emacs
ERROR() {
printf 'error: '
printf "${@}"
printf '\n'
exit 1
}
VERBOSE() {
[ X"${verbose}" = X"no" ] && return 0
printf "${@}"
printf '\n'
}
USAGE() {
if [ -n "${1}" ]
then
printf 'error: '
printf "${@}"
printf '\n'
fi
cat <<EOF
usage: ${0##*/} <options...> <arguments....>
--help -h this message
--verbose -v more messages
--copy src dst -c src dst copy indented and tabified source to destination
--in-place files... -i files... in place tabify and indent of each file
--overwrite -o for --copy to overwrite destination
--no-indent -n only remove extraneous whitespace, no re-indentation
--debug -D show debug information
EOF
exit 1
}
EmacsIndentAndTabity() {
local verbose overwrite source destination out
verbose="${1}"; shift
overwrite="${1}"; shift
source=$(realpath "${1}"); shift
destination=$(realpath "${1}"); shift
out='cat > /dev/null'
[ -f "${source}" ] || USAGE 'source file: "%s" does not exist' "${source}"
[ -d "${destination}" ] && USAGE 'destination: "%s" cannot be a directory' "${destination}"
[ -f "${destination}" ] && [ X"${overwrite}" != X"yes" ] && USAGE 'file: "%s" already exists' "${destination}"
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 the-source-file "'"${source}"'")
(setq the-output-file "'"${destination}"'")
(setq init-dir (expand-file-name (concat "~" init-file-user "/.emacs.d")))
(load (concat init-dir "/init.el"))
(find-file the-source-file)
(set-buffer-file-coding-system '"'"'unix)
(write-file the-output-file nil)
)' 2>&1 < /dev/null | eval ${out}
}
# main program
verbose=no
debug=no
overwrite=no
InPlace=no
copy=no
# parse options
while getopts :hvcioD-: option
do
# convert long options
if [ X"${option}" = X"-" ]
then
option="${OPTARG%%=*}"
OPTARG="${OPTARG#${option}}"
OPTARG="${OPTARG#=}"
fi
case "${option}" in
(v|verbose)
verbose=yes
;;
(i|in-place)
InPlace=yes
overwrite=yes
;;
(c|copy)
copy=yes
;;
(o|overwrite)
overwrite=yes
;;
(--)
break
;;
(D|debug)
debug=yes
;;
(h|help)
USAGE
;;
('?')
USAGE 'invalid option: -%s' "${OPTARG}"
;;
(*)
USAGE 'invalid option: --%s' "${option}"
;;
esac
done
shift $((OPTIND - 1))
# verify arguments
#[ ${#} -ne 0 ] && USAGE 'extraneous arguments: %s' "${*}"
[ ${#} -eq 0 ] && USAGE 'missing arguments'
# enable debugging
[ X"${debug}" = X"yes" ] && set -x
[ X"${InPlace}" = X"${copy}" ] && USAGE 'must have only one option set'
# execute indent
if [ X"${copy}" = X"yes" ]
then
[ -z "${1}" ] && USAGE 'missing source file'
[ -z "${2}" ] && USAGE 'missing destination file'
EmacsIndentAndTabity "${verbose}" "${overwrite}" "${1}" "${2}"
elif [ X"${InPlace}" = X"yes" ]
then
for source in "${@}"
do
EmacsIndentAndTabity "${verbose}" "${overwrite}" "${source}" "${source}"
done
fi