forked from jtbm37/all-the-icons-dired
-
Notifications
You must be signed in to change notification settings - Fork 6
/
all-the-icons-dired.el
142 lines (119 loc) · 5.32 KB
/
all-the-icons-dired.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
;;; all-the-icons-dired.el --- Shows icons for each file in dired mode -*- lexical-binding: t; -*-
;; Copyright (C) 2016-2020 jtbm37
;; Copyright (C) 2021 Jimmy Yuen Ho Wong
;; Author: jtbm37
;; Maintainer: Jimmy Yuen Ho Wong <wyuenho@gmail.com>
;; Version: 2.0
;; Keywords: files icons dired
;; Package-Requires: ((emacs "26.1") (all-the-icons "2.2.0"))
;; URL: https://github.com/wyuenho/all-the-icons-dired
;; 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, simply add this to your init.el:
;; (add-hook 'dired-mode-hook 'all-the-icons-dired-mode)
;; To manually install, add this to your init.el before the hook mentioned above.
;; (add-to-load-path (expand-file-name "~/path/to/all-the-icons-dired"))
;; (load "all-the-icons-dired.el")
;;; Code:
(require 'dired)
(require 'all-the-icons)
(require 'subr-x)
(require 'image)
(require 'jit-lock)
(require 'font-core)
(require 'font-lock)
(require 'map)
(defface all-the-icons-dired-dir-face
'((t (:inherit dired-directory)))
"Face for the directory icon."
:group 'all-the-icons-faces)
(defcustom all-the-icons-dired-lighter " all-the-icons-dired-mode"
"Lighter of all-the-icons-dired-mode"
:group 'all-the-icons
:type 'string)
(defcustom all-the-icons-dired-v-adjust 0.01
"The default vertical adjustment of the icon in the Dired buffer."
:group 'all-the-icons
:type 'number)
(defcustom all-the-icons-dired-monochrome t
"Whether to show the icons as the same color as the text on the same line."
:group 'all-the-icons
:type 'boolean)
(defvar all-the-icons-dired-mode)
(defun all-the-icons-dired--icon (file)
"Return the icon for FILE."
(if (file-directory-p file)
(all-the-icons-icon-for-dir file
:face 'all-the-icons-dired-dir-face
:v-adjust all-the-icons-dired-v-adjust)
(apply 'all-the-icons-icon-for-file file
(append
`(:v-adjust ,all-the-icons-dired-v-adjust)
(when all-the-icons-dired-monochrome
`(:face ,(face-at-point)))))))
(defun all-the-icons-dired--put-icon (pos)
"Propertize POS with icon."
(let* ((file (dired-get-filename 'relative 'noerror))
(icon (all-the-icons-dired--icon file))
(image (copy-sequence (get-text-property 0 'display icon)))
(props (map-delete (copy-sequence (text-properties-at 0 icon)) 'display)))
(if (or (not (eq (car image) 'image)) (member file '("." "..")))
(put-text-property (1- pos) pos 'display
(if (member file '("." ".."))
" "
(concat " " icon " ")))
(setf (image-property image :margin) (cons (/ (window-text-width nil t) (window-text-width)) 0))
(add-text-properties (1- pos) pos (append props `(display ,image) )))))
(defun all-the-icons-dired--fontify-region (start end &optional loudly)
"Add icons using text properties from START to END.
START, END and the optional argument LOUDLY is passed to
`font-lock-default-fontify-region'."
(let ((extended-region (font-lock-default-fontify-region start end loudly)))
(when (and (consp extended-region)
(eq (car extended-region) 'jit-lock-bounds))
(setq start (cadr extended-region))
(setq end (cddr extended-region)))
(with-silent-modifications
(save-excursion
(goto-char start)
(while (< (point) end)
(when-let ((pos (dired-move-to-filename)))
(all-the-icons-dired--put-icon pos))
(forward-line 1))))
extended-region))
(defun all-the-icons-dired--setup ()
"Set up `all-the-icons-dired'."
(add-function :override (local 'font-lock-fontify-region-function) #'all-the-icons-dired--fontify-region)
(setq-local font-lock-extra-managed-props (cons 'display font-lock-extra-managed-props))
(cond (jit-lock-mode
(jit-lock-refontify))
(font-lock-mode
(font-lock-fontify-region (point-min) (point-max)))))
(defun all-the-icons-dired--teardown ()
"Tear down `all-the-icons-dired'."
(font-lock-unfontify-buffer)
(remove-function (local 'font-lock-fontify-region-function) #'all-the-icons-dired--fontify-region)
(setq-local font-lock-extra-managed-props (remove 'display font-lock-extra-managed-props))
(cond (jit-lock-mode
(jit-lock-refontify))
(font-lock-mode
(font-lock-fontify-region (point-min) (point-max)))))
;;;###autoload
(define-minor-mode all-the-icons-dired-mode
"Display all-the-icons icon for each file in a Dired buffer."
:lighter all-the-icons-dired-lighter
(when (derived-mode-p 'dired-mode)
(if all-the-icons-dired-mode
(all-the-icons-dired--setup)
(all-the-icons-dired--teardown))))
(provide 'all-the-icons-dired)
;;; all-the-icons-dired.el ends here