diff --git a/dmenu_path b/dmenu_path old mode 100644 new mode 100755 index 338bac4..3f168aa --- a/dmenu_path +++ b/dmenu_path @@ -1,4 +1,10 @@ #!/bin/sh +HISTORY="$1" + +if stest -v -qfrw "$HISTORY"; then + unset HISTORY +fi + cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"} if [ -d "$cachedir" ]; then cache=$cachedir/dmenu_run @@ -7,7 +13,6 @@ else fi IFS=: if stest -dqr -n "$cache" $PATH; then - stest -flx $PATH | sort -u | tee "$cache" -else - cat "$cache" + stest -flx $PATH | sort -u > "$cache" fi +awk 'NR==FNR { sub("^[0-9]+\t","") } !x[$0]++' "$HISTORY" "$cache" diff --git a/dmenu_run b/dmenu_run index 834ede5..444aeb0 100755 --- a/dmenu_run +++ b/dmenu_run @@ -1,2 +1,43 @@ #!/bin/sh -dmenu_path | dmenu "$@" | ${SHELL:-"/bin/sh"} & + +historyfile="$DMENU_RUN_USE_HISTORY" + +if [ -n "$historyfile" ]; then + if [ "$historyfile" = "DEFAULT" ]; then + cachedir=${XDG_CACHE_HOME:-"$HOME/.cache"} + if [ -d "$cachedir" ]; then + historyfile=$cachedir/dmenu_history + else + historyfile=$HOME/.dmenu_history # if no xdg dir, fall back to dotfile in ~ + fi + fi +fi + +dmenu_path $historyfile | dmenu "$@" \ + | awk -v histfile=$historyfile ' + BEGIN { + FS=OFS="\t" + if(!histfile) + exit + while ( (getline < histfile) > 0 ) { + count=$1 + sub("^[0-9]+\t","") + fname=$0 + history[fname]=count + } + close(histfile) + } + + { + history[$0]++ + print + } + + END { + if(!histfile) + exit + for (f in history) + print history[f],f | "sort -t '\t' -k1rn >" histfile + } + ' \ + | while read cmd; do ${SHELL:-"/bin/sh"} -c "$cmd" & done