blob: 7bba6bd15c3dcccc5d2af263dbc91ad6c8dacabd (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
;;; all-the-icons-ivy.el --- Shows icons while using ivy and counsel -*- lexical-binding: t; -*-
;; Copyright (C) 2017 asok
;; Author: asok
;; Version: 0.2.0
;; Package-Version: 20180225.630
;; Keywords: faces
;; Package-Requires: ((emacs "24.4") (all-the-icons "2.4.0") (ivy "0.8.0"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; To use this package, do
;;
;; (all-the-icons-ivy-setup)
;;
;; Or if you prefer to only set transformers
;; for a subset of ivy commands:
;;
;; (require 'all-the-icons-ivy)
;; (ivy-set-display-transformer 'ivy-switch-buffer 'all-the-icons-ivy-buffer-transformer)
;;; Code:
(require 'all-the-icons)
(require 'ivy)
(defgroup all-the-icons-ivy nil
"Shows icons while using ivy and counsel."
:group 'ivy)
(defcustom all-the-icons-ivy-buffer-commands
'(ivy-switch-buffer ivy-switch-buffer-other-window counsel-projectile-switch-to-buffer)
"Commands to use with `all-the-icons-ivy-buffer-transformer'."
:type '(repeat function)
:group 'all-the-icons-ivy)
(defcustom all-the-icons-ivy-file-commands
'(counsel-find-file counsel-projectile-find-file counsel-projectile-find-dir)
"Commands to use with `all-the-icons-ivy-file-transformer'."
:type '(repeat function)
:group 'all-the-icons-ivy)
(defun all-the-icons-ivy--buffer-propertize (b s)
"If buffer B is modified apply `ivy-modified-buffer' face on string S."
(if (and (buffer-file-name b)
(buffer-modified-p b))
(propertize s 'face 'ivy-modified-buffer)
s))
(defun all-the-icons-ivy--icon-for-mode (mode)
"Apply `all-the-icons-for-mode' on MODE but either return an icon or nil."
(let ((icon (all-the-icons-icon-for-mode mode)))
(unless (symbolp icon)
icon)))
(defun all-the-icons-ivy--buffer-transformer (b s)
"Return a candidate string for buffer B named S preceded by an icon.
Try to find the icon for the buffer's B `major-mode'.
If that fails look for an icon for the mode that the `major-mode' is derived from."
(let ((mode (buffer-local-value 'major-mode b)))
(format "%s\t%s"
(propertize "\t" 'display (or
(all-the-icons-ivy--icon-for-mode mode)
(all-the-icons-ivy--icon-for-mode (get mode 'derived-mode-parent))))
(all-the-icons-ivy--buffer-propertize b s))))
(defun all-the-icons-ivy-file-transformer (s)
"Return a candidate string for filename S preceded by an icon."
(format "%s\t%s"
(propertize "\t" 'display (all-the-icons-icon-for-file s))
s))
(defun all-the-icons-ivy-buffer-transformer (s)
"Return a candidate string for buffer named S.
Assume that sometimes the buffer named S might not exists.
That can happen if `ivy-switch-buffer' does not find the buffer and it
falls back to `ivy-recentf' and the same transformer is used."
(let ((b (get-buffer s)))
(if b
(all-the-icons-ivy--buffer-transformer b s)
(all-the-icons-ivy-file-transformer s))))
;;;###autoload
(defun all-the-icons-ivy-setup ()
"Set ivy's display transformers to show relevant icons next to the candidates."
(dolist (cmd all-the-icons-ivy-buffer-commands)
(ivy-set-display-transformer cmd 'all-the-icons-ivy-buffer-transformer))
(dolist (cmd all-the-icons-ivy-file-commands)
(ivy-set-display-transformer cmd 'all-the-icons-ivy-file-transformer)))
(provide 'all-the-icons-ivy)
;;; all-the-icons-ivy.el ends here
|