about summary refs log tree commit diff
path: root/Completion/Unix/Command/_todo.sh
blob: fc984f7d43ff3da50932eae46ff4d28264e24f12 (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
#compdef todo.sh

# See http://todotxt.com for todo.sh.
#
# Featurettes:
#  - "replace" will complete the original text for editing.
#  - completing priorities will cycle through A to Z (even without
#    menu completion).

setopt localoptions braceccl

local expl curcontext="$curcontext" state line pri nextstate
local -a cmdlist itemlist
integer NORMARG

_arguments -s -n : \
  '-d[alternate config file]:config file:_files' \
  '-f[force, no confirmation]' \
  '-h[display help]' \
  '-p[plain mode, no colours]' \
  '-v[verbose mode, confirmation messages]' \
  '-V[display version etc.]' \
  '1:command:->commands' \
  '*:arguments:->arguments' && return 0

local txtmsg="text, can include p:<project> and @<where>"

case $state in
  (commands)
  cmdlist=(
    "add:Add TODO ITEM to todo.txt."
    "append:Adds to item on line NUMBER the text TEXT."
    "archive:Moves done items from todo.txt to done.txt."
    "del:Deletes the item on line NUMBER in todo.txt."
    "do:Marks item on line NUMBER as done in todo.txt."
    "list:Displays all todo items containing TERM(s), sorted by priority."
    "listall:Displays items including done ones containing TERM(s)"
    "listpri:Displays all items prioritized at PRIORITY."
    "prepend:Adds to the beginning of the item on line NUMBER text TEXT."
    "pri:Adds or replace in NUMBER the priority PRIORITY (upper case letter)."
    "replace:Replace in NUMBER the TEXT."
    "remdup:Remove exact duplicates from todo.txt."
    "report:Adds the number of open and done items to report.txt."
  )
  _describe -t todo-commands 'todo.sh command' cmdlist
  ;;

  (arguments)
  case $words[NORMARG] in
    (append|del|do|prepend|pri|replace)
    if (( NORMARG == CURRENT - 1 )); then
      itemlist=(${${(M)${(f)"$(todo.sh list)"}##<-> *}/(#b)(<->) (*)/${match[1]}:${match[2]}})
      _describe -t todo-items 'todo item' itemlist
    else
      case $words[NORMARG] in
	(pri)
	nextstate=pri
	;;
	(append|prepend)
	_message $txtmsg
	;;
	(replace)
	compadd -Q -- "${(qq)$(todo.sh list "^0*${words[CURRENT-1]} ")##<-> }"
	;;
      esac
    fi
    ;;

    (add)
    _message $txtmsg
    ;;

    (list|listall)
    # This completes stuff beginning with p: (projects) or @ (contexts);
    # these are todo.sh conventions.
    _wanted search expl 'context or project' \
      compadd ${${=${${(M)${(f)"$(todo.sh list)"}##<-> *}##<-> }}:#^(p:*|@*)}
    ;;

    (listpri)
    nextstate=pri
    ;;

    (*)
    return 1
    ;;
  esac
  ;;
esac

case $nextstate in
  (pri)
  if [[ $words[CURRENT] = (|[A-Z]) ]]; then
    if [[ $words[CURRENT] = (|Z) ]]; then
      pri=A
    else
      # cycle priority
      pri=$words[CURRENT]
      pri=${(#)$(( #pri + 1 ))}
    fi
    _wanted priority expl 'priority' compadd -U -S '' -- $pri
  else
    _wanted priority expl 'priority' compadd {A-Z}
  fi
  ;;
esac