Add zsh anything.el plugin
[zsh.git] / .zsh / zaw / sources / applications.zsh
1 #
2 # zaw-src-applications
3 #
4 # zaw source for desktop applications
5 #
6
7 function zaw-src-applications() {
8 emulate -L zsh
9 setopt local_options extended_glob null_glob
10
11 case "$OSTYPE" in
12 [Dd]arwin*)
13 candidates=( )
14 # Use the spotlight index to get application paths
15 (( ${+commands[mdfind]} )) && \
16 candidates+=(${(f)"$(mdfind -onlyin / 'kMDItemKind == "Application"' 2>/dev/null)"})
17
18 # Use locate if available and no output from spotlight or if forced use by ZAW_SRC_APPLICATIONS_USE_LOCATE
19 if (( ${+commands[locate]} )) && [ -n "$ZAW_SRC_APPLICATIONS_USE_LOCATE" -o ${#candidates} -eq 0 ]; then
20 # Apps inside apps are not normally useful
21 if [ -n "$ZAW_SRC_APPLICATIONS_INTERNAL_APPS_OK" ]; then
22 candidates+=(${(f)"$(locate -i '*.app' 2>/dev/null)"})
23 elif [ ${+commands[grep]} -eq 1 ]; then
24 candidates+=(${(f)"$((locate -i '*.app' | grep -iv '\.app/') 2>/dev/null)"})
25 fi
26 fi
27
28 # Glob common locations anyway since both of previous indexes may
29 # be stale or non-existent
30 candidates+=({,~}/Applications{,/Utilities}/*.app(N^M) /System/Library/CoreServices/*.app(N^M))
31
32 candidates=(${(iou)candidates[@]})
33 actions=("zaw-callback-launch-macapp" "zaw-callback-append-to-buffer")
34 act_descriptions=("execute application" "append to edit buffer")
35 ;;
36 [Ll]inux*|*[Bb][Ss][Dd]*|[Ss]olaris*|[Ss]un[Oo][Ss]*)
37 local d
38 local -a match mbegin mend
39 for d in /usr/share/applications/*.desktop; do
40 local name="" comment="" exec_="" terminal=0 no_display=0
41 while read line; do
42 case "${line}" in
43 Name=(#b)(*))
44 name="${match[1]}"
45 ;;
46 Comment=(#b)(*))
47 comment="${match[1]}"
48 ;;
49 Exec=(#b)(*))
50 exec_="${match[1]}"
51 ;;
52 Terminal=true)
53 terminal=1
54 ;;
55
56 NoDisplay=true)
57 no_display=1
58 ;;
59 esac
60 done < "${d}"
61
62 if (( no_display )); then
63 continue
64 fi
65
66 # TODO: % expansion in $exec_
67 # remove args that match %* from $exec_
68 exec_="${(@m)${(z)exec_}:#%*}"
69
70 if ! (( terminal )); then
71 # disown
72 exec_="${exec_} &!"
73 fi
74
75 candidates+=( "${exec_}" )
76 cand_descriptions+=( "${name} - ${comment}" )
77 done
78 actions=("zaw-callback-execute" "zaw-callback-append-to-buffer")
79 act_descriptions=("execute application" "append to edit buffer")
80 ;;
81 *)
82 # Unsupported OS
83 candidates=( )
84 actions=("zaw-callback-execute" "zaw-callback-append-to-buffer")
85 act_descriptions=("execute application" "append to edit buffer")
86 ;;
87 esac
88
89 }
90
91 function zaw-callback-launch-macapp() {
92 BUFFER="open -a \"${(j:; :)@}\""
93 zle accept-line
94 }
95
96 zaw-register-src -n applications zaw-src-applications