summaryrefslogtreecommitdiff
path: root/alot/completion.py
blob: 716554c3b6e0aa98c601614fd5598f1699c0dcae (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
This file is part of alot.

Alot is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

Notmuch is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with notmuch.  If not, see <http://www.gnu.org/licenses/>.

Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com>
"""

import re
import os

import command


class Completer:
    def complete(self, original):
        """takes a string that's the prefix of a word,
        returns a list of suffix-strings that complete the original"""
        return list()


class QueryCompleter(Completer):
    """completion for a notmuch query string"""
    # TODO: boolean connectors and braces?
    def __init__(self, dbman):
        self.dbman = dbman
        self._contactscompleter = ContactsCompleter()
        self._tagscompleter = TagsCompleter(dbman)
        self.keywords = ['tag', 'from', 'to', 'subject', 'attachment',
                         'is', 'id', 'thread', 'folder']

    def complete(self, original):
        prefix = original.split(' ')[-1]
        m = re.search('(tag|is|to):(\w*)', prefix)
        if m:
            cmd, params = m.groups()
            if cmd == 'to':
                return self._contactscompleter.complete(params)
            else:
                return self._tagscompleter.complete(params, last=True)
        else:
            plen = len(prefix)
            matched = filter(lambda t: t.startswith(prefix), self.keywords)
            return [t[plen:] + ':' for t in matched]


class TagsCompleter(Completer):
    """completion for a comma separated list of tagstrings"""

    def __init__(self, dbman):
        self.dbman = dbman

    def complete(self, original, last=False):
        otags = original.split(',')
        prefix = otags[-1]
        tags = self.dbman.get_all_tags()
        if len(otags) > 1 and last:
            return []
        else:
            matching = [t[len(prefix):] for t in tags if t.startswith(prefix)]
            if last:
                return matching
            else:
                return [t + ',' for t in matching]


class ContactsCompleter(Completer):
    """completes contacts"""

    def complete(self, prefix):
        # TODO
        return []


class AccountCompleter(Completer):
    """completes own mailaddresses"""

    def __init__(self, accountman):
        self.accountman = accountman

    def complete(self, prefix):
        valids = self.accountman.get_main_addresses()
        return [a[len(prefix):] for a in valids if a.startswith(prefix)]


class CommandCompleter(Completer):
    """completes commands"""

    def __init__(self, dbman, mode):
        self.dbman = dbman
        self.mode = mode

    def complete(self, original):
        #TODO refine <tab> should get current querystring
        cmdlist = command.COMMANDS['global']
        cmdlist.update(command.COMMANDS[self.mode])
        olen = len(original)
        return [t[olen:] + '' for t in cmdlist if t.startswith(original)]


class CommandLineCompleter(Completer):
    """completion for commandline"""

    def __init__(self, dbman, accountman, mode):
        self.dbman = dbman
        self.accountman = accountman
        self.mode = mode
        self._commandcompleter = CommandCompleter(dbman, mode)
        self._querycompleter = QueryCompleter(dbman)
        self._tagscompleter = TagsCompleter(dbman)
        self._contactscompleter = ContactsCompleter()
        self._pathcompleter = PathCompleter()

    def complete(self, prefix):
        words = prefix.split(' ', 1)
        if len(words) <= 1:  # we complete commands
            return self._commandcompleter.complete(prefix)
        else:
            cmd, params = words
            if cmd in ['search', 'refine']:
                return self._querycompleter.complete(params)
            if cmd == 'retag':
                return self._tagscompleter.complete(params)
            if cmd == 'toggletag':
                return self._tagscompleter.complete(params, last=True)
            if cmd in ['to', 'compose']:
                return self._contactscompleter.complete(params)
            if cmd in ['attach', 'edit', 'save']:
                return self._pathcompleter.complete(params)
            else:
                return []


class PathCompleter(Completer):
    """completion for paths"""
    def complete(self, prefix):
        if not prefix:
            return ['~/']
        dir = os.path.expanduser(os.path.dirname(prefix))
        fileprefix = os.path.basename(prefix)
        res = []
        if os.path.isdir(dir):
            for f in os.listdir(dir):
                if f.startswith(fileprefix):
                    res.append(f[len(fileprefix):])
        return res