# editor handling functions. Cooler than a cool thing and unashamedly ripped # off from .cdrc (author unknown) # Author Chris Rouch 21/6/95 #usage: # e -l Displays the most recently used MAXFILES documents. # e -foostring Edits most recent file whose name contains the string foostring. # e -8 Edits the eighth most recent file # e -r Resets the file history # e foo Edits foo # e Edits last file # e -? Displays a usage message #if you want multiple shells to share the same history (either concurrently or # serially) create a file and place its name in the shell variable EDHISTFILE EDHISTFILE=$HOME/.edhistory alias e=_editor alias p="_editor --nolog" function _editor { EDITOR=${EDITOR-vi} #EDITOR is vi if not already set log="yes" case $1 in "--nolog") shift log= ;; esac # preset some integers typeset -i edlen i status # and declare t (why?) typeset t t="$@" #save parameters in $t if [ "$EDHISTFILE" -a -r "$EDHISTFILE" ]; then # use a file typeset EDHIST= set -- `<$EDHISTFILE` ((i=-1)) # equivelant to 'let i=-1' while [ $# -gt 0 ]; do #load the EDHIST array EDHIST[i=i+1]=$1 shift done fi set -- $t #reset parameters to saved values edlen=${#EDHIST[*]} #length of array case "$@" in "") #last file #here i think ((...)) is an arithmetic if if ((edlen>0)) then # something on the stack file=${EDHIST[0]} $EDITOR ${EDHIST[0]} ; status=$? else # what the hell, just do it, and let $EDITOR complain file="$@" $EDITOR $@ ; status=$? fi;; -) #previous file #here i think ((...)) is an arithmetic if if ((edlen>1)) then # something on the stack file=${EDHIST[1]} $EDITOR ${EDHIST[1]} ; status=$? else # what the hell, just do it, and let $EDITOR complain file="$@" $EDITOR $@ ; status=$? fi;; -\?) #usage message echo "e -l Displays the most recently used MAXFILES documents." echo "e -foostring Edits most recent file whose name contains the string foostring." echo "e -8 Edits the eighth most recent file" echo "e -r Resets the file history" echo "e foo Edits foo " echo "e Edits last file " echo "e -? Displays a usage message" return 0;; '-l') #list files typeset -i num # make num a three digit right justified value ((i=edlen)) # list in reverse order while (((i=i-1) >=0)) do ((num=i)) if [ -n "${EDHIST[i]}" ] then echo "$num ${EDHIST[i]}" fi done return 0;; -r) # remove entries unset EDHIST[*] if [ "$EDHISTFILE" ] then # keep EDHISTFILE in step >| $EDHISTFILE fi return 0;; -[0-9]|-[0-9][0-9]) # -nn: go to nth previous file #${1#-} removes the leading - from $1 # the rest checks thatit is in range if (((i=${1#-})=edlen)) then file="./-$t" shift $EDITOR $file $@ fi ;; *) # filename file="$@" case $file in /*) : # full path is good ;; *) file=$PWD/$file ;; esac $EDITOR $@ ; status=$? ;; esac if [ -n "$log" ]; then _edins $file #insert onto stack fi if [ "$EDHISTFILE" ] then # keep EDHISTFILE in step echo ${EDHIST[*]} >| $EDHISTFILE fi # echo '[?1h=\c' # Application keypad, reversed by [?1l> return $status } function _edins { MAXFILES=20 #allow up to 20 entries # insert file onto stack typeset -i i ((i=-1)) # loop thru array while (((i=i+1) <${#EDHIST[*]})) do # echo "check :${EDHIST[$i]}: vs. :$@:" case "${EDHIST[$i]}" in "$@") break;; esac done if ((i>$MAXFILES)) then ((i=$MAXFILES)) fi while (((i=i-1)>=0)) do #shuffle up. NB this will remove the current entry, so # if we found a match it will move from the middle to the end EDHIST[i+1]=${EDHIST[i]} done EDHIST[0]=$@ #and insert here at the end }