fasd 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. # Fasd is originally written based on code from z (https://github.com/rupa/z)
  2. # by rupa deadwyler under the WTFPL license. Most if not all of the code has
  3. # been rewritten.
  4. # Copyright (C) 2011, 2012 by Wei Dai. All rights reserved.
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining
  7. # a copy of this software and associated documentation files (the
  8. # "Software"), to deal in the Software without restriction, including
  9. # without limitation the rights to use, copy, modify, merge, publish,
  10. # distribute, sublicense, and/or sell copies of the Software, and to
  11. # permit persons to whom the Software is furnished to do so, subject to
  12. # the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included
  15. # in all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. # make zsh do word splitting inside this function
  25. [ "$ZSH_VERSION" ] && emulate sh && setopt localoptions
  26. case $1 in
  27. --init) shift
  28. while [ "$1" ]; do
  29. case $1 in
  30. env)
  31. { # Load configuration files
  32. if [[ -s ${XDG_CONFIG_HOME:-"${HOME}/.config"}/fasd/config ]]; then
  33. source ${XDG_CONFIG_HOME:-"${HOME}/.config"}/fasd/config
  34. else
  35. [[ -s /etc/fasdrc ]] && source /etc/fasd
  36. [[ -s ${HOME}/.fasdrc ]] && source ${HOME}/.fasdrc
  37. fi
  38. # set default options
  39. (( ! ${+_FASD_DATA} )) && _FASD_DATA="$HOME/.fasd"
  40. (( ! ${+_FASD_BLACKLIST} )) && _FASD_BLACKLIST="--help"
  41. (( ! ${+_FASD_SHIFT} )) && _FASD_SHIFT="sudo busybox"
  42. (( ! ${+_FASD_IGNORE} )) && _FASD_IGNORE="fasd ls echo"
  43. (( ! ${+_FASD_SINK} )) && _FASD_SINK=/dev/null
  44. (( ! ${+_FASD_TRACK_PWD} )) && _FASD_TRACK_PWD=1
  45. (( ! ${+_FASD_MAX} )) && _FASD_MAX=2000
  46. (( ! ${+_FASD_BACKENDS} )) && _FASD_BACKENDS=native
  47. (( ! ${+_FASD_FUZZY} )) && _FASD_FUZZY=2
  48. (( ! ${+_FASD_VIMINFO} )) && _FASD_VIMINFO="$HOME/.viminfo"
  49. (( ! ${+_FASD_RECENTLY_USED_XBEL} )) && \
  50. _FASD_RECENTLY_USED_XBEL="$HOME/.local/share/recently-used.xbel"
  51. if (( ! ${+_FASD_AWK} )); then
  52. # awk preferences
  53. local awk; for awk in mawk gawk original-awk nawk awk; do
  54. $awk "" && _FASD_AWK=$awk && break
  55. done
  56. fi
  57. } >> ${_FASD_SINK:-/dev/null} 2>&1
  58. ;;
  59. esac; shift
  60. done
  61. ;;
  62. # if "$_fasd_cur" or "$2" is a query, then output shell code to be eval'd
  63. --word-complete-trigger)
  64. shift; [ "$2" ] && local _fasd_cur="$2" || return
  65. case $_fasd_cur in
  66. ,*) printf %s\\n "$1 e $_fasd_cur";;
  67. f,*) printf %s\\n "$1 f ${_fasd_cur#?}";;
  68. d,*) printf %s\\n "$1 d ${_fasd_cur#?}";;
  69. *,,) printf %s\\n "$1 e $_fasd_cur";;
  70. *,,f) printf %s\\n "$1 f ${_fasd_cur%?}";;
  71. *,,d) printf %s\\n "$1 d ${_fasd_cur%?}";;
  72. esac
  73. ;;
  74. --sanitize) shift; printf %s\\n "$*" | \
  75. sed 's/\([^\]\)$( *[^ ]* *\([^)]*\)))*/\1\2/g
  76. s/\([^\]\)[|&;<>$`{}]\{1,\}/\1 /g'
  77. ;;
  78. --proc) shift # process commands
  79. # stop if we don't own $_FASD_DATA or $_FASD_RO is set
  80. [ -f "$_FASD_DATA" -a ! -O "$_FASD_DATA" ] || [ "$_FASD_RO" ] && return
  81. # blacklists
  82. local each; for each in $_FASD_BLACKLIST; do
  83. case " $* " in *\ $each\ *) return;; esac
  84. done
  85. # shifts
  86. while true; do
  87. case " $_FASD_SHIFT " in
  88. *\ $1\ *) shift;;
  89. *) break;;
  90. esac
  91. done
  92. # ignores
  93. case " $_FASD_IGNORE " in
  94. *\ $1\ *) return;;
  95. esac
  96. shift; fasd --add "$@" # add all arguments except command
  97. ;;
  98. --add|-A) shift # add entries
  99. # stop if we don't own $_FASD_DATA or $_FASD_RO is set
  100. [ -f "$_FASD_DATA" -a ! -O "$_FASD_DATA" ] || [ "$_FASD_RO" ] && return
  101. # find all valid path arguments, convert them to simplest absolute form
  102. local paths="$(while [ "$1" ]; do
  103. [ -e "$1" ] && printf %s\\n "$1"; shift
  104. done | sed '/^[^/]/s@^@'"$PWD"'/@
  105. s@/\.\.$@/../@;s@/\(\./\)\{1,\}@/@g;:0
  106. s@[^/][^/]*//*\.\./@/@;t 0
  107. s@^/*\.\./@/@;s@//*@/@g;s@/\.\{0,1\}$@@;s@^$@/@' 2>> "$_FASD_SINK" \
  108. | tr '\n' '|')"
  109. # add current pwd if the option is set
  110. [ "$_FASD_TRACK_PWD" = "1" -a "$PWD" != "$HOME" ] && paths="$paths|$PWD"
  111. [ -z "${paths##\|}" ] && return # stop if we have nothing to add
  112. # maintain the file
  113. local tempfile
  114. tempfile="$(mktemp "$_FASD_DATA".XXXXXX)" || return
  115. $_FASD_AWK -v list="$paths" -v now="$(date +%s)" -v max="$_FASD_MAX" -F"|" '
  116. BEGIN {
  117. split(list, files, "|")
  118. for(i in files) {
  119. path = files[i]
  120. if(path == "") continue
  121. paths[path] = path # array for checking
  122. rank[path] = 1
  123. time[path] = now
  124. }
  125. }
  126. $2 >= 1 {
  127. if($1 in paths) {
  128. rank[$1] = $2 + 1 / $2
  129. time[$1] = now
  130. } else {
  131. rank[$1] = $2
  132. time[$1] = $3
  133. }
  134. count += $2
  135. }
  136. END {
  137. if(count > max)
  138. for(i in rank) print i "|" 0.9*rank[i] "|" time[i] # aging
  139. else
  140. for(i in rank) print i "|" rank[i] "|" time[i]
  141. }' "$_FASD_DATA" 2>> "$_FASD_SINK" >| "$tempfile"
  142. if [ $? -ne 0 -a -f "$_FASD_DATA" ]; then
  143. env rm -f "$tempfile"
  144. else
  145. env mv -f "$tempfile" "$_FASD_DATA"
  146. fi
  147. ;;
  148. --delete|-D) shift # delete entries
  149. # stop if we don't own $_FASD_DATA or $_FASD_RO is set
  150. [ -f "$_FASD_DATA" -a ! -O "$_FASD_DATA" ] || [ "$_FASD_RO" ] && return
  151. # turn valid arguments into entry-deleting sed commands
  152. local sed_cmd="$(while [ "$1" ]; do printf %s\\n "$1"; shift; done | \
  153. sed '/^[^/]/s@^@'"$PWD"'/@;s@/\.\.$@/../@;s@/\(\./\)\{1,\}@/@g;:0
  154. s@[^/][^/]*//*\.\./@/@;t 0
  155. s@^/*\.\./@/@;s@//*@/@g;s@/\.\{0,1\}$@@
  156. s@^$@/@;s@\([.[\/*^$]\)@\\\1@g;s@^\(.*\)$@/^\1|/d@' 2>> "$_FASD_SINK")"
  157. # maintain the file
  158. local tempfile
  159. tempfile="$(mktemp "$_FASD_DATA".XXXXXX)" || return
  160. sed "$sed_cmd" "$_FASD_DATA" 2>> "$_FASD_SINK" >| "$tempfile"
  161. if [ $? -ne 0 -a -f "$_FASD_DATA" ]; then
  162. env rm -f "$tempfile"
  163. else
  164. env mv -f "$tempfile" "$_FASD_DATA"
  165. fi
  166. ;;
  167. --query) shift # query the db, --query [$typ ["$fnd" [$mode]]]
  168. [ -f "$_FASD_DATA" ] || return # no db yet
  169. [ "$1" ] && local typ="$1"
  170. [ "$2" ] && local fnd="$2"
  171. [ "$3" ] && local mode="$3"
  172. # cat all backends
  173. local each _fasd_data; for each in $_FASD_BACKENDS; do
  174. _fasd_data="$_fasd_data
  175. $(fasd --backend $each)"
  176. done
  177. [ "$_fasd_data" ] || _fasd_data="$(cat "$_FASD_DATA")"
  178. # set mode specific code for calculating the prior
  179. case $mode in
  180. rank) local prior='times[i]';;
  181. recent) local prior='sqrt(100000/(1+t-la[i]))';;
  182. *) local prior='times[i] * frecent(la[i])';;
  183. esac
  184. if [ "$fnd" ]; then # dafault matching
  185. local bre="$(printf %s\\n "$fnd" | sed 's/\([*\.\\\[]\)/\\\1/g
  186. s@ @[^|]*@g;s/\$$/|/')"
  187. bre='^[^|]*'"$bre"'[^|/]*|'
  188. local _ret="$(printf %s\\n "$_fasd_data" | grep "$bre")"
  189. [ "$_ret" ] && _ret="$(printf %s\\n "$_ret" | while read -r line; do
  190. [ -${typ:-e} "${line%%\|*}" ] && printf %s\\n "$line"
  191. done)"
  192. if [ "$_ret" ]; then
  193. _fasd_data="$_ret"
  194. else # no case mathcing
  195. _ret="$(printf %s\\n "$_fasd_data" | grep -i "$bre")"
  196. [ "$_ret" ] && _ret="$(printf %s\\n "$_ret" | while read -r line; do
  197. [ -${typ:-e} "${line%%\|*}" ] && printf %s\\n "$line"
  198. done)"
  199. if [ "$_ret" ]; then
  200. _fasd_data="$_ret"
  201. elif [ "${_FASD_FUZZY:-0}" -gt 0 ]; then # fuzzy matching
  202. local fuzzy_bre="$(printf %s\\n "$fnd" | \
  203. sed 's/\([*\.\\\[]\)/\\\1/g;s/\$$/|/
  204. s@\(\\\{0,1\}[^ ]\)@\1[^|/]\\{0,'"$_FASD_FUZZY"'\\}@g
  205. s@ @[^|]*@g')"
  206. fuzzy_bre='^[^|]*'"$fuzzy_bre"'[^|/]*|'
  207. _ret="$(printf %s\\n "$_fasd_data" | grep -i "$fuzzy_bre")"
  208. [ "$_ret" ] && _ret="$(printf %s\\n "$_ret" | while read -r line; do
  209. [ -${typ:-e} "${line%%\|*}" ] && printf %s\\n "$line"
  210. done)"
  211. [ "$_ret" ] && _fasd_data="$_ret" || _fasd_data=
  212. fi
  213. fi
  214. else # no query arugments
  215. _fasd_data="$(printf %s\\n "$_fasd_data" | while read -r line; do
  216. [ -${typ:-e} "${line%%\|*}" ] && printf %s\\n "$line"
  217. done)"
  218. fi
  219. # query the database
  220. [ "$_fasd_data" ] && printf %s\\n "$_fasd_data" | \
  221. $_FASD_AWK -v t="$(date +%s)" -F"|" '
  222. function frecent(time) {
  223. dx = t-time
  224. if( dx < 3600 ) return 6
  225. if( dx < 86400 ) return 4
  226. if( dx < 604800 ) return 2
  227. return 1
  228. }
  229. {
  230. if(!paths[$1]) {
  231. times[$1] = $2
  232. la[$1] = $3
  233. paths[$1] = 1
  234. } else {
  235. times[$1] += $2
  236. if($3 > la[$1]) la[$1] = $3
  237. }
  238. }
  239. END {
  240. for(i in paths) printf "%-10s %s\n", '"$prior"', i
  241. }' - 2>> "$_FASD_SINK"
  242. ;;
  243. --backend)
  244. case $2 in
  245. native) cat "$_FASD_DATA";;
  246. viminfo)
  247. < "$_FASD_VIMINFO" sed -n '/^>/{s@~@'"$HOME"'@
  248. s/^..//
  249. p
  250. }' | $_FASD_AWK -v t="$(date +%s)" '{
  251. t -= 60
  252. print $0 "|1|" t
  253. }'
  254. ;;
  255. recently-used)
  256. local nl="$(printf '\\\nX')"; nl="${nl%X}" # slash newline for sed
  257. tr -d '\n' < "$_FASD_RECENTLY_USED_XBEL" | \
  258. sed 's@file:/@'"$nl"'@g;s@count="@'"$nl"'@g' | sed '1d;s/".*$//' | \
  259. tr '\n' '|' | sed 's@|/@'"$nl"'@g' | $_FASD_AWK -F'|' '{
  260. sum = 0
  261. for( i=2; i<=NF; i++ ) sum += $i
  262. print $1 "|" sum
  263. }'
  264. ;;
  265. current)
  266. for path in *; do
  267. printf "$PWD/%s|1\\n" "$path"
  268. done
  269. ;;
  270. spotlight)
  271. mdfind '(kMDItemFSContentChangeDate >= $time.today) ||
  272. kMDItemLastUsedDate >= $time.this_month' \
  273. | sed '/Library\//d
  274. /\.app$/d
  275. s/$/|2/'
  276. ;;
  277. *) eval "$2";;
  278. esac
  279. ;;
  280. *) # parsing logic and processing
  281. local fnd= last= _FASD_BACKENDS="$_FASD_BACKENDS" _fasd_data= comp= exec=
  282. while [ "$1" ]; do case $1 in
  283. --complete) [ "$2" = "--" ] && shift; set -- $2; local lst=1 r=r comp=1;;
  284. --query|--add|--delete|-A|-D) fasd "$@"; return $?;;
  285. --version) [ -z "$comp" ] && echo "1.0.1" && return;;
  286. --) while [ "$2" ]; do shift; fnd="$fnd $1"; last="$1"; done;;
  287. -*) local o="${1#-}"; while [ "$o" ]; do case $o in
  288. s*) local show=1;;
  289. l*) local lst=1;;
  290. i*) [ -z "$comp" ] && local interactive=1 show=1;;
  291. r*) local mode=rank;;
  292. t*) local mode=recent;;
  293. e*) o="${o#?}"; if [ "$o" ]; then # there are characters after "-e"
  294. local exec="$o" # anything after "-e"
  295. else # use the next argument
  296. local exec="${2:?"-e: Argument needed "}"
  297. shift
  298. fi; break;;
  299. b*) o="${o#?}"; if [ "$o" ]; then
  300. _FASD_BACKENDS="$o"
  301. else
  302. _FASD_BACKENDS="${2:?"-b: Argument needed"}"
  303. shift
  304. fi; break;;
  305. B*) o="${o#?}"; if [ "$o" ]; then
  306. _FASD_BACKENDS="$_FASD_BACKENDS $o"
  307. else
  308. _FASD_BACKENDS="$_FASD_BACKENDS ${2:?"-B: Argument needed"}"
  309. shift
  310. fi; break;;
  311. a*) local typ=e;;
  312. d*) local typ=d;;
  313. f*) local typ=f;;
  314. R*) local r=r;;
  315. [0-9]*) local _fasd_i="$o"; break;;
  316. h*) [ -z "$comp" ] && echo "fasd [options] [query ...]
  317. [f|a|s|d|z] [options] [query ...]
  318. options:
  319. -s list paths with scores
  320. -l list paths without scores
  321. -i interactive mode
  322. -e <cmd> set command to execute on the result file
  323. -b <name> only use <name> backend
  324. -B <name> add additional backend <name>
  325. -a match files and directories
  326. -d match directories only
  327. -f match files only
  328. -r match by rank only
  329. -t match by recent access only
  330. -R reverse listing order
  331. -h show a brief help message
  332. -[0-9] select the nth entry
  333. fasd [-A|-D] [paths ...]
  334. -A add paths
  335. -D delete paths" >&2 && return;;
  336. esac; o="${o#?}"; done;;
  337. *) fnd="$fnd $1"; last="$1";;
  338. esac; shift; done
  339. # guess whether the last query is selected from tab completion
  340. case $last in
  341. /?*) if [ -z "$show$lst" -a -${typ:-e} "$last" -a "$exec" ]; then
  342. $exec "$last"
  343. return
  344. fi;;
  345. esac
  346. local R; [ -z "$r" ] && R=r || R= # let $R be the opposite of $r
  347. fnd="${fnd# }"
  348. local res
  349. res="$(fasd --query 2>> "$_FASD_SINK")" # query the database
  350. [ $? -gt 0 ] && return
  351. if [ 0 -lt ${_fasd_i:-0} ] 2>> "$_FASD_SINK"; then
  352. res="$(printf %s\\n "$res" | sort -n${R} | \
  353. sed -n "$_fasd_i"'s/^[^ ]*[ ]*//p')"
  354. elif [ "$interactive" ] || [ "$exec" -a -z "$fnd$lst$show" -a -t 1 ]; then
  355. if [ "$(printf %s "$res" | sed -n '$=')" -gt 1 ]; then
  356. res="$(printf %s\\n "$res" | sort -n${R})"
  357. printf %s\\n "$res" | sed = | sed 'N;s/\n/ /' | sort -nr >&2
  358. printf "> " >&2
  359. local i; read i; [ 0 -lt "${i:-0}" ] 2>> "$_FASD_SINK" || return 1
  360. fi
  361. res="$(printf %s\\n "$res" | sed -n "${i:-1}"'s/^[^ ]*[ ]*//p')"
  362. elif [ "$lst" ]; then
  363. [ "$res" ] && printf %s\\n "$res" | sort -n${r} | sed 's/^[^ ]*[ ]*//'
  364. return
  365. elif [ "$show" ]; then
  366. [ "$res" ] && printf %s\\n "$res" | sort -n${r}
  367. return
  368. elif [ "$fnd" ] && [ "$exec" -o ! -t 1 ]; then # exec or subshell
  369. res="$(printf %s\\n "$res" | sort -n | sed -n '$s/^[^ ]*[ ]*//p')"
  370. else # no args, show
  371. [ "$res" ] && printf %s\\n "$res" | sort -n${r}
  372. return
  373. fi
  374. if [ "$res" ]; then
  375. fasd --add "$res"
  376. [ -z "$exec" ] && exec='printf %s\n'
  377. $exec "$res"
  378. fi
  379. ;;
  380. esac
  381. #case $- in
  382. # *i*) ;; # assume being sourced, do nothing
  383. # *) # assume being executed as an executable
  384. # if [ -x "$_FASD_SHELL" -a -z "$_FASD_SET" ]; then
  385. # _FASD_SET=1 exec $_FASD_SHELL "$0" "$@"
  386. # else
  387. # fasd "$@"
  388. # fi;;
  389. #esac