-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-imgtog.el
149 lines (122 loc) · 5.71 KB
/
org-imgtog.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
;;; org-imgtog.el --- Auto-toggle inline images in org-mode -*- lexical-binding: t -*-
;; Copyright (C) 2023 Dean Gao - MIT License
;; Author: Dean Gao <[email protected]>
;; Description: Automatically toggle Org mode inline images as your cursor enters and exits them.
;; Homepage: https://github.com/gaoDean/org-imgtog
;; Package-Requires: ((emacs "27.1"))
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Commentary:
;; This package automates toggling Org mode inline images much like org-fragtog.
;; Inline images are hidden for editing when your cursor steps onto them, and
;; re-enabled when the cursor leaves.
;;; Code:
(require 'org)
(require 'org-element)
(defgroup org-imgtog nil
"Auto toggle inline images in org-mode."
:group 'org)
(defcustom org-imgtog-preview-delay 0.0
"Seconds of delay before image is toggled."
:group 'org-imgtog
:type 'number)
(defcustom org-imgtog-preview-delay-only-remote nil
"Only apply preview delay to remote inline images.
If nil, apply to all images.
This is for users of the remote inline images
who do not turn on caching."
:group 'org-imgtog
:type 'boolean)
(defvar-local org-imgtog--prev-hidden-img-elem nil
"The previous image that was hidden")
;;;###autoload
(define-minor-mode org-imgtog-mode
"A minor mode that automatically toggles Org mode inline images.
They are hidden when your cursor steps onto them for easier editing,
and shown after your cursor leaves."
:init-value nil
;; Fix nil error in `org-element-context'
;; when using `org-imgtog' without Org mode.
;; Taken from org-fragtog
(setq org-complex-heading-regexp (or org-complex-heading-regexp ""))
(if org-imgtog-mode
(add-hook 'post-command-hook #'org-imgtog--post-cmd nil t)
(remove-hook 'post-command-hook #'org-imgtog--post-cmd t)))
(defun org-imgtog--remote-image-p ()
"Check if the point is on a remote image."
(let ((elem (org-element-context))
(image-file-regex (image-file-name-regexp)))
(and (eq (car elem) 'link)
(or (string= (org-element-property :type elem) "http")
(string= (org-element-property :type elem) "https"))
(string-match-p image-file-regex (org-element-property :path elem)))))
(defun org-imgtog--on-image-p ()
"Check if the point is on an image. Returns element context"
(let ((elem (org-element-context))
(image-file-regex (image-file-name-regexp)))
(and (eq (car elem) 'link)
(or (string= (org-element-property :type elem) "file")
(string= (org-element-property :type elem) "http")
(string= (org-element-property :type elem) "https"))
(string-match-p image-file-regex (org-element-property :path elem))
(org-element-context))))
(defun org-imgtog--img-point (elem)
"Gets the beginging and end of the image under the cursor"
(let* ((start (org-element-property :begin elem))
(end (org-element-property :end elem)))
(cons start end)))
(defun org-imgtog--hide-img (elem)
"Hide the image at point"
(setq org-imgtog--prev-hidden-img-elem elem)
(if (> org-imgtog-preview-delay 0)
(when (org-imgtog--on-image-p)
(let ((start-end (org-imgtog--img-point elem)))
(org-remove-inline-images (car start-end) (cdr start-end))))
(let ((start-end (org-imgtog--img-point elem)))
(org-remove-inline-images (car start-end) (cdr start-end)))))
(defun org-imgtog--show-img (elem)
"Show the image at point"
(setq org-imgtog--prev-hidden-img-elem nil)
(let ((start-end (org-imgtog--img-point elem)))
(org-display-inline-images nil nil (car start-end) (cdr start-end))))
(defun org-imgtog--hide-img-with-delay (elem)
(run-with-timer org-imgtog-preview-delay
nil
#'org-imgtog--hide-img
elem))
(defun org-imgtog--post-cmd ()
"Runs after `post-command-hook`. Handles toggling.
Image hidden and cursor not on image -> show image
Image not hidden and cursor on image -> hide image"
(let ((hidden-img org-imgtog--prev-hidden-img-elem) ;; is there a hidden image
(cursor-on-img (org-imgtog--on-image-p))) ;; is cursor on image
(when (and hidden-img (not cursor-on-img))
(org-imgtog--show-img hidden-img))
(when (and cursor-on-img (not hidden-img))
(cond ((and
(> org-imgtog-preview-delay 0)
org-imgtog-preview-delay-only-remote
(org-imgtog--remote-image-p))
(org-imgtog--hide-img-with-delay cursor-on-img))
((and
(> org-imgtog-preview-delay 0)
(not org-imgtog-preview-delay-only-remote))
(org-imgtog--hide-img-with-delay cursor-on-img))
(t (org-imgtog--hide-img cursor-on-img))))))
(provide 'org-imgtog)
;;; org-imgtog.el ends here