-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnerd-icons-completion.el
151 lines (131 loc) · 6.12 KB
/
nerd-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
144
145
146
147
148
149
150
151
;;; nerd-icons-completion.el --- Add icons to completion candidates -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Hongyu Ding <[email protected]>
;; Author: Hongyu Ding <[email protected]>
;; Keywords: lisp
;; Version: 0.0.1
;; Package-Requires: ((emacs "25.1") (nerd-icons "0.0.1") (compat "30"))
;; URL: https://github.com/rainstormstudio/nerd-icons-completion
;; Keywords: convenient, files, icons
;; 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 nerd-icons to completion candidates.
;; nerd-icons-completion is inspired by
;; `all-the-icons-completion': https://github.com/iyefrat/all-the-icons-completion
;;; Code:
(require 'nerd-icons)
(require 'compat)
(defgroup nerd-icons-completion nil
"Add icons to completion candidates."
:group 'appearance
:group 'convenience
:prefix "nerd-icons-completion")
(defface nerd-icons-completion-dir-face
'((t nil))
"Face for the directory icon."
:group 'nerd-icons-faces)
(cl-defgeneric nerd-icons-completion-get-icon (_cand _cat)
"Return the icon for the candidate CAND of completion category CAT."
"")
(cl-defmethod nerd-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
(nerd-icons-icon-for-dir cand :face 'nerd-icons-completion-dir-face)
" "))
(t (concat (nerd-icons-icon-for-file cand) " "))))
(cl-defmethod nerd-icons-completion-get-icon (cand (_cat (eql project-file)))
"Return the icon for the candidate CAND of completion category project-file."
(nerd-icons-completion-get-icon cand 'file))
(cl-defmethod nerd-icons-completion-get-icon (cand (_cat (eql buffer)))
"Return the icon for the candidate CAND of completion category buffer."
(let* ((mode (buffer-local-value 'major-mode (get-buffer cand)))
(icon (nerd-icons-icon-for-mode mode))
(parent-icon (nerd-icons-icon-for-mode
(get mode 'derived-mode-parent))))
(concat
(if (symbolp icon)
(if (symbolp parent-icon)
(nerd-icons-faicon "nf-fa-sticky_note_o")
parent-icon)
icon)
" ")))
(autoload 'bookmark-get-filename "bookmark")
(cl-defmethod nerd-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)))
(nerd-icons-completion-get-icon fname 'file)
(concat (nerd-icons-octicon "nf-oct-bookmark" :face 'nerd-icons-completion-dir-face) " ")))
(defun nerd-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 (nerd-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 (nerd-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 (nerd-icons-completion-get-icon (cdr orig) (car orig)) "")))
cands)))
(cat
(lambda (cands)
(mapcar (lambda (x)
(list x (nerd-icons-completion-get-icon x cat) ""))
cands)))
(aff)))
(funcall orig metadata prop)))
;; For the byte compiler
(defvar marginalia-mode)
;;;###autoload
(defun nerd-icons-completion-marginalia-setup ()
"Hook to `marginalia-mode-hook' to bind `nerd-icons-completion-mode' to it."
(nerd-icons-completion-mode (if marginalia-mode 1 -1)))
;;;###autoload
(define-minor-mode nerd-icons-completion-mode
"Add icons to completion candidates."
:global t
(if nerd-icons-completion-mode
(progn
(advice-add #'completion-metadata-get :around #'nerd-icons-completion-completion-metadata-get)
(advice-add (compat-function completion-metadata-get) :around #'nerd-icons-completion-completion-metadata-get))
(progn
(advice-remove #'completion-metadata-get #'nerd-icons-completion-completion-metadata-get)
(advice-remove (compat-function completion-metadata-get) #'nerd-icons-completion-completion-metadata-get))))
(provide 'nerd-icons-completion)
;;; nerd-icons-completion.el ends here