
#*************************************************************************
# This file contains functions and aliases which can be cut out and put
# in your .skshrc file.  They are not included there by default, since
# many people will not want them, or will want modified versions.
# These commands should operate with SKsh version 1.3 or later.
# They are also simple skeletons which can be expanded to add features.
#
# Also, please note that these commands have not been tested extensively.
#*************************************************************************


#*************************************************************************
# This is a "man" function which can be used to retrieve documentation
# files stored in a MAN: directory.  These files (which are often
# included with public domain software as "readme" files) should be
# copied to a name such as "MAN:prog.MAN".  Then, "man prog" can be used
# to retrieve the files with the "more" command.  One useful extention
# would permit the use of a $PAGER variable instead of always using
# "more".
#*************************************************************************

function man {
   if [ ! -f "MAN:$1.MAN" ]
   then
      echo "No manual entry for $1"
   else
      more "MAN:$1.MAN"
   fi
}


#*************************************************************************
#  This function accepts a number of parameters, and prompts the user
#  for whether these files should be deleted.  For example, "qrm *.o"
#  would prompt the user at each .o file.  Answering "Y" or "y" causes
#  the file to be removed; otherwise, it is ignored.  A useful extention
#  would look at only the first character of the response, thus allowing
#  "yes" to be used in place of "y".
#*************************************************************************

function qrm {    # query rm
   local ans

   for file in $*
   do
      echo -n "Remove $file ? "
      read ans

      if [ "$ans" = 'y' -o "$ans" = 'Y' ]
      then
         rm -v "$file"
      fi
   done
}


#*************************************************************************
# This alias will remove emacs backup files by using the "qrm" function
# defined above.  It could be extended to remove ".o" files, or anything
# else.
#*************************************************************************

alias clean='qrm *~'


#*************************************************************************
# The following functions implement directory push and pop operations.
# The first, pushd, accepts one argument.  It cd's to that directory, but
# pushes the current working directory onto a stack, first, so that it
# can later be retrieved with a popd.
#*************************************************************************

function pushd {
   if [ $# -ne 1 ]
   then
      echo "Usage: $0 new_dir"
   else
      DIRSTACK="$PWD,$DIRSTACK"
      export DIRSTACK
      cd "$1"
   fi
}

#*************************************************************************
# This function implements the popd operation.  It accepts no arguments,
# and simply pops a directory off the stack and returns to that point.
# it complains if the directory stack is empty.
#*************************************************************************

function popd {
   local comma_pos new_dir

   if [ $# -ne 0 ]
   then
      echo "Usage: $0"
      return
   elif [ -z "$DIRSTACK" ]
   then
      echo 'The directory stack is empty'
      return
   fi
      
   comma_pos = $(expr index "$DIRSTACK" ',')
   new_dir   = $(expr substr "$DIRSTACK" 1 $(expr $comma_pos - 1))
   DIRSTACK  = $(expr substr "$DIRSTACK" $(expr $comma_pos + 1) 32000)
   export DIRSTACK
   cd "$new_dir"
}


#*************************************************************************
# These aliases clear and print the directory stack, respectively.  The
# directory stack will be printed with a trailing comma, which is how it
# is stored.  This could be cleaned up quite simply.
#*************************************************************************

alias cleard='DIRSTACK=""'
alias dstack='echo "$DIRSTACK"'
