about summary refs log tree commit diff
path: root/Completion/Darwin/Command/_open
blob: 3926276c6366bcbadb71151e8a859e97269ce887 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#compdef open

_open_absolute_application_path() {
  local expl curcontext
  zstyle -T ":completion:${curcontext}:files" prefix-needed && \
    [[ "$PREFIX" != [/~]* && compstate[nmatches] -ne 0 ]] && return 1
  _wanted files expl 'application file' _path_files -P "$PREFIX[1]" -W /
}

_open() {
  local curcontext="$curcontext" state line expl

  _arguments -C \
    '-a[specify application]: :->open_mac_applications' \
    '-e[open with TextEdit]' \
    '-f[Reads input from standard input and opens with TextEdit]' \
    '*: :->open_files'

  case "$state" in
    open_mac_applications)
      _alternative \
        "commands: :_mac_applications" \
        "files:: _open_absolute_application_path"
      ;;
    open_files)
      local app
      if [[ -n "$words[(r)-a]" ]]; then
        app="${(Q)words[words[(i)-a] + 1]}"
      elif [[ -n "$words[(r)-e]" || -n "$words[(r)-f]" ]]; then
        app="Text Edit"
      fi
      if [[ -n "$app" ]]; then
        _wanted files expl "file for $app" _mac_files_for_application "$app"
      else
        _webbrowser
      fi
      ;;
  esac
}

_open "$@"