1#autoload 2 3# Find paths of applications and preserve them into _mac_apps. 4# Used by _mac_applications and _mac_files_for_application. 5 6_mac_apps_caching_policy () { 7 # Rebuild if cache is more than a day old 8 local -a oldp 9 oldp=( "$1"(Nmw+1) ) 10 (( $#oldp )) 11} 12 13 14# _mac_apps_*_retrieve 15# 16# Get search applications from directories specified in app_dir_root. 17# Paths to applications are stored in _mac_apps. 18 19_mac_apps_spotlight_retrieve () { 20 typeset mdfind_query="kMDItemContentType == 'com.apple.application-*'" 21 22 for i in ${app_dir_root}; do 23 _mac_apps+=(${(f)"$(_call_program command \ 24 mdfind -onlyin ${(q)i} ${(q)mdfind_query})"}) 25 done 26} 27 28_mac_apps_old_retrieve () { 29 # Get directories which may contain applications 30 typeset -aU app_dir 31 if [[ -z "$app_dir" ]] && \ 32 ! zstyle -a ":completion:${curcontext}:commands" application-dir app_dir 33 then 34 typeset -a app_dir_stop_pattern 35 app_dir_stop_pattern=( "*.app" "contents#" "*data" "*plugins#" "*plug?ins#" "fonts#" "document[[:alpha:]]#" "*help" "resources#" "images#" "*configurations#" ) 36 typeset app_dir_pattern 37 app_dir_pattern="(^(#i)(${(j/|/)app_dir_stop_pattern}))" 38 app_dir=( ${^app_dir_root}/(${~app_dir_pattern}/)#(N) ) 39 fi 40 41 # Get application bundles 42 local -a app_result 43 44 if ! zstyle -t ":completion:${curcontext}:commands" ignore-bundle; then 45 app_result=( ${^app_dir}*/Contents/(MacOS|MacOSClassic)(N) ) 46 _mac_apps+=( ${app_result[@]%/Contents/MacOS*} ) 47 fi 48 49 # Get single file applications 50 if ! zstyle -t ":completion:${curcontext}:commands" ignore-single; then 51 autoload -Uz zargs 52 local app_cand nargs envvars 53 app_cand=( ${^app_dir}^*.[a-z]#/..namedfork/rsrc(.UrN,.RN^U) ) 54 envvars="$(builtin typeset -x)" 55 nargs=$(( $(command sysctl -n kern.argmax) - $#envvars - 2048 )) 56 app_result="$(zargs --max-chars $nargs ${app_cand[@]} -- grep -l APPL)" 57 _mac_apps+=( ${${(f)app_result}%/..namedfork/rsrc} ) 58 fi 59} 60 61 62_retrieve_mac_apps() { 63 local cache_policy 64 zstyle -s ":completion:*:*:$service:*" cache-policy cache_policy 65 if [[ -z "$cache_policy" ]]; then 66 zstyle ":completion:*:*:$service:*" cache-policy _mac_apps_caching_policy 67 fi 68 69 if ( (( ${#_mac_apps} == 0 )) || _cache_invalid Mac_applications ) \ 70 && ! _retrieve_cache Mac_applications; then 71 72 # Get application search method 73 typeset retrieve 74 if ! zstyle -s ":completion:*:*:$service:commands" search-method retrieve 75 then 76 if [[ -d /.Spotlight-V100 ]]; then 77 # / is indexed to use Spotlight 78 retrieve=_mac_apps_spotlight_retrieve 79 else 80 # Fall back to the old way 81 retrieve=_mac_apps_old_retrieve 82 fi 83 zstyle ":completion:*:*:$service:commands" search-method $retrieve 84 fi 85 86 # Get root directories to search applications 87 typeset -a app_dir_root 88 if ! zstyle -a ":completion:${curcontext}" application-path app_dir_root 89 then 90 if [[ $retrieve = _mac_apps_old_retrieve ]]; then 91 app_dir_root=( {,/Developer,/Network,"$HOME"}/{Applications*(N),Desktop} ) 92 else 93 app_dir_root=( / ) 94 fi 95 96 zstyle ":completion:*" application-path $app_dir_root 97 fi 98 99 typeset -g -Ua _mac_apps 100 $retrieve 101 102 _store_cache Mac_applications _mac_apps 103 fi 104} 105 106_retrieve_mac_apps "$@" 107