completion/ivy: add ivy-rich; rewrite custom transformer #331

+ivy-buffer-transformer does *most* of what ivy-rich does, so lets cut down on
our own code, bring in ivy-rich, and add our customizations on top of it.

This fixes ivy-use-virtual-buffers support, too.
This commit is contained in:
Henrik Lissner 2018-01-08 14:41:41 -05:00
parent 35594f0729
commit 1c2683ce9d
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 51 additions and 36 deletions

View file

@ -1,36 +1,42 @@
;;; completion/ivy/autoload/ivy.el -*- lexical-binding: t; -*- ;;; completion/ivy/autoload/ivy.el -*- lexical-binding: nil; -*-
(defsubst +ivy--icon-for-mode (mode) (defun +ivy--is-workspace-or-other-buffer-p (buffer)
"Apply `all-the-icons-for-mode' on MODE but either return an icon or nil." (let ((buffer (car buffer)))
(let ((icon (all-the-icons-icon-for-mode mode))) (when (stringp buffer)
(unless (symbolp icon) icon))) (setq buffer (get-buffer buffer)))
(and (not (eq buffer (current-buffer)))
(+workspace-contains-buffer-p buffer))))
;;
;; Library
;;
;;;###autoload ;;;###autoload
(defun +ivy-buffer-transformer (str) (defun +ivy-buffer-transformer (str)
(let* ((buf (get-buffer str)) (let ((buf (get-buffer str))
(path (buffer-file-name buf)) (project-root (doom-project-root)))
(mode (buffer-local-value 'major-mode buf)) (require 'ivy-rich)
(faces (cond (buf
(with-current-buffer buf (with-current-buffer buf
(cond ((string-match-p "^ ?\\*" (buffer-name buf)) (let* ((indicator (ivy-rich-switch-buffer-indicators))
'font-lock-comment-face) (size (ivy-rich-switch-buffer-size))
((buffer-modified-p buf) (buf-name (ivy-rich-switch-buffer-buffer-name))
'doom-modeline-buffer-modified) (mode (ivy-rich-switch-buffer-major-mode))
(buffer-read-only (project (ivy-rich-switch-buffer-project))
'error))))) (path (ivy-rich-switch-buffer-path project)))
(propertize (cond ((string-match-p "^ ?\\*" (buffer-name buf))
(format "%-40s %s%-20s %s" (setq buf-name (propertize buf-name 'face 'font-lock-comment-face)))
str ((and buffer-file-name
(if +ivy-buffer-icons (not (file-in-directory-p (buffer-file-name buf) project-root)))
(concat (propertize " " 'display (setq buf-name (propertize buf-name 'face 'ivy-remote))))
(or (+ivy--icon-for-mode mode) (ivy-rich-switch-buffer-format (list buf-name size indicator mode project path)))))
(+ivy--icon-for-mode (get mode 'derived-mode-parent)))) ((and (eq ivy-virtual-abbreviate 'full)
"\t") ivy-rich-switch-buffer-align-virtual-buffer)
"") (ivy-rich-switch-buffer-virtual-buffer))
mode ((eq ivy-virtual-abbreviate 'full)
(or (and path (abbreviate-file-name (file-name-directory (file-truename path)))) (propertize (abbreviate-file-name str) 'str 'ivy-virtual))
"")) (t (propertize str 'face 'ivy-virtual)))))
'face faces)))
;;;###autoload ;;;###autoload
(defun +ivy/switch-workspace-buffer (&optional arg) (defun +ivy/switch-workspace-buffer (&optional arg)

View file

@ -38,7 +38,11 @@ immediately runs it on the current candidate (ending the ivy session)."
;; highlight til EOL ;; highlight til EOL
ivy-format-function #'ivy-format-function-line ivy-format-function #'ivy-format-function-line
;; disable magic slash on non-match ;; disable magic slash on non-match
ivy-magic-slash-non-match-action nil) ivy-magic-slash-non-match-action nil
;; don't show recent files in switch-buffer
ivy-use-virtual-buffers nil
;; ...but if that ever changes, show their full path
ivy-virtual-abbreviate 'full)
(after! magit (setq magit-completing-read-function #'ivy-completing-read)) (after! magit (setq magit-completing-read-function #'ivy-completing-read))
(after! yasnippet (push #'+ivy-yas-prompt yas-prompt-functions)) (after! yasnippet (push #'+ivy-yas-prompt yas-prompt-functions))
@ -57,12 +61,6 @@ immediately runs it on the current candidate (ending the ivy session)."
[remap describe-function] #'counsel-describe-function [remap describe-function] #'counsel-describe-function
[remap describe-variable] #'counsel-describe-variable) [remap describe-variable] #'counsel-describe-variable)
;; 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)
(nconc ivy-sort-functions-alist (nconc ivy-sort-functions-alist
'((persp-kill-buffer . nil) '((persp-kill-buffer . nil)
(persp-remove-buffer . nil) (persp-remove-buffer . nil)
@ -74,12 +72,22 @@ immediately runs it on the current candidate (ending the ivy session)."
(+workspace/delete . nil)))) (+workspace/delete . nil))))
;; Show more buffer information in switch-buffer commands
(def-package! ivy-rich
:after ivy
:config
(dolist (cmd '(ivy-switch-buffer +ivy/switch-workspace-buffer))
(ivy-set-display-transformer cmd '+ivy-buffer-transformer)))
(def-package! swiper :commands (swiper swiper-all)) (def-package! swiper :commands (swiper swiper-all))
(def-package! counsel (def-package! counsel
:requires ivy :requires ivy
:config :config
(ivy-set-display-transformer #'counsel-recentf #'abbreviate-file-name)
(require 'counsel-projectile) (require 'counsel-projectile)
(setq counsel-find-file-ignore-regexp "\\(?:^[#.]\\)\\|\\(?:[#~]$\\)\\|\\(?:^Icon?\\)") (setq counsel-find-file-ignore-regexp "\\(?:^[#.]\\)\\|\\(?:[#~]$\\)\\|\\(?:^Icon?\\)")

View file

@ -7,3 +7,4 @@
(package! smex) (package! smex)
(package! swiper) (package! swiper)
(package! ivy-hydra) (package! ivy-hydra)
(package! ivy-rich)