Skip to content

Provide a mechanism to customize the default message selection#128

Open
laarmen wants to merge 2 commits intoakissinger:masterfrom
laarmen:default_thread_callback
Open

Provide a mechanism to customize the default message selection#128
laarmen wants to merge 2 commits intoakissinger:masterfrom
laarmen:default_thread_callback

Conversation

@laarmen
Copy link
Copy Markdown
Collaborator

@laarmen laarmen commented Mar 14, 2026

This is my solution for #125. Not as elegant as my first solution to just call next_unread() on the panel once created, but that one had some issues when the thread was entirely unread, plus maybe some race condition somewhere for @klement. I'm wagering this version doesn't have the same issue.

Example binding that this allows:

dodo.keymap.search_keymap['Q'] = ('open unread', lambda p: p.open_current_thread(default_message=lambda m: m.next_unread(m.default_message())))

I played around with it and it has handled anything I've thrown at it without crashing on me nor behaving weirdly.

laarmen added 2 commits March 15, 2026 23:26
This needs to be done through a callback passed in the constructor, as
the thread selection only happens after the model has been refreshed for
the first time, which might be way after the creation of the panel
itself.

A real-life example of a need for this is someone wishing to open
threads directly on the first unread message rather than the first
search match, see akissinger#125

Note that open_current_thread needs to have its new argument marked as
keyword-only as this method is connected to an event that provides
positional argument that we just don't care about.
That makes the return value of this method suitable as a default_message
provider, otherwise we'd have an invalid index if a thread doesn't have
any matching unread messages. That would be awkward.

With that, you can now have a custom keybinding such as this:

dodo.keymap.search_keymap['Q'] = ('open unread', lambda p: p.open_current_thread(default_message=lambda m: m.next_unread(m.default_message())))
@laarmen laarmen force-pushed the default_thread_callback branch from 5c70714 to 6b066f2 Compare March 15, 2026 22:26
@klement
Copy link
Copy Markdown
Contributor

klement commented Mar 17, 2026

This works fine, with my “select last unread else last read” lambda becoming:

dodo.keymap.search_keymap['<enter>'] = ('open first unread', lambda p: p.open_current_thread(
      default_message=lambda m: next(
          (idx for idx in m.iterate_indices()
           if m.message_at(idx)['id'] in m.matches and 'unread' in m.message_at(idx)['tags']),
          m.get_last_msg_idx()
      )
  ))

and also requiring #129 not to crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants