1#compdef task
2#
3# Copyright 2010 - 2019 Johannes Schlatow
4# Copyright 2009 P.C. Shyamshankar
5#
6# Permission is hereby granted, free of charge, to any person obtaining a copy
7# of this software and associated documentation files (the "Software"), to deal
8# in the Software without restriction, including without limitation the rights
9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10# copies of the Software, and to permit persons to whom the Software is
11# furnished to do so, subject to the following conditions:
12#
13# The above copyright notice and this permission notice shall be included
14# in all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22# SOFTWARE.
23#
24# https://www.opensource.org/licenses/mit-license.php
25#
26typeset -g _task_cmds _task_projects _task_tags _task_config _task_modifiers
27_task_projects=(${(f)"$(task _projects)"})
28_task_tags=($(task _tags))
29_task_zshids=( ${(f)"$(task _zshids)"} )
30_task_config=($(task _config))
31_task_columns=($(task _columns))
32_task_modifiers=(
33  'before' \
34  'after' \
35  'none' \
36  'any' \
37  'is' \
38  'isnt' \
39  'has' \
40  'hasnt' \
41  'startswith' \
42  'endswith' \
43  'word' \
44  'noword'
45)
46_task_conjunctions=(
47  'and' \
48  'or' \
49  'xor' \
50  '\)' \
51  '\(' \
52  '<' \
53  '<=' \
54  '=' \
55  '!=' \
56  '>=' \
57  '>'
58)
59_task_cmds=($(task _commands; task _aliases))
60_task_zshcmds=( ${(f)"$(task _zshcommands)"} sentinel:sentinel:sentinel )
61
62_task_aliases=($(task _aliases))
63
64_task() {
65    _arguments -s -S \
66        "*::task default:_task_default"
67    return 0
68}
69
70local -a reply args word
71word=$'[^\0]#\0'
72
73# priorities
74local -a task_priorities
75_regex_words values 'task priorities' \
76  'H:High' \
77  'M:Middle' \
78  'L:Low'
79task_priorities=("$reply[@]")
80
81# projects
82local -a task_projects
83task_projects=(
84  /"$word"/
85  ":values:task projects:compadd -a _task_projects"
86)
87
88local -a _task_dates
89_regex_words values 'task dates' \
90  'tod*ay:Today' \
91  'yes*terday:Yesterday' \
92  'tom*orrow:Tomorrow' \
93  'sow:Start of week' \
94  'soww:Start of work week' \
95  'socw:Start of calendar week' \
96  'som:Start of month' \
97  'soq:Start of quarter' \
98  'soy:Start of year' \
99  'eow:End of week' \
100  'eoww:End of work week' \
101  'eocw:End of calendar week' \
102  'eom:End of month' \
103  'eoq:End of quarter' \
104  'eoy:End of year' \
105  'mon:Monday' \
106  'tue:Tuesday'\
107  'wed:Wednesday' \
108  'thu:Thursday' \
109  'fri:Friday' \
110  'sat:Saturday' \
111  'sun:Sunday' \
112  'good*friday:Good Friday' \
113  'easter:Easter' \
114  'eastermonday:Easter Monday' \
115  'ascension:Ascension' \
116  'pentecost:Pentecost' \
117  'midsommar:Midsommar' \
118  'midsommarafton:Midsommarafton' \
119  'later:Later' \
120  'someday:Some Day'
121_task_dates=("$reply[@]")
122
123local -a _task_reldates
124_regex_words values 'task reldates' \
125  'hrs:n hours' \
126  'day:n days' \
127  '1st:first' \
128  '2nd:second' \
129  '3rd:third' \
130  'th:4th, 5th, etc.' \
131  'wks:weeks'
132_task_reldates=("$reply[@]")
133
134task_dates=(
135  \( "$_task_dates[@]" \|
136    \( /$'[0-9][0-9]#'/- \( "$_task_reldates[@]" \) \)
137  \)
138)
139
140local -a task_zshids
141if (( $#_task_zshids )); then
142  _regex_words values 'task IDs' $_task_zshids
143  task_zshids=("$reply[@]")
144fi
145
146_regex_words values 'task frequencies' \
147  'daily:Every day' \
148  'day:Every day' \
149  'weekdays:Every day skipping weekend days' \
150  'weekly:Every week' \
151  'biweekly:Every two weeks' \
152  'fortnight:Every two weeks' \
153  'monthly:Every month' \
154  'quarterly:Every three months' \
155  'semiannual:Every six months' \
156  'annual:Every year' \
157  'yearly:Every year' \
158  'biannual:Every two years' \
159  'biyearly:Every two years'
160_task_freqs=("$reply[@]")
161
162local -a _task_frequencies
163_regex_words values 'task frequencies' \
164  'd:days' \
165  'w:weeks' \
166  'q:quarters' \
167  'y:years'
168_task_frequencies=("$reply[@]")
169
170task_freqs=(
171  \( "$_task_freqs[@]" \|
172     \( /$'[0-9][0-9]#'/- \( "$_task_frequencies[@]" \) \)
173  \)
174)
175
176# attributes
177local -a task_attributes
178_regex_words -t ':' default 'task attributes' \
179  'des*cription:Task description text' \
180  'status:Status of task - pending, completed, deleted, waiting' \
181  'pro*ject:Project name:$task_projects' \
182  'pri*ority:priority:$task_priorities' \
183  'du*e:Due date:$task_dates' \
184  're*cur:Recurrence frequency:$task_freqs' \
185  'un*til:Expiration date:$task_dates' \
186  'li*mit:Desired number of rows in report' \
187  'wa*it:Date until task becomes pending:$task_dates' \
188  'ent*ry:Date task was created:$task_dates' \
189  'end:Date task was completed/deleted:$task_dates' \
190  'st*art:Date task was started:$task_dates' \
191  'sc*heduled:Date task is scheduled to start:$task_dates' \
192  'dep*ends:Other tasks that this task depends upon:$task_zshids'
193task_attributes=("$reply[@]")
194
195args=(
196  \( "$task_attributes[@]" \|
197  \( /'(project|description|status|entry|end|start|scheduled|depends|due|wait|recur|priority|until|limit).'/- \( /$'[^:]#:'/ ":default:modifiers:compadd -S ':' -a _task_modifiers" \) \) \|
198  \( /'(rc).'/- \( /$'[^:]#:'/ ":arguments:config:compadd -S ':' -a _task_config" \) \) \|
199  \( /'(+|-)'/- \( /"$word"/ ":values:remove tag:compadd -a _task_tags" \) \) \|
200  \( /"$word"/ \)
201  \) \#
202)
203_regex_arguments _task_attributes "${args[@]}"
204
205## task commands
206
207# filter completion
208(( $+functions[_task_filter] )) ||
209_task_filter() {
210  _task_attributes "$@"
211
212  # TODO complete conjunctions only if the previous word is a filter expression, i.e. attribute, ID, any non-command
213  _describe -t default 'task conjunctions' _task_conjunctions
214}
215
216# execute completion
217(( $+functions[_task_execute] )) ||
218_task_execute() {
219  _files
220}
221
222# id-only completion
223(( $+functions[_task_id] )) ||
224_task_id() {
225  _describe -t values 'task IDs' _task_zshids
226}
227
228# subcommand-only function
229(( $+functions[_task_subcommands] )) ||
230_task_subcommands() {
231  local -a subcommands
232  local _zshcmd
233  local cmd category desc
234  local lastcategory=''
235  # The list is sorted by category, in the right order.
236  for _zshcmd in "$_task_zshcmds[@]"; do
237    # Parse out the three fields
238    cmd=${_zshcmd%%:*}
239    category=${${_zshcmd#*:}%%:*}
240    desc=${_zshcmd#*:*:}
241
242    # Present each category as soon as the first entry in the *next* category
243    # is seen.
244    if [[ $category != $lastcategory && -n $lastcategory ]]; then
245      _describe -t ${lastcategory}-commands "task ${lastcategory} command" subcommands
246      subcommands=()
247    fi
248
249    # Log the subcommand; we will process it in some future iteration.
250    subcommands+=( "$cmd:$desc" )
251
252    lastcategory=$category
253  done
254}
255
256## first level completion => task sub-command completion
257(( $+functions[_task_default] )) ||
258_task_default() {
259    local cmd ret=1
260
261  integer i=1
262  while (( i < $#words ))
263  do
264    cmd="${_task_cmds[(r)$words[$i]]}"
265    if (( $#cmd )); then
266      _call_function ret _task_${cmd} ||
267        _call_function ret _task_filter ||
268          _message "No command remaining."
269      return ret
270    fi
271    (( i++ ))
272  done
273
274  # update IDs
275  _task_zshids=( ${(f)"$(task _zshids)"} )
276
277  _task_subcommands
278  _describe -t tasks 'task IDs' _task_zshids
279  _describe -t aliases 'task aliases' _task_aliases
280  _call_function ret _task_filter
281
282  return ret
283}
284
285_task "$@"
286