-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconsult-omni.el
2676 lines (2348 loc) · 126 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.2
;; Package-Requires: (
;; (emacs "28.1")
;; (consult "1.9"))
;;
;; Homepage: https://github.com/armindarvish/consult-omni
;; Keywords: convenience
;;; Commentary:
;; consult-omni is a package for getting search results from one or
;; several custom sources (web search engines, AI assistants,
;; elfeed database, org notes, local files, desktop applications,
;; mail servers, ...) directly in Emacs minibuffer.
;; consult-omni provides wrappers and macros around
;; consult (https://github.com/minad/consult), to make it easier
;; for users to get results from different sources and combine
;; local and web sources in an omni-style search.
;; In other words, consult-omni enables getting consult-style
;; multi-source or dynamically completed results in minibuffer for
;; a wide range of sources including Emacs functions/packages
;; (e.g. Emacs buffers, org files, elfeed,...), command-line programs
;; (e.g. grep, find, gh, ...), or web search engines (Google, Bing, ...).
;;; 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-")
;;; User Options (a.k.a. Custom Variables)
;; The following user options modify the behavior of consult-omni.
(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)"
:group 'consult-omni
: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 static 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"
:group 'consult-omni
: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
"Default function when selecting a link."
:group 'consult-omni
: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."
:group 'consult-omni
: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
"Default function when selecting a link."
:group 'consult-omni
:type '(choice (function :tag "(Default) EWW" eww-browse-url)
(function :tag "Custom Function")))
(defcustom consult-omni-default-search-engine nil
"Default search engine name."
:group 'consult-omni
: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
"Default function when previewing a link."
:group 'consult-omni
: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."
:group 'consult-omni
: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."
:group '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
"Default function when selecting a link."
:group 'consult-omni
: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."
:group 'consult-omni
: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 items,
depending on the source search capabilities\) of the search results are
omitted and the rest are shown."
:group 'consult-omni
:type 'integer)
(defcustom consult-omni-default-timeout 30
"Default timeout in seconds for synchronous requests."
:group 'consult-omni
:type 'integer)
(defcustom consult-omni-url-use-queue nil
"Use `url-queue-retrieve'?"
:group 'consult-omni
:type 'boolean)
(defcustom consult-omni-url-queue-parallel-processes 15
"The number of concurrent `url-queue-retrieve' processes."
:group 'consult-omni
:type 'integer)
(defcustom consult-omni-url-queue-timeout 120
"How long to let a job live once it's started (in seconds)."
:group 'consult-omni
:type '(integer :tag "Timeout in seconds"))
(defcustom consult-omni-log-buffer-name " *consult-omni-log*"
"String for consult-omni-log buffer name."
:group 'consult-omni
:type 'string)
(defcustom consult-omni-log-level nil
"How to make logs for consult-omni requests?
This can be set to:
\='nil: Does not log anything
\='info: Logs URLs and http response header.
\='debug: Logs URLs and the entire http response.
When non-nil, information is logged to `consult-omni-log-buffer-name'."
:group 'consult-omni
: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"
:group 'consult-omni
: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.)"
:group 'consult-omni
: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?"
:group 'consult-omni
:type 'boolean)
(defcustom consult-omni-highlight-matches-in-file t
"Should `consult-omni' highlight search queries in files (preview or return)?"
:group 'consult-omni
:type 'boolean)
(defcustom consult-omni-highlight-match-ignore-case t
"Should `consult-omni' ignore case when highlighting matches?"
:group 'consult-omni
:type 'boolean)
(defcustom consult-omni-default-interactive-command #'consult-omni-multi
"Which command should `consult-omni' call?"
:group 'consult-omni
: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?"
:group 'consult-omni
: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?"
:group 'consult-omni
:type '(choice (const :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-async-min-input consult-async-min-input
"Minimum number of characters needed, before async process is called.
This applies to dynamic collection commands, e.g., `consult-omni-google'.
This is similar to `consult-async-min-input' but specifically for
consult-omni dynamic commands.
By default inherits from `consult-async-min-input'."
:group 'consult-omni
:type '(natnum :tag "Number of characters"))
(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 similar to `consult-async-input-debounce' but
specifically for consult-omni dynamic commands.
By default inherits from `consult-async-input-debounce'."
:group 'consult-omni
: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'."
:group 'consult-omni
:type '(float :tag "delay in seconds"))
(defcustom consult-omni-dynamic-refresh-delay consult-async-refresh-delay
"Refreshing delay of the completion UI or 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'."
:group 'consult-omni
:type '(float :tag "delay in seconds"))
(defcustom 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)
("Other" . #'consult-omni--choose-other-source-for-new))
"Alist of search engine names and URLs.
car of each item is the name of the engine
cdr of items must be either:
- a search url string with %s for the query
- an elisp funciton that takes a single string input for query"
:group 'consult-omni
:type '(alist :key-type string :value-type (choice (string :tag "a search url string with %s for the query")
(function :tag " an elisp funciton that takes a single string input for query"))))
;;; Other Variables
;; The following variables define search categories.
(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.")
;; The following history variables store search histories for
;; different categories.
(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.")
;; The following variables are generally for internal use
(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--async-log-buffer " *consult-omni--async-log*"
"Name of buffer for logging async processes info.")
(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))
"Face used for items in minibuffer.")
(defface consult-omni-prompt-face
`((t :inherit 'font-lock-variable-use-face))
"Face used for prompts in minibuffer.")
(defface consult-omni-warning-face
`((t :inherit 'font-lock-warning-face))
"Face used for notes source types in minibuffer.")
(defface consult-omni-engine-title-face
`((t :inherit 'font-lock-variable-use-face))
"Face used for search engine source types in minibuffer.")
(defface consult-omni-ai-title-face
`((t :inherit 'font-lock-operator-face))
"Face used for AI assistant source types in minibuffer.")
(defface consult-omni-files-title-face
`((t :inherit 'consult-file))
"Face used for file source types in minibuffer.")
(defface consult-omni-notes-title-face
`((t :inherit 'font-lock-bracket-face))
"Face used for notes source types in minibuffer.")
(defface consult-omni-scholar-title-face
`((t :inherit 'font-lock-function-call-face))
"Face used for academic literature source types in minibuffer.")
(defface consult-omni-source-type-face
`((t :inherit 'font-lock-comment-face))
"Face used for source annotation in minibuffer.")
(defface consult-omni-date-face
`((t :inherit 'font-lock-preprocessor-face))
"Face used for date annotation in minibuffer.")
(defface consult-omni-domain-face
`((t :inherit 'font-lock-string-face))
"Face used for domain annotation in minibuffer.")
(defface consult-omni-path-face
`((t :inherit 'font-lock-string-face))
"Face used for path annotation in minibuffer.")
(defface consult-omni-snippet-face
`((t :inherit 'font-lock-doc-face))
"Face used for source annotation in minibuffer.")
(defface consult-omni-keyword-face
`((t :inherit 'font-lock-keyword-face))
"Face used for keyword annotation in minibuffer.")
(defface consult-omni-comment-face
`((t :inherit 'font-lock-comment-face))
"Face used for source annotation in minibuffer.")
(defface consult-omni-highlight-match-face
`((t :inherit 'consult-highlight-match))
"Face used for highlighting matches in minibuffer.")
(defface consult-omni-preview-match-face
`((t :inherit 'consult-preview-match))
"Face used for hilighlighting matches in preview buffer.")
;;; Backend Functions
;; These functions are meant for internal use and/or programmers
(defun consult-omni-properties-to-plist (string &optional ignore-keys)
"Return 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)
do (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)
"Propertize ITEM by PROPS plist.
When BEG and or END are non-nil, adds properties to positions BEG to END."
(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)
"Set the STRING width to a fixed value, WIDTH.
Set the string with depedning on the following conditions:
- If the STRING is longer than WIDTH, truncate the STRING and add
ellipsis, \"...\".
- If the STRING is shorter than WIDTH, add whitespace to the STRING.
- If TRUNCATE-POS is non-nil, truncate from position TRUNCATE-POS in the
STRING.
- If ADD-POS is non-nil, add 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%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)
"Set 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)
"Set 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)
"Highlight REGEXP in STR.
Case is ignored, if IGNORE-CASE is non-nil.
If a regular expression contains capturing groups, only these are
highlighted. If no capturing groups are used, highlight the whole match.
\(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)
"Highlight MATCH-STR in BUFFER using an overlay.
Case is ignored when IGNORE-CASE is non-nil.
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)
"Toggle highlight overlays in BUFFER.
BUFFER defaults to the current buffer."
(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)
"Add key value pairs in PARAMS to URL as “&key=val”.
PARAMS 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)
"Convert 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)
"Find and pulses REGEXP for DELAY seconds.
DELAY defaults to `pulse-delay'."
(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)
"Find and pulses region from BEG to END for DELAY seconds.
DELAY defaults to `pulse-delay'."
(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)
"Pulse line at point momentarily for DELAY seconds.
DELAY defaults to `pulse-delay'."
(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)
"Insert STRING in the buffer `consult-omni-log-buffer-name'.
This is used for logging the response form `consult-omni-url-retrieve-sync'."
(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 in BUFFER.
BUFFER defaults to the current buffer.
This would for example be “HTTP/1.1 200 OK” from an HTTP response."
(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)
"Extract the body from RESPONSE-DATA."
(plist-get response-data :data))
(defun consult-omni--url-retrieve-error-handler (&rest _args)
"Handle 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)
"Retrieve URL with SETTINGS.
Passes all the arguments to `url-retrieve', `url-retrieve-queue' or
`url-retrieve-snchronously'.
Description of Arguments:
SYNC when non-nil, retrieve URL sunchronously
(see `url-retrieve-synchronously'.)
TYPE http request type (e.g. “GET”, “POST”)
PARAMS key values pairs added to the base url
using `consult-omni--make-url-string'.
HEADERS key value pairs passed to headers
(e.g `url-request-extra-headers').
DATA are http request data passed to data (e.g. `url-request-data').
PARSER a function that is executed in the `url-retrieve' response and
the results are passed to CALLBACK. It is called
without any arguments in the response buffer
\(i.e. (funcall PARSER) \) This is for example suitable for
`json-read'.
CALLBACK a 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. (funcall CALLBACK (funcall PARSER))\).
ERROR 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', and 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)
"Handle errors for request backend.
See `request' for more details on ARGS, SYMBOL-STATUS and ERROR-THROWN."
(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'.
Fetch URL *synchronously* using `request'.
Refer to `request' documents for details on ARGS, PARAMS, HEADERS, DATA,
PARSER, PLACEHOLDER, ERROR, and ENCODING."
(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)
"Handle errors for `plz' backend.
Refer to `plz' documentation for more details on PLZ-ERROR."
(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)
"Retrieve URL with BACKEND.
This is a wrapper that passes the ARGS to the corresponding
BACKEND function. \(i.e. `consult-omni-url-retrieve',
`request', `plz', ...\). See backend functions for details.
Description of Arguments:
SYNC if non-nil, retrieve URL sunchronously.
TYPE http request type \(e.g. “GET”, “POST”\)
PARAMS key value pairs added to the base url using
`consult-omni--make-url-string'.
HEADERS key value pairs passed to headers
\(e.g. `url-request-extra-headers'\).
DATA http request data passed to data \(e.g. `url-request-data'\).
PARSER a function that is executed in the `url-retrieve' buffer,
and the results are passed to CALLBACK.
See `consult-omni-url-retrieve', `request', or `plz' for more
info.
CALLBACK a function that is executed when the request is complete.
It takes one argument, PARSED-DATA \(e.g. the output of
the PARSER above.\)
It is called by (funcall CALLBACK (funcall PARSER)). See
`consult-omni-url-retrieve', `request', or `plz' for more info.
ERROR 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.
Kills the buffers stored in`consult-gh--preview-buffers-list'.
Ask for confirmation if the buffer is modified and remove the buffers that
are killed from the list."
(interactive)
(when consult-omni--hidden-buffers-list
(mapc (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
(mapc (lambda (buff) (if (and (buffer-live-p buff) (not (get-buffer-process buff)))
(kill-buffer buff)))