Refactor real-buffer system

This commit is contained in:
Henrik Lissner 2016-06-08 14:38:30 -04:00
parent 2ad73e974d
commit 57fa809023

View file

@ -98,28 +98,19 @@ 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))
(if (eq doom-buffer (current-buffer)) (cond ((doom/popup-p)
(if (one-window-p) (doom/popup-close))
(progn (t
(when (= (length (get-buffer-window-list doom-buffer nil t)) 1) (if (> (length (get-buffer-window-list (current-buffer) nil t)) 1)
(doom-mode-init t)) (bury-buffer)
(when arg (message "Already in scratch buffer"))) (when (and buffer-file-name (buffer-modified-p))
(doom/previous-real-buffer)) (if (yes-or-no-p "Buffer is unsaved, save it?")
(let ((new-dir (doom/project-root))) (save-buffer)
(if (doom/popup-p) (set-buffer-modified-p nil)))
(doom/popup-close) (kill-buffer)
(if (> (length (get-buffer-window-list (current-buffer) nil t)) 1) (unless (doom/real-buffer-p)
(bury-buffer) (doom/previous-real-buffer)))))
(when (and buffer-file-name (buffer-modified-p)) t)
(if (yes-or-no-p "Buffer is unsaved, save it?")
(save-buffer)
(set-buffer-modified-p nil)))
(kill-this-buffer))
(unless (doom/real-buffer-p (current-buffer))
(doom/previous-real-buffer))
(when (get-buffer-window-list doom-buffer nil t)
(doom|update-scratch-buffer new-dir)))))
t)
;;;###autoload ;;;###autoload
(defun doom/kill-unreal-buffers () (defun doom/kill-unreal-buffers ()
@ -168,45 +159,41 @@ the buffer if it is being displayed in another window."
"Switch to the previous buffer and avoid special buffers. If there's nothing "Switch to the previous buffer and avoid special buffers. If there's nothing
left, create a scratch buffer." left, create a scratch buffer."
(let* ((start-buffer (current-buffer)) (let* ((start-buffer (current-buffer))
(move-func (if (< n 0) 'switch-to-next-buffer 'switch-to-prev-buffer)) (move-func (if (> n 0) 'switch-to-next-buffer 'switch-to-prev-buffer))
(real-buffers (doom/get-real-buffers))
(realc (length real-buffers))
(max 25) (max 25)
(i 0) (i 0)
(continue t)) (continue t)
(if (or (= realc 0) destbuf)
(and (= realc 1) (eq (car real-buffers) (current-buffer)))) (setq destbuf
(progn (catch 'goto
(doom|update-scratch-buffer) (if (not (doom/get-real-buffers))
(switch-to-buffer doom-buffer-name) (throw 'goto doom-buffer)
(message "Nowhere to go")) (funcall move-func)
(funcall move-func) (while (not (and (doom/real-buffer-p)
(while (and continue) (doom/project-p)))
(let ((current-buffer (current-buffer))) (if (or (eq (current-buffer) start-buffer)
(cond ((or (eq current-buffer start-buffer) (>= i max))
(>= i max)) (throw 'goto doom-buffer)
(doom|update-scratch-buffer) (funcall move-func))
(switch-to-buffer doom-buffer-name) (cl-incf i))
(setq continue nil)) (current-buffer))))
((not (memq current-buffer real-buffers)) (when (eq destbuf doom-buffer)
(funcall move-func)) (doom|update-scratch-buffer)
(t (message "Nowhere to go"))
(setq continue nil)))) (switch-to-buffer destbuf)))
(cl-incf i)))))
;;;###autoload ;;;###autoload
(defun doom/real-buffer-p (&optional buffer) (defun doom/real-buffer-p (&optional buffer)
"Returns whether BUFFER a 'real' buffer or not. Real means it isn't a popup, "Returns whether BUFFER a 'real' buffer or not. Real means it isn't a popup,
temporary, scratch or special buffer." temporary, scratch or special buffer."
(setq buffer (get-buffer (or buffer (current-buffer)))) (setq buffer (or (and (bufferp buffer) buffer)
(and (stringp buffer) (get-buffer buffer))
(current-buffer)))
(when (buffer-live-p buffer) (when (buffer-live-p buffer)
(with-current-buffer buffer (with-current-buffer buffer
(not (or (apply #'derived-mode-p (not (or (apply #'derived-mode-p (-filter 'symbolp doom-unreal-buffers))
(-filter 'symbolp doom-unreal-buffers)) (--any? (string-match-p it (buffer-name buffer))
(--any? (if (stringp it) (-filter 'stringp doom-unreal-buffers)))))))
(string-match-p it (buffer-name buffer))
(eq major-mode it))
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