Skip to content

Commit

Permalink
taglist: implement untag command
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mjg committed Feb 10, 2021
1 parent 158f898 commit 4a62d59
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
10 changes: 9 additions & 1 deletion alot/buffers/taglist.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 37 additions & 0 deletions alot/commands/taglist.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from . import Command, registerCommand
from .globals import SearchCommand
from .. import commands

MODE = 'taglist'

Expand Down Expand Up @@ -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))
7 changes: 7 additions & 0 deletions docs/source/usage/modes/taglist.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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


0 comments on commit 4a62d59

Please sign in to comment.