#!/bin/bash # REQUIREMENTS # * Emacs 23 (maybe older versions work too) # * erlang-mode R14, you can install it in your R13 system # INSTALL # * Copy this script to directory ejabberd/src, or exmpp/ # USE # * Call without arguments to indent all the *.erl files in this dir and subdirs # * Call with an argument and it will indent the specified file ERLMODE=`emacs --batch 2>&1 | grep erlang-mode | awk '{print $2}' | tail -n 1` ERLMODE_CHARS=`echo $ERLMODE | wc -m` if [ "$ERLMODE_CHARS" -gt 5 ] ; then EMACS_SYSTEM_INITS="--no-init-file --no-site-file --load $ERLMODE" else EMACS_SYSTEM_INITS="" fi EMALOCALCONF=.emacs.batch LOG=emacs.log.tmp [ -f $EMALOCALCONF ] || echo "(setq erlang-mode-hook (function (lambda () (setq indent-tabs-mode nil) (setq c-indent-level 4))))" >$EMALOCALCONF indent_file () { echo -n "Indenting $1 ... " if [ "$IS_MODE_RECENT" -eq 0 ] ; then echo -n "Cleaning ..." # In each line delete all spaces and tabs before another # characters, to ensure emacs indent the line perl -pi -e 's/^[\t ]*//' $1 fi # If a line starts with a single %, add another one to ensure # correct indentation perl -pi -e 's/^%([^%])/%%\1/' $1 # Now call emacs to indent this erlang file emacs \ --batch \ $EMACS_SYSTEM_INITS \ --load $EMALOCALCONF \ --file $1 \ --eval "(progn (delete-trailing-whitespace) (erlang-indent-current-buffer) (save-buffer))" >$LOG 2>&1 if [ $? -eq 0 ]; then echo "done." else echo "problem: " perl -pi -e 's/^/ /' $LOG cat $LOG echo -n "Error indenting $FILEX : " tail -n 1 $LOG fi } FILE=$1 indent_some () { if [ "$FILE" = "" ] ; then for FILEX in `find ./ -type f -name '*.[eh]rl' | sort`; do indent_file $FILEX done else FILEX=$FILE indent_file $FILEX fi rm $LOG } # Check if erlang-mode can be used, and it is R14 or newer emacs --batch --file acl.erl \ --eval "(progn (delete-trailing-whitespace) (erlang-indent-current-buffer))" IS_MODE_INSTALLED=$? # Check if erlang-mode can be used, and it is R14 or newer emacs --batch --file gen_storage.erl \ --eval "(progn (delete-trailing-whitespace) (erlang-indent-current-buffer))" IS_MODE_RECENT=$? if [ "$IS_MODE_INSTALLED" -eq 0 ] ; then echo "Indenting..." indent_some else echo "ERROR: It seems Emacs doesn't have erlang-mode installed. Aborting..." fi rm $EMALOCALCONF