blob: a3f59fe8ead40933f4f7834f961b1817e987353d (
plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#!/usr/bin/env bash
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# add-modelines, a simple script to add comments to
# the beginning and end of source files for LibreOffice devs
# Blame goes to Jesse Adelman (at least at first)
# someone AT boldandbusted dotty-dot com
# http://www.boldandbusted.com/
# (c) 2010 Bold and Busted LLC
# NOTE: At present, this script only works for files with C-like comments.
# NOTE: If you don't specify -p, the script will act on the current working directory.
# NOTE: If no arguments are specified, the definitions below are in effect.
# TO DO
# - Deuglify?
# - Make source file type agnostic modelines?
# - Too many/too few comments?
# - Handle top level source directories with whitespace names? (Do they exist?)
# Turn off globbing, helps with SourceFiles
set -f
# POSIX
set -o posix
# Change these to taste
FirstLine='/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */'
LastLine='/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */'
SourceFiles='*.cxx *.cpp *.hxx *.hpp *.c *.h *.m *.mm *.idl *.src *.hrc'
# Set defaults (don't change these)
ModelineReplace="false"
# Functions
function SetEnvironment()
{
if [ -n "$(which tail)" ] && [ -n "$(which head)" ]; then
{
headCMD=$(which head)
tailCMD=$(which tail)
}
else
{
echo "Missing head or tail, exiting..."
exit 1
}
fi
if [ -n "$(which find)" ]; then
findCMD=$(which find)
else
{
echo "Missing find, exiting..."
exit 1
}
fi
}
function EditFile()
{
local FileToEdit
local currentFirstLine
local currentLastLine
FileToEdit="$1"
currentFirstLine=$($headCMD -1 "$FileToEdit")
currentLastLine=$($tailCMD -1 "$FileToEdit")
case "$ModelineReplace" in
"true" )
if [ "${currentFirstLine:0:6}" = "${FirstLine:0:6}" ]; then
{
echo "$FirstLine" > "$FileToEdit".new
$tailCMD -n +2 "$FileToEdit" >> "$FileToEdit".new
}
fi
if [ -e "$FileToEdit.new" ]; then
{
echo "$LastLine" >> "$FileToEdit".new
}
fi
if [ "${currentLastLine:0:6}" = "${LastLine:0:6}" ]; then
{
$headCMD -n -1 "$FileToEdit" > "$FileToEdit".new
echo "$LastLine" >> "$FileToEdit".new
}
fi
mv "$FileToEdit".new "$FileToEdit"
echo "$FileToEdit updated" ;;
"false" )
if [ "${currentFirstLine:0:6}" != "${FirstLine:0:6}" ]; then
if [ "${currentLastLine:0:6}" != "${LastLine:0:6}" ]; then
{
echo "$FirstLine" > "$FileToEdit".new
cat "$FileToEdit" >> "$FileToEdit".new
if [ "x${currentLastLine}" != "x" ] ; then
echo "" >> "$FileToEdit".new
fi
echo "$LastLine" >> "$FileToEdit".new
mv "$FileToEdit".new "$FileToEdit"
echo "$FileToEdit updated"
}
fi
fi ;;
esac
}
function PrintUsage()
{
echo "Usage: $0 [-z] [-s \"<sourcefile glob>\"] [-p <path to source>]"
}
# Main
SetEnvironment
# Get command line options
while getopts "zs:p:" opt; do
case $opt in
z) ModelineReplace="true" ;;
s) SourceFiles="$OPTARG" ;;
p) findPath="$OPTARG" ;;
*) PrintUsage
exit 1 ;;
esac
done
if [ $OPTIND -gt 1 ]; then
shift $((OPTIND - 1))
fi
if [ $# -gt 1 ]; then
{
PrintUsage
echo "Remember to quote the source file globs after -s"
exit 1
}
fi
# Create GNU find expressions that traverse the filesystem once and only once
if [ -z "$findPath" ]; then
findArgs='.'
else
findArgs="$findPath"
fi
for FileType in ${SourceFiles}; do
findArgs="$findArgs"' ( -iname '"$FileType"' -print -o -true ) -a '
done
# This gets rid of the final " -a " in the find argument list
findArgs=(${findArgs:0:(${#findArgs}-3)})
for file in $($findCMD "${findArgs[@]}"); do
EditFile "$file"
echo "Completed: " "$file"
done
# vim:set shiftwidth=4 softtabstop=4 expandtab:
|