Permalink
Browse files

no-indent mode and bug fixes

Signed-off-by: Christopher Hall <hsw@openmoko.com>
  • Loading branch information...
1 parent f2c8ee6 commit fa11978c6c2e19c42f12dc7d3a74d74abf03de84 @hxw committed Nov 8, 2012
Showing with 32 additions and 18 deletions.
  1. +32 −18 IndentAndTabify
View
@@ -11,17 +11,21 @@ USAGE()
{
[ -z "$1" ] || echo error: $*
echo usage: $(basename "$0") '<options...>'
- echo ' --copy source-file destination-file Copy indented and tabified source to destination'
- echo ' --in-place source-files... In place tabify and indent of each file'
- echo ' --overwrite For --copy to overwrite destination'
+ 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 source destination out
+ local verbose overwrite indent source destination out
verbose="$1"; shift
overwrite="$1"; shift
+ indent="$1"; shift
source=$(readlink -m "$1"); shift
destination=$(readlink -m "$1"); shift
@@ -44,23 +48,29 @@ EmacsIndentAndTabity()
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}'")
+ (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)
- (tabify (point-min) (point-max))
- (indent-region (point-min) (point-max))
+ (untabify (point-min) (point-max))
+
+ (when (string-equal "yes" "'"${indent}"'")
+ (indent-region (point-min) (point-max)))
- (write-file "'${destination}'" nil)
+ (write-file "'"${destination}"'" nil)
(kill-emacs 0))
' 2>&1 | eval ${out}
}
@@ -73,11 +83,12 @@ verbose=no
overwrite=no
InPlace=no
copy=no
-debug=''
+indent=yes
+debug=no
getopt=/usr/local/bin/getopt
[ -x "${getopt}" ] || getopt=getopt
-args=$(${getopt} -o hvoicd: --long=help,verbose,overwrite,in-place,copy,debug: -- "$@") || exit 1
+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}"
@@ -105,9 +116,14 @@ do
shift
;;
+ -n|--no-indent)
+ indent=no
+ shift
+ ;;
+
-d|--debug)
- debug=$2
- shift 2
+ debug=yes
+ shift
;;
--)
@@ -125,8 +141,7 @@ do
esac
done
-#echo verbose = ${verbose}
-#echo ARGS = "$@"
+[ X"${debug}" = X"yes" ] && set -x
[ X"${InPlace}" = X"${copy}" ] && USAGE must have only one option set
@@ -135,14 +150,13 @@ then
[ -z "$1" ] && USAGE missing source file
[ -z "$2" ] && USAGE missing destination file
- EmacsIndentAndTabity "${verbose}" "${overwrite}" "$1" "$2"
-fi
+ EmacsIndentAndTabity "${verbose}" "${overwrite}" "${indent}" "$1" "$2"
-if [ X"${InPlace}" = X"yes" ]
+elif [ X"${InPlace}" = X"yes" ]
then
for source in "$@"
do
- EmacsIndentAndTabity "${verbose}" "${overwrite}" "${source}" "${source}"
+ EmacsIndentAndTabity "${verbose}" "${overwrite}" "${indent}" "${source}" "${source}"
done
fi

0 comments on commit fa11978

Please sign in to comment.