From 4a62d59894892f5d644a8d3aca68214a56105651 Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Thu, 31 Dec 2020 14:25:56 +0100 Subject: [PATCH] taglist: implement untag command The usual untag command requires an argument. In the taglist, we have a a shortcut available since a tag is selected. Use that for quickly removing tags. --- alot/buffers/taglist.py | 10 +++++++- alot/commands/taglist.py | 37 +++++++++++++++++++++++++++++ docs/source/usage/modes/taglist.rst | 7 ++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/alot/buffers/taglist.py b/alot/buffers/taglist.py index 0d5c48d29..864881f22 100644 --- a/alot/buffers/taglist.py +++ b/alot/buffers/taglist.py @@ -104,8 +104,16 @@ def focus_last(self): lastpos = allpos[0] self.body.set_focus(lastpos) + def get_selected_tagline(self): + """ + returns curently focussed :class:`urwid.AttrMap` tagline widget + from the result list. + """ + cols, _ = self.taglist.get_focus() + return cols + def get_selected_tag(self): """returns selected tagstring or throws AttributeError if none""" - cols, _ = self.taglist.get_focus() + cols = self.get_selected_tagline() tagwidget = cols.original_widget.get_focus() return tagwidget.tag diff --git a/alot/commands/taglist.py b/alot/commands/taglist.py index 3c1dc42de..f257efd2b 100644 --- a/alot/commands/taglist.py +++ b/alot/commands/taglist.py @@ -6,6 +6,7 @@ from . import Command, registerCommand from .globals import SearchCommand +from .. import commands MODE = 'taglist' @@ -45,3 +46,39 @@ async def apply(self, ui): tagstring = '"%s"' % tagstring cmd = SearchCommand(query=['tag:%s' % tagstring]) await ui.apply_command(cmd) + + +@registerCommand(MODE, 'untag') +class UntagCommand(Command): + + """remove selected tag from all messages within original buffer""" + async def apply(self, ui): + taglistbuffer = ui.current_buffer + taglinewidget = taglistbuffer.get_selected_tagline() + try: + tag = taglistbuffer.get_selected_tag() + except AttributeError: + logging.debug("taglist untag without tag selection") + return + tagstring = 'tag:"%s"' % tag + querystring = taglistbuffer.querystring + if querystring: + fullquerystring = '(%s) AND %s' % (querystring, tagstring) + else: + fullquerystring = tagstring + + def refresh(): + if taglinewidget in taglistbuffer.taglist: + taglistbuffer.taglist.remove(taglinewidget) + if tag in taglistbuffer.tags: + taglistbuffer.tags.remove(tag) + taglistbuffer.rebuild() + ui.update() + + try: + ui.dbman.untag(fullquerystring, [tag]) + except DatabaseROError: + ui.notify('index in read-only mode', priority='error') + return + + await ui.apply_command(commands.globals.FlushCommand(callback=refresh)) diff --git a/docs/source/usage/modes/taglist.rst b/docs/source/usage/modes/taglist.rst index c1eed6f48..4142e6e00 100644 --- a/docs/source/usage/modes/taglist.rst +++ b/docs/source/usage/modes/taglist.rst @@ -19,3 +19,10 @@ The following commands are available in taglist mode: search for messages with selected tag within original buffer +.. _cmd.taglist.untag: + +.. describe:: untag + + remove selected tag from all messages within original buffer + +