-
Notifications
You must be signed in to change notification settings - Fork 0
/
dired-file-preview.el
294 lines (261 loc) · 12.1 KB
/
dired-file-preview.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
;;; dired-file-preview.el --- File preview support for dired -*- lexical-binding: t -*-
;; Author: SpringHan
;; Maintainer: SpringHan
;; Version: 1.0
;; Package-Requires: ((emacs "27.1"))
;; Homepage: https://github.com/SpringHan/dired-file-preview.git
;; Keywords: files
;; This file is not part of GNU Emacs
;; This file 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, 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; File preview support for dired.
;;; Code:
(require 'dired-x)
(defcustom dired-file-preview-preview-buffer nil
"The buffer name of preview buffer."
:type 'string
:group 'dired)
(defcustom dired-file-preview-new-tab-p nil
"If current dired window and preview window are in a new tab."
:type 'boolean
:group 'dired)
(defcustom dired-file-preview-current-tab nil
"The tab of dired and its preview buffer."
:type 'cons
:group 'dired)
(defcustom dired-file-preview-window nil
"The preview window."
:type 'window
:group 'dired)
(defcustom dired-file-preview-sync-file-timer nil
"The timer to sync file."
:type 'timer
:group 'dired)
(defcustom dired-file-preview-current-file nil
"Current preview file."
:type 'string
:group 'dired)
(defcustom dired-file-preview-hide-details-p nil
"If the dired defaults to hide details."
:type 'boolean
:group 'dired)
(defcustom dired-file-preview-literal-p t
"If preview file without highlight."
:type 'boolean
:group 'dired)
;;;###autoload
(define-minor-mode dired-file-preview-mode
"File preview mode for dired."
:group 'dired
(if dired-file-preview-mode
(progn
(if dired-hide-details-mode
(setq dired-file-preview-hide-details-p t)
(dired-hide-details-mode t)
(setq dired-file-preview-hide-details-p nil))
(dired-file-preview--setup))
(cancel-timer dired-file-preview-sync-file-timer)
(delete-window dired-file-preview-window)
(kill-buffer dired-file-preview-preview-buffer)
(setq-local dired-file-preview-sync-file-timer nil
dired-file-preview-window nil
dired-file-preview-preview-buffer nil
dired-file-preview-current-file nil)
(unless dired-file-preview-hide-details-p
(dired-hide-details-mode -1))))
(defun dired-file-preview--setup ()
"File preview setup."
(unless dired-file-preview-preview-buffer
(let ((dired-buffer (current-buffer))
(width (number-to-string (- (* 0.7 (frame-width)))))
(buffer-name (format "*Dired-Preview:%s*" (buffer-name))))
(when (dired-file-preview--other-windows-p)
(setq dired-file-preview-new-tab-p t)
(tab-bar-new-tab)
(switch-to-buffer dired-buffer))
(setq-local dired-file-preview-window
(split-window nil
(string-to-number
(progn
(string-match "\\(.*\\)\\.\\(.*\\)" width)
(match-string 1 width)))
t)
dired-file-preview-preview-buffer buffer-name
dired-file-preview-current-tab (tab-bar--current-tab-index))
(select-window dired-file-preview-window)
(switch-to-buffer dired-file-preview-preview-buffer)
(other-window 1)
(setq-local dired-file-preview-sync-file-timer
(run-with-timer
0 0.2 #'dired-file-preview-sync-file)))))
(defun dired-file-preview-sync-file ()
"Sync current file."
(if (memq major-mode '(dired-mode wdired-mode))
(if dired-file-preview-mode
(progn
(when (eq major-mode 'dired-mode)
(let ((current-file (ignore-errors (dired-get-filename)))
file-mode)
(when (and current-file
(not (file-directory-p current-file))
(not (equal current-file dired-file-preview-current-file)))
(setq file-mode (assoc-default current-file
auto-mode-alist
'string-match))
(with-current-buffer dired-file-preview-preview-buffer
(when (eq major-mode 'image-mode)
(text-mode))
(setq-local buffer-read-only nil)
(erase-buffer)
(insert-file-contents current-file)
(if (eq file-mode 'image-mode)
(image-mode)
(unless dired-file-preview-literal-p
(eval
`(progn
(setq-local ,(intern (concat (symbol-name file-mode) "-hook"))
nil)
(,file-mode)))))
(goto-char (point-min)))
(setq-local dired-file-preview-current-file current-file))))
(let (width preview-buffer current-window windows)
(unless (dired-file-preview--window-exists-in-tab
(get-buffer-window dired-file-preview-preview-buffer))
(unless (eq dired-file-preview-current-tab (tab-bar--current-tab-index))
(tab-bar-close-tab dired-file-preview-current-tab)
(setq-local dired-file-preview-current-tab (tab-bar--current-tab-index))
(message "[Dired-File-Preview]: Deleted the former tab which includes dired & preview buffer."))
(setq width (number-to-string (- (* 0.7 (frame-width))))
preview-buffer dired-file-preview-preview-buffer
current-window (selected-window)
windows (dired-file-preview--other-windows-p t))
(cond ((and (= windows 2)
(prog2 (other-window 1)
(memq major-mode '(dired-mode wdired-mode))
(select-window current-window)))
;; Check if there're two dired window in current tab,
;; If it's true, delete another one.
(delete-other-windows current-window))
((> windows 1)
(tab-bar-new-tab)
(switch-to-buffer (current-buffer))
(setq dired-file-preview-new-tab-p t)
(setq-local dired-file-preview-current-tab
(tab-bar--current-tab-index))))
(setq-local dired-file-preview-window
(split-window nil
(string-to-number
(progn
(string-match "\\(.*\\)\\.\\(.*\\)" width)
(match-string 1 width)))
t))
(select-window dired-file-preview-window)
(switch-to-buffer preview-buffer)
(select-window current-window))))
(let ((preview-buffer (dired-file-preview--get-preview-buffer))
(buffer-name (buffer-name)))
(if preview-buffer
(unless (dired-file-preview--get-dired-buffer preview-buffer)
(with-current-buffer buffer-name
(setq-local dired-file-preview-mode t)
(when (null dired-hide-details-mode)
(dired-hide-details-mode t)))
(with-current-buffer preview-buffer
(rename-buffer (format "*Dired-Preview:%s*" buffer-name)))
(with-current-buffer buffer-name
(setq-local dired-file-preview-sync-file-timer
(dired-file-preview--get-preview-timer)
dired-file-preview-preview-buffer preview-buffer
dired-file-preview-window
(get-buffer-window preview-buffer)
dired-file-preview-current-tab (tab-bar--current-tab-index)
dired-file-preview-current-file nil)))
(cancel-timer (dired-file-preview--get-preview-timer))
(with-current-buffer buffer-name
(setq-local dired-file-preview-mode t)
(when (null dired-hide-details-mode)
(dired-hide-details-mode t))
(delete-other-windows)
(dired-file-preview--setup)))))
(let* ((preview-buffer (dired-file-preview--get-preview-buffer))
(dired-buffer (when preview-buffer
(dired-file-preview--get-dired-buffer
preview-buffer)))
tmp)
(if dired-buffer
(when (and (not (minibufferp))
(not (eq (current-buffer) preview-buffer)))
(cond ((and (setq tmp (get-buffer-window preview-buffer))
(dired-file-preview--window-exists-in-tab tmp))
;; To check if the current buffer is neither current dired buffer nor preview buffer
(delete-window tmp))
((dired-file-preview--window-exists-in-tab
(setq tmp (get-buffer-window dired-buffer)))
(delete-window tmp))))
(cancel-timer (dired-file-preview--get-preview-timer))
(when preview-buffer
(kill-buffer preview-buffer)
(if dired-file-preview-new-tab-p
(tab-bar-close-tab dired-file-preview-current-tab)
(delete-other-windows)))))))
(defun dired-file-preview--get-preview-buffer ()
"Get the preview buffer in `buffer-list'."
(catch 'buffer
(dolist (buffer (buffer-list))
(when (string-match-p "\\*Dired-Preview:\\(.*\\)\\*" (buffer-name buffer))
(throw 'buffer buffer)))))
(defun dired-file-preview--get-dired-buffer (preview-buffer)
"Get the dired buffer by PREVIEW-BUFFER."
(when (bufferp preview-buffer)
(setq preview-buffer (buffer-name preview-buffer)))
(catch 'buffer
(dolist (buffer dired-buffers)
(when (and (stringp (buffer-name (cdr buffer)))
(string= (buffer-name (cdr buffer))
(progn
(string-match "\\*Dired-Preview:\\(.*\\)\\*" preview-buffer)
(match-string 1 preview-buffer))))
(throw 'buffer (cdr buffer))))))
(defun dired-file-preview--window-exists-in-tab (window)
"Check if the WINDOW is exists in current tab."
(catch 'result
(let ((start-window (selected-window)))
(when (eq start-window window)
(throw 'result t))
(other-window 1)
(while (not (eq start-window (selected-window)))
(when (eq (selected-window) window)
(select-window start-window)
(throw 'result t))
(other-window 1)))))
(defun dired-file-preview--get-preview-timer ()
"Get the preview timer in `timer-list'."
(catch 'result
(dolist (timer timer-list)
(when (eq (timer--function timer) 'dired-file-preview-sync-file)
(throw 'result timer)))))
(defun dired-file-preview--other-windows-p (&optional get-number)
"To check if there're other windows in current tab.
If GET-NUMBER is non-nil, return the number of windows in current tab."
(let ((current-window (get-buffer-window))
(num 1))
(other-window 1)
(if get-number
(progn
(while (not (eq (get-buffer-window) current-window))
(setq num (1+ num))
(other-window 1))
num)
(prog1 (not (eq current-window (get-buffer-window)))
(select-window current-window)))))
(provide 'dired-file-preview)
;;; dired-file-preview.el ends here