#-------------------------------------------------------------
# h
# History manipulation function.  It displays the command history in reverse order.
# The number of commands displayed is set by $list_size.  After the initial listing
# a "> " prompt appears.  The user has three choices: 1) Press dot "." to exit 
# doing nothing more, 2) Press "enter" to display the next listing of commands,
# or 3) Enter the number of a command which is executed.  Since the command is
# executed from this function it isn't added to the command history.
function h () {
	local list_size=10

	# If $1 is a number then just execute it without displaying list
	if [ $1 ]; then
		local command=`fc -ln  $1 $1`
		echo $command
		$command
		return
	fi

   # Initialize the bottom of the list
	local bottom=$HISTSIZE
	let bottom=HISTCMD-HISTSIZE
	if [ $bottom -lt 1 ];then
		bottom=1
	fi

   # Setup the start of the listing
	local start=$HISTCMD
	let start=start-list_size
	if [ $start -lt $bottom ];then
		start=$bottom
	fi

   # Setup the end of the listing
	local stop=$HISTCMD
	let stop=stop-2

   # Display the first listing
	fc -lr  $start $stop

   # Get user input on what to do next
	local response=""
	while [ "$response" = "" ]; do
		read -a response -p "> "
      # Display next listing if enter pressed
		if [ "$response" = "" ]; then
			let stop=start-1
			if [ $stop -le $bottom ];then
				stop=$bottom
			fi
			let start=start-list_size
			if [ $start -lt $bottom ];then
				start=$bottom
			fi
			fc -lr  $start $stop
		fi
	done

	# Exit if dot pressed
	if [ "$response" = "." ]; then 
		return
	fi

   # Try to execute the line number entered by user
	local command=`fc -ln  $response $response`
	echo "$command" >| /tmp/$$.command
	chmod 700 /tmp/$$.command
	/tmp/$$.command
	rm -f /tmp/$$.command
} # h

