#!/bin/sh - # # Change the indentation of a piece of source code. # Please refer to the usage message for details. # # Copyright (C) 2007-2009 Oliver Fromme # All rights reserved. Standard 2-clause BSD license and disclaimer apply, # please refer to the file /usr/share/examples/etc/bsd-style-copyright on # FreeBSD or ask the author for a copy. Usage() { cat >&2 <<-tac Usage: ${0##*/} [ ...] Changes the indentation size from to . Only the indentation is changed, other whitespace is left as-is. Input may use tabs or spaces for indentation. Output will always uses spaces. You may use "unexpand -t" to convert them back to tabs if you prefer this. If no files are specified, standand input is used. The result is written to standard output. tac exit 1 } export TAB_OLD="$1" TAB_NEW="$2" shift 2 case "$TAB_OLD/$TAB_NEW" in *[!0-9/]*|*/*/*|/*|*/|0/*|*/0) Usage ;; esac expand -t$TAB_OLD "$@" | awk ' BEGIN { tab_old = ENVIRON["TAB_OLD"] tab_new = ENVIRON["TAB_NEW"] } { old = index($0, $1) - 1 itab = int(old / tab_old) ispc = old % tab_old new = itab * tab_new + ispc printf "%*s%s\n", new, "", substr($0, old + 1) } ' #--