about summary refs log tree commit diff
path: root/Completion/User/_urls
blob: 265fadae8904b56020232085b3392bb5a614f611 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#compdef curl

# Usage: _urls [-f]
# Options:
#  -f : complete files first.
#
# Configuration styles used:
#
#  urls:path
#    The path to a directory containing a URL database, such as:
#
#      % cd ~/.zsh/urls
#      % find . -ls
#      ... drwxr-xr-x ... 512 Sep  3 02:46 .
#      ... drwxr-xr-x ... 512 Sep  3 02:48 ./http
#      ... drwxr-xr-x ... 512 Sep  3 02:52 ./http/www.zsh.org
#      ... drwxr-xr-x ... 512 Sep  3 03:01 ./http/www.zsh.org/mla
#      ... drwxr-xr-x ... 512 Sep  3 03:01 ./http/www.zsh.org/mla/workers
#      ... drwxr-xr-x ... 512 Sep  3 03:01 ./http/www.zsh.org/mla/workers/1999
#      ... -rw-r--r-- ...   0 Sep  3 03:01 ./http/www.zsh.org/mla/workers/1999/index.html
#      ... drwxr-xr-x ... 512 Sep  3 02:48 ./http/sunsite.auc.dk
#      ... drwxr-xr-x ... 512 Sep  3 02:48 ./http/sunsite.auc.dk/zsh
#      ... drwxr-xr-x ... 512 Sep  3 02:47 ./bookmark
#      ... drwxr-xr-x ... 512 Sep  3 02:48 ./bookmark/zsh
#      ... -rw-r--r-- ...  27 Sep  3 02:47 ./bookmark/zsh/home
#      ... -rw-r--r-- ...  20 Sep  3 02:48 ./bookmark/zsh/meta
#      % cat bookmark/zsh/home 
#      http://sunsite.auc.dk/zsh/
#      % cat bookmark/zsh/meta
#      http://www.zsh.org/
#
#  urls:local
#    Specify a local web server as an array with three elements:
#      <hostname> <doc root> <user area>
#    where hostname is the name of the web server, doc root is the path to
#    the default web pages for the server and user area is the directory
#    name used by a user placing web pages within their home area.
#    E.g.:
#      compstyle '*:urls:local' www /usr/local/apache/htdocs public_html

local ipre scheme host user uhosts ret=1 expl
local urls_path localhttp
_style -s urls path urls_path || urls_path="${ZDOTDIR:-$HOME}/.zsh/urls"
_style -a urls local localhttp
local localhttp_servername="$localhttp[1]"
local localhttp_documentroot="$localhttp[2]"
local localhttp_userdir="$localhttp[3]"

if [[ "$1" = -f ]]; then
  shift
  _tags -C -f files && _files "$@" && return
fi

ipre="$IPREFIX"

if ! compset -P '(#b)([-+.a-z0-9]#):' &&
   _wanted -C argument prefixes expl 'URL prefix'; then
  [[ -d $urls_path/bookmark ]] &&
    compadd "$@" "$expl[@]" -S '' bookmark: && ret=0
  compadd "$@" "$expl[@]" -S '' file: ftp:// gopher:// http:// && ret=0
  return ret
fi
scheme="$match[1]"

case "$scheme" in
  http|ftp|gopher)
    if ! compset -P //; then
      _wanted -C "$scheme" prefixes expl 'end of prefix' &&
          compadd "$expl[@]" "$@" -S '' //
      return
    fi
  ;;
  file)
    if ! compset -P //; then
      _wanted -C file files expl 'local file' || return 1

      if [[ -prefix / ]]; then
	_path_files "$expl[@]" "$@" -S '' -g '*(^/)' && ret=0
	_path_files "$expl[@]" "$@" -S/ -r '/' -/ && ret=0
      elif [[ -z "$PREFIX" ]]; then
	compadd "$expl[@]" -S '/' -r '/' - "${PWD%/}" && ret=0
      fi
      return ret
    fi
  ;;
  bookmark)
    if [[ -f "$urls_path/$scheme/$PREFIX$SUFFIX" &&
	  -s "$urls_path/$scheme/$PREFIX$SUFFIX" ]]; then
      _wanted -C bookmark bookmarks expl bookmarks &&
          compadd "$expl[@]" "$@" -QU - \
              "$ipre$(<"$urls_path/$scheme/$PREFIX$SUFFIX")" && ret=0
    else
      if _wanted -C bookmark files expl 'bookmark'; then
        _path_files -W "$urls_path/$scheme" "$expl[@]" -S '' -g '*(^/)' && 
            ret=0
        _path_files -W "$urls_path/$scheme" -S/ -r '/' -/ && ret=0
      fi
    fi
    return ret
  ;;
esac

# Complete hosts
if ! compset -P '(#b)([^/]#)/' &&
   _wanted hosts expl host; then
  uhosts=($urls_path/$scheme/$PREFIX*$SUFFIX(/:t))
  (( $#uhosts )) || _hosts -S/ && ret=0
  [[ "$scheme" = http ]] && uhosts=($uhosts $localhttp_servername)
  compadd "$expl[@]" "$@" -QS/ - $uhosts && ret=0
  return ret
fi
host="$match[1]"

# Complete part after hostname

_wanted -C local files expl 'local file' || return 1

if [[ "$localhttp_servername" = "$host" ]]; then
  if compset -P \~; then
    if ! compset -P '(#b)([^/]#)/'; then
      _users -S/
      return
    fi
    user="$match[1]"
    _path_files "$expl[@]" -W ~$user/$localhttp_userdir -g '*(^/)' && ret=0
    _path_files "$expl[@]" -W ~$user/$localhttp_userdir -S/ -r '/' -/ && ret=0
  else
    _path_files "$expl[@]" -W $localhttp_documentroot -g '*(^/)' && ret=0
    _path_files "$expl[@]" -W $localhttp_documentroot -S/ -r '/' -/ && ret=0
  fi
else
  _path_files "$expl[@]" -W $urls_path/$scheme/$host -g '*(^/)' && ret=0
  _path_files "$expl[@]" -W $urls_path/$scheme/$host -S/ -r '/' -/ && ret=0
fi
return $ret