Search code examples
listemacslispelisplist-comprehension

List buffers associated with files?


I'm very new to using lisp, so I'm sorry if this is a trivial question. I haven't been able to find solutions after a while googling, though I'm sure that this is fault on my part.

So. I'm trying to write a command which will revert all open buffers. Simple. I just do

(setq revert-without-query (buffer-list))
(mapc 'revert-buffer (buffer-list))` 

Unfortunately, this ends up failing if there are any buffers which aren't associated with files- which is to say, always.

Doing C-x C-b to list-buffers prints something like

CRM   Buffer        Size  Mode            File
      init.el       300   Emacs-lisp      ~/.spacemacs.d/init.el
      %scratch%      30   Test            

Ok. Easy enough. If I was allowed to mix lisp and python, I'd do something like

(setq revert-without-query [b for b in buffer-list if b.File != ""])
;; Or would I test for nil? Decisions, decisions...

Upon some digging, I found that there exists remove-if. Unfortunately, being completely new to lisp, I have no idea how to access the list, their attributes, or... well... anything. Mind helping me out?


Solution

  • One possibility would be checking buffer-file-name which will return nil if the buffer isn't visiting a file, eg.

    (cl-loop for buf in (buffer-list)
       if (buffer-file-name buf)
       collect buf)
    

    or

    (cl-remove-if-not 'buffer-file-name (buffer-list))