-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhover.el
237 lines (212 loc) · 10.4 KB
/
hover.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
(require 'pos-tip)
;; defface is not work great
(defface my-pos-tip-face
'((t :height 15.0 :family "monospace"))
"Face for pos-tip hover.")
(setq pos-tip-face 'my-pos-tip-face)
(setq pos-tip-foreground-color "#FFFFFF")
(setq pos-tip-background-color "#FFFFFF")
;; (setq pos-tip-background-color "yellow")
;; (setq pos-tip-border-color "purple")
;; (setq pos-tip-border-width 2)
;; (setq pos-tip-internal-border-width 1)
;; (setq pos-tip-use-relative-coordinates t)
;; (setq pos-tip-stay-near-point t)
(setq pos-tip-max-width 220)
(setq pos-tip-max-height 220)
;; naming is complicated TODO: change it
(defun my-momentarily-display-after-line-end (string &optional timeout)
(let ((ov (make-overlay (line-end-position) (line-end-position))))
(overlay-put
ov 'after-string
(concat (propertize " " 'display
`(space :align-to (- middle-fringe
,(1+ (length string)))))
(propertize string 'face '(:height 2.0))))
(sit-for (or timeout 5))
(delete-overlay ov)))
(defun my-momentarily-small-display-after-line-end (string &optional timeout)
(let ((ov (make-overlay (line-end-position) (line-end-position))))
(overlay-put
ov 'after-string
(concat (propertize " " 'display
`(space :align-to (- middle-fringe
,(1+ (length string)))))
(propertize string 'face '(:height 1.2))))
(sit-for (or timeout 5))
(delete-overlay ov)))
(defun my-momentarily-display-after-cursor-end (string &optional timeout)
(let ((ov (make-overlay (point) (point))))
(overlay-put ov 'after-string
(concat (propertize " " 'display
'(space :align-to (+ left-fringe 10)))
(propertize string 'display
'(raise -1)
'face '(:height 3.5))
"\n\n"))
(sit-for (or timeout 5))
(delete-overlay ov)))
;; It Also works on bioxiv.org
;; example: https://www.biorxiv.org/content/10.1101/2023.01.21.524489v1
(defun hover-arxiv-abstract ()
(interactive)
(let* ((url (thing-at-point 'url))
(script-path (expand-file-name "arxiv_abstract.py" (file-name-directory (locate-library "hover-arxiv-abstract"))))
(if (and url (string-match "rxiv" url))
(let ((output (shell-command-to-string (concat "python3 " script-path " " url))))
(my-momentarily-small-display-after-line-end output))
(message "No arxiv url found at cursor position."))))
(defun display-image-at-cursor ()
"Display an image at the cursor position in the current buffer."
(interactive)
(let* ((file (thing-at-point 'filename))
(image (create-image file)))
(if (and file (file-exists-p file))
(if image
(let ((overlay (make-overlay (point) (point))))
(overlay-put overlay 'before-string (propertize " " 'display image))
(run-at-time 3 nil (lambda (ov) (delete-overlay ov)) overlay))
(error "Failed to create image from file %s" file))
(error "No file path at point"))))
(defun display-image-from-user-input (file)
"Display image from user input"
(interactive "fImage file: ")
(if (file-exists-p file)
(let ((image (create-image file)))
(if image
(let ((overlay (make-overlay (point) (point))))
(overlay-put overlay 'before-string (propertize " " 'display image))
(run-at-time 3 nil (lambda (ov) (delete-overlay ov)) overlay))
(error "Failed to create image from file %s" file)))
(error "File %s does not exist" file)))
(defun display-image-from-python-script (sentence)
"Run a Python script, pass a sentence as an argument, and display the image generated by the script at the cursor position in the current buffer."
(interactive "sEnter a sentence: ")
(let* ((cwd (file-name-directory (buffer-file-name)))
(script "lukimage.py")
(output (shell-command-to-string (format "cd %s/image && python3 %s %s" cwd script (shell-quote-argument sentence))))
(file-path (progn
(string-match "\\(.*\\)\\.\\(jpg\\|png\\|gif\\)$" output)
(concat cwd "image/" (match-string 1 output) "." (match-string 2 output))))
)
(message (format "Output: %s" output))
(message (format "File path: %s" file-path))
(sleep-for 2)
(if (file-exists-p file-path)
(let ((image (create-image file-path)))
(if image
(let ((overlay (make-overlay (point) (point))))
(overlay-put overlay 'before-string (propertize " " 'display image))
(run-at-time 3 nil (lambda (ov) (delete-overlay ov)) overlay))
(error "Failed to create image from file %s" file)))
(error "File %s does not exist" file))))
(defun evil-display-image-from-python-script ()
"Run a Python script, pass a sentence as an argument, and display the image generated by the script at the cursor position in the current buffer."
(interactive)
(let* (
(sentence (buffer-substring (region-beginning) (region-end)))
(cwd "~/.spacemacs.d/image/")
(script "lukimage.py")
(output (shell-command-to-string (format "cd %s && python3 %s %s" cwd script (shell-quote-argument sentence))))
(file-path (progn
(string-match "\\(.*\\)\\.\\(jpg\\|png\\|gif\\)$" output)
(concat cwd (match-string 1 output) "." (match-string 2 output))))
)
(message (format "Output: %s" output))
(message (format "File path: %s" file-path))
(sleep-for 2)
(if (file-exists-p file-path)
(let ((image (create-image file-path)))
(if image
(let ((overlay (make-overlay (point) (point))))
(overlay-put overlay 'before-string (propertize " " 'display image))
(run-at-time 3 nil (lambda (ov) (delete-overlay ov)) overlay))
(error "Failed to create image from file %s" file)))
(error "File %s does not exist" file))))
(defun evil-display-image-from-bing-script ()
"Run a bing script, pass a sentence as an argument, and display the image generated by the script at the cursor position in the current buffer."
(interactive)
(let* (
(sentence (buffer-substring (region-beginning) (region-end)))
(cwd (file-name-directory (buffer-file-name)))
(script "bing_lukimage.py")
(output (shell-command-to-string (format "cd %s/image && python3 %s %s" cwd script (shell-quote-argument sentence))))
(file-path (progn
(string-match "\\(.*\\)\\.\\(jpg\\|png\\|gif\\)$" output)
(concat cwd (match-string 1 output) "." (match-string 2 output))))
)
(message (format "Output: %s" output))
(message (format "File path: %s" file-path))
(sleep-for 2)
(if (file-exists-p file-path)
(let ((image (create-image file-path)))
(if image
(let ((overlay (make-overlay (point) (point))))
(overlay-put overlay 'before-string (propertize " " 'display image))
(run-at-time 3 nil (lambda (ov) (delete-overlay ov)) overlay))
(error "Failed to create image from file %s" file)))
(error "File %s does not exist" file))))
(defun evil-buffer-diagram-image-from-bing-script ()
"Run a bing script, pass a sentence as an argument, and display the image generated by the script at the new buffer."
(interactive)
(let* (
(sentence (buffer-substring (region-beginning) (region-end)))
(sentence (concat "diagram of " sentence ))
(message sentence)
(cwd (file-name-directory (buffer-file-name)))
(script "bing_lukimage.py")
(output (shell-command-to-string (format "cd %s/image && python3 %s %s" cwd script (shell-quote-argument sentence))))
(file-path (progn
(string-match "\\(.*\\)\\.\\(jpg\\|png\\|gif\\)$" output)
(concat cwd (match-string 1 output) "." (match-string 2 output))))
)
(message (format "Output: %s" output))
(message (format "File path: %s" file-path))
(sleep-for 2)
(if (file-exists-p file-path)
(let ((image-buffer (get-buffer-create "*image*")))
(switch-to-buffer-other-window image-buffer)
(setq buffer-read-only nil)
(erase-buffer)
(let ((image (create-image file-path)))
(if image
(insert-image image)
(error "Failed to create image from file %s" file))))
(error "File %s does not exist" file))))
(defun evil-hover-diagram-image-from-bing-script ()
"Run a bing script, pass a sentence as an argument, and display the image generated by the script at the cursor position in the current buffer."
(interactive)
(let* (
(sentence (buffer-substring (region-beginning) (region-end)))
(sentence (concat "diagram of " sentence ))
(message sentence)
(cwd (file-name-directory (buffer-file-name)))
(script "bing_lukimage.py")
(output (shell-command-to-string (format "cd %s/image && python3 %s %s" cwd script (shell-quote-argument sentence))))
(file-path (progn
(string-match "\\(.*\\)\\.\\(jpg\\|png\\|gif\\)$" output)
(concat cwd (match-string 1 output) "." (match-string 2 output))))
)
(message (format "Output: %s" output))
(message (format "File path: %s" file-path))
;; change sleep time
(sleep-for 2)
(if (file-exists-p file-path)
(let ((image (create-image file-path)))
(if image
;; change overlay time
(let ((overlay (make-overlay (point) (point))))
(overlay-put overlay 'before-string (propertize " " 'display image))
(run-at-time 7 nil (lambda (ov) (delete-overlay ov)) overlay))
(error "Failed to create image from file %s" file)))
(error "File %s does not exist" file))))
(defun my-specific-mode-hook ()
(interactive)
(add-hook 'post-command-hook 'hover_arxiv_abstract nil t))
(add-hook 'my-specific-mode-hook 'my-specific-mode-hook)
(defun my-escape-specific-mode ()
(interactive)
(my-specific-mode -1))
(defun current_time ()
(interactive)
(my-momentarily-display-after-line-end (shell-command-to-string "date +%T")))