summaryrefslogtreecommitdiff
path: root/alot/completion.py
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2012-09-18 20:28:07 +0100
committerPatrick Totzke <patricktotzke@gmail.com>2012-09-22 10:30:37 +0100
commit603901876ff1804a7fd516bf1f94ea67798074bf (patch)
treeb24c1267fd767a36e587e5f574364181b7a4ba24 /alot/completion.py
parenta90d06c4cac264ddc1e82fe3a4c265b45d2198e9 (diff)
make StringlistCompleter more flexible
add ability to match not only prefix but anywhere in a word; default to case-insensitive matching
Diffstat (limited to 'alot/completion.py')
-rw-r--r--alot/completion.py20
1 files changed, 16 insertions, 4 deletions
diff --git a/alot/completion.py b/alot/completion.py
index 0ad7702e..c2b0e18e 100644
--- a/alot/completion.py
+++ b/alot/completion.py
@@ -45,16 +45,27 @@ class Completer(object):
class StringlistCompleter(Completer):
"""completer for a fixed list of strings"""
- def __init__(self, resultlist):
+ def __init__(self, resultlist, ignorecase=True, match_anywhere=False):
"""
:param resultlist: strings used for completion
:type resultlist: list of str
+ :param liberal: match case insensitive and not prefix-only
+ :type liberal: bool
"""
self.resultlist = resultlist
+ self.flags = re.IGNORECASE if ignorecase else 0
+ self.match_anywhere = match_anywhere
def complete(self, original, pos):
pref = original[:pos]
- return [(a, len(a)) for a in self.resultlist if a.startswith(pref)]
+
+ re_prefix = '.*' if self.match_anywhere else ''
+
+ def match(s, m):
+ r = re_prefix + m + '.*'
+ return re.match(r, s, flags=self.flags) is not None
+
+ return [(a, len(a)) for a in self.resultlist if match(a, pref)]
class MultipleSelectionCompleter(Completer):
@@ -251,9 +262,10 @@ class ArgparseOptionCompleter(Completer):
class AccountCompleter(StringlistCompleter):
"""completes users' own mailaddresses"""
- def __init__(self):
+ def __init__(self, **kwargs):
resultlist = settings.get_main_addresses()
- StringlistCompleter.__init__(self, resultlist)
+ StringlistCompleter.__init__(self, resultlist, match_anywhere=True,
+ **kwargs)
class CommandNameCompleter(Completer):