And a couple of fixes for the 'history' patch:

- fixed the code in the BEGIN block of the inline awk program in dmenu_run;
  if no history file was supplied the awk script was just ignoring any
output from dmenu

- improved the history/cache parsing/de-duplication awk one-liner in
  dmenu_path; the former 'NR==FNR' test was not enough: in case of a not
supplied or empty history file it attempted to remove a count followed by a
tab from the file names in the cache. Obviously to find such an occurrence
would be crazy rare, so it was quite harmless (I suppose that you don't
have any executables in your path of the form "count\tname", do you ?
Anyway, now you can :) )

New patch attached.

diff --git a/dmenu_path b/dmenu_path
old mode 100644
new mode 100755
index 338bac4..b81a85a
--- 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,7 @@ 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 '!cache { sub("^[0-9]+\t","") } !x[$0]++' "$HISTORY" cache=1 "$cache"
+
diff --git a/dmenu_run b/dmenu_run
index 834ede5..e93a790 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) {
+                               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

Reply via email to