-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-whitespaces.sh
More file actions
executable file
·129 lines (71 loc) · 3.35 KB
/
Copy pathfix-whitespaces.sh
File metadata and controls
executable file
·129 lines (71 loc) · 3.35 KB
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
#!/bin/sh
# Copyright (C) 2014-2026 Olivier Boudeville
#
# Author: Olivier Boudeville [olivier (dot) boudeville (at) esperide (dot) com]
#
# This file is part of the Ceylan-Hull toolbox (see http://hull.esperide.org).
quiet_opt="--quiet"
usage="Usage: $(basename $0) [-h--help] [${quiet_opt}] A_FILE: fixes whitespace problems (general whitespace clean-up, and untabification) in the specified file.
Useful to properly whitespace-format files that shall be committed (even if not using Emacs as editor of choice)."
# Note that unfortunately Emacs still changes a lot and breaks backward
# compatibility. For example this script worked on older versions of Emacs and
# on 28.2, but for some reason does nothing on 27.1..., then works again with
# 30.2.
# Refer to https://howtos.esperide.org/GNULinux.html#using-emacs for a complete
# install procedure of Emacs.
# Refer to http://myriad.esperide.org/#emacs-settings for the prior
# configuration of Emacs.
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "${usage}"
exit
fi
verbose=0
if [ "$1" = "${quiet_opt}" ]; then
verbose=1
shift
fi
target_file="$1"
if [ ! $# -eq 1 ]; then
echo " Error, exactly one parameter needed.
${usage}" 1>&2
exit 5
fi
if [ ! -f "${target_file}" ]; then
echo " Error, file '${target_file}' not found." 1>&2
exit 10
fi
emacs="$(which emacs 2>/dev/null)"
if [ ! -x "${emacs}" ]; then
echo " Error, emacs not found." 1>&2
exit 15
fi
#echo "Emacs version : $("${emacs}" -version)"
# Nevertheless, using (fill-region (point-min) (point-max) instead of
# (whitespace-cleanup) works, this is still a mystery....
#echo "Fixing '${target_file}'..."
# Error output silenced to avoid for example "Ignoring unknown mode
# ‘erlang-mode’".
#${emacs} "${target_file}" --batch --eval="(whitespace-cleanup)" -f save-buffer #1>/dev/null 2>&1
# BASE_DIR to be defined:
#${emacs} "${target_file}" --batch --eval="(add-to-list 'load-path "BASE_DIR/")" --eval="(require 'whitespace)" --eval='(whitespace-cleanup)' --eval='(save-buffer 0)' #1>/dev/null 2>&1
#${emacs} "${target_file}" --batch --eval='(load "BASE_DIR/whitespace.el")' --eval='(whitespace-cleanup)' --eval='(save-buffer 0)' #1>/dev/null 2>&1
#${emacs} "${target_file}" --batch --eval='(load user-init-file)' --eval='(whitespace-cleanup)' --eval='(save-buffer 0)'
#emacs -batch "${target_file}" -eval '(whitespace-cleanup)' -f save-buffer
#emacs -batch "${target_file}" -l ~/.emacs.d/init.el -eval '(whitespace-cleanup)' -f save-buffer
#emacs -batch "${target_file}" --eval '(progn(package-initialize)(whitespace-cleanup))' -eval '(whitespace-cleanup)' -f save-buffer
#emacs -batch "${target_file}" -l ~/.emacs.d/minimal-for-whitespace-cleanup.el
# Brutal, requires to use our conventions, yet at least is functional:
init_el="${HOME}/.emacs.d/init.el"
if [ ! -f "${init_el}" ]; then
echo " Error, no init.el found (no '${init_el}')." 1>&2
exit 50
fi
# Unfortunately at a moment did not seem to work anymore (at least not in all
# contexts):
if ! ${emacs} "${target_file}" --batch --eval="(load-file \"${init_el}\")" --eval='(whitespace-cleanup)' --eval='(untabify-buffer)' --eval='(save-buffer 0)' 1>/dev/null 2>&1; then
echo " Error, processing of '${target_file}' failed." 1>&2
exit 20
fi
[ $verbose -eq 1 ] || echo " + file '${target_file}' cleaned up"
# Possibly created by emacs:
/bin/rm -f "${target_file}~" 2>/dev/null