-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconsult-omni-embark.el
377 lines (311 loc) · 15.6 KB
/
consult-omni-embark.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
;;; consult-omni-embark.el --- Embark Actions for `consult-omni' -*- lexical-binding: t -*-
;; Copyright (C) 2024 Armin Darvish
;; Author: Armin Darvish
;; Maintainer: Armin Darvish
;; Created: 2024
;; Version: 0.2
;; Package-Requires: (
;; (emacs "28.1")
;; (consult "1.9")
;; (consult-omni "0.2"))
;;
;; Homepage: https://github.com/armindarvish/consult-omni
;; Keywords: convenience
;;; Commentary:
;; provides Embark actions for consult-omni
;;; Code:
;;; Requirements
(require 'consult-omni)
(require 'embark-consult)
;;; User Options (a.k.a. Custom Variables)
;; The following user options modify the behavior of consult-omni-embark.
(defcustom consult-omni-embark-default-term #'eshell
"Consult-omni default terminal to use in embark actions."
:group 'consult-omni
:type '(choice (function :tag "(Default) eshell" #'eshell)
(function :tag "shell" #'shell)
(function :tag "term" #'term)
(function :tag "ansi-term" #'ansi-term)
(function :tag "vterm" #'vterm)
(function :tag "vterm" #'eat)
(function :tag "Custom Function" function)))
;;; Define Embark Action Functions
;; Embark actions for general sources (e.g. search engines)
(defun consult-omni-embark-default-action (cand)
"Call the default action on CAND.
Gets the default callback function from `consult-omni--sources-alist'."
(let* ((source (and (stringp cand) (get-text-property 0 :source cand))))
(funcall (consult-omni--get-source-prop source :on-callback) cand)))
(add-to-list 'embark-default-action-overrides '(consult-omni . consult-omni-embark-default-action))
(defun consult-omni-embark-insert-title (cand)
"Insert the title of CAND at point."
(if-let ((title (and (stringp cand) (get-text-property 0 :title cand))))
(insert (format " %s " title))))
(defun consult-omni-embark-copy-title-as-kill (cand)
"Copy the title of CAND to `kill-ring'."
(if-let ((title (and (stringp cand) (get-text-property 0 :title cand))))
(kill-new (string-trim title))))
(defun consult-omni-embark-insert-url-link (cand)
"Insert the url link of CAND at point."
(let* ((url (and (stringp cand) (get-text-property 0 :url cand)))
(url (and (stringp url) (string-trim url)))
(title (and (stringp cand) (get-text-property 0 :title cand))))
(when url
(cond
((derived-mode-p 'org-mode)
(insert (cond
((and url title) (format " [[%s][%s]] " url title))
(url (format " [[%s]] " url))
(t ""))))
((derived-mode-p 'markdown-mode)
(insert (cond
((and url title) (format " [%s](%s) " url title))
(url (format " <%s> " url))
(t ""))))
(t
(insert (cond
((and url title) (format " %s (%s) " title url))
(url (format " %s " url))
(t ""))))))))
(defun consult-omni-embark-copy-url-as-kill (cand)
"Copy the url of CAND to `kill-ring'."
(if-let ((url (and (stringp cand) (get-text-property 0 :url cand))))
(kill-new (format " %s " (string-trim url)))))
(defun consult-omni-embark-external-browse-link (cand)
"Open the url of CAND with `consult-omni-default-browse-function'."
(if-let* ((url (and (stringp cand) (get-text-property 0 :url cand))))
(funcall consult-omni-default-browse-function url)))
(defun consult-omni-embark-alternate-browse-link (cand)
"Open the url of CAND with `consult-omni-alternate-browse-function'."
(if-let* ((url (and (stringp cand) (get-text-property 0 :url cand))))
(funcall consult-omni-alternate-browse-function url)))
(defun consult-omni-embark-external-browse-search-link (cand)
"Open the “search url” of CAND in the external browser.
The “search url” is the source search page withthe search term from CAND."
(if-let* ((search-url (and (stringp cand) (get-text-property 0 :search-url cand))))
(funcall #'browse-url search-url)))
(defun consult-omni-embark-show-preview (cand)
"Open a preview of CAND.
Gets the preview function from `consult-omni--sources-alist'."
(let* ((source (and (stringp cand) (get-text-property 0 :source cand))))
(funcall (consult-omni--get-source-prop source :on-preview) cand)))
;;; Define Embark Keymaps
(defvar-keymap consult-omni-embark-general-actions-map
:doc "Keymap for consult-omni-embark"
:parent embark-general-map
"i t" #'consult-omni-embark-insert-title
"i u" #'consult-omni-embark-insert-url-link
"w t" #'consult-omni-embark-copy-title-as-kill
"w u" #'consult-omni-embark-copy-url-as-kill
"o o" #'consult-omni-embark-external-browse-link
"o O" #'consult-omni-embark-alternate-browse-link
"o s" #'consult-omni-embark-external-browse-search-link
"o p" #'consult-omni-embark-show-preview)
(add-to-list 'embark-keymap-alist '(consult-omni . consult-omni-embark-general-actions-map))
;;; Scholar
;; Embark actions for sources searching academic article
(defcustom consult-omni-embark-scholar-make-note-func #'consult-omni-embark-scholar-default-note
"Function for making note template for scholar articles."
:group 'consult-omni
:type '(choice (function :tag "(Default) Make Note with Title, Link, Journal, Authors... " consult-omni-embark-scholar-default-note)
(function :tag "Custom Function")))
(defun consult-omni-embark-scholar-external-browse-doi (cand)
"Open the DOI url of CAND in external browser."
(if-let* ((doi (and (stringp cand) (get-text-property 0 :doi cand))))
(funcall #'browse-url (concat "https://doi.org/" doi))))
(defun consult-omni-embark-scholar-copy-authors-as-kill (cand)
"Copy the authors of CAND to `kill-ring'."
(if-let ((authors (and (stringp cand) (get-text-property 0 :authors cand))))
(kill-new (string-trim (format " %s " authors)))))
(defun consult-omni-embark-scholar-insert-authors (cand)
"Insert the authors of CAND at point."
(if-let ((authors (and (stringp cand) (get-text-property 0 :authors cand))))
(insert (string-trim (mapconcat #'identity authors ", ")))))
(defun consult-omni-embark-scholar-default-note (cand)
"Make note on CAND article.
This can be used for making notes for scholar articles."
(let* ((url (and (stringp cand) (get-text-property 0 :url cand)))
(url (and (stringp url) (string-trim url)))
(doi (and (stringp cand) (get-text-property 0 :doi cand)))
(doi (if (and doi (stringp doi)) (concat "https://doi.org/" doi)))
(source (and (stringp cand) (get-text-property 0 :source cand)))
(url (if (and (equal source "Scopus") doi)
doi
url))
(title (and (stringp cand) (get-text-property 0 :title cand)))
(authors (and (stringp cand) (get-text-property 0 :authors cand)))
(authors (cond
((and (listp authors) (= (length authors) 1))
(car authors))
((listp authors)
(mapconcat #'identity authors ", "))
(t authors)))
(journal (and (stringp cand) (get-text-property 0 :journal cand)))
(date (and (stringp cand) (get-text-property 0 :date cand))))
(cond
((derived-mode-p 'org-mode)
(concat
"\n"
(cond
((and url title) (format "** [[%s][%s]]\n" url title))
(url (format "** [[%s]]\n" url))
(title (format "** %s\n" title)))
(if authors (format "\n%s" authors))
(if journal (format "\nin =%s= " journal))
(if date (format "published on [%s]\n" date) "\n")
"\n*** Notes\n"))
((derived-mode-p 'markdown-mode)
(concat
"\n"
(cond
((and url title) (format "## [%s](%s)\n" url title))
(url (format "## <%s>\n" url))
(title (format "## %s\n" title)))
(if authors (format "\n%s" authors))
(if journal (format "\nin **%s** " journal))
(if date (format "published on %s\n" date) "\n")
"\n### Notes\n"))
(t
(concat
"\n"
(cond
((and url title) (format "** %s (%s)\n" title url))
(url (format "** %s\n" url))
(title (format "** %s\n" title)))
(if authors (format "\n%s" authors))
(if journal (format "\nin %s " journal))
(if date (format "published on %s\n" date) "\n")
"\n*** Notes\n")))))
(defun consult-omni-embark-scholar-insert-note (cand)
"Insert note snippet for CAND article.
Uses `consult-omni-embark-scholar-make-note-func' to make template."
(insert (funcall consult-omni-embark-scholar-make-note-func cand)))
(defvar-keymap consult-omni-embark-scholar-actions-map
:doc "Keymap for consult-omni-embark-scholar"
:parent consult-omni-embark-general-actions-map
"o d" #'consult-omni-embark-scholar-external-browse-doi
"w a" #'consult-omni-embark-scholar-copy-authors-as-kill
"i a" #'consult-omni-embark-scholar-insert-authors
"i n" #'consult-omni-embark-scholar-insert-note)
(add-to-list 'embark-keymap-alist '(consult-omni-scholar . consult-omni-embark-scholar-actions-map))
(add-to-list 'embark-default-action-overrides '(consult-omni-scholar . consult-omni-embark-default-action))
;;; Apps
;; Embark actions for sources searching applications
(defun consult-omni-embark-apps-open-filemanager (cand)
"Open CAND's filepath with system's file manager."
(if-let* ((path (and (stringp cand) (get-text-property 0 :path cand))))
(pcase system-type
('darwin (call-process "open" nil 0 nil path "-R"))
('cygwin (call-process "cygstart" nil 0 nil path))
('windows-nt (and (fboundp 'w32-shell-execute) (w32-shell-execute "open" path)))
(_ (call-process "xdg-open" nil 0 nil path)))))
(defun consult-omni-embark-apps-find-file (cand)
"Open CAND's filepath with `find-file'."
(if-let* ((path (and (stringp cand) (get-text-property 0 :path cand)))
(directory (and (file-exists-p (file-truename path)) (file-truename path)))
(default-directory directory))
(call-interactively #'find-file)))
(defun consult-omni-embark-apps-open-externally (cand)
"Open CAND's filepath using system's default application."
(if-let ((path (and (stringp cand) (get-text-property 0 :path cand))))
(pcase system-type
('darwin (call-process "open" nil 0 nil path))
('cygwin (call-process "cygstart" nil 0 nil path))
('windows-nt (and (fboundp 'w32-shell-execute) (w32-shell-execute "open" path)))
(_ (call-process "xdg-open" nil 0 nil path)))
nil))
(defun consult-omni-embark-apps-open-term (cand)
"Open CAND's filepath in `consult-omni-embark-default-term'."
(if-let* ((path (and (stringp cand) (get-text-property 0 :path cand)))
(directory (and (file-exists-p (file-truename path)) (file-truename path)))
(default-directory directory))
(funcall consult-omni-embark-default-term)))
(defun consult-omni-embark-apps-insert-path (cand)
"Insert the title of CAND at point."
(if-let ((path (and (stringp cand) (get-text-property 0 :path cand))))
(insert (format " %s " path))))
(defun consult-omni-embark-apps-copy-path-as-kill (cand)
"Copy the title of CAND to `kill-ring'."
(if-let ((path (and (stringp cand) (get-text-property 0 :path cand))))
(kill-new (format " %s " path))))
;;; Define Embark Keymaps
(defvar-keymap consult-omni-embark-apps-actions-map
:doc "Keymap for consult-omni-embark"
:parent consult-omni-embark-general-actions-map
"x" #'consult-omni-embark-apps-open-externally
"f" #'consult-omni-embark-apps-find-file
"o f" #'consult-omni-embark-apps-find-file
"o o" #'consult-omni-embark-apps-open-filemanager
"o t" #'consult-omni-embark-apps-open-term
"w p" #'consult-omni-embark-apps-copy-path-as-kil)
(add-to-list 'embark-keymap-alist '(consult-omni-apps . consult-omni-embark-apps-actions-map))
(add-to-list 'embark-default-action-overrides '(consult-omni-apps . consult-omni-embark-default-action))
;;; Calc
;; Embark actions for calc source
(defun consult-omni-embark-calc-copy-results-as-kill (cand)
"Copy the results of the calculator, CAND, to `kill-ring'."
(if-let ((results (and (stringp cand) (get-text-property 0 :title cand))))
(kill-new (format " %s " results))))
(defun consult-omni-embark-calc-insert-results (cand)
"Insert the results of the calculator, CAND, at point."
(if-let (results (and (stringp cand) (get-text-property 0 :title cand)))
(insert (format " %s " results))))
(defun consult-omni-embark-calc-copy-formula-as-kill (cand)
"Copy the results of the calculator, CAND, to `kill-ring'."
(if-let ((formula (and (stringp cand) (get-text-property 0 :query cand))))
(kill-new (format " %s " formula))))
(defun consult-omni-embark-calc-insert-formula (cand)
"Insert the results of the calculator, CAND, at point."
(if-let (formula (and (stringp cand) (get-text-property 0 :query cand)))
(insert (format " %s " formula))))
;;; Define Embark Keymaps
(defvar-keymap consult-omni-embark-calc-actions-map
:doc "Keymap for consult-omni-embark"
:parent embark-general-map
"w r" #'consult-omni-embark-calc-copy-results-as-kill
"w f" #'consult-omni-embark-calc-copy-formula-as-kill
"i r" #'consult-omni-embark-calc-insert-results
"i f" #'consult-omni-embark-calc-insert-formula)
(add-to-list 'embark-keymap-alist '(consult-omni-calc . consult-omni-embark-calc-actions-map))
(add-to-list 'embark-default-action-overrides '(consult-omni-calc . consult-omni-embark-default-action))
;;; Video
;; Embark actions for sources searching videos (e.g. Youtube)
(defcustom consult-omni-embark-video-default-player (executable-find "mpv")
"External video player for consult-omni.
Can be:
- an elisp function that takes a URL argument \(e.g. mpv-pay-url\)
- a string for external command line program"
:type '(choice (string :tag "(Default) mpv executable command" (executable-find "mpv"))
(function :tag "play with mpv package" mpv-play-url)
(function :tag "Custom Function")
(string :tag "Custom Executable Command")))
(defun consult-omni-play-url-with-app (url)
"Play video at URL with `consult-omni-embark-video-default-player'."
(interactive (let* ((cand (consult-omni-youtube nil "Search Youtube: " t))
(link (get-text-property 0 :url cand)))
(list link)))
(cond
((stringp consult-omni-embark-video-default-player)
(if-let ((cmd (executable-find consult-omni-embark-video-default-player)))
(progn
(start-process "consult-omni-mpv" nil cmd url)
(message "Opening with %s ..." consult-omni-embark-video-default-player))
(message "executable %s not found" consult-omni-embark-video-default-player)))
((symbolp consult-omni-embark-video-default-player)
(if (functionp consult-omni-embark-video-default-player)
(progn (funcall consult-omni-embark-video-default-player url)
(message "Opening with %s ..." consult-omni-embark-video-default-player))
(message "Symbol function definition is void: %s" consult-omni-embark-video-default-player)))))
(defun consult-omni-embark-video-play-with-app (cand)
"Open CAND's video URL with `consult-omni-play-url-with-app'."
(if-let* ((url (and (stringp cand) (get-text-property 0 :url cand))))
(consult-omni-play-url-with-app url)))
(defvar-keymap consult-omni-embark-video-actions-map
:doc "Keymap for consult-omni-embark-video"
:parent consult-omni-embark-general-actions-map
"o x" #'consult-omni-embark-video-play-with-app)
(add-to-list 'embark-keymap-alist '(consult-omni-video . consult-omni-embark-video-actions-map))
(add-to-list 'embark-default-action-overrides '(consult-omni-video . consult-omni-embark-default-action))
;;; Provide `consul-web-embark' module
(provide 'consult-omni-embark)
;;; consult-omni-embark.el ends here