-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhelm-icons.el
195 lines (172 loc) · 6.89 KB
/
helm-icons.el
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
;;; helm-icons.el --- Helm icons -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Ivan Yonchovski
;; Author: Ivan Yonchovski <yyoncho@gmail.com>
;; Contributor: Ellis Kenyő <me@elken.dev>
;; Keywords: convenience
;; Version: 0.1
;; URL: /~https://github.com/yyoncho/helm-icons
;; Package-Requires: ((emacs "25.1") (dash "2.14.1") (f "0.20.0") (treemacs "2.7"))
;; 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 candidate copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package plugs icons into `helm' standard functions.
;;; Code:
(require 'dash)
(require 'seq)
(require 'f)
(require 'treemacs-faces nil t)
(require 'treemacs-icons nil t)
(require 'treemacs-themes nil t)
(require 'all-the-icons nil t)
(require 'nerd-icons nil t)
(defgroup helm-icons nil
"Helm treemacs icons."
:group 'helm)
(defcustom helm-icons-mode->icon
'((dired-mode . dir-closed)
(emacs-lisp-mode . "el")
(spacemacs-buffer-mode . "el"))
"Lookup Emacs mode -> `treemacs' icon key."
:type '(alist :key-type symbol :value-type sexp))
(defcustom helm-icons-provider
'treemacs
"Provider to load symbols from."
:type '(choice (const all-the-icons)
(const nerd-icons)
(const treemacs))
:group 'helm)
(with-eval-after-load 'treemacs-icons
(defun helm-icons--treemacs-icon (file)
"docstring"
(let ((icon (cond
((symbolp file) file)
((f-dir? file) 'dir-closed)
((f-file? file) (f-ext file)))))
(let* ((theme (treemacs--find-theme
(treemacs-theme->name
(treemacs-current-theme))))
(icons (treemacs-theme->gui-icons theme)))
(ht-get icons icon)))))
(defun helm-icons--get-icon (file)
"Get icon for FILE."
(cond ((eq helm-icons-provider 'all-the-icons)
(require 'all-the-icons)
(concat
(or (cond ((not (stringp file)) (all-the-icons-octicon "gear"))
((or
(member (f-base file) '("." ".."))
(f-dir? file))
(all-the-icons-octicon "file-directory")))
(all-the-icons-icon-for-file file))
" "))
((eq helm-icons-provider 'nerd-icons)
(require 'nerd-icons)
(concat
(or (cond ((not (stringp file)) (nerd-icons-octicon "nf-oct-gear"))
((or
(member (f-base file) '("." ".."))
(f-dir? file))
(nerd-icons-octicon "nf-oct-file_directory")))
(nerd-icons-icon-for-file file))
" "))
((eq helm-icons-provider 'treemacs)
(helm-icons--treemacs-icon file))))
(defun helm-icons--get-icon-for-mode (mode)
"Get icon for mode. First it will use the customized
helm-icons-mode->icon to resolve the icon, otherwise it tries to
use the provider."
(or (-some->> (assoc major-mode helm-icons-mode->icon)
(cl-rest)
helm-icons--get-icon)
(cond ((eq helm-icons-provider 'all-the-icons)
(-let ((icon (all-the-icons-icon-for-mode mode)))
(when (stringp icon) (concat icon " "))))
((eq helm-icons-provider 'nerd-icons)
(-let ((icon (nerd-icons-icon-for-mode mode)))
(when (stringp icon) (concat icon " "))))
(t nil))))
(defun helm-icons-buffers-add-icon (candidates _source)
"Add icon to buffers source.
CANDIDATES is the list of candidates."
(-map (-lambda ((display . buffer))
(cons (concat
(with-current-buffer buffer
(or (helm-icons--get-icon-for-mode major-mode)
(-some->> (buffer-file-name)
helm-icons--get-icon)
(helm-icons--get-icon 'fallback)))
display)
buffer))
candidates))
(defun helm-icons-files-add-icons (candidates _source)
"Add icon to files source.
CANDIDATES is the list of candidates."
(-map (-lambda (candidate)
(-let [(display . file-name) (if (listp candidate)
candidate
(cons candidate candidate))]
(cons (concat (cond
((helm-icons--get-icon file-name))
((helm-icons--get-icon 'fallback)))
display)
file-name)))
candidates))
(defun helm-icons-add-transformer (fn source)
"Add FN to `filtered-candidate-transformer' slot of SOURCE."
(setf (alist-get 'filtered-candidate-transformer source)
(-uniq (append
(-let [value (alist-get 'filtered-candidate-transformer source)]
(if (seqp value) value (list value)))
(list fn)))))
(defun helm-icons--make (orig name class &rest args)
"The advice over `helm-make-source'.
ORIG is the original function.
NAME, CLASS and ARGS are the original params."
(let ((result (apply orig name class args)))
(cl-case class
((helm-recentf-source helm-source-ffiles helm-locate-source helm-fasd-source helm-ls-git-source)
(helm-icons-add-transformer
#'helm-icons-files-add-icons
result))
((helm-source-buffers helm-source-projectile-buffer)
(helm-icons-add-transformer
#'helm-icons-buffers-add-icon
result)))
(cond
((or (-any? (lambda (source-name) (s-match source-name name))
'("Projectile files"
"Projectile projects"
"Projectile directories"
"Projectile recent files"
"Projectile files in current Dired buffer"
"dired-do-rename.*"
"Elisp libraries (Scan)")))
(helm-icons-add-transformer
#'helm-icons-files-add-icons
result)))
result))
(defun helm-icons--setup ()
"Setup icons based on which provider is set."
(cond ((eq helm-icons-provider 'all-the-icons)
(require 'all-the-icons))
((eq helm-icons-provider 'nerd-icons)
(require 'nerd-icons))
((eq helm-icons-provider 'treemacs)
(require 'treemacs-themes)
(require 'treemacs-icons))))
;;;###autoload
(defun helm-icons-enable ()
"Enable `helm-icons'."
(interactive)
(advice-add 'helm-make-source :around #'helm-icons--make)
(helm-icons--setup))
(provide 'helm-icons)
;;; helm-icons.el ends here