diff --git a/alot/commands/globals.py b/alot/commands/globals.py index aa1fd4368..2b709df37 100644 --- a/alot/commands/globals.py +++ b/alot/commands/globals.py @@ -31,6 +31,7 @@ from ..completion.tags import TagsCompleter from ..widgets.utils import DialogBox from ..db.errors import DatabaseLockedError +from ..db.errors import DatabaseROError from ..db.envelope import Envelope from ..settings.const import settings from ..settings.errors import ConfigError, NoMatchingAccount @@ -1122,7 +1123,7 @@ def __init__(self, alias, query=None, flush=True, **kwargs): self.flush = flush Command.__init__(self, **kwargs) - def apply(self, ui): + async def apply(self, ui): msg = 'saved alias "%s" for query string "%s"' % (self.alias, self.query) @@ -1136,7 +1137,7 @@ def apply(self, ui): # flush index if self.flush: - ui.apply_command(commands.globals.FlushCommand()) + return await ui.apply_command(commands.globals.FlushCommand()) @registerCommand( @@ -1153,7 +1154,7 @@ class RemoveQueryCommand(Command): """remove named query string for given alias""" repeatable = False - def __init__(self, alias, flush=True, **kwargs): + def __init__(self, alias, afterwards=None, flush=True, **kwargs): """ :param alias: name to use for query string :type alias: str @@ -1162,13 +1163,14 @@ def __init__(self, alias, flush=True, **kwargs): """ self.alias = alias self.flush = flush + self.afterwards = afterwards Command.__init__(self, **kwargs) - def apply(self, ui): + async def apply(self, ui): msg = 'removed alias "%s"' % (self.alias) try: - ui.dbman.remove_named_query(self.alias) + ui.dbman.remove_named_query(self.alias, afterwards=self.afterwards) logging.debug(msg) ui.notify(msg) except DatabaseROError: @@ -1177,7 +1179,7 @@ def apply(self, ui): # flush index if self.flush: - ui.apply_command(commands.globals.FlushCommand()) + await ui.apply_command(commands.globals.FlushCommand()) @registerCommand( diff --git a/alot/commands/namedqueries.py b/alot/commands/namedqueries.py index f10724a8d..8c96f61ec 100644 --- a/alot/commands/namedqueries.py +++ b/alot/commands/namedqueries.py @@ -4,7 +4,12 @@ import argparse from . import Command, registerCommand +from .globals import ConfirmCommand +from .globals import FlushCommand +from .globals import PromptCommand +from .globals import RemoveQueryCommand from .globals import SearchCommand +from ..db.errors import DatabaseROError MODE = 'namedqueries' @@ -28,3 +33,89 @@ async def apply(self, ui): cmd = SearchCommand(query=query) await ui.apply_command(cmd) + + +@registerCommand(MODE, 'query-rename', arguments=[ + (['newalias'], {'help': 'new name for the query'}), +]) +class NamedqueriesRenameCommand(Command): + + """rename a query""" + def __init__(self, newalias, **kwargs): + self.newalias = newalias + self.complete_count = 0 + + def afterwards(self): + self.complete_count += 1 + if self.complete_count == 2: + self.ui.current_buffer.rebuild() + + async def apply(self, ui): + oldalias = ui.current_buffer.get_selected_query() + querydict = ui.dbman.get_named_queries() + query_string = querydict[oldalias] + self.ui = ui # save for callback + try: + ui.dbman.save_named_query(self.newalias, query_string, + afterwards=self.afterwards) + except DatabaseROError: + ui.notify('index in read-only mode', priority='error') + return + ui.dbman.remove_named_query(oldalias, afterwards=self.afterwards) + return await ui.apply_command(FlushCommand()) + + +@registerCommand(MODE, 'query-duplicate', arguments=[ + (['newalias'], {'help': 'name for duplicated query', 'nargs': '?'}), +]) +class NamedqueriesDuplicateCommand(Command): + + """create a copy of the query""" + def __init__(self, newalias, **kwargs): + self.newalias = newalias + + def afterwards(self): + self.ui.current_buffer.rebuild() + + async def apply(self, ui): + oldalias = ui.current_buffer.get_selected_query() + self.newalias = self.newalias or (oldalias + '_copy') + querydict = ui.dbman.get_named_queries() + query_string = querydict[oldalias] + self.ui = ui # save for callback + try: + ui.dbman.save_named_query(self.newalias, query_string, + afterwards=self.afterwards) + except DatabaseROError: + ui.notify('index in read-only mode', priority='error') + return + return await ui.apply_command(FlushCommand()) + + +@registerCommand(MODE, 'query-refine') +class NamedqueriesRefineCommand(Command): + + """refine a query string""" + async def apply(self, ui): + alias = ui.current_buffer.get_selected_query() + query_string = ui.dbman.get_named_queries()[alias] + await ui.apply_command(PromptCommand('savequery {} {}'.format( + alias, query_string))) + ui.current_buffer.rebuild() + + +@registerCommand(MODE, 'query-remove') +class NamedqueriesRemoveCommand(Command): + + def afterwards(self): + self.ui.current_buffer.rebuild() + + """remove the selected namedquery""" + async def apply(self, ui): + self.ui = ui + alias = ui.current_buffer.get_selected_query() + await ui.apply_command(ConfirmCommand( + msg=['Remove named query {}'.format(alias)])) + # SequenceCanceled raised if not confirmed + await ui.apply_command( + RemoveQueryCommand(alias, afterwards=self.afterwards))