reddit / emacs

Help: Occur buffer: include both, the buffer name and the regexp in the occur buffer title name

Danrobi1 · 2022-10-10 22:13
Im trying to have both to be displayed in the occur buffer title name. The buffer name from where the regexp is from and also the regexp too. I know about `occur-rename-buffer` but that gives only the buffer name not the regexp. When I run `ibuffer` I see this: `*Occur: init.el*` and `*Occur: init.el*<2>`. That's not very practical. Something like: `*Occur: init.el*<defun>` and `*Occur: init.el*<global-set-key>` would be much better. I've found this: [rename-occur-buffer-term](https://www.reddit.com/r/emacs/comments/l9b049/is_there_a_way_to_have_multiple_occur_buffers_for/) which it does display the regexp but not the buffer-name :/ I tried to put both together but my e-lisp knowledge is very, very limited atm. Is anybody here already have a fix for this?
organic-bookworm · 2022-10-11 11:02
> I've found this Why, the top [answer](https://www.reddit.com/r/emacs/comments/l9b049/comment/glheho5/) provides specific snippet, which does what you want. However, I would prefer using `occur-revert-arguments` directly (defun my/occur-rename-buffer () (interactive) (rename-buffer (format "%s<%s>" (let (buffer-list-update-hook) (occur-rename-buffer)) (regexp-quote (car occur-revert-arguments)))) )
Danrobi1 · 2022-10-11 12:05
Wow, that's Awesome! Your snippet is the best. No way I'd have known about `occur-revert-arguments`. Very much appreciated. Thank you very much. You're the best ;)
organic-bookworm · 2022-10-11 15:07
> No way I'd have known about `occur-revert-arguments`. Me neither. I read source code of `occur-rename-buffer`. Just keep in mind, that this variable is an implementation detail, that may potentially break at some point in future Emacs releases. It's just less code and errors, than regex parsing (which may change as well).
sebhoagie · 2022-10-11 20:07
This is what I have in my init file to rename occur buffers (just checked out the thread you shared, funny to see an earlier version of this code in there). (defun hoagie-rename-and-select-occur-buffer () "Renames the current buffer to *Occur: [term] [buffer]*. Meant to be added to `occur-hook'." (cl-destructuring-bind (search-term _ (buffer-name &rest _)) occur-revert-arguments (pop-to-buffer (rename-buffer (format "*Occur: %s %s*" search-term buffer-name) t)))) (add-hook 'occur-hook #'hoagie-rename-and-select-occur-buffer) Compared to the other solution shared here, this one switches focus to the Occur, which I find more natural (closing Occur is one `q` away)
Danrobi1 · 2022-10-12 00:23
Thanks for sharing. I now have 2 versions. Awesome! Occur is so much better like that. Occur should behave like that by default. Thanks again!