summaryrefslogtreecommitdiff
path: root/alot/completion/commandline.py
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2019-08-17 10:00:30 +0100
committerPatrick Totzke <patricktotzke@gmail.com>2019-08-17 11:10:37 +0100
commitd8d1429ec3daf8f2a67ffccbc2aa1d54eb3639c6 (patch)
tree3f13fd449bacd97432e18ba4fb0fb4b19ac0b5ed /alot/completion/commandline.py
parentb5e612c69b625271424b626da24d941ddbe39391 (diff)
refactor prompt completion
This just splits the file completion.py into several files, one for each Completer subclass.
Diffstat (limited to 'alot/completion/commandline.py')
-rw-r--r--alot/completion/commandline.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/alot/completion/commandline.py b/alot/completion/commandline.py
new file mode 100644
index 00000000..f5739f38
--- /dev/null
+++ b/alot/completion/commandline.py
@@ -0,0 +1,54 @@
+# Copyright (C) 2011-2019 Patrick Totzke <patricktotzke@gmail.com>
+# This file is released under the GNU GPL, version 3 or a later revision.
+# For further details see the COPYING file
+
+from .completer import Completer
+from .command import CommandCompleter
+from ..helper import split_commandline
+
+
+class CommandLineCompleter(Completer):
+ """completes command lines: semicolon separated command strings"""
+
+ def __init__(self, dbman, mode, currentbuffer=None):
+ """
+ :param dbman: used to look up available 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)
+
+ @staticmethod
+ def get_context(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):
+ newtext = before + ccmd + after
+ newpos = pos + (ccpos - cpos)
+ res.append((newtext, newpos))
+ return res