-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpreview-it.el
More file actions
294 lines (243 loc) · 9.2 KB
/
Copy pathpreview-it.el
File metadata and controls
294 lines (243 loc) · 9.2 KB
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
;;; preview-it.el --- Preview anything at point -*- lexical-binding: t; -*-
;; Copyright (C) 2020-2026 Shen, Jen-Chieh
;; Created date 2020-10-13 12:46:22
;; Author: Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/jcs-elpa/preview-it
;; Version: 1.1.0
;; Package-Requires: ((emacs "26.1") (posframe "1.1.7") (request "0.3.0") (gh-md "0.1.1"))
;; Keywords: convenience preview image path file
;; This file is NOT part of GNU Emacs.
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This package is to attempt to preview any data as possible. Data can
;; refers to `file', `image', or `url'.
;;
;; You may enabled this package by doing the following execution.
;;
;; `(preview-it-mode 1)`
;;
;; Or you can either enabled it globally by doing this.
;;
;; `(global-preview-it-mode 1)`
;;
;;; Code:
(require 'cl-lib)
(require 'faces)
(require 'ffap)
(require 'shr)
(require 'image-mode)
(require 'posframe)
(require 'request)
(require 'gh-md)
(defgroup preview-it nil
"Preview anything at point."
:prefix "preview-it-"
:group 'tool
:link '(url-link :tag "Repository" "https://github.com/jcs-elpa/preview-it"))
(defface preview-it-background
'((((background light)) :background "#E9EAED")
(t :background "#2A2D38"))
"Background color of the preview buffer."
:group 'preview-it)
(defcustom preview-it-delay 0.4
"Seconds delay to show preview."
:type 'float
:group 'preview-it)
(defcustom preview-it-render-md nil
"Set to non-nil, render markdown file."
:type 'boolean
:group 'preview-it)
(defcustom preview-it-color-text "▓▒░"
"String to display color."
:type 'string
:group 'preview-it)
(defconst preview-it--buffer-name "*preview-it*"
"Name of the preview buffer.")
(defvar preview-it--image-extensions
'("jpg" "png" "jpeg" "gif" "bmp" "svg")
"List of image extensions.")
(defvar preview-it--timer nil
"Display timer after hovering.")
(defvar preview-it--url-request nil
"Request when browsing URL.")
;;
;; (@* "Entry" )
;;
(defun preview-it--enable ()
"Enable 'preview-it-mode'."
(add-hook 'pre-command-hook #'preview-it--stop-preview nil t)
(add-hook 'post-command-hook #'preview-it--start-preview nil t))
(defun preview-it--disable ()
"Disable 'preview-it-mode'."
(remove-hook 'pre-command-hook #'preview-it--stop-preview t)
(remove-hook 'post-command-hook #'preview-it--start-preview t)
(preview-it--stop-preview))
;;;###autoload
(define-minor-mode preview-it-mode
"Minor mode 'preview-it-mode'."
:require 'preview-it
:group 'preview-it
(if preview-it-mode (preview-it--enable) (preview-it--disable)))
(defun preview-it--turn-on-preview-it-mode ()
"Turn on the 'preview-it-mode'."
(preview-it-mode 1))
;;;###autoload
(define-globalized-minor-mode global-preview-it-mode
preview-it-mode preview-it--turn-on-preview-it-mode
:require 'preview-it)
;;
;; (@* "Util" )
;;
(defmacro preview-it--mute-apply (&rest body)
"Execute BODY without message."
(declare (indent 0) (debug t))
`(let (message-log-max)
(with-temp-message (or (current-message) nil)
(let ((inhibit-message t)) ,@body))))
(defun preview-it--is-contain-list-string-regexp (in-list in-str)
"Check if IN-STR contain in any string in the IN-LIST."
(cl-some (lambda (lb-sub-str) (string-match-p lb-sub-str in-str)) in-list))
(defun preview-it--kill-timer (timer)
"Safe way to kill TIMER."
(when (timerp timer) (cancel-timer timer)))
(defun preview-it--text-file-p (filename)
"Return non-nil if FILENAME is a text file; not binary file."
(let ((inhibit-message t)message-log-max)
(with-current-buffer (find-file-noselect filename :no-warn)
(prog1 (not (eq buffer-file-coding-system 'no-conversion))
(kill-buffer)))))
(defun preview-it--is-file-p (path)
"Check if PATH a file path."
(and (file-exists-p path) (not (file-directory-p path))))
(defun preview-it--thing-at-point (thing &optional no-properties)
"Same with function `thing-at-point' but respect region.
Arguments THING and NO-PROPERTIES have same effect to function `thing-at-point'."
(if (use-region-p)
(if no-properties
(buffer-substring-no-properties (region-beginning) (region-end))
(buffer-substring (region-beginning) (region-end)))
(thing-at-point thing no-properties)))
(defmacro preview-it--with-preview-buffer (&rest body)
"Execute BODY inside preview buffer."
(declare (indent 0) (debug t))
`(with-current-buffer (get-buffer-create preview-it--buffer-name)
(let ((inhibit-read-only t)) ,@body)))
(defmacro preview-it--try-display (&rest body)
"Try to display frame after executing BODY form."
(declare (indent 0) (debug t))
`(progn
(preview-it--with-preview-buffer (erase-buffer))
,@body
(preview-it--show)))
;;
;; (@* "Url" )
;;
(defun preview-it--reset-request ()
"Reset URL request."
(when preview-it--url-request (request-abort preview-it--url-request)))
(cl-defun preview-it--receive-data (&key data &allow-other-keys)
"Callback after receiving URL DATA."
(preview-it--with-preview-buffer
(erase-buffer)
(insert data)
;; TODO: The check here is fragile. Need a much dedicated method to
;; detect html string.
(when (string-match-p "<!doctype html>" data)
(shr-render-region (point-min) (point-max))))
(preview-it--show))
;;
;; (@* "Core" )
;;
(defun preview-it--content-empty-p ()
"Return non-nil if content is empty."
(string-empty-p (preview-it--with-preview-buffer (buffer-string))))
(defun preview-it--save-window-layout (fnc &rest args)
"Advice execute around FNC, ARGS."
(save-window-excursion (apply fnc args))
(advice-remove 'gh-md--callback #'preview-it--save-window-layout)) ; Remove self
(defun preview-it--render-file (path)
"Render local file PATH."
(let ((ext (file-name-extension path)))
(cond
((member ext preview-it--image-extensions)
(preview-it--with-preview-buffer
(ignore-errors (insert-image-file path))
(image-mode)))
((and preview-it-render-md (member ext '("md" "markdown")))
(advice-add 'gh-md--callback :around #'preview-it--save-window-layout)
(let ((gh-md-buffer-name preview-it--buffer-name))
(preview-it--with-preview-buffer
(ignore-errors (insert-file-contents path))
(gh-md-render-buffer))))
;; TODO: This method is very slow, need to find other replacement.
((preview-it--text-file-p path)
(setq path (expand-file-name path))
(preview-it--with-preview-buffer
(insert-file-contents path)
(let ((buffer-file-name path)) (delay-mode-hooks (set-auto-mode)))
(ignore-errors (font-lock-ensure)))))))
(defun preview-it--render-url (path)
"Render http PATH."
(setq preview-it--url-request
(request
path
:type "GET"
:parser 'buffer-string
:success 'preview-it--receive-data)))
(defun preview-it--render-color (color)
"Render COLOR."
(preview-it--with-preview-buffer
(insert (propertize preview-it-color-text 'font-lock-face `(:foreground ,color)) " " color)))
;;;###autoload
(defun preview-it ()
"Preview thing at point."
(interactive)
(cond
((when-let ((path (ffap-guesser)))
(preview-it--try-display
(cond
((preview-it--is-file-p path) ; file
(preview-it--mute-apply (preview-it--render-file path)))
;; TODO: Not sure if there are other cases.
((string-match-p "http[s]*://" path) ; request
(preview-it--render-url path))))))
((when-let* ((text (preview-it--thing-at-point 'symbol t))
(color (color-values text)))
(preview-it--try-display
(preview-it--render-color text))))))
(defun preview-it--show ()
"Show preview frame."
(unless (preview-it--content-empty-p)
(posframe-show preview-it--buffer-name :position (point)
:border-width 10
:background-color (face-background 'preview-it-background))
(add-hook 'post-command-hook #'preview-it--post)))
(defun preview-it--next ()
"Hide tooltip after first post command."
(preview-it--stop-preview)
(remove-hook 'post-command-hook #'preview-it--next))
(defun preview-it--post ()
"Register for next post command."
(add-hook 'post-command-hook #'preview-it--next)
(remove-hook 'post-command-hook #'preview-it--post))
(defun preview-it--start-preview ()
"Trigger to start previewing."
(preview-it--stop-preview)
(preview-it--reset-request)
(preview-it--kill-timer preview-it--timer)
(setq preview-it--timer (run-with-idle-timer preview-it-delay nil #'preview-it)))
(defun preview-it--stop-preview ()
"Trigger to stop previewing."
(posframe-hide preview-it--buffer-name))
(provide 'preview-it)
;;; preview-it.el ends here