3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 # add-modelines, a simple script to add comments to
11 # the beginning and end of source files for LibreOffice devs
13 # Blame goes to Jesse Adelman (at least at first)
14 # someone AT boldandbusted dotty-dot com
15 # http://www.boldandbusted.com/
16 # (c) 2010 Bold and Busted LLC
18 # NOTE: At present, this script only works for files with C-like comments.
19 # NOTE: If you don't specify -p, the script will act on the current working directory.
20 # NOTE: If no arguments are specified, the definitions below are in effect.
24 # - Make source file type agnostic modelines?
25 # - Too many/too few comments?
26 # - Handle top level source directories with whitespace names? (Do they exist?)
28 # Turn off globbing, helps with SourceFiles
34 # Change these to taste
35 FirstLine
='/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */'
36 LastLine
='/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */'
37 SourceFiles
='*.cxx *.cpp *.hxx *.hpp *.c *.h *.m *.mm *.idl *.src *.hrc'
39 # Set defaults (don't change these)
40 ModelineReplace
="false"
44 function SetEnvironment
()
46 if [ -n "$(which tail)" ] && [ -n "$(which head)" ]; then
53 echo "Missing head or tail, exiting..."
57 if [ -n "$(which find)" ]; then
61 echo "Missing find, exiting..."
70 local currentFirstLine
75 currentFirstLine
=$
($headCMD -1 "$FileToEdit")
76 currentLastLine
=$
($tailCMD -1 "$FileToEdit")
78 case "$ModelineReplace" in
80 if [ "${currentFirstLine:0:6}" = "${FirstLine:0:6}" ]; then
82 echo "$FirstLine" > "$FileToEdit".new
83 $tailCMD -n +2 "$FileToEdit" >> "$FileToEdit".new
86 if [ -e "$FileToEdit.new" ]; then
88 echo "$LastLine" >> "$FileToEdit".new
91 if [ "${currentLastLine:0:6}" = "${LastLine:0:6}" ]; then
93 $headCMD -n -1 "$FileToEdit" > "$FileToEdit".new
94 echo "$LastLine" >> "$FileToEdit".new
97 mv "$FileToEdit".new
"$FileToEdit"
98 echo "$FileToEdit updated" ;;
100 if [ "${currentFirstLine:0:6}" != "${FirstLine:0:6}" ]; then
101 if [ "${currentLastLine:0:6}" != "${LastLine:0:6}" ]; then
103 echo "$FirstLine" > "$FileToEdit".new
104 cat "$FileToEdit" >> "$FileToEdit".new
105 if [ "x${currentLastLine}" != "x" ] ; then
106 echo "" >> "$FileToEdit".new
108 echo "$LastLine" >> "$FileToEdit".new
109 mv "$FileToEdit".new
"$FileToEdit"
110 echo "$FileToEdit updated"
117 function PrintUsage
()
119 echo "Usage: $0 [-z] [-s \"<sourcefile glob>\"] [-p <path to source>]"
126 # Get command line options
128 while getopts "zs:p:" opt
; do
130 z
) ModelineReplace
="true" ;;
131 s
) SourceFiles
="$OPTARG" ;;
132 p
) findPath
="$OPTARG" ;;
138 if [ $OPTIND -gt 1 ]; then
139 shift $
((OPTIND
- 1))
142 if [ $# -gt 1 ]; then
145 echo "Remember to quote the source file globs after -s"
150 # Create GNU find expressions that traverse the filesystem once and only once
151 if [ -z "$findPath" ]; then
157 for FileType
in ${SourceFiles}; do
158 findArgs
="$findArgs"' ( -iname '"$FileType"' -print -o -true ) -a '
161 # This gets rid of the final " -a " in the find argument list
162 findArgs
=(${findArgs:0:(${#findArgs}-3)})
164 for file in $
($findCMD "${findArgs[@]}"); do
166 echo "Completed: " "$file"
169 # vim:set shiftwidth=4 softtabstop=4 expandtab: