diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2012-09-01 12:59:42 +0100 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2012-09-03 22:14:09 +0100 |
commit | 2d0d9baf55e9cbcc2c952fdc40dac25f50946017 (patch) | |
tree | 6bd3d1dba74dc147434014713bc94b008cf70f17 /alot/completion.py | |
parent | 4650909ea9ab273d047c323b9febd3802ce370ef (diff) |
Add CommandLineCompleter
that completes semi-colon separated sequences of command strings
Diffstat (limited to 'alot/completion.py')
-rw-r--r-- | alot/completion.py | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/alot/completion.py b/alot/completion.py index e4e07645..02b33605 100644 --- a/alot/completion.py +++ b/alot/completion.py @@ -11,6 +11,7 @@ import alot.commands as commands from alot.buffers import EnvelopeBuffer from alot.settings import settings from alot.utils.booleanaction import BooleanAction +from alot.helper import split_commandline class Completer(object): @@ -256,7 +257,7 @@ class AccountCompleter(StringlistCompleter): class CommandNameCompleter(Completer): - """completes commands""" + """completes command names""" def __init__(self, mode): """ @@ -275,8 +276,8 @@ class CommandNameCompleter(Completer): return [(t, len(t)) for t in matching] -class CommandLineCompleter(Completer): - """completion for commandline""" +class CommandCompleter(Completer): + """completes one command consisting of command name and parameters""" def __init__(self, dbman, mode, currentbuffer=None): """ @@ -404,6 +405,53 @@ class CommandLineCompleter(Completer): return res +class CommandLineCompleter(Completer): + """completes command lines: semicolon separated command strings""" + + def __init__(self, dbman, mode, currentbuffer=None): + """ + :param dbman: used to look up avaliable tagstrings + :type dbman: :class:`~alot.db.DBManager` + :param mode: mode identifier + :type mode: str + :param currentbuffer: currently active buffer. If defined, this will be + used to dynamically extract possible completion + strings + :type currentbuffer: :class:`~alot.buffers.Buffer` + """ + self._commandcompleter = CommandCompleter(dbman, mode, currentbuffer) + + def get_context(self, line, pos): + """ + computes start and end position of substring of line that is the + command string under given position + """ + commands = split_commandline(line) + [''] + i = 0 + start = 0 + end = len(commands[i]) + while pos > end: + i += 1 + start = end + 1 + end += 1 + len(commands[i]) + return start, end + + def complete(self, line, pos): + cstart, cend = self.get_context(line, pos) + before = line[:cstart] + after = line[cend:] + cmdstring = line[cstart:cend] + cpos = pos - cstart + + res = [] + for ccmd, ccpos in self._commandcompleter.complete(cmdstring, cpos): + logging.debug((before, cmdstring, after, ccmd)) + newtext = before + ccmd + after + newpos = pos + (ccpos - cpos) + res.append((newtext, newpos)) + return res + + class PathCompleter(Completer): """completion for paths""" def complete(self, original, pos): |