reddit / emacs

Problem with ibuffer predicates

teckau22 · 2022-11-25 21:11
I'm trying to create a group in `ibuffer` that will include various buffers matching different regular expressions. Below is a simplified version of my current attempt: (defun my-agenda-group () (or (string-match-p "^\\*Org-Agenda\\*.*" (buffer-name)) (string-match-p ".org\\'" buffer-file-name))) (setq ibuffer-saved-filter-groups (quote (("Default" ("Agenda" (predicate . (my-agenda-group))))))) If I understand correctly, `ibuffer` will apply `my-agenda-group` to every buffer, and will display that buffer under "Agenda" if the function returns non-nil for the buffer. I have called that function inside various buffers myself to check manually that it works correctly. However, this same function does not work within `ibuffer-saved-filter-groups` and I am unable to figure out why.
0lMon · 2022-11-26 01:36
Hej Hej, One thing I noticed: My Org agenda buffer is called "*Org Agenda*" without the "-". One of the problems should be that your function is causing an error on some buffers. The correct implementation should be the following: (defun my-agenda-group () (or (string-match-p "^\\*Org-Agenda\\*.*" (buffer-name)) (when buffer-file-name (string-match-p ".org\\'" buffer-file-name)))) This will prevent the error in string-match-p when buffer-file-name is nil. To use your filter you should call the following function: (ibuffer-switch-to-saved-filter-groups "Default") or just put it in a hook: (add-hook 'ibuffer-mode-hook (lambda () (ibuffer-switch-to-saved-filter-groups "Default"))) Hope this will help. You could of course just rewrite your filter to: (setq ibuffer-saved-filter-groups (quote (("Default" ("Agenda" (or (mode . org-agenda-mode) (mode . org-mode)))))) This should give you a similar effect. Cheers!
teckau22 · 2022-11-26 02:11
Thank you! This is exactly what the problem was: `buffer-file-name` is nil for buffers such as \*Org Agenda\*, \*Messages\*, etc. This solves everything :)
github-alphapapa · 2022-11-26 02:54
You may find Bufler helpful, as it does what Ibuffer does more easily: https://github.com/alphapapa/bufler.el
teckau22 · 2022-11-26 03:18
Thanks, I'll take a look!