-
Notifications
You must be signed in to change notification settings - Fork 11
/
all-the-icons-completion.el
143 lines (131 loc) · 5.71 KB
/
all-the-icons-completion.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
;;; all-the-icons-completion.el --- Add icons to completion candidates -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2021 Itai Y. Efrat
;;
;; Author: Itai Y. Efrat <https://github.com/iyefrat>
;; Maintainer: Itai Y. Efrat <[email protected]>
;; Created: June 06, 2021
;; Modified: June 06, 2021
;; Version: 1.0
;; Keywords: convenient, lisp
;; Homepage: https://github.com/iyefrat/all-the-icons-completion
;; Package-Requires: ((emacs "26.1") (all-the-icons "5.0"))
;;
;; This file is not part of GNU Emacs.
;;
;; Licence:
;;
;; 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:
;;
;; Add icons to completion candidates.
;;
;;; Code:
(require 'all-the-icons)
(defgroup all-the-icons-completion nil
"Add icons to completion candidates."
:version "26.1"
:group 'appearance
:group 'convenience
:prefix "all-the-icons-completion")
(defface all-the-icons-completion-dir-face
'((t nil))
"Face for the directory icon."
:group 'all-the-icons-faces)
(cl-defgeneric all-the-icons-completion-get-icon (_cand _cat)
"Return the icon for the candidate CAND of completion category CAT."
"")
(cl-defmethod all-the-icons-completion-get-icon (cand (_cat (eql file)))
"Return the icon for the candidate CAND of completion category file."
(cond ((string-match-p "\\/$" cand)
(concat
(all-the-icons-icon-for-dir cand :face 'all-the-icons-completion-dir-face)
" "))
(t (concat (all-the-icons-icon-for-file cand) " "))))
(cl-defmethod all-the-icons-completion-get-icon (cand (_cat (eql project-file)))
"Return the icon for the candidate CAND of completion category project-file."
(all-the-icons-completion-get-icon cand 'file))
(cl-defmethod all-the-icons-completion-get-icon (cand (_cat (eql buffer)))
"Return the icon for the candidate CAND of completion category buffer."
(with-current-buffer cand
(let ((icon (all-the-icons-icon-for-buffer)))
(concat (if (stringp icon) icon (all-the-icons-faicon "sticky-note-o")) " "))))
(autoload 'bookmark-get-filename "bookmark")
(cl-defmethod all-the-icons-completion-get-icon (cand (_cat (eql bookmark)))
"Return the icon for the candidate CAND of completion category bookmark."
(if-let (fname (bookmark-get-filename cand))
(all-the-icons-completion-get-icon fname 'file)
(concat (all-the-icons-octicon "bookmark" :face 'all-the-icons-completion-dir-face) " ")))
(defun all-the-icons-completion-completion-metadata-get (orig metadata prop)
"Meant as :around advice for `completion-metadata-get', Add icons as prefix.
ORIG should be `completion-metadata-get'
METADATA is the metadata.
PROP is the property which is looked up."
(if (eq prop 'affixation-function)
(let ((cat (funcall orig metadata 'category))
(aff (or (funcall orig metadata 'affixation-function)
(when-let ((ann (funcall orig metadata 'annotation-function)))
(lambda (cands)
(mapcar (lambda (x) (list x "" (funcall ann x))) cands))))))
(cond
((and (eq cat 'multi-category) aff)
(lambda (cands)
(mapcar (lambda (x)
(pcase-exhaustive x
(`(,cand ,prefix ,suffix)
(let ((orig (get-text-property 0 'multi-category cand)))
(list cand
(concat (all-the-icons-completion-get-icon (cdr orig) (car orig))
prefix)
suffix)))))
(funcall aff cands))))
((and cat aff)
(lambda (cands)
(mapcar (lambda (x)
(pcase-exhaustive x
(`(,cand ,prefix ,suffix)
(list cand
(concat (all-the-icons-completion-get-icon cand cat)
prefix)
suffix))))
(funcall aff cands))))
((eq cat 'multi-category)
(lambda (cands)
(mapcar (lambda (x)
(let ((orig (get-text-property 0 'multi-category x)))
(list x (all-the-icons-completion-get-icon (cdr orig) (car orig)) "")))
cands)))
(cat
(lambda (cands)
(mapcar (lambda (x)
(list x (all-the-icons-completion-get-icon x cat) ""))
cands)))
(aff)))
(funcall orig metadata prop)))
;; For the byte compiler
(defvar marginalia-mode)
;;;###autoload
(defun all-the-icons-completion-marginalia-setup ()
"Hook to `marginalia-mode-hook' to bind `all-the-icons-completion-mode' to it."
(all-the-icons-completion-mode (if marginalia-mode 1 -1)))
;;;###autoload
(define-minor-mode all-the-icons-completion-mode
"Add icons to completion candidates."
:global t
(if all-the-icons-completion-mode
(advice-add #'completion-metadata-get :around #'all-the-icons-completion-completion-metadata-get)
(advice-remove #'completion-metadata-get #'all-the-icons-completion-completion-metadata-get)))
(provide 'all-the-icons-completion)
;;; all-the-icons-completion.el ends here