Skip to content

Instantly share code, notes, and snippets.

@AlexBaranosky
Last active January 2, 2016 11:29
Show Gist options
  • Save AlexBaranosky/8296589 to your computer and use it in GitHub Desktop.
Save AlexBaranosky/8296589 to your computer and use it in GitHub Desktop.
;; works, but only for already opened buffers
(defun projectile-cleanup-project-buffers ()
(interactive)
(dolist (buffer (projectile-project-buffer-names))
(condition-case nil
(with-current-buffer buffer
(esk-cleanup-buffer))
(buffer-read-only nil))))
;; doesn't work - intended to get it to run against all
;; project buffers even ones that aren't opened yet
(defun projectile-cleanup-project-files ()
(interactive)
(dolist (filename (projectile-current-project-files))
(condition-case nil
(with-current-buffer (find-file filename)
(esk-cleanup-buffer))
(buffer-read-only nil))))
@bbatsov
Copy link

bbatsov commented Jan 7, 2014

find-file does not return a buffer object/name. It opens the file and a buffer and selects it. You can iterate through the files, run esk-cleanup-buffer, save the buffer and kill it. I guess it might be useful if I added to projectile some command that abstracts the iteration, saving and killing away.

@AlexBaranosky
Copy link
Author

@bbatsov yeah I was thinking it'd be nice to have 2 functions:

  1. that calls a supplied fn for every buffer in the project
  2. that calls a supplied fn for every file in the project

@AlexBaranosky
Copy link
Author

@bbatsov so pumped to have learned enough elisp to have got this to work, thanks to your tip :)

(defun cleanup-file (filename)
  (interactive "sFile: ")
  (find-file filename)
  (let ((name (buffer-name))
        (filename (buffer-file-name)))
    (if (not (and filename (file-exists-p filename)))
        (error "Buffer '%s' is not visiting a file!" name)
      (progn
        (esk-cleanup-buffer)
        (save-buffer)))))

(defun projectile-current-project-file-full-paths ()
  (let ((root (projectile-project-root)))
    (mapcar (lambda (filename)
              (expand-file-name filename root))
            (projectile-current-project-files))))

(defun projectile-cleanup-project-files ()
  (interactive)
  (dolist (filename (projectile-current-project-file-full-paths))
    (cleanup-file filename)))

@bbatsov
Copy link

bbatsov commented Jan 8, 2014

Yep, I think you're right. Open a ticket about the two functions in Projectile's issue tracker and I'll attend to them at some point.

Glad to hear my previous comments where helpful. Btw, without kill-buffer in cleanup-file you'll end up with a ton of open buffers in any bigger project.

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