Refactor defuns-buffer

This commit is contained in:
Henrik Lissner 2016-06-17 20:07:05 -04:00
parent cf968a2848
commit ac47e5a943

View file

@ -41,41 +41,45 @@ Inspired from http://demonastery.org/2013/04/emacs-evil-narrow-region/"
(defalias 'wg-save-session 'ignore)) (defalias 'wg-save-session 'ignore))
;;;###autoload ;;;###autoload
(defun doom/get-buffers (&optional project-p) (defun doom/get-buffers (&optional all-p)
"Get all buffers in the current workgroup. "Get all buffers in the current workgroup.
If PROJECT-P is non-nil, get all buffers in current workgroup If PROJECT-P is non-nil, get all buffers in current workgroup
If both are non-nil, get all project buffers across all workgroups" If both are non-nil, get all project buffers across all workgroups"
(let* ((assocbuf (wg-workgroup-associated-buffers nil)) (let ((buffers (if (wg-current-workgroup t)
(buffers (if (wg-current-workgroup t) (doom/get-buffers-in-workgroup)
(--filter (memq it assocbuf) (buffer-list)) (buffer-list)))
(buffer-list)))
project-root) project-root)
(append (aif (and project-p (doom/project-root t)) (append (aif (and (not all-p) (doom/project-root t))
(funcall (if (eq project-p 'not) '-remove '-filter) (funcall (if (eq all-p 'not) '-remove '-filter)
(lambda (b) (projectile-project-buffer-p b it)) (lambda (b) (projectile-project-buffer-p b it))
buffers) buffers)
buffers)))) buffers)
(list doom-buffer))))
;;;###autoload ;;;###autoload
(defun doom/get-buffer-names (&optional project-p) (defun doom/get-buffers-in-workgroup ()
(mapcar #'buffer-name (doom/get-buffers project-p))) (let ((assoc-bufs (wg-workgroup-associated-buffers nil)))
(--filter (memq it assoc-bufs) (buffer-list))))
;;;###autoload ;;;###autoload
(defun doom/get-visible-windows (&optional buffer-list) (defun doom/get-buffer-names (&optional buffer-list)
(mapcar #'get-buffer-window (doom/get-visible-buffers (or buffer-list (doom/get-buffers))))) (mapcar #'buffer-name (or buffer-list (doom/get-buffers))))
;;;###autoload
(defun doom/get-visible-windows (&optional window-list)
"Get a list of the visible windows in the current frame (that aren't popups)."
(-remove #'doom/popup-p (or window-list (window-list))))
;;;###autoload ;;;###autoload
(defun doom/get-visible-buffers (&optional buffer-list) (defun doom/get-visible-buffers (&optional buffer-list)
"Get a list of buffers that are not buried (i.e. visible)" "Get a list of unburied buffers in the current project and workgroup."
(-filter #'get-buffer-window (or buffer-list (doom/get-buffers)))) (-filter #'get-buffer-window (or buffer-list (doom/get-buffers))))
;;;###autoload ;;;###autoload
(defun doom/get-buried-buffers (&optional buffer-list) (defun doom/get-buried-buffers (&optional buffer-list)
"Get a list of buffers that are buried (i.e. not visible)" "Get a list of buried buffers in the current project and workgroup."
(let* ((buffers (or buffer-list (doom/get-buffers))) (-remove 'get-buffer-window (or buffer-list (doom/get-buffers))))
(old-len (length buffers)))
(-remove 'get-buffer-window buffers)))
;;;###autoload ;;;###autoload
(defun doom/get-matching-buffers (pattern &optional buffer-list) (defun doom/get-matching-buffers (pattern &optional buffer-list)
@ -98,28 +102,31 @@ Inspired from http://demonastery.org/2013/04/emacs-evil-narrow-region/"
"Kill buffer (but only bury scratch buffer), then switch to a real buffer. Only buries "Kill buffer (but only bury scratch buffer), then switch to a real buffer. Only buries
the buffer if it is being displayed in another window." the buffer if it is being displayed in another window."
(interactive (list t)) (interactive (list t))
(cond ((doom/popup-p) (cond ((eq (current-buffer) doom-buffer)
(doom/popup-close)) (doom-mode-init t))
(t (t
(if (> (length (get-buffer-window-list (current-buffer) nil t)) 1) (let* ((old-project (doom/project-root))
(bury-buffer) (buffer (current-buffer))
(when (and buffer-file-name (buffer-modified-p)) (only-buffer-window-p (= (length (get-buffer-window-list buffer nil t)) 1)))
(when (and only-buffer-window-p buffer-file-name (buffer-modified-p))
(if (yes-or-no-p "Buffer is unsaved, save it?") (if (yes-or-no-p "Buffer is unsaved, save it?")
(save-buffer) (save-buffer)
(set-buffer-modified-p nil))) (set-buffer-modified-p nil)))
(kill-buffer) (when arg
(unless (doom/real-buffer-p) (doom/previous-real-buffer)
(doom/previous-real-buffer))))) (when (eq buffer (current-buffer))
(switch-to-buffer doom-buffer t t)
(doom|update-scratch-buffer old-project))
(when only-buffer-window-p
(kill-buffer buffer))))))
t) t)
;;;###autoload ;;;###autoload
(defun doom/kill-unreal-buffers () (defun doom/kill-unreal-buffers ()
"Kill all buried, unreal buffers in current frame. See `doom-unreal-buffers'" "Kill all buried, unreal buffers in current frame. See `doom-unreal-buffers'"
(interactive) (interactive)
(let* ((all-buffers (doom/get-buffers)) (let ((kill-list (-remove 'doom/real-buffer-p
(real-buffers (doom/get-real-buffers all-buffers)) (doom/get-buried-buffers (buffer-list)))))
(kill-list (--filter (not (memq it real-buffers))
(doom/get-buried-buffers all-buffers))))
(mapc 'kill-buffer kill-list) (mapc 'kill-buffer kill-list)
(doom/kill-process-buffers) (doom/kill-process-buffers)
(message "Cleaned up %s buffers" (length kill-list)))) (message "Cleaned up %s buffers" (length kill-list))))
@ -128,7 +135,7 @@ the buffer if it is being displayed in another window."
(defun doom/kill-process-buffers () (defun doom/kill-process-buffers ()
"Kill all buffers that represent running processes and aren't visible." "Kill all buffers that represent running processes and aren't visible."
(interactive) (interactive)
(let ((buffer-list (doom/get-buffers)) (let ((buffer-list (buffer-list))
(killed-processes 0)) (killed-processes 0))
(dolist (p (process-list)) (dolist (p (process-list))
(let* ((process-name (process-name p)) (let* ((process-name (process-name p))
@ -163,14 +170,16 @@ left, create a scratch buffer."
(max 25) (max 25)
(i 0) (i 0)
(continue t) (continue t)
(buffers (doom/get-real-buffers (doom/get-buffers t)))
destbuf) destbuf)
(setq destbuf (setq destbuf
(catch 'goto (catch 'goto
(if (not (doom/get-real-buffers)) (if (or (not buffers)
(throw 'goto doom-buffer) (= (length buffers) 1))
(progn (message "No other buffers in workgroup")
(throw 'goto (current-buffer)))
(funcall move-func) (funcall move-func)
(while (not (and (doom/real-buffer-p) (while (not (memq (current-buffer) buffers))
(doom/project-p)))
(if (or (eq (current-buffer) start-buffer) (if (or (eq (current-buffer) start-buffer)
(>= i max)) (>= i max))
(throw 'goto doom-buffer) (throw 'goto doom-buffer)
@ -189,11 +198,12 @@ temporary, scratch or special buffer."
(setq buffer (or (and (bufferp buffer) buffer) (setq buffer (or (and (bufferp buffer) buffer)
(and (stringp buffer) (get-buffer buffer)) (and (stringp buffer) (get-buffer buffer))
(current-buffer))) (current-buffer)))
(when (buffer-live-p buffer) (or (eq buffer doom-buffer)
(with-current-buffer buffer (when (buffer-live-p buffer)
(not (or (apply #'derived-mode-p (-filter 'symbolp doom-unreal-buffers)) (with-current-buffer buffer
(--any? (string-match-p it (buffer-name buffer)) (not (or (apply #'derived-mode-p (-filter 'symbolp doom-unreal-buffers))
(-filter 'stringp doom-unreal-buffers))))))) (--any? (string-match-p it (buffer-name buffer))
(-filter 'stringp doom-unreal-buffers))))))))
;; Inspired by spacemacs <https://github.com/syl20bnr/spacemacs/blob/master/spacemacs/funcs.el> ;; Inspired by spacemacs <https://github.com/syl20bnr/spacemacs/blob/master/spacemacs/funcs.el>
;;;###autoload ;;;###autoload
@ -283,7 +293,7 @@ buffers regardless of project."
"Kill all buffers so that workgroups2 will wipe its current session." "Kill all buffers so that workgroups2 will wipe its current session."
(interactive) (interactive)
(let (confirm-kill-emacs) (let (confirm-kill-emacs)
(mapc 'kill-buffer (doom/get-buffers)) (mapc 'kill-buffer (doom/get-buffers t))
(kill-this-buffer) (kill-this-buffer)
(delete-other-windows) (delete-other-windows)
(wg-save-session t) (wg-save-session t)