-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
consult-omni.el
2440 lines (2145 loc) · 114 KB
/
consult-omni.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; consult-omni.el --- Emacs Omni Search Package -*- lexical-binding: t -*-
;; Copyright (C) 2024 Armin Darvish
;; Author: Armin Darvish
;; Maintainer: Armin Darvish
;; Created: 2024
;; Version: 0.1
;; Package-Requires: ((emacs "28.1") (consult "1.4"))
;; Homepage: https://github.com/armindarvish/consult-omni
;; Keywords: convenience
;;; Commentary:
;;; Code:
;;; Requirements
(eval-when-compile
(require 'json)
(require 'request nil t)
(require 'plz nil t))
(require 'consult)
(require 'url)
(require 'url-queue)
;;; Group
(defgroup consult-omni nil
"Consulting omni sources"
:group 'convenience
:group 'minibuffer
:group 'consult
:group 'web
:group 'search
:prefix "consult-omni-")
;;; Customization Variables
(defcustom consult-omni-sources-modules-to-load (list)
"List of source modules/features to load.
This variable is a list of symbols;
each symbol being a source featue (e.g. consult-omni-brave)"
:type '(repeat :tag "list of source modules/features to load" symbol))
(defcustom consult-omni-intereactive-commands-type 'both
"Type of interactive commands to make?.
This variable can be a symbol:
'static only make statis commands
'dynamic only make dynamic commands
otherwise make both commands
dynamic commands are dynamically completed in the minibuffer.
static commands fetch search results once without dynamic completion"
:type '(choice (const :tag "(Default) Make both static and dynamic commands" 'both)
(const :tag "Only make DYNAMIC interactive commands" 'dynamic)
(const :tag "Only make STATIC interactive commands" 'static)
))
(defcustom consult-omni-default-browse-function #'browse-url
"consult-omni default function when selecting a link"
:type '(choice (function :tag "(Default) Browse URL" browse-url)
(function :tag "Custom Function")))
(defcustom consult-omni-default-new-function #'consult-omni-external-search
"default function when selecting a non-existing new candidate"
:type '(choice (function :tag "(Default) Search in External Browser" consult-omni-external-search)
(function :tag "Custom Function")))
(defcustom consult-omni-alternate-browse-function #'eww-browse-url
"consult-omni default function when selecting a link"
:type '(choice (function :tag "(Default) EWW" eww-browse-url)
(function :tag "Custom Function")))
(defcustom consult-omni-default-search-engine nil
"Consult-omni's default search engine name"
:type '(choice (string :tag "Bing" "Bing")
(string :tag "Brave" "Brave")
(string :tag "DuckDuckGo" "DuckDuckGo")
(string :tag "Google" "Google")
(string :tag "Perplexity" "Perplexity")
(string :tag "PubMed" "PubMed")
(string :tag "Wikipedia" "Wikipedia")
(string :tag "YouTube" "YouTube")))
(defcustom consult-omni-default-preview-function #'eww-browse-url
"consult-omni default function when previewing a link"
:type '(choice (function :tag "(Default) EWW" eww-browse-url)
(function :tag "Custom Function")))
(defcustom consult-omni-show-preview nil
"Should`consult-omni' show previews?
This turns previews on/off globally for all consult-omni sources."
:type 'boolean)
(defcustom consult-omni-preview-key consult-preview-key
"Preview key for consult-omni.
This is similar to `consult-preview-key' but explicitly For consult-omni."
:type '(choice (const :tag "Any Key" Any)
(List :tag "Debounced"
(const :Debounce)
(Float :tag "Seconds" 0.1)
(const Any))
(const :tag "No Preview" nil)
(Key :tag "Key")
(repeat :tag "List Of Keys" Key)))
(defcustom consult-omni-default-format-candidate #'consult-omni--highlight-format-candidate
"consult-omni default function when selecting a link"
:type '(choice (function :tag "(Default) Adds Metadata and Highlights Query" #'consult-omni--highlight-format-candidate)
(function :tag "Simple and Fast Foramting (No Metadata)" #'consult-omni--simple-format-candidate)
(function :tag "Custom Function")))
(defcustom consult-omni-default-count 5
"Number Of search results to retrieve."
:type 'integer)
(defcustom consult-omni-default-page 0
"Offset of search results to retrieve.
If this is set to N, the first N “pages”
(or other first N entities, items for example,
depending on the source search capabilities)
of the search results are omitted and the rest are shown."
:type 'integer)
(defcustom consult-omni-default-timeout 30
"Default timeout in seconds for synchronous requests."
:type 'integer)
(defcustom consult-omni-url-use-queue nil
"Use `url-queue-retrieve'?"
:type 'boolean)
(defcustom consult-omni-url-queue-parallel-processes 15
"The number of concurrent url-queue-retrieve processes."
:type 'integer)
(defcustom consult-omni-url-queue-timeout 120
"How long to let a job live once it's started (in seconds)."
:type '(integer :tag "Timeout in seconds"))
(defcustom consult-omni-log-buffer-name " *consult-omni-log*"
"String for consult-omni-log buffer name"
:type 'string)
(defcustom consult-omni-log-level nil
"How to make logs for consult-omni requests?
This can be set to nil, 'info or 'debug
nil: Does not log anything
info: Logs URLs and http response header.
Messages erros occuring in colelcting items.
debug: Logs URLs and the entire http response.
Messages erros occuring in colelcting items.
When non-nil, information is logged to `consult-omni-log-buffer-name'."
:type '(choice
(const :tag "No Logging" nil)
(const :tag "Just HTTP Header" info)
(const :tag "Full Response" debug)))
(defcustom consult-omni-group-by :source
"What field to use to group the results in the minibuffer?
By default it is set to :source. but can be any of:
nil Do not group
:title group by candidate's string
:url group by URL
:domain group by the domain of the URL
:source group by source name
symbol group by another property of the candidate"
:type '(radio (const :tag "URL path" :url)
(const :tag "Domain of URL path":domain)
(const :tag "Name of the search engine or source" :source)
(const :tag "Custom other field (constant)" :any)
(const :tag "Do not group" nil)))
(defcustom consult-omni-multi-sources nil
"List of sources used by `consult-omni-multi'.
This variable is a list of strings or symbols;
- strings can be name of a source, a key from `consult-omni-sources-alist',
which can be made with the convinient macro `consult-omni-define-source'
or by using `consult-omni--make-source-from-consult-source'.
- symbols can be other consult sources
(see `consult-buffer-sources' for example.)"
:type '(choice (repeat :tag "list of source names" string)))
(defcustom consult-omni-highlight-matches-in-minibuffer t
"Should `consult-omni' highlight search queries in the minibuffer?"
:type 'boolean)
(defcustom consult-omni-highlight-matches-in-file t
"Should `consult-omni' highlight search queries in files (preview or return)?"
:type 'boolean)
(defcustom consult-omni-highlight-match-ignore-case t
"Should `consult-omni' ignore case when highlighting matches?"
:type 'boolean)
(defcustom consult-omni-default-interactive-command #'consult-omni-multi
"Which command should `consult-omni' call?"
:type '(choice (function :tag "(Default) multi-source dynamic search" consult-omni-multi)
(function :tag "multi-source static search" consult-omni-multi-static)
(function :tag "Other custom interactive command")))
(defcustom consult-omni-http-retrieve-backend 'url
"Which backend should `consult-omni' use for http requests?"
:type '(choice
(const :tag "(Default) Built-in Emacs's url-retrive" 'url)
(const :tag "`emacs-request' backend" 'request)
(const :tag "`plz' backend" 'plz)))
(defcustom consult-omni-default-autosuggest-command nil
"Which command should `consult-omni' use for auto suggestion on search input?"
:type '(choice (cons :tag "(Default) no autosuggestion" nil)
(function :tag "Brave autosuggestion (i.e. `consult-omni-brave-autosuggest')" consult-omni-brave-autosuggest)
(function :tag "Google autosuggestion (i.e. `consult-omni-dynamic-google-autosuggest')" consult-omni-dynamic-google-autosuggest)
(function :tag "Other custom interactive command")))
(defcustom consult-omni-dynamic-input-debounce consult-async-input-debounce
"Input debounce for dynamic commands.
The dynamic collection process is started only when
there has not been new input for consult-omni-dynamic-input-debounce seconds.
This is similarto `consult-async-input-debounce' but
specifically for consult-omni dynamic commands.
By default inherits from `consult-async-input-debounce'."
:type '(float :tag "delay in seconds"))
(defcustom consult-omni-dynamic-input-throttle consult-async-input-throttle
"Input throttle for dynamic commands.
The dynamic collection process is started only every
`consult-omni-dynamic-input-throttle' seconds. this is similar
to `consult-async-input-throttle' but specifically for
consult-omni dynamic commands.
By default inherits from `consult-async-input-throttle'."
:type '(float :tag "delay in seconds"))
(defcustom consult-omni-dynamic-refresh-delay consult-async-refresh-delay
"refreshing delay of the completion ui for dynamic commands.
The completion UI is only updated every
`consult-omni-dynamic-refresh-delay' seconds.
This is similar to `consult-async-refresh-delay' but specifically
for consult-omni dynamic commands.
By default inherits from `consult-async-refresh-delay'. "
:type '(float :tag "delay in seconds"))
;;; Other Variables
(defvar consult-omni-sources--all-modules-list (list)
"List of all source modules.")
(defvar consult-omni-category 'consult-omni
"Category symbol for the consult-omni seach")
(defvar consult-omni-scholar-category 'consult-omni-scholar
"Category symbol for scholar search")
(defvar consult-omni-apps-category 'consult-omni-apps
"Category symbol for app launcher")
(defvar consult-omni-calc-category 'consult-omni-calc
"Category symbol for calculators")
(defvar consult-omni-video-category 'consult-omni-video
"Category symbol for video search")
(defvar consult-omni-dictionary-category 'consult-omni-dictionary
"Category symbol for dictionary search")
(defvar consult-omni--selection-history (list)
"History variable that keeps selected items.")
(defvar consult-omni--search-history (list)
"History variable that keeps search terms.")
(defvar consult-omni--email-select-history (list)
"History variable that keeps selected email result.")
(defvar consult-omni--calc-select-history (list)
"History variable that keeps selected calculator result.")
(defvar consult-omni--apps-select-history (list)
"History variable that keeps list of apps launched.")
(defvar consult-omni-sources-alist (list)
"Alist of all sources.
This is an alist mapping source names to source property lists.
This alist is used to define how to process data form
a source (e.g. format data) or find what commands to run on
selecting candidates from a source, etc.
You can use the convinient macro `consult-omni-define-source'
or the command `consult-omni--make-source-from-consult-source'
to add to this alist.")
(defvar consult-omni--hidden-buffers-list (list)
"List of currently open hidden buffers")
(defvar consult-omni--override-group-by nil
"Override grouping in `consult-group' based on user input.
This is used in dynamic collection to change grouping.")
(defconst consult-omni-http-end-of-headers-regexp
(rx (or "\r\n\r\n" "\n\n"))
"Regular expression matching the end of HTTP headers.")
(defvar consult-omni-async-processes (list)
"List of processes for async candidates colleciton")
(defvar consult-omni-dynamic-timers (list)
"List of timers for dynamic candidates colleciton")
(defvar consult-omni--async-log-buffer " *consult-omni--async-log*"
"name of buffer for logging async processes info")
(defvar consult-omni-dynamic-timers (list)
"List of timers for dynamic candidates colleciton")
(defvar consult-omni--async-log-buffer " *consult-omni--async-log*"
"name of buffer for logging async processes info")
(defvar consult-omni--search-engine-alist '(("Bing" . "https://www.bing.com/search?q=%s")
("Brave" . "https://search.brave.com/search?q=%s")
("DuckDuckGo" . "https://duckduckgo.com/?q=%s")
("Google" . "https://www.google.com/search?q=%s")
("Perplexity" . "https://www.perplexity.ai/search?q=%s")
("PubMed" . "https://pubmed.ncbi.nlm.nih.gov/?q=%s")
("Wikipedia" . "https://en.wikipedia.org/wiki/Special:Search/%s")
("YouTube" . "https://www.youtube.com/search?q=%s")
("gptel" . #'consult-omni--gptel-preview
)
)
"Alist of search engine name and URLs.
car of each item is the name of the engine
cdr of items must be:
- a search url string with %s for the query
- an elisp funciton that takes query string")
(defvar consult-omni--min-timeout 2
"Minimum timeout in seconds for `consult-omni--multi-static'")
(defvar consult-omni--max-timeout 120
"Maximum timeout in seconds for `consult-omni--multi-static'")
(defvar consult-omni--slow-warning-message "Give me a few seconds to sort it out in this big mess!"
"The message to show when collection takes a long time.")
;;; Faces
(defface consult-omni-default-face
`((t :inherit 'default))
"Default face used for listing items in minibuffer.")
(defface consult-omni-prompt-face
`((t :inherit 'font-lock-variable-use-face))
"The face used for prompts in minibuffer.")
(defface consult-omni-warning-face
`((t :inherit 'font-lock-warning-face))
"The face for notes source types in minibuffer.")
(defface consult-omni-engine-title-face
`((t :inherit 'font-lock-variable-use-face))
"The face for search engine source types in minibuffer.")
(defface consult-omni-ai-title-face
`((t :inherit 'font-lock-operator-face))
"The face for AI assistant source types in minibuffer.")
(defface consult-omni-files-title-face
`((t :inherit 'font-lock-number-face))
"The face for file source types in minibuffer.")
(defface consult-omni-notes-title-face
`((t :inherit 'font-lock-warning-face))
"The face for notes source types in minibuffer.")
(defface consult-omni-scholar-title-face
`((t :inherit 'font-lock-function-call-face))
"The face for academic literature source types in minibuffer.")
(defface consult-omni-source-type-face
`((t :inherit 'font-lock-comment-face))
"The face for source annotation in minibuffer.")
(defface consult-omni-date-face
`((t :inherit 'font-lock-preprocessor-face))
"The face for date annotation in minibuffer.")
(defface consult-omni-domain-face
`((t :inherit 'font-lock-string-face))
"The face for domain annotation in minibuffer.")
(defface consult-omni-path-face
`((t :inherit 'font-lock-warning-face))
"The face for path annotation in minibuffer.")
(defface consult-omni-snippet-face
`((t :inherit 'font-lock-doc-face))
"The face for source annotation in minibuffer.")
(defface consult-omni-keyword-face
`((t :inherit 'font-lock-keyword-face))
"The face for keyword annotation in minibuffer.")
(defface consult-omni-comment-face
`((t :inherit 'font-lock-comment-face))
"The face for source annotation in minibuffer.")
(defface consult-omni-highlight-match-face
`((t :inherit 'consult-highlight-match))
"Highlight match face for `consult-omni'.")
(defface consult-omni-preview-match-face
`((t :inherit 'consult-preview-match))
"Preview match face in `consult-omni' preview buffers.")
;;; Bakcend Functions
(defun consult-omni-properties-to-plist (string &optional ignore-keys)
"Returns a plist of the text properties of STRING.
Ommits keys in IGNORE-KEYs."
(let ((properties (text-properties-at 0 string))
(pl nil))
(cl-loop for k in properties
when (keywordp k)
collect (unless (member k ignore-keys) (push (list k (plist-get properties k)) pl)))
(apply #'append pl)))
(defun consult-omni-propertize-by-plist (item props &optional beg end)
"Propertizes ITEM by PROPS plist"
(if (stringp item)
(if (or beg end)
(let ((beg (or beg 0))
(end (if (and end (< end 0))
(+ (length item) end)
(and end (min end (length item))))))
(add-text-properties beg end props item)
item)
(apply #'propertize item props))
nil))
(defun consult-omni--set-string-width (string width &optional truncate-pos add-pos)
"Sets the STRING width to a fixed value, WIDTH.
Sets the string with depdning on the following conditions:
- If the STRING is longer than WIDTH, it truncates the STRING
and adds ellipsis, \"...\".
- If the STRING is shorter than WIDTH,
it adds whitespace to the STRING.
- If TRUNCATE-POS is non-nil, it truncates from position
TRUNCATE-POS in the STRING.
- If ADD-POS is non-nil, it adds whitespace to psition
ADD-POS in the STRING."
(let* ((string (format "%s" string))
(w (length string)))
(when (< w width)
(if (and add-pos (< add-pos w))
(setq string (format "%s%s%s" (substring string 0 add-pos) (consult-omni-propertize-by-plist (make-string (- width w) ?\s) (text-properties-at add-pos string)) (substring string add-pos)))
(setq string (format "%s%s" (substring string) (make-string (- width w) ?\s)))))
(when (> w width)
(if (and truncate-pos (< truncate-pos (- width 3)) (>= truncate-pos 0))
(setq string (format "%s%s%s" (substring string 0 truncate-pos) (propertize (substring string truncate-pos (+ truncate-pos 3)) 'display "...") (substring string (- 0 (- width truncate-pos 3)))))
(setq string (format "%s%s"
(substring string 0 (- width 3))
(propertize (substring string (- width 3) width) 'display "...")
(propertize (substring string width) 'invisible t)))))
string))
(defun consult-omni--justify-left (string prefix maxwidth)
"Sets the width of STRING+PREFIX justified from left.
It uses `consult-omni--set-string-width' and sets the width
of the concatenate of STRING+PREFIX (e.g. `(concat PREFIX STRING)`)
within MAXWIDTH.
This can be used for aligning marginalia info in minibuffer."
(let ((s (length string))
(w (length prefix)))
(if (> maxwidth w)
(consult-omni--set-string-width string (- maxwidth w) 0)
string)))
(defun consult-omni--set-url-width (domain path width)
"It sets the length of DOMAIN+PATH to fit within WIDTH."
(when (stringp domain)
(let* ((result)
(path-width (and (stringp path) (length path)))
(path-target-width (- width (length domain))))
(cond
((<= path-target-width 0)
(setq result (consult-omni--set-string-width domain width)))
((and (integerp path-target-width) (> path-target-width 10))
(setq result (concat domain (consult-omni--set-string-width path path-target-width (floor (/ path-target-width 2))))))
(t
(setq result (consult-omni--set-string-width (concat domain path) width))))
result)))
(defun consult-omni--highlight-match (regexp str ignore-case)
"Highlights REGEXP in STR.
If a regular expression contains capturing groups,
only these are highlighted.
If no capturing groups are used, highlight the whole match.
Case is ignored, if ignore-case is non-nil.
(This is adapted from `consult--highlight-regexps'.)"
(save-match-data
(let ((i 0))
(while (and (let ((case-fold-search ignore-case))
(string-match regexp str i))
(> (match-end 0) i))
(let ((m (match-data)))
(setq i (cadr m)
m (or (cddr m) m))
(while m
(when (car m)
(add-face-text-property (car m) (cadr m)
'consult-omni-highlight-match-face nil str))
(setq m (cddr m)))))))
str)
(defun consult-omni--overlay-match (match-str buffer ignore-case)
"Highlights MATCH-STR in BUFFER using an overlay.
If IGNORE-CASE is non-nil, it uses case-insensitive match.
This is provided for convinience, if needed in formating candidates
or preview buffers."
(let ((buffer (or (and buffer (get-buffer buffer)) (current-buffer))))
(when (buffer-live-p buffer)
(with-current-buffer buffer
(save-match-data
(save-mark-and-excursion
(remove-overlays (point-min) (point-max) 'consult-omni-overlay t)
(goto-char (point-min))
(let ((case-fold-search ignore-case)
(consult-omni-overlays (list)))
(while (search-forward match-str nil t)
(when-let* ((m (match-data))
(beg (car m))
(end (cadr m))
(overlay (make-overlay beg end)))
(overlay-put overlay 'consult-omni-overlay t)
(overlay-put overlay 'face 'consult-omni-highlight-match-face))))))))))
(defun consult-omni-overlays-toggle (&optional buffer)
"Toggles overlay highlights in consult-omni view/preview buffers."
(interactive)
(let ((buffer (or buffer (current-buffer))))
(with-current-buffer buffer
(dolist (o (overlays-in (point-min) (point-max)))
(when (overlay-get o 'consult-omni-overlay)
(if (and (overlay-get o 'face) (eq (overlay-get o 'face) 'consult-omni-highlight-match-face))
(overlay-put o 'face nil)
(overlay-put o 'face 'consult-omni-highlight-match-face)))))))
(defun consult-omni--numbers-human-readable (number &optional unit separator base prefixes)
"Convert number to a human-redable string.
SEPARATOR is a string placed between unmber and unit
UNIT is a string used as unit
BASE is the number base used to derive prefix
PREFIXES is a list of chars for each magnitude
(e.g. '(“” “K” “M” “G” ...) for none, kilo, mega, giga, ...
adapted from `file-size-human-readable'."
(let* ((power (if (and base (numberp base)) (float base) 1000.0))
(prefixes (or prefixes '("" "k" "M" "G" "T" "P" "E" "Z" "Y" "R" "Q")))
(number (pcase number
((pred numberp)
number)
((pred stringp)
(string-to-number number))
(_ 0))))
(while (and (>= number power) (cdr prefixes))
(setq number (/ number power)
prefixes (cdr prefixes)))
(let* ((prefix (car-safe prefixes)))
(format (if (and (< number 10)
(>= (mod number 1.0) 0.05)
(< (mod number 1.0) 0.95))
"%.1f%s%s%s"
"%.0f%s%s%s")
number
prefix
(or separator " ")
unit))))
(defun consult-omni--make-url-string (url params &optional ignore-keys)
"Adds key value pairs in PARAMS to URL as “&key=val”.
PARMAS should be an alist with keys and values to add to the URL.
key in IGNORE-KEYS list will be ignored."
(let* ((url (if (equal (substring-no-properties url -1 nil) "?")
url
(concat url "?")))
(list (append (list url) (cl-loop for (key . value) in params
collect
(unless (member key ignore-keys)
(format "&%s=%s" key value))))))
(mapconcat #'identity list)))
(defun consult-omni-hashtable-to-plist (hashtable &optional ignore-keys)
"Converts a HASHTABLE to a plist.
Ommits keys in IGNORE-KEYS."
(let ((pl nil))
(maphash
(lambda (k v)
(unless (member k ignore-keys)
(push (list k v) pl)))
hashtable)
(apply #'append pl)))
(defun consult-omni-expand-variable-function (var)
"Call the function if VAR is a function."
(if (functionp var)
(funcall var)
var))
(defun consult-omni--pulse-regexp (regexp &optional delay)
"Finds and pulses REGEXP"
(goto-char (point-min))
(while (re-search-forward regexp nil t)
(when-let* ((m (match-data))
(beg (car m))
(end (cadr m))
(ov (make-overlay beg end))
(pulse-delay (or delay 0.075))
)
(pulse-momentary-highlight-overlay ov 'highlight))
))
(defun consult-omni--pulse-region (beg end &optional delay)
"Finds and pulses region from BEG to END"
(let ((ov (make-overlay beg end))
(pulse-delay (or delay 0.075))
)
(pulse-momentary-highlight-overlay ov 'highlight))
)
(defun consult-omni--pulse-line (&optional delay)
"Pulses line at point momentarily"
(let* ((pulse-delay (or delay 0.075))
(ov (make-overlay (car (bounds-of-thing-at-point 'line)) (cdr (bounds-of-thing-at-point 'line)))))
(pulse-momentary-highlight-overlay ov 'highlight))
)
(defun consult-omni--url-log (string)
"Logs the response from `consult-omni-url-retrieve-sync'
the log is inserted in the buffer `consult-omni-log-buffer-name'."
(with-current-buffer (get-buffer-create consult-omni-log-buffer-name)
(goto-char (point-min))
(insert "**********************************************\n")
(goto-char (point-min))
(insert (format-time-string "%F - %T%n" (current-time)))
(insert string)
(insert "\n")
(goto-char (point-min))
(insert "\n\n**********************************************\n")))
(defun consult-omni--parse-http-response (&optional buffer)
"Parse the first header line.
This would for example be such as “HTTP/1.1 200 OK”."
(with-current-buffer (or buffer (current-buffer))
(save-excursion
(goto-char (point-min))
(when (re-search-forward "\\=[ \t\n]*HTTP/\\(?1:[0-9\\.]+\\) +\\(?2:[0-9]+\\)" url-http-end-of-headers t)
`(:http-version ,(match-string 1) :code ,(string-to-number (match-string 2)))))))
(defun consult-omni--url-response-body (response-data)
"Extracts the response body from `url-retrieve'."
(plist-get response-data :data))
(defun consult-omni--url-retrieve-error-handler (&rest args)
"Handles errors for consult-omni-url-retrieve functions."
(message "consult-omni: url-retrieve got an error: %s" (consult-omni--parse-http-response)))
(cl-defun consult-omni-url-retrieve (url &rest settings &key (sync 'nil) (type "GET") params headers data parser callback error timeout &allow-other-keys)
"Retrieves URL with settings.
Passes all the arguments to
`url-retrieve', `url-retrieve-queue' or `url-retrieve-snchronously'.
Description of Arguments:
SYNC when non-nil, it retrieves URL sunchronously
(see `url-retrieve-synchronously'.)
TYPE is the http request type (e.g. “GET”, “POST”)
PARAMS are parameters added to the base url
using `consult-omni--make-url-string'.
HEADERS are headers passed to headers (e.g. `url-request-extra-headers').
DATA are http request data passed to data (e.g. `url-request-data').
PARSER is a function that is executed in the url-retrieve
response and the results are passed to CALLBACK. It is called wthout any arguments
in the response buffer (i.e. it called like (funcall PARSER))
This is for example suitable for #'json-read.
CALLBACK is the function that is executed when the request is complete.
It takes one argument, PARSED-DATA which is the output of the PARSER above.
(i.e. it is called like (funcall CALLBACK (funcall PARSER)))
ERROR is a function that handles errors. It is called without any arguments
in the response buffer.
TIMEOUT is the time in seconds for timing out synchronous requests.
This is ignored in async requests.
Note that when `consult-omni-url-use-queue' is set to t, this function uses `url-queue-retrieve' sets url-queue-parallel-processes and url-queue-timeout
to `consult-omni-url-queue-parallel-processes',
and `consult-omni-url-queue-timeout', respectively."
(let* ((url-request-method type)
(url-request-extra-headers headers)
(url-request-data data)
(url-with-params (consult-omni--make-url-string url params))
(url-debug (if consult-omni-log-level t nil))
(url-queue-parallel-processes consult-omni-url-queue-parallel-processes)
(url-queue-timeout consult-omni-url-queue-timeout)
(retriever (if consult-omni-url-use-queue #'url-queue-retrieve #'url-retrieve))
(response-data '(:status nil :data nil))
(buffer (if sync
(if timeout
(with-timeout
(timeout
(setf response-data (plist-put response-data :status 'timeout))
nil)
(url-retrieve-synchronously url-with-params 'silent nil timeout))
(url-retrieve-synchronously url-with-params 'silent nil timeout))
(funcall retriever url-with-params
(lambda (status &rest args)
(let* ((parsed-data (condition-case nil
(if parser (funcall parser) (buffer-substring (point-min) (point-max)))
(error (funcall error)))))
(setf response-data (plist-put response-data :status status))
(when parsed-data
(setf response-data (plist-put response-data :data (funcall callback parsed-data)))))) nil 'silent))))
(when (and buffer (buffer-live-p buffer))
(add-to-list 'consult-omni--hidden-buffers-list buffer)
(if sync
(with-current-buffer buffer
(save-excursion
(goto-char (point-min))
(let* ((end-of-headers (if (and (bound-and-true-p url-http-end-of-headers)
(number-or-marker-p url-http-end-of-headers))
url-http-end-of-headers
(point-min)))
(response (buffer-substring (point-min) (pos-eol)))
(header (buffer-substring (point-min) end-of-headers))
(body (buffer-substring end-of-headers (point-max))))
(when consult-omni-log-level
(cond
((eq consult-omni-log-level 'info)
(consult-omni--url-log (format "URL: %s\nRESPONSE: %s" url response)))
((eq consult-omni-log-level 'debug)
(consult-omni--url-log (format "URL: %s\n\nRESPONSE-HEADER:\n%s\n\nRESPONSE-BODY: %s\n" url header body)))))
(setf response-data (plist-put response-data :status response))
(delete-region (point-min) (+ end-of-headers 1))
(goto-char (point-min))
(if-let* ((parsed-data (condition-case nil
(funcall parser)
(error (funcall error)))))
(setf response-data (plist-put response-data :data (funcall callback parsed-data)))))))))
response-data))
(cl-defun consult-omni--request-error-handler (&rest args &key symbol-status error-thrown &allow-other-keys)
"Handles errors for request backend.
See `request' for more details."
(message "consult-omni: <request> %s - %s" symbol-status error-thrown))
(cl-defun consult-omni--request-sync (url &rest args &key params headers data parser placeholder error encoding &allow-other-keys)
"Convinient wrapper for `request'.
Passes all the arguments to request and fetches the
results *synchronously*.
Refer to `request' documents for details."
(unless (functionp 'request)
(error "Request backend not available. Either install the package “emacs-request” or change the custom variable `consult-omni-retrieve-backend'"))
(let (candidates)
(request
url
:sync t
:params params
:headers headers
:parser parser
:error (or error #'consult-omni--request-error-handler)
:data data
:encoding (or encoding 'utf-8)
:success (cl-function (lambda (&key data &allow-other-keys)
(setq candidates data))))
candidates))
(cl-defun consult-omni--plz-error-handler (plz-error &rest args)
"Handles errors for `plz' backend.
Refer to `plz' documentation for more details."
(message "consult-omni: <plz> %s" plz-error))
(defun consult-omni--json-parse-buffer ()
"Default json parser used in consult-omni."
(let ((end-of-headers (if (and (bound-and-true-p url-http-end-of-headers)
(number-or-marker-p url-http-end-of-headers))
url-http-end-of-headers
(point-min))))
(goto-char end-of-headers)
(json-parse-buffer :object-type 'hash-table :array-type 'list :false-object :false :null-object :null)))
(cl-defun consult-omni--fetch-url (url backend &rest args &key type params headers data parser callback error encoding timeout sync &allow-other-keys)
"Retrieves URL with support for different BACKENDs.
This is a wrapper that passes the args to corresponding
BACKEND functions. (i.e. `consult-omni-url-retrieve',
`request', `plz', ...) See backend functions for details.
Description of Arguments:
SYNC if SYNC is non-nil, it retrieves URL sunchronously.
TYPE is the http request type (e.g. “GET”, “POST”)
PARAMS are parameters added to the base url
using `consult-omni--make-url-string'.
HEADERS are headers passed to headers (e.g. `url-request-extra-headers').
DATA are http request data passed to data (e.g. `url-request-data').
PARSER is a function that is executed in the url-retrieve
response and the results are passed to CALLBACK.
See `consult-omni-url-retrieve', `request', or `plz' for more info.
CALLBACK is the function that is executed when the request is complete.
It takes one argument, PARSED-DATA which is the output of the PARSER above.
(i.e. it is called like (funcall CALLBACK (funcall PARSER)))
See `consult-omni-url-retrieve', `request', or `plz' for more info.
ERROR is a function that handles errors. It is called without any arguments
in the response buffer.
ENCODING is the encoding used for the request backend (e.g. 'utf-8)
TIMEOUT is the time in seconds for timing out synchronous requests.
This is ignored in async requests."
(cond
((eq backend 'plz)
(if sync
(funcall callback (funcall #'plz (or type 'get) (consult-omni--make-url-string url params)
:headers headers
:as parser
:then 'sync
:else (or error #'consult-omni--plz-error-handler)
:timeout (or timeout consult-omni-default-timeout)))
(funcall #'plz (or type 'get) (consult-omni--make-url-string url params)
:headers headers
:as parser
:then callback
:else (or error #'consult-omni--plz-error-handler)
:timeout (or timeout consult-omni-default-timeout))))
((eq backend 'url)
(if sync
(consult-omni--url-response-body
(funcall #'consult-omni-url-retrieve url
:sync sync
:type (or type "GET")
:params params
:headers headers
:parser parser
:data data
:error (or error #'consult-omni--url-retrieve-error-handler)
:callback (or callback #'identity)
:timeout (or timeout consult-omni-default-timeout)))
(funcall #'consult-omni-url-retrieve url
:sync sync
:type (or type "GET")
:params params
:headers headers
:parser parser
:data data
:error (or error #'consult-omni--url-retrieve-error-handler)
:callback (or callback #'identity)
:timeout (or timeout consult-omni-default-timeout))))
((eq backend 'request)
(if sync
(funcall callback
(request-response-data
(funcall #'request url
:sync sync
:params params
:headers headers
:parser parser
:data data
:error (or error #'consult-omni--request-error-handler)
:encoding (or encoding 'utf-8)
:timeout (or timeout consult-omni-default-timeout))))
(funcall #'request url
:params params
:headers headers
:parser parser
:data data
:error (or error #'consult-omni--request-error-handler)
:encoding (or encoding 'utf-8)
:timeout (or timeout consult-omni-default-timeout)
:complete (cl-function (lambda (&key data &allow-other-keys)
(funcall (or callback #'identity) data))))))))
(defun consult-omni--kill-hidden-buffers ()
"Kill all open preview buffers stored in `consult-gh--preview-buffers-list'.
It asks for confirmation if the buffer is modified
and removes the buffers that are killed from the list."
(interactive)
(when consult-omni--hidden-buffers-list
(mapcar (lambda (buff) (if (and (buffer-live-p buff) (not (get-buffer-process buff)))
(kill-buffer buff))) consult-omni--hidden-buffers-list))
(setq consult-omni--hidden-buffers-list nil))
(defun consult-omni--kill-url-dead-buffers ()
"Kill buffers in `url-dead-buffer-list'."
(interactive)
(when url-dead-buffer-list
(mapcar (lambda (buff) (if (and (buffer-live-p buff) (not (get-buffer-process buff)))
(kill-buffer buff)))
url-dead-buffer-list))
(setq url-dead-buffer-list nil))
(defun consult-omni--async-log (formatted &rest args)
"Log FORMATTED ARGS to variable `consult-omni--async-log-buffer'."
(with-current-buffer (get-buffer-create consult-omni--async-log-buffer)
(goto-char (point-max))
(insert (apply #'format formatted args))))
(defun consult-omni--get-source-prop (source prop)
"Get PROP for SOURCE from `consult-omni-sources-alist'."
(plist-get (cdr (assoc source consult-omni-sources-alist)) prop))
(defun consult-omni-dynamic--split-thingatpt (thing &optional split-initial)
"Return THING at point.
If SPLIT-INITIAL is non-nil use `consult--async-split-initial'
to format the string."
(when-let (str (thing-at-point thing t))
(if split-initial
(consult--async-split-initial (format "%s" str))
str)))
(defun consult-omni--read-search-string (&optional initial)
"Read a string from the minibuffer.
This is used to get initial input for static commands, when
`consult-omni-default-autosuggest-command' is nil."
(consult--read nil
:prompt "Search: "
:initial initial
:category 'consult-omni
:history 'consult-omni--search-history
:add-history (consult-omni--add-history '(symbol))))
(cl-defun consult-omni--simple-format-candidate (&rest args &key source query url search-url title snippet &allow-other-keys)
"Returns a simple formatted string for candidates.
Description of Arguments:
SOURCE the name string of the source for candidate
QUERY the query string used for searching
URL a string pointing to url of the candidate
SEARCH-URL a string pointing to the url for
the search results of QUERY on the SOURCE website
TITLE the title of the candidate
SNIPPET a string containing a snippet/description of candidate"
(let* ((frame-width-percent (floor (* (frame-width) 0.1)))
(title-str (consult-omni--set-string-width title (* 5 frame-width-percent))))
(concat title-str
(when source (concat "\t" source)))))
(cl-defun consult-omni--highlight-format-candidate (&rest args &key source query url search-url title snippet face &allow-other-keys)
"Returns a highlighted formatted string for candidates.
Description of Arguments:
SOURCE the name string of the source for candidate
QUERY the query string used for searching
URL a string pointing to url of the candidate
SEARCH-URL a string pointing to the url for
the search results of QUERY on the SOURCE website
TITLE the title of the candidate
SNIPPET a string containing a snippet/description of candidate
FACE the face used for the title"
(let* ((frame-width-percent (floor (* (frame-width) 0.1)))
(source (and (stringp source) (propertize source 'face 'consult-omni-source-type-face)))
(match-str (and (stringp query) (not (equal query ".*")) (consult--split-escaped query)))
(face (or (consult-omni--get-source-prop source :face) face 'consult-omni-default-face))
(title-str (propertize title 'face face))
(title-str (consult-omni--set-string-width title-str (* 4 frame-width-percent)))
(snippet (and (stringp snippet) (consult-omni--set-string-width snippet (* 3 frame-width-percent))))
(snippet (and (stringp snippet) (propertize snippet 'face 'consult-omni-snippet-face)))
(urlobj (and url (url-generic-parse-url url)))
(domain (and (url-p urlobj) (url-domain urlobj)))