1#compdef todo.sh
2
3# See http://todotxt.com for todo.sh.
4#
5# Featurettes:
6#  - "replace" will complete the original text for editing
7#  - completing priorities will cycle through A to Z (even without
8#    menu completion)
9#  - list and listall will complete +<project> and @<where> from
10#    values in existing entries
11#  - will complete after + and @ if typed in message text
12
13setopt localoptions braceccl
14
15local expl curcontext="$curcontext" state line pri nextstate item
16local -a cmdlist itemlist match mbegin mend
17integer NORMARG
18
19_arguments -s -n : \
20  '-@[hide context names]' \
21  '-\+[hide project names]' \
22  '-c[color mode]' \
23  '-d[alternate config file]:config file:_files' \
24  '-f[force, no confirmation]' \
25  '-h[display help]' \
26  '-p[plain mode, no colours]' \
27  '-P[hide priority labels]' \
28  "-a[don't auto-archive tasks when done]" \
29  '-A[auto-archive tasks when done]' \
30  '-n[automatically remove blank lines]' \
31  '-N[preserve line numbers]' \
32  '-t[add current date to task on creation]' \
33  "-T[don't add current date to task]" \
34  '-v[verbose mode, confirmation messages]' \
35  '-vv[extra verbose (debug)]' \
36  '-V[display version etc.]' \
37  '-x[disable final filter]' \
38  '1:command:->commands' \
39  '*:arguments:->arguments' && return 0
40
41local projmsg="context or project"
42local txtmsg="text with contexts or projects"
43
44# Skip "command" as command prefix if words after
45if [[ $words[NORMARG] == command && NORMARG -lt CURRENT ]]; then
46  (( NORMARG++ ))
47fi
48
49case $state in
50  (commands)
51  cmdlist=(
52    "add:add TODO ITEM to todo.txt."
53    "addm:add TODO ITEMs, one per line, to todo.txt."
54    "addto:add text to file (not item)"
55    "append:adds to item on line NUMBER the text TEXT."
56    "archive:moves done items from todo.txt to done.txt."
57    "command:run internal commands only"
58    "del:deletes the item on line NUMBER in todo.txt."
59    "depri:remove prioritization from item"
60    "do:marks item on line NUMBER as done in todo.txt."
61    "help:display help"
62    "list:displays all todo items containing TERM(s), sorted by priority."
63    "listall:displays items including done ones containing TERM(s)"
64    "listcon:list all contexts"
65    "listfile:display all files in .todo directory"
66    "listpri:displays all items prioritized at PRIORITY."
67    "move:move item between files"
68    "prepend:adds to the beginning of the item on line NUMBER text TEXT."
69    "pri:adds or replace in NUMBER the priority PRIORITY (upper case letter)."
70    "replace:replace in NUMBER the TEXT."
71    "remdup:remove exact duplicates from todo.txt."
72    "report:adds the number of open and done items to report.txt."
73  )
74  _describe -t todo-commands 'todo.sh command' cmdlist
75  ;;
76
77  (arguments)
78  case $words[NORMARG] in
79    (append|command|del|move|mv|prepend|pri|replace|rm)
80    if (( NORMARG == CURRENT - 1 )); then
81      nextstate=item
82    else
83      case $words[NORMARG] in
84	(pri)
85	nextstate=pri
86	;;
87	(append|prepend)
88	nextstate=proj
89	;;
90	(move|mv)
91	nextstate=file
92	;;
93	(replace)
94	item=${words[CURRENT-1]##0##}
95	compadd -Q -- "${(qq)$(todo.sh -p list "^[ 0]*$item " | sed '/^--/,$d')##<-> (\([A-Z]\) |)}"
96	;;
97      esac
98    fi
99    ;;
100
101    (depri|do|dp)
102    nextstate=item
103    ;;
104
105    (a|add|addm|list|ls|listall|lsa)
106    nextstate=proj
107    ;;
108
109    (addto)
110    if (( NORMARG == CURRENT - 1 )); then
111      nextstate=file
112    else
113      nexstate=proj
114    fi
115    ;;
116
117    (listfile|lf)
118    if (( NORMARG == CURRENT -1 )); then
119      nextstate=file
120    else
121      _message "Term to search file for"
122    fi
123    ;;
124
125    (listpri|lsp)
126    nextstate=pri
127    ;;
128
129    (*)
130    return 1
131    ;;
132  esac
133  ;;
134esac
135
136case $nextstate in
137  (file)
138  _path_files -W ~/.todo
139  ;;
140
141  (item)
142  itemlist=(${${(M)${(f)"$(todo.sh -p list | sed '/^--/,$d')"}##<-> *}/(#b)(<->) (*)/${match[1]}:${match[2]}})
143  _describe -t todo-items 'todo item' itemlist
144  ;;
145
146  (pri)
147  if [[ $words[CURRENT] = (|[A-Z]) ]]; then
148    if [[ $words[CURRENT] = (|Z) ]]; then
149      pri=A
150    else
151      # cycle priority
152      pri=$words[CURRENT]
153      pri=${(#)$(( #pri + 1 ))}
154    fi
155    _wanted priority expl 'priority' compadd -U -S '' -- $pri
156  else
157    _wanted priority expl 'priority' compadd {A-Z}
158  fi
159  ;;
160
161  (proj)
162  # This completes stuff beginning with + (projects) or @ (contexts);
163  # these are todo.sh conventions.
164  if [[ ! -prefix + && ! -prefix @ ]]; then
165    projmsg=$txtmsg
166  fi
167  # In case there are quotes, ignore anything up to whitespace before
168  # the + or @ (which may not even be there yet).
169  compset -P '*[[:space:]]'
170  _wanted search expl $projmsg \
171    compadd $(todo.sh lsprj) $(todo.sh lsc)
172  ;;
173esac
174