Add zsh anything.el plugin
[zsh.git] / .zsh / zaw / sources / open-file.zsh
1 #
2 # zaw-open-file
3 #
4 # zaw source for xdg-open to open file
5 #
6
7 function zaw-src-open-file() {
8 local root parent d f
9 setopt local_options null_glob
10
11 if (( $# == 0 )); then
12 root="${PWD}/"
13 else
14 root="$1"
15 fi
16
17 parent="${root:h}"
18 if [[ "${parent}" != */ ]]; then
19 parent="${parent}/"
20 fi
21 candidates+=("${parent}")
22 cand_descriptions+=("../")
23
24 # TODO: symlink to directory
25 for d in "${root%/}"/*(/); do
26 candidates+=("${d}/")
27 cand_descriptions+=("${d:t}/")
28 done
29
30 for f in "${root%/}"/*(^/); do
31 candidates+=("${f}")
32 cand_descriptions+=("${f:t}")
33 done
34
35 actions=( "zaw-callback-open-file" "zaw-callback-append-to-buffer" )
36 act_descriptions=( "open file or directory" "append to edit buffer" )
37 # TODO: open multiple files
38 #options=( "-m" )
39 options=( "-t" "${root}" )
40 }
41
42 zaw-register-src -n open-file zaw-src-open-file
43
44 function zaw-callback-open-file() {
45 local open
46 case "${(L)OSTYPE}" in
47 linux*|*bsd*)
48 open="xdg-open"
49 ;;
50 darwin*)
51 open="open"
52 ;;
53 *)
54 # TODO: what is the best fallback?
55 open="xdg-open"
56 ;;
57 esac
58
59 # TODO: symlink to directory
60 if [[ -d "$1" ]]; then
61 zaw zaw-src-open-file "$1"
62 else
63 BUFFER="${open} ${(q)1}"
64 zle accept-line
65 fi
66 }