Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow excluding certain types of things from history #165

Open
lrustand opened this issue Nov 12, 2024 · 5 comments
Open

Allow excluding certain types of things from history #165

lrustand opened this issue Nov 12, 2024 · 5 comments

Comments

@lrustand
Copy link

I'm using exwm and consult-buffer. I have set up exwm to set the buffer titles to the actual X11 window titles. The web browser sets the window title to the title of the visited website. Because of this there are many new unique entries selected every day, and these keep filling up the prescient history with useless entries that will never be used again. How can I exclude exwm entries from being saved in prescient?

@okamsn
Copy link
Contributor

okamsn commented Nov 16, 2024

If the remembered candidates are not reused, then they are eventually forgotten. Do you have a way to identify programmatically which buffers are the Exwm buffers? There is the command prescient-forget, to which you can pass the candidate you would like to forget.

I am not familiar with Exwm, but I would guess that there is a hook in which you could run prescient-forget on the buffer's name after switching to the buffer.

@okamsn
Copy link
Contributor

okamsn commented Nov 16, 2024

Also, Prescient only affects sorting candidates during completion. It does not affect history variables like consult--buffer-history. If you want to remove those candidates from the command's completion history (accessed via M-n, M-p, etc.), that is a question for Consult.

@lrustand
Copy link
Author

lrustand commented Nov 16, 2024

If the remembered candidates are not reused, then they are eventually forgotten.

Yeah, this is true. But there are just so many of them that they fill up 95% of the prescient history, and this seems to push out legitimate history entries. Things that I use quite frequently keep disappearing from the top of the completion candidates, and I suspect this is because the illegitimate history entries fill up the entire history.

I am not familiar with Exwm, but I would guess that there is a hook in which you could run prescient-forget on the buffer's name after switching to the buffer.

Well, this could be a way to work around the problem at the very least, but I would like to prevent them from being saved in the first place. This workaround would still cause some legitimate history entries to be forced off the list, no? Wouldn't one history entry fall off the list each time an exwm buffer is added to the list assuming the history list is full? Switching to and from exwm windows is something I do probably hundreds of times a day, so I assume this still would lead to a significant loss of legitimate history.

Also, Prescient only affects sorting candidates during completion. It does not affect history variables like consult--buffer-history.

But it does save the selected candidate, which I would like to avoid for exwm buffers and some other types of stuff.

I didn't mention it in the original post, but I have some other completion stuff that also ends up being saved in the prescient history. I have a custom thingy that I use to open urls in qutebrowser, with completion from my browsing history. All selected candidates from this also ends up being saved, which I also would like to avoid, since qutebrowser already saves the browsing history and I have even disabled sorting for this command, since that is also handled by the browsing history.

This last thing here has its own command, does that make it easier to exclude it from the prescient history?

I did a little digging in the source code and found that there is a vertico-prescient--exit-commands, which specifies what commands should have their histories saved by prescient. However, this list contains exit-minibuffer, so I guess this will include all commands that use minibuffer completion anyways.

Maybe I could just put some advice around vertico-prescient--remember-minibuffer-contents that checks if minibuffer-contents-no-properties is a valid exwm buffer. I don't quite see how I can do this in a clean way the way the code is currently structured though, since I would really want to force the variable txt to be nil, but this is fetched directly from the minibuffer contents. Any ideas?

EDIT: The last part here was a brainfart. See below for solution to this part of my question.

@lrustand
Copy link
Author

lrustand commented Nov 16, 2024

Okay, I had a small brainfart when talking about the advice around the remember function. I figured out how to solve the exwm part of my problem like this:

 (defun dont-remember-qutebrowser-buffers (orig-fun &rest args)
   "Exclude qutebrowser buffers from prescient history."
   (if-let* ((selected-candidate (substring (minibuffer-contents-no-properties) 0 -1))
             (selected-buffer (get-buffer selected-candidate))
             (selected-buffer-x11-class (buffer-local-value 'exwm-class-name
                                                            selected-buffer))
             (is-qb (string= "qutebrowser" selected-buffer-x11-class)))
       (message "Selected buffer is QB, not saving")
     (funcall orig-fun)))
 (advice-add 'vertico-prescient--remember-minibuffer-contents :around #'dont-remember-qutebrowser-buffers)

My second problem (detecting that the command we just ran was my qutebrowser launcher) is still a bit tricky though, as I don't know how to check what the command was that has just exited. But this you might be able to help me with hopefully?

EDIT: Btw, there is a weird non-letter character (a square box with numbers in it), at the end of every history entry. Do you know what this is and why it is there?

EDIT2: When I'm thinking a bit more about this, maybe all of consult-buffer should be excluded from the history? Since these entries are only valid while those buffers are open, so the saved history would have a high likelyhood of containing a lot of invalid entries when the user closes buffers.

@okamsn
Copy link
Contributor

okamsn commented Nov 20, 2024

My second problem (detecting that the command we just ran was my qutebrowser launcher) is still a bit tricky though, as I don't know how to check what the command was that has just exited. But this you might be able to help me with hopefully?

In general, there's the variable last-command. For detecting consult-buffer inside vertico-prescient--remember-minibuffer-contents, the variable current-minibuffer-command seems to work. It looks like current-minibuffer-command was added in Emacs 28.

Btw, there is a weird non-letter character (a square box with numbers in it), at the end of every history entry. Do you know what this is and why it is there?

I believe Consult adds those characters to distinguish candidates with the same text that would otherwise be equivalent. In older versions of Emacs, workarounds were needed to know which candidate was selected, such as when a text candidate was used a key for key-value pairs. I don't know if those workarounds are still needed for more recent versions of Emacs.

See the function consult--tofu-append in consult.el.

When I'm thinking a bit more about this, maybe all of consult-buffer should be excluded from the history? Since these entries are only valid while those buffers are open, so the saved history would have a high likelyhood of containing a lot of invalid entries when the user closes buffers.

Maybe advising the function add-to-history would work for you. There's also the user option history-length, which you could increase.

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

No branches or pull requests

2 participants