pazz / alot

Terminal-based Mail User Agent
GNU General Public License v3.0
703 stars 163 forks source link

Adding to Contrib Hooks wiki page / maintaing focus position in search buffer #1070

Closed bx closed 7 years ago

bx commented 7 years ago

I wrote some hooks to maintain the position of focus in a search buffer after it loses and regains focus (based on #633), that others may find useful. I was thinking of adding it to the Contrib Hooks wiki page and although it seems like github will allow me to edit that page I thought I'd check in with the project maintainers before doing so.

These are the hooks (adopted from pazz/alot@47139cb77cd37ee7ad9e7a157da43f2fa16f342) I am using to retain position focus in a search buffer:

def pre_buffer_open(ui, dbm, buf):
    current = ui.current_buffer
    if isinstance(current, alot.buffers.SearchBuffer):
        current.focused_thread = current.get_selected_thread()   # remember focus

def post_buffer_focus(ui, dbm, buf, success):
    if success and hasattr(buf, "focused_thread"):
        tid = buf.focused_thread.get_thread_id()
        for pos, tlw in enumerate(buf.threadlist.get_lines()):
            if tlw.get_thread().get_thread_id() == tid:
                buf.body.set_focus(pos)
                break
lucc commented 7 years ago

Why do you save the position before opening a new buffer? I have the version from https://github.com/pazz/alot/issues/828#issuecomment-173211684 . It just saves the position before rebuilding the buffer.

You are free to update the wiki. If your hook is related to something in there put it next to it and try to explain the differences.

pazz commented 7 years ago

why did we not aimply push the first commit from #633? it seems that this is a commonly used feature, so it could make sense to me it into master directly?

lucc commented 7 years ago

Maybe because it was slow? That was before my time :)

lucc commented 7 years ago

But I agree that it would be nice to have in core (as I mentioned above I also use sucha hook). Do you think it is interesting to extend the idea to other buffer types (thread->keep focus and folds; still others?)?

bx commented 7 years ago

It would be great to see pazz/alot@747139cb77cd37ee7ad9e7a157da43f2fa16f342 pulled into core so an external hook wouldn't be necessary. @lucc I am not too familiar with the internal workings of alot, so I just chose a hook that made sense to me, is there a different hook you prefer I use? Also I would also be interesting in having thread focus and folding be saved, ideally with an option to save across restarts but that seems like a separate feature request (or two).

lucc commented 7 years ago

@bx I don't have a preference which hook you use, I was hoping to learn something from you about the difference between the two.

Maybe keeping the focus position in threads is not as problematic as in search results as I assume the former never grow as long as the latter (what is the biggest thread you have, 10, 50, 100? For me notmuch search --format=json '*' | jq '.[].total' | sort -un | tail -n 1 says 79).

Keeping the fold status is certanly a different PR.

bx commented 7 years ago

@lucc Out of all the buffer-related hooks listed in the docs, pre_buffer_open seemed to be the correct chose since it is invoked when the focus is about to leave the (search) buffer