Convert +ivy/switch-buffer to transformers + add mode icons #169

This commit is contained in:
Henrik Lissner 2017-08-21 21:54:04 +02:00
parent 11e55a5a4f
commit 8e0e4d67b1
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 47 additions and 63 deletions

View file

@ -1,72 +1,47 @@
;;; completion/ivy/autoload/ivy.el -*- lexical-binding: t; -*- ;;; completion/ivy/autoload/ivy.el -*- lexical-binding: t; -*-
;; Show more information in ivy-switch-buffer; and only display (defsubst +ivy--icon-for-mode (mode)
;; workgroup-relevant buffers. "Apply `all-the-icons-for-mode' on MODE but either return an icon or nil."
(defun +ivy--get-buffers (&optional buffer-list) (let ((icon (all-the-icons-icon-for-mode mode)))
(when-let (buffer-list (delq (current-buffer) (or buffer-list (doom-buffer-list)))) (unless (symbolp icon) icon)))
(let* ((min-name
(+ 5 (cl-loop for buf in buffer-list (defun +ivy-buffer-transformer (str)
maximize (length (buffer-name buf))))) (let* ((buf (get-buffer str))
(min-mode (path (buffer-file-name buf))
(+ 5 (cl-loop for buf in buffer-list (mode (buffer-local-value 'major-mode buf))
maximize (length (symbol-name (buffer-local-value 'major-mode buf))))))) (faces
(cl-loop
with project-root = (doom-project-root)
for buf in buffer-list
collect
(cl-destructuring-bind (type mode path)
(with-current-buffer buf (with-current-buffer buf
(list (concat (cond ((string-match-p "^ ?\\*" (buffer-name buf))
(let ((buffer-name (buffer-name buf)))
(propertize
buffer-name
'face (cond ((string-match-p "^ ?\\*" buffer-name)
'font-lock-comment-face) 'font-lock-comment-face)
((not (string= project-root (doom-project-root))) ((buffer-modified-p buf)
'warning) 'doom-modeline-buffer-modified)
(buffer-read-only (buffer-read-only
'error)))) 'error)))))
(when (and buffer-file-name (buffer-modified-p)) (propertize
(propertize "[+]" 'face 'doom-modeline-buffer-modified))) (format "%-40s %s%-20s %s"
(propertize (symbol-name major-mode) 'face 'font-lock-constant-face) str
(when buffer-file-name (if +ivy-buffer-icons
(abbreviate-file-name (concat (propertize " " 'display
(file-name-directory buffer-file-name))))) (or (+ivy--icon-for-mode mode)
(format (format "%%-%ds %%-%ds %%s" min-name min-mode) (+ivy--icon-for-mode (get mode 'derived-mode-parent))))
type mode (or path ""))))))) "\t")
"")
(defun +ivy--select-buffer-action (buffer) mode
(ivy--switch-buffer-action (or (and path (abbreviate-file-name (file-name-directory (file-truename path))))
(string-remove-suffix ""))
"[+]" 'face faces)))
(substring buffer 0 (string-match-p (regexp-quote " ") buffer)))))
(defun +ivy--select-buffer-other-window-action (buffer)
(ivy--switch-buffer-other-window-action
(string-remove-suffix
"[+]"
(substring buffer 0 (string-match-p (regexp-quote " ") buffer)))))
;;;###autoload ;;;###autoload
(defun +ivy/switch-workspace-buffer (&optional other-window-p) (defun +ivy/switch-workspace-buffer (&optional arg)
"Switch to an open buffer in the current workspace. "Switch to another buffer within the current workspace.
If OTHER-WINDOW-P (universal arg), then open target in other window." If ARG (universal argument), open selection in other-window."
(interactive "P") (interactive "P")
(+ivy/switch-buffer other-window-p t)) (ivy-read "Switch to workspace buffer: "
(mapcar #'buffer-name (delq (current-buffer) (doom-buffer-list)))
;;;###autoload :action (if arg
(defun +ivy/switch-buffer (&optional other-window-p workspace-only-p) #'ivy--switch-buffer-other-window-action
"Switch to an open buffer in the global buffer list. #'ivy--switch-buffer-action)
If OTHER-WINDOW-P (universal arg), then open target in other window.
If WORKSPACE-ONLY-P (universal arg), limit to buffers in the current workspace."
(interactive "P")
(ivy-read (format "%s buffers: " (if workspace-only-p "Workspace" "Global"))
(+ivy--get-buffers (unless workspace-only-p (buffer-list)))
:action (if other-window-p
#'+ivy--select-buffer-other-window-action
#'+ivy--select-buffer-action)
:matcher #'ivy--switch-buffer-matcher :matcher #'ivy--switch-buffer-matcher
:keymap ivy-switch-buffer-map :keymap ivy-switch-buffer-map
:caller #'+ivy/switch-workspace-buffer)) :caller #'+ivy/switch-workspace-buffer))

View file

@ -1,5 +1,8 @@
;;; completion/ivy/config.el -*- lexical-binding: t; -*- ;;; completion/ivy/config.el -*- lexical-binding: t; -*-
(defvar +ivy-buffer-icons nil
"If non-nil, show buffer mode icons in `ivy-switch-buffer' and the like.")
(defvar +ivy-task-tags (defvar +ivy-task-tags
'(("TODO" . warning) '(("TODO" . warning)
("FIXME" . error)) ("FIXME" . error))
@ -46,7 +49,7 @@ session)."
[remap apropos] #'counsel-apropos [remap apropos] #'counsel-apropos
[remap describe-face] #'counsel-describe-face [remap describe-face] #'counsel-describe-face
[remap find-file] #'counsel-find-file [remap find-file] #'counsel-find-file
[remap switch-to-buffer] #'+ivy/switch-buffer [remap switch-to-buffer] #'ivy-switch-buffer
[remap persp-switch-to-buffer] #'+ivy/switch-workspace-buffer [remap persp-switch-to-buffer] #'+ivy/switch-workspace-buffer
[remap recentf] #'counsel-recentf [remap recentf] #'counsel-recentf
[remap imenu] #'counsel-imenu [remap imenu] #'counsel-imenu
@ -59,6 +62,12 @@ session)."
[remap describe-variable] #'counsel-describe-variable [remap describe-variable] #'counsel-describe-variable
[remap describe-face] #'counsel-describe-face) [remap describe-face] #'counsel-describe-face)
;; Show more buffer information in switch-buffer commands
(ivy-set-display-transformer 'ivy-switch-buffer #'+ivy-buffer-transformer)
(ivy-set-display-transformer 'ivy-switch-buffer-other-window #'+ivy-buffer-transformer)
(ivy-set-display-transformer '+ivy/switch-workspace-buffer #'+ivy-buffer-transformer)
(ivy-set-display-transformer 'counsel-recentf #'abbreviate-file-name)
(when (featurep! :feature workspaces) (when (featurep! :feature workspaces)
(nconc ivy-sort-functions-alist (nconc ivy-sort-functions-alist
'((persp-kill-buffer . nil) '((persp-kill-buffer . nil)