+######################################################################
+# A ls with memory to notice what files have been added/removed from
+# the current directory
+
+function lsn () {
+ [[ "${LSN_STATE_FILE}" ]] || LSN_STATE_FILE=".lsn-state"
+
+ if [[ $1 == "--mem" ]] || [[ "$1" == "-m" ]]
+ then
+
+ \ls -pa | sort > "${LSN_STATE_FILE}"
+ echo "State updated." >&2
+
+ elif [[ $1 == "+" ]]
+ then
+
+ shift
+
+ TMP=$(mktemp /tmp/lsn.XXXXXX)
+ \ls -d "$@" > ${TMP}
+ cat "${LSN_STATE_FILE}" >> ${TMP}
+ sort ${TMP} | uniq -u > "${LSN_STATE_FILE}"
+ \rm ${TMP}
+
+ elif [[ "$1" ]]
+ then
+ cat >&2 <<EOF
+lsn [--help|-h] [--mem|-m] [+ <file> ...]
+
+Where
+
+ --help|-h
+ prints this help
+
+ --mem|-m
+ stores the current state of the directory
+
+ + <file> [<file> ...]
+ switches the presence/absence of the indicated files in the
+ stored state
+
+EOF
+ if [[ ! "$1" == "-h" ]] && [[ ! "$1" == "--help" ]]
+ then
+ echo >&2 "Unknown option \`\`$1''."
+ return 1
+ fi
+
+ else
+
+ if [[ -f "${LSN_STATE_FILE}" ]]
+ then
+ TMP=$(mktemp /tmp/lsn.XXXXXX)
+ \ls -pa | sort > ${TMP}
+ if diff > /dev/null ${TMP} "${LSN_STATE_FILE}"
+ then
+ echo "${VT_GREEN_FG}${VT_BOLD}No change (since $(date +"%b %d, %Y" -r "${LSN_STATE_FILE}"))${VT_RESET}"
+ else
+ \comm -1 -3 "${LSN_STATE_FILE}" ${TMP}
+ \comm -2 -3 "${LSN_STATE_FILE}" ${TMP} | while read line
+ do
+ echo "${VT_RED_FG}${VT_BOLD}${line}${VT_RESET} (missing)"
+ done
+ fi
+ \rm ${TMP}
+ else
+ echo "${VT_RED_FG}${VT_BOLD}No lsn state here.${VT_RESET}" >&2
+ return 1
+ fi
+
+ fi
+}
+