_task 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #compdef task
  2. #
  3. # Copyright 2010 - 2015 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. # http://www.opensource.org/licenses/mit-license.php
  25. #
  26. typeset -g _task_cmds _task_projects _task_tags _task_config _task_modifiers
  27. _task_projects=($(task _projects))
  28. _task_tags=($(task _tags))
  29. _task_ids=($(task _ids))
  30. _task_zshids=( ${(f)"$(task _zshids)"} )
  31. _task_config=($(task _config))
  32. _task_columns=($(task _columns))
  33. _task_modifiers=(
  34. 'before' \
  35. 'after' \
  36. 'none' \
  37. 'any' \
  38. 'is' \
  39. 'isnt' \
  40. 'has' \
  41. 'hasnt' \
  42. 'startswith' \
  43. 'endswith' \
  44. 'word' \
  45. 'noword'
  46. )
  47. _task_conjunctions=(
  48. 'and' \
  49. 'or' \
  50. 'xor' \
  51. '\)' \
  52. '\(' \
  53. '<' \
  54. '<=' \
  55. '=' \
  56. '!=' \
  57. '>=' \
  58. '>'
  59. )
  60. _task_cmds=($(task _commands; task _aliases))
  61. _task_zshcmds=( ${(f)"$(task _zshcommands)"} sentinel:sentinel:sentinel )
  62. _task_aliases=($(task _aliases))
  63. _task() {
  64. _arguments -s -S \
  65. "*::task default:_task_default"
  66. return 0
  67. }
  68. local -a reply args word
  69. word=$'[^\0]#\0'
  70. # priorities
  71. local -a task_priorities
  72. _regex_words values 'task priorities' \
  73. 'H:High' \
  74. 'M:Middle' \
  75. 'L:Low'
  76. task_priorities=("$reply[@]")
  77. # projects
  78. local -a task_projects
  79. task_projects=(
  80. /"$word"/
  81. ":values:task projects:compadd -a _task_projects"
  82. )
  83. local -a _task_dates
  84. _regex_words values 'task dates' \
  85. 'tod*ay:Today' \
  86. 'yes*terday:Yesterday' \
  87. 'tom*orrow:Tomorrow' \
  88. 'sow:Start of week' \
  89. 'soww:Start of work week' \
  90. 'socw:Start of calendar week' \
  91. 'som:Start of month' \
  92. 'soq:Start of quarter' \
  93. 'soy:Start of year' \
  94. 'eow:End of week' \
  95. 'eoww:End of work week' \
  96. 'eocw:End of calendar week' \
  97. 'eom:End of month' \
  98. 'eoq:End of quarter' \
  99. 'eoy:End of year' \
  100. 'mon:Monday' \
  101. 'tue:Tuesday'\
  102. 'wed:Wednesday' \
  103. 'thu:Thursday' \
  104. 'fri:Friday' \
  105. 'sat:Saturday' \
  106. 'sun:Sunday' \
  107. 'good*friday:Good Friday' \
  108. 'easter:Easter' \
  109. 'eastermonday:Easter Monday' \
  110. 'ascension:Ascension' \
  111. 'pentecost:Pentecost' \
  112. 'midsommar:Midsommar' \
  113. 'midsommarafton:Midsommarafton' \
  114. 'later:Later' \
  115. 'someday:Some Day'
  116. _task_dates=("$reply[@]")
  117. local -a _task_reldates
  118. _regex_words values 'task reldates' \
  119. 'hrs:n hours' \
  120. 'day:n days' \
  121. '1st:first' \
  122. '2nd:second' \
  123. '3rd:third' \
  124. 'th:4th, 5th, etc.' \
  125. 'wks:weeks'
  126. _task_reldates=("$reply[@]")
  127. task_dates=(
  128. \( "$_task_dates[@]" \|
  129. \( /$'[0-9][0-9]#'/- \( "$_task_reldates[@]" \) \)
  130. \)
  131. )
  132. local -a task_zshids
  133. _regex_words values 'task IDs' $_task_zshids
  134. task_zshids=("$reply[@]")
  135. _regex_words values 'task frequencies' \
  136. 'daily:Every day' \
  137. 'day:Every day' \
  138. 'weekdays:Every day skipping weekend days' \
  139. 'weekly:Every week' \
  140. 'biweekly:Every two weeks' \
  141. 'fortnight:Every two weeks' \
  142. 'monthly:Every month' \
  143. 'quarterly:Every three months' \
  144. 'semiannual:Every six months' \
  145. 'annual:Every year' \
  146. 'yearly:Every year' \
  147. 'biannual:Every two years' \
  148. 'biyearly:Every two years'
  149. _task_freqs=("$reply[@]")
  150. local -a _task_frequencies
  151. _regex_words values 'task frequencies' \
  152. 'd:days' \
  153. 'w:weeks' \
  154. 'q:quarters' \
  155. 'y:years'
  156. _task_frequencies=("$reply[@]")
  157. task_freqs=(
  158. \( "$_task_freqs[@]" \|
  159. \( /$'[0-9][0-9]#'/- \( "$_task_frequencies[@]" \) \)
  160. \)
  161. )
  162. # attributes
  163. local -a task_attributes
  164. _regex_words -t ':' default 'task attributes' \
  165. 'des*cription:Task description text' \
  166. 'status:Status of task - pending, completed, deleted, waiting' \
  167. 'pro*ject:Project name:$task_projects' \
  168. 'pri*ority:priority:$task_priorities' \
  169. 'du*e:Due date:$task_dates' \
  170. 're*cur:Recurrence frequency:$task_freqs' \
  171. 'un*til:Expiration date:$task_dates' \
  172. 'li*mit:Desired number of rows in report' \
  173. 'wa*it:Date until task becomes pending:$task_dates' \
  174. 'ent*ry:Date task was created:$task_dates' \
  175. 'end:Date task was completed/deleted:$task_dates' \
  176. 'st*art:Date task was started:$task_dates' \
  177. 'sc*heduled:Date task is scheduled to start:$task_dates' \
  178. 'dep*ends:Other tasks that this task depends upon:$task_zshids'
  179. task_attributes=("$reply[@]")
  180. args=(
  181. \( "$task_attributes[@]" \|
  182. \( /'(project|description|status|entry|end|start|scheduled|depends|due|wait|recur|priority|until|limit).'/- \( /$'[^:]#:'/ ":default:modifiers:compadd -S ':' -a _task_modifiers" \) \) \|
  183. \( /'(rc).'/- \( /$'[^:]#:'/ ":arguments:config:compadd -S ':' -a _task_config" \) \) \|
  184. \( /'(+|-)'/- \( /"$word"/ ":values:remove tag:compadd -a _task_tags" \) \) \|
  185. \( /"$word"/ \)
  186. \) \#
  187. )
  188. _regex_arguments _task_attributes "${args[@]}"
  189. ## task commands
  190. # filter completion
  191. (( $+functions[_task_filter] )) ||
  192. _task_filter() {
  193. _task_attributes "$@"
  194. # TODO complete conjunctions only if the previous word is a filter expression, i.e. attribute, ID, any non-command
  195. _describe -t default 'task conjunctions' _task_conjunctions
  196. }
  197. # execute completion
  198. (( $+functions[_task_execute] )) ||
  199. _task_execute() {
  200. _files
  201. }
  202. # id-only completion
  203. (( $+functions[_task_id] )) ||
  204. _task_id() {
  205. _describe -t values 'task IDs' _task_zshids
  206. }
  207. # subcommand-only function
  208. (( $+functions[_task_subcommands] )) ||
  209. _task_subcommands() {
  210. local -a subcommands
  211. local _zshcmd
  212. local cmd category desc
  213. local lastcategory=''
  214. # The list is sorted by category, in the right order.
  215. for _zshcmd in "$_task_zshcmds[@]"; do
  216. # Parse out the three fields
  217. cmd=${_zshcmd%%:*}
  218. category=${${_zshcmd#*:}%%:*}
  219. desc=${_zshcmd#*:*:}
  220. # Present each category as soon as the first entry in the *next* category
  221. # is seen.
  222. if [[ $category != $lastcategory && -n $lastcategory ]]; then
  223. _describe -t ${lastcategory}-commands "task ${lastcategory} command" subcommands
  224. subcommands=()
  225. fi
  226. # Log the subcommand; we will process it in some future iteration.
  227. subcommands+=( "$cmd:$desc" )
  228. lastcategory=$category
  229. done
  230. }
  231. ## first level completion => task sub-command completion
  232. (( $+functions[_task_default] )) ||
  233. _task_default() {
  234. local cmd ret=1
  235. integer i=1
  236. while (( i < $#words ))
  237. do
  238. cmd="${_task_cmds[(r)$words[$i]]}"
  239. if (( $#cmd )); then
  240. _call_function ret _task_${cmd} ||
  241. _call_function ret _task_filter ||
  242. _message "No command remaining."
  243. return ret
  244. fi
  245. (( i++ ))
  246. done
  247. # update IDs
  248. _task_zshids=( ${(f)"$(task _zshids)"} )
  249. _task_subcommands
  250. _describe -t tasks 'task IDs' _task_zshids
  251. _describe -t aliases 'task aliases' _task_aliases
  252. _call_function ret _task_filter
  253. return ret
  254. }
  255. _task "$@"