about summary refs log tree commit diff
path: root/Completion/Darwin/Command/_open
diff options
context:
space:
mode:
Diffstat (limited to 'Completion/Darwin/Command/_open')
-rw-r--r--Completion/Darwin/Command/_open45
1 files changed, 37 insertions, 8 deletions
diff --git a/Completion/Darwin/Command/_open b/Completion/Darwin/Command/_open
index 9478dd562..3926276c6 100644
--- a/Completion/Darwin/Command/_open
+++ b/Completion/Darwin/Command/_open
@@ -1,12 +1,41 @@
 #compdef open
 
-local curcontext="$curcontext" state line
+_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 /
+}
 
-_arguments -C \
-  '-a[specify application]:application:->open_mac_applications' \
-  '-e[open with TextEdit]' \
-  '*:files:_webbrowser'
+_open() {
+  local curcontext="$curcontext" state line expl
 
-if [[ -n "$state" ]]; then
-  _mac_applications || _files
-fi
+  _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 "$@"