forked from manateelazycat/snails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snails-core.el
1310 lines (1142 loc) · 47.6 KB
/
snails-core.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
;; -*- lexical-binding: t; -*-
;; ;;; snails-core.el --- A modern, easy-to-expand fuzzy search framework
;; Filename: snails-core.el
;; Description: A modern, easy-to-expand fuzzy search framework
;; Author: Andy Stewart <[email protected]>
;; Maintainer: Andy Stewart <[email protected]>
;; Copyright (C) 2019, Andy Stewart, all rights reserved.
;; Created: 2019-05-16 21:26:09
;; Version: 6.5
;; Last-Updated: 2019-09-01 16:05:13
;; By: Andy Stewart
;; URL: http://www.emacswiki.org/emacs/download/snails-core.el
;; Keywords:
;; Compatibility: GNU Emacs 26.1.92
;;
;; Features that might be required by this library:
;;
;; `cl-lib' `subr-x'
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; A modern, easy-to-expand fuzzy search framework
;;
;;; Installation:
;;
;; Put snails.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; If you are using Mac, install exec-path-from-shell from https://github.com/purcell/exec-path-from-shell.
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'snails)
;;
;; No need more.
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET snails RET
;;
;;; Change log:
;;
;; 2019/09/01
;; * We need init `snails-candiate-list' with `snails-backends', otherwise first launch will fail.
;;
;; 2019/08/27
;; * Make `snails' function support customize search string.
;; * Fix search-object condition order.
;; * Add new options `snails-default-backends' and `snails-prefix-backends'.
;; * Fix issue #29
;;
;; 2019/08/25
;; * Support search content with input prefix.
;;
;; 2019/08/20
;; * Call `snails-init-face-with-theme' when user execute snails command.
;;
;; 2019/08/08
;; * Run snails-mode-hook in `snails-create-input-buffer', evil users should be hook evil code after `snails-mode-hook'.
;;
;; 2019/07/29
;; * Make `snails-mode-hook' works, sorry i forgot add `run-hooks'.
;;
;; 2019/07/28
;; * Optimize performance: fixed rendering every 100 milliseconds, instead of rendering once backend return candidates, avoiding rendering computation waste.
;; * `snails-select-line-number' is not need anymore, `snails-select-line-overlay' is enough.
;; * Add `snails-render-bufer' to timer when first start.
;; * Keep offset of selected candidate.
;; * Optimize start performance: delay 50 milliseconds to start search backend.
;;
;; 2019/07/26
;; * Foucs out to hide snails frame on Mac.
;; * Snails will search symbol around point when you press prefix key before call snails.
;; * Make async process buffer's name starts with " *" to hide process buffer tab when search.
;; * Make `snails' support backends and search-object arguments.
;; * Add new command `snails-search-point'.
;; * Add `snails-flash-line'.
;; * Improve `snails-sort-candidates'
;; * Add option `snails-fame-width-proportion' and `snails-fame-height-proportion'.
;;
;; 2019/07/25
;; * Set undecorated parameter in `make-frame' function.
;; * Try to raise snails frame when focus default frame by alt + tab switcher of OS.
;; * Quit snails when lost input focus.
;; * Support ansi color from asynchronous backend process.
;; * Adjust ansi color code.
;;
;; 2019/07/24
;; * Don't ask user when snails kill buffer of backend process.
;; * Test GUI environment when start snails.
;; * Don't wrap long line in content buffer.
;; * Call `exec-path-from-shell' at snails-core.el
;; * Use fuzz match algorithm provide by `fuz' libary.
;;
;; 2019/07/23
;; * Kill old subprocess immediately, don't wait `run-with-idle-timer'
;; * Split input window with on line height.
;; * Make color along with current theme.
;; * Quit snails if it has opened.
;; * Add device to disable window configuration change snail frame.
;; * Exit snails when enter to minibuffer.
;; * Add new command `snails-candidate-copy'
;; * Add parent-frame parameter, then snails frame won't hide when switch to other application.
;;
;; 2019/07/22
;; * Delete other window first, make sure only one window in frame.
;; * Finish `snails-select-next-backend' and `snails-select-prev-backend'
;; * Use setq in macro, we can update backend code later.
;; * Make `snails' support customize backend.
;; * Fixed error that `set-buffer' on killed buffer.
;; * Use `expand-file-name' expand default-directory, fd don't like unexpand directory.
;; * Fix selected delete buffer error when call `buffer-string' in `snails-create-async-process'
;; * Give up creating subprocess if input ticker already expired.
;; * Kill all subprocess and process buffers when call `snails-quit'
;; * Fix bug that select previous candidate item will select header line sometimes.
;; * Add header index after header line.
;; * Use `run-with-idle-timer' instead `run-with-timer' to improve performance of subprocess search.
;; * 0.1 second is enough for `run-with-idle-timer'.
;; * Fix `string-join' depend.
;; * Disable scrollbar and fringle in new frame even user not disable them in theme.
;;
;; 2019/07/20
;; * Finish document.
;;
;; 2019/05/16
;; * First released.
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Require
(require 'cl-lib)
(require 'subr-x)
(when (featurep 'cocoa)
(require 'exec-path-from-shell)
(exec-path-from-shell-initialize))
;;; Code:
(defcustom snails-mode-hook '()
"Snails mode hook."
:type 'hook
:group 'snails)
(defcustom snails-fame-width-proportion 0.618
"The width of snails frame, width ratio of the parent frame."
:type 'integer
:group 'snails)
(defcustom snails-fame-height-proportion 0.618
"The height of snails frame, height ratio of the parent frame."
:type 'integer
:group 'snails)
(defcustom snails-default-backends
'(snails-backend-awesome-tab-group snails-backend-buffer snails-backend-recentf snails-backend-bookmark)
"The default backend"
:type 'cons
:group 'snails)
(defcustom snails-prefix-backends
'((">" '(snails-backend-command))
("@" '(snails-backend-imenu))
("#" '(snails-backend-current-buffer))
("!" '(snails-backend-rg))
("?" '(snails-backend-projectile snails-backend-fd snails-backend-mdfind snails-backend-everything)))
"The prefix/backends pair."
:type 'cons
:group 'snails)
(defface snails-header-line-face
'((t (:inherit font-lock-function-name-face :underline t :height 1.2)))
"Face for header line"
:group 'snails)
(defface snails-header-index-face
'((t (:inherit font-lock-function-name-face :underline t)))
"Face for header index"
:group 'snails)
(defface snails-candiate-content-face
'((t))
"Face for candidate content.
Note, candidate name is display name you can see in content buffer.
Candidate content use for confirm, it's invisible, it doesn't
need to set face attribute, such as foreground and background."
:group 'snails)
(defface snails-select-line-face
'((t (:inherit region)))
"Face for select line."
:group 'snails)
(defface snails-input-buffer-face
'((t (:height 250)))
"Face for input area."
:group 'snails)
(defface snails-content-buffer-face
'((t (:height 130)))
"Face for content area."
:group 'snails)
(defface snails-copy-candidate-face
'((t (:foreground "Gold" :bold t)))
"Face copy candidate."
:group 'snails)
(defvar snails-input-buffer " *snails input*"
"The buffer name of search input buffer.")
(defvar snails-content-buffer " *snails content*"
"The buffer name of search content buffer.")
(defvar snails-frame nil
"The popup frame use for show search result.")
(defvar snails-start-buffer nil
"The buffer before snails start.")
(defvar snails-start-buffer-lines nil
"The line number of start buffer.")
(defvar snails-frame-active-p nil
"The parent frame of popup frame.")
(defvar snails-select-line-overlay nil
"Select line overlay, use to highlight selected candidate.")
(defvar snails-header-line-overlays nil
"The list overlay to render backend header line.")
(defvar snails-backends nil
"Contain the real backends use in `snails'.")
(defvar snails-search-backends nil
"Search backends, default is nil will search with input prefix.
Or backends pass from function `snails'.")
(defvar snails-input-ticker 0
"Input ticker to unique search request.
If a search result return with old input ticker,
search result will be drop.")
(defvar snails-candiate-list nil
"The list to contain candidate list,
use for find candidate position to change select line.")
(defvar snails-backend-subprocess-hash
(make-hash-table :test 'equal)
"The hash table contain the subprocess of async backend.")
(defvar snails-fuz-library-load-status "uncheck"
"The variable use for check `fuz' library is load.
Init status with `uncheck'.
If `fuz' library is not found, set with `unload'.
If `fuz' library has load, set with `load'.")
(defvar snails-project-root-dir nil
"The project dir when start snails.")
(defvar snails-need-render nil
"Mark if you need to re-render after the word selection.")
(defvar snails-select-backend-name nil
"Record backend name of selected candidate.")
(defvar snails-select-candidate-offset nil
"Record candidate offset of selected candidate.")
(defvar snails-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-g") 'snails-quit)
(define-key map (kbd "ESC ESC ESC") 'snails-quit)
(define-key map (kbd "M-h") 'snails-quit)
(define-key map (kbd "C-n") 'snails-select-next-item)
(define-key map (kbd "C-p") 'snails-select-prev-item)
(define-key map (kbd "M-n") 'snails-select-next-item)
(define-key map (kbd "M-p") 'snails-select-prev-item)
(define-key map (kbd "C-v") 'snails-select-next-backend)
(define-key map (kbd "M-v") 'snails-select-prev-backend)
(define-key map (kbd "M-j") 'snails-select-next-backend)
(define-key map (kbd "M-k") 'snails-select-prev-backend)
(define-key map (kbd "C-m") 'snails-candidate-do)
(define-key map (kbd "RET") 'snails-candidate-do)
map)
"Keymap used by `snails-mode'.")
(define-derived-mode snails-mode text-mode "snails"
(interactive)
;; Kill all local variables.
(kill-all-local-variables)
;; Switch new mode.
(setq major-mode 'snails-mode)
(setq mode-name "snails")
;; Injection keymap.
(use-local-map snails-mode-map))
(defun snails (&optional backends search-object)
"Start snails to search.
`backends' is list of backend, default is nil, you can customize your own search backend list.
`search-object', default is nil, nothing fill in input buffer,
you can set `search-object' with t to search symbol around point,
or set it with any string you want."
(interactive)
(if (display-graphic-p)
(if (and snails-frame
(frame-live-p snails-frame))
;; Quit snails if it has opened.
(snails-quit)
;; Set `snails-search-backends' if argument backends is set.
(when (and (listp backends)
(> (length backends) 0))
(setq snails-search-backends backends))
;; Record buffer before start snails.
(setq snails-start-buffer (current-buffer))
(setq snails-start-buffer-lines (line-number-at-pos (point-max)))
;; Init face with theme.
(snails-init-face-with-theme)
;; Create input and content buffer.
(snails-create-input-buffer)
(snails-create-content-buffer)
;; Create popup frame to show search result.
(snails-create-frame)
;; Search.
(cond
;; Search with customize string when `search-object' is string.
((and (stringp search-object)
(not (string-empty-p search-object)))
(snails-search search-object))
;; Search symbol around point when `search-object' is t.
(search-object
(run-with-timer
0.05 nil
(lambda ()
(let ((search-string (or (with-current-buffer snails-start-buffer
(snails-pointer-string)) "")))
(with-current-buffer snails-input-buffer
(insert search-string))
(snails-search search-string)))))
;; Just launch with empty string when `search-object' is nil.
(t
(snails-search ""))))
(message "Snails render candidates in new frame that only can be run in a graphical environment.")))
(defun snails-search-point ()
"Search symbol at point"
(interactive)
(snails nil t))
(defun snails-select-next-item ()
"Select next candidate item."
(interactive)
(with-current-buffer snails-content-buffer
;; Goto current line.
(goto-char (overlay-start snails-select-line-overlay))
;; Jump to next candidate item position.
(snails-jump-to-next-item)
;; Update select line
(snails-update-select-line)
))
(defun snails-select-prev-item ()
"Select previous candidate item."
(interactive)
(with-current-buffer snails-content-buffer
;; Goto current line.
(goto-char (overlay-start snails-select-line-overlay))
;; Jump to previous candidate item position.
(snails-jump-to-previous-item)
;; Update select line.
(snails-update-select-line)
))
(defun snails-select-next-backend ()
(interactive)
(with-current-buffer snails-content-buffer
(snails-select-backend-first-candidate
(snails-get-next-backend-overlay))
))
(defun snails-select-prev-backend ()
(interactive)
(with-current-buffer snails-content-buffer
(snails-select-backend-first-candidate
(snalis-get-prev-backend-overlay))))
(defun snails-candidate-do ()
"Confirm current candidate."
(interactive)
(let ((candidate-info (snails-candidate-get-info)))
(when candidate-info
(snails-backend-do
(nth 0 candidate-info)
(nth 1 candidate-info)))))
(defun snails-quit ()
"Quit snails."
(interactive)
;; Delete frame first.
(ignore-errors (delete-frame snails-frame t))
(setq snails-frame nil)
(setq snails-frame-active-p nil)
(setq snails-project-root-dir nil)
(setq snails-start-buffer nil)
(setq snails-select-line-overlay nil)
(setq snails-need-render nil)
(setq snails-select-backend-name nil)
(setq snails-select-candidate-offset nil)
(setq snails-search-backends nil)
;; Kill all subprocess and process buffers.
(maphash
(lambda (name process)
(when process
(kill-buffer (process-buffer process)))
(when (and process
(process-live-p process))
(kill-process process)))
snails-backend-subprocess-hash))
(defun snails-create-input-buffer ()
"Create input buffer."
(with-current-buffer (get-buffer-create snails-input-buffer)
;; Clean buffer.
(erase-buffer)
;; Switch snails mode.
(snails-mode)
(run-hooks 'snails-mode-hook)
;; Set input buffer face.
(buffer-face-set 'snails-input-buffer-face)
;; Disable hl-line, header-line and mode-line in input buffer.
(setq-local global-hl-line-overlay nil)
(setq-local header-line-format nil)
(setq-local mode-line-format nil)
))
(defun snails-create-content-buffer ()
"Create content buffer."
(with-current-buffer (get-buffer-create snails-content-buffer)
;; Clean buffer.
(erase-buffer)
;; Set coent buffer face.
(buffer-face-set 'snails-content-buffer-face)
;; Disable header-line, mode-line, long line and cursor shape in content buffer.
(setq-local header-line-format nil)
(setq-local mode-line-format nil)
(setq-local truncate-lines t)
(setq-local cursor-type nil)
))
(defun snails-monitor-input (begin end length)
"This is input monitor callback to hook `after-change-functions'."
;; Send new input to all backends when user change input.
(when (string-equal (buffer-name) snails-input-buffer)
(with-current-buffer snails-content-buffer
(let* ((input (with-current-buffer snails-input-buffer
(buffer-substring (point-min) (point-max)))))
(snails-search input)))))
(defun snails-create-frame ()
"Create popup frame."
(let* ((edges (frame-edges))
(x (nth 0 edges))
(y (nth 1 edges))
(width (- (nth 2 edges) x))
(height (nth 3 edges))
(frame-width (truncate (* snails-fame-width-proportion width)))
(frame-height (truncate (* snails-fame-height-proportion height)))
(frame-x (+ x (/ (- width frame-width) 2)))
(frame-y (+ y (/ (- height frame-height) 3))))
;; Set project directory.
(setq snails-project-root-dir
(let ((project (project-current)))
(when project
(expand-file-name (cdr project))
)))
;; Make popup frame, and position at center of current frame.
(setq snails-frame
(make-frame
'((parent-frame . (window-frame))
(skip-taskbar . t)
(minibuffer . nil)
(visibility . nil)
(internal-border-width . 0)
(left-fringe . 0)
(right-fringe . 0)
(vertical-scroll-bars . nil)
(horizontal-scroll-bars . nil)
(undecorated . t)
(unsplittable . t)
)))
;; Configuration frame.
(with-selected-frame snails-frame
;; Delete other window first, make sure only one window in frame.
(delete-other-windows)
;; Disable menu in snails frame.
(set-frame-parameter snails-frame 'menu-bar-lines 0)
;; Set frame position and size.
(set-frame-position snails-frame frame-x frame-y)
(set-frame-size snails-frame frame-width frame-height t)
;; Set input window margin and switch to input buffer.
(switch-to-buffer snails-input-buffer)
(set-window-margins (selected-window) 1 1)
;; Split window with one line height of input buffer.
(split-window (selected-window) (line-pixel-height) nil t)
;; Set content window margin and switch to content buffer.
(other-window 1)
(switch-to-buffer snails-content-buffer)
(set-window-margins (selected-window) 1 1)
;; Add monitor callback in input change hook.
(other-window 1)
(add-hook 'after-change-functions 'snails-monitor-input nil t)
;; Focus out to hide snails frame on Mac.
(when (featurep 'cocoa)
(add-hook 'focus-out-hook 'snails-quit)))
;; Set active flag, use for advice-add detect.
(setq snails-frame-active-p t)
;; Show popup frame.
;; `select-frame-set-input-focus' is necessary for gnome-shell DE.
(make-frame-visible snails-frame)
(select-frame-set-input-focus snails-frame)))
(defun snails-search (input)
"Search input with backends."
(let ((search-content input))
;; Update input ticker.
(setq snails-input-ticker (+ snails-input-ticker 1))
;; Set backends.
(if snails-search-backends
;; Search special backends if `snails-search-backends' is not nil.
;; Snails won't filter with prefix in this situation.
(setq snails-backends snails-search-backends)
;; Try search backends with prefix if `snails-search-backends' is nil.
(let ((prefix (snails-input-prefix input))
match-prefix)
;; Search prefix backend if match prefix in `snails-prefix-backends'.
(catch 'search-prefix-backend
(dolist (prefix-backend snails-prefix-backends)
(when (equal prefix (car prefix-backend))
(setq snails-backends (eval (cadr prefix-backend)))
(setq search-content (substring input 1))
(setq match-prefix t)
(throw 'search-prefix-backend nil)
)))
;; Search default backends if not match any prefix in `snails-prefix-backends'.
(unless match-prefix
(setq snails-backends snails-default-backends))))
;; Init `snails-candiate-list' with `snails-backends'.
(setq snails-candiate-list (make-list (length snails-backends) nil))
;; Search.
(snails-input-search search-content)))
(defun snails-input-prefix (input)
"Get input prefix, return \"\" if input is empty."
(cond ((equal (length input) 0)
"")
(t
(substring input 0 1))))
(defun snails-input-search (input)
"Search input with backends inf `snails-backends'."
;; Call all backends with new input.
(dolist (backend snails-backends)
(let ((search-func (cdr (assoc "search" (eval backend)))))
(funcall search-func input snails-input-ticker 'snails-update-callback))))
(defun snails-update-callback (backend-name input-ticker candidates)
"Update candiate callback, use by backend."
;; Just update candidates when input ticker is newest.
;; Candidate will not render if backend return input ticker is old.
(when (and (equal input-ticker snails-input-ticker)
candidates)
(let* ((backend-names (snails-get-backend-names))
(backend-index (cl-position backend-name backend-names)))
(when backend-index
;; Update candidates by backend index.
(setq snails-candiate-list (snails-update-list-by-index snails-candiate-list backend-index candidates))
;; Flag to re-render.
(setq snails-need-render t)
))))
(defun snails-update-list-by-index (list n val)
"Update candidates with backend index."
(when list
(nconc (cl-subseq list 0 n)
(cons val (nthcdr (1+ n) list)))))
(defun snails-get-backend-names ()
"Get all backend names."
(mapcar (lambda (b) (eval (cdr (assoc "name" (eval b))))) snails-backends))
;; Add `snails-render-bufer' to timer when first start.
(when (not (featurep 'snails))
(run-with-timer 0 0.1 'snails-render-bufer))
(defun snails-render-bufer ()
"Render candidates when `snails-need-render' flag is set."
(when snails-need-render
(with-current-buffer snails-content-buffer
;; Record select line offset.
(snails-record-select-line-offset)
;; Clean buffer first.
(erase-buffer)
;; Clean all header line overlays.
(setq snails-header-line-overlays nil)
;; Reset select line variables.
(setq snails-select-line-overlay (make-overlay (point) (point) (current-buffer) t))
(overlay-put snails-select-line-overlay 'face `snails-select-line-face)
(let* ((candiate-index 0)
(backend-names (snails-get-backend-names))
(effective-backend-index 1)
(effective-backend-number (length (cl-remove-if #'booleanp snails-candiate-list)))
header-line-start
header-line-end
header-index-start
header-index-end
candidate-content-start
candidate-content-end)
;; Render backend result.
(dolist (candiate-list snails-candiate-list)
;; Just render backend result when return candidate is not nil.
(when candiate-list
;; Render header line with overlay.
(setq header-line-start (point))
(insert (nth candiate-index backend-names))
(setq header-line-end (point))
(let ((header-line-overlay (make-overlay header-line-start header-line-end)))
(add-to-list 'snails-header-line-overlays header-line-overlay)
(overlay-put header-line-overlay
'face
'snails-header-line-face
))
;; Insert backend index.
(setq header-index-start (point))
(insert (format " [%s/%s]\n" effective-backend-index effective-backend-number))
(backward-char)
(setq header-index-end (point))
(overlay-put (make-overlay header-index-start header-index-end)
'face
'snails-header-index-face)
(forward-char)
(setq effective-backend-index (+ effective-backend-index 1))
;; Render candidate list.
(dolist (candiate candiate-list)
;; Render candidate display name.
(insert (nth 0 candiate))
;; Render candidate real content.
(setq candidate-content-start (point))
(insert (format "%s" (nth 1 candiate)))
(setq candidate-content-end (point))
(let ((candidate-content-overlay (make-overlay candidate-content-start candidate-content-end)))
(overlay-put candidate-content-overlay 'display "")
(overlay-put candidate-content-overlay
'face
'snails-candiate-content-face))
(insert "\n"))
;; Insert new empty line at last candiate of backend.
(insert "\n"))
;; Update candidate index to fetch name of next backend.
(setq candiate-index (+ candiate-index 1))))
;; Restore select line offset.
(snails-restore-select-line-offset)
)
;; Reset render flag.
(setq snails-need-render nil)))
(defun snails-record-select-line-offset ()
"Record select line offset."
(if snails-select-line-overlay
;; Record select line offset when `snails-select-line-overlay' is non-nil.
(catch 'line-offset
(dolist (header-line-overlay snails-header-line-overlays)
(when (> (overlay-end snails-select-line-overlay) (overlay-end header-line-overlay))
(setq snails-select-backend-name (buffer-substring (overlay-start header-line-overlay) (overlay-end header-line-overlay)))
(setq snails-select-candidate-offset
(- (line-number-at-pos (overlay-end snails-select-line-overlay))
(line-number-at-pos (overlay-start header-line-overlay))))
(throw 'line-offset nil))
))
;; Select first candiate offset if `snails-select-line-overlay' is nil.
(setq snails-select-backend-name (nth 0 (snails-get-backend-names)))
(setq snails-select-candidate-offset 1)
))
(defun snails-restore-select-line-offset ()
"Restore select line offset."
(unless (catch 'restore-line-offset
(dolist (header-line-overlay snails-header-line-overlays)
;; Restore select line offset before render content buffer.
(when (string-equal snails-select-backend-name (buffer-substring (overlay-start header-line-overlay) (overlay-end header-line-overlay)))
(goto-char (overlay-start header-line-overlay))
(forward-line snails-select-candidate-offset)
(snails-update-select-line)
(throw 'restore-line-offset t)
))
nil)
;; Select first candidate if backend not exists when restore select line offset.
(goto-char (point-min))
(snails-select-next-item)
(snails-update-select-line)
))
(defun snails-pointer-string ()
"Get string around cursor."
(if (use-region-p)
;; Get region string if mark is set.
(buffer-substring-no-properties (region-beginning) (region-end))
;; Get current symbol or string, and remove prefix char before return.
(let* ((current-string (if (snails-in-string-p)
(buffer-substring-no-properties
(1+ (car (snails-string-start+end-points)))
(cdr (snails-string-start+end-points)))
""))
(current-symbol (if (or (string-empty-p current-string)
(string-match-p "[[:space:]]" current-string))
;; Get symbol around point if string around point is empty or include spaces.
(thing-at-point 'symbol)
;; Otherwise, get string around point.
current-string)))
(cond ((string-prefix-p "." current-symbol)
(string-remove-prefix "." current-symbol))
((string-prefix-p "#" current-symbol)
(string-remove-prefix "#" current-symbol))
(t current-symbol)))
))
(defun snails-string-start+end-points (&optional state)
"Return a cons of the points of open and close quotes of the string.
The string is determined from the parse state STATE, or the parse state
from the beginning of the defun to the point.
This assumes that `snails-in-string-p' has already returned true, i.e.
that the point is already within a string."
(save-excursion
(let ((start (nth 8 (or state (snails-current-parse-state)))))
(goto-char start)
(forward-sexp 1)
(cons start (1- (point))))))
(defun snails-current-parse-state ()
"Return parse state of point from beginning of defun."
(let ((point (point)))
(beginning-of-defun)
(parse-partial-sexp (point) point)))
(defun snails-in-string-p (&optional state)
(or (nth 3 (or state (snails-current-parse-state)))
(and
(eq (get-text-property (point) 'face) 'font-lock-string-face)
(eq (get-text-property (- (point) 1) 'face) 'font-lock-string-face))
(and
(eq (get-text-property (point) 'face) 'font-lock-doc-face)
(eq (get-text-property (- (point) 1) 'face) 'font-lock-doc-face))
))
(defun snails-color-blend (c1 c2 alpha)
"Blend two colors C1 and C2 with ALPHA.
C1 and C2 are hexidecimal strings.
ALPHA is a number between 0.0 and 1.0 which corresponds to the
influence of C1 on the result."
(apply #'(lambda (r g b)
(format "#%02x%02x%02x"
(ash r -8)
(ash g -8)
(ash b -8)))
(cl-mapcar
(lambda (x y)
(round (+ (* x alpha) (* y (- 1 alpha)))))
(color-values c1) (color-values c2))))
(defun snails-get-theme-colors ()
"We need adjust snails's colors when user switch new theme."
(let* ((white "#FFFFFF")
(black "#000000")
(bg-mode (frame-parameter nil 'background-mode))
(bg-unspecified (string= (face-background 'default) "unspecified-bg"))
(fg-unspecified (string= (face-foreground 'default) "unspecified-fg"))
(fg (cond
((and fg-unspecified (eq bg-mode 'dark)) "gray80")
((and fg-unspecified (eq bg-mode 'light)) "gray20")
(t (face-foreground 'default))))
(bg (cond
((and bg-unspecified (eq bg-mode 'dark)) "gray20")
((and bg-unspecified (eq bg-mode 'light)) "gray80")
(t (face-background 'default))))
;; for light themes
(bg-dark (snails-color-blend black bg 0.1))
(bg-more-dark (snails-color-blend black bg 0.15))
(fg-dark (snails-color-blend fg bg-dark 0.7))
(fg-more-dark (snails-color-blend black fg 0.2))
;; for dark themes
(bg-light (snails-color-blend white bg 0.05))
(bg-more-light (snails-color-blend white bg 0.1))
(fg-light (snails-color-blend fg bg 0.7))
(fg-more-light (snails-color-blend white fg 0.3)))
(cond
((eq bg-mode 'dark)
(list bg-light fg-dark bg-more-light fg-more-light))
(t
(list bg-dark fg-light bg-more-dark fg-more-dark)
))))
(defun snails-init-face-with-theme ()
(let* ((colors (snails-get-theme-colors))
(content-bg-color (nth 0 colors))
(input-bg-color (nth 2 colors))
(input-fg-color (nth 3 colors)))
;; Set input buffer face.
(set-face-attribute 'snails-input-buffer-face nil
:background input-bg-color
:foreground input-fg-color)
;; Set coent buffer face.
(set-face-attribute 'snails-content-buffer-face nil
:background content-bg-color)
))
(defun snails-jump-to-next-item ()
"Select next candidate item."
;; Forward line.
(forward-line)
;; Skip empty line and header line.
(while (and (not (eobp))
(or
(snails-empty-line-p)
(snails-header-line-p)))
(forward-line))
;; Adjust line if reach bottom line.
(when (and (eobp)
(snails-empty-line-p))
(previous-line 2)))
(defun snails-jump-to-previous-item ()
"Select previous candidate item."
;; Previous line.
(previous-line)
(move-beginning-of-line 1)
;; Skip empty line and header line.
(while (and (not (bobp))
(or
(snails-empty-line-p)
(snails-header-line-p)))
(previous-line)
(move-beginning-of-line 1))
;; Adjust line if reach to line.
(when (bobp)
(forward-line)))
(defun snails-get-next-backend-overlay ()
(catch 'backend-overlay
(let (next-backend-overaly)
(dolist (header-line-overlay snails-header-line-overlays)
(when (> (point) (overlay-end header-line-overlay))
(throw 'backend-overlay next-backend-overaly))
(setq next-backend-overaly header-line-overlay)
))))
(defun snalis-get-prev-backend-overlay ()
(catch 'backend-overlay
(let (found-current)
(dolist (header-line-overlay snails-header-line-overlays)
(when found-current
(throw 'backend-overlay header-line-overlay))
(setq found-current (> (point) (overlay-end header-line-overlay)))
))))
(defun snails-select-backend-first-candidate (backend-overlay)
(when backend-overlay
(goto-line (line-number-at-pos (overlay-start backend-overlay)))
(snails-jump-to-next-item)
(snails-update-select-line)
))
(defun snails-get-candidate-backend-name (candidate-point)
"Get backend name at selected candidate position."
(catch 'backend-name
(dolist (header-line-overlay snails-header-line-overlays)
(when (> candidate-point (overlay-end header-line-overlay))
(throw 'backend-name (buffer-substring (overlay-start header-line-overlay) (overlay-end header-line-overlay))))
)))
(defun snails-backend-do (backend-name candidate)
"Confirm candidate with special backend."
(catch 'backend-do
(dolist (backend snails-backends)
(let ((name (cdr (assoc "name" (eval backend))))
(do-func (cdr (assoc "do" (eval backend)))))
(when (equal (eval name) backend-name)
;; Quit frame first.
(snails-quit)
;; Call backend do function.
(funcall do-func candidate)
(throw 'backend-do nil)
)))))
(defun snails-keep-cursor-visible ()
"Scrol window to keep cursor in visible area of window."
;; Scrol window to keep cursor in visible area of window.
(when (get-buffer-window snails-content-buffer)
(set-window-point (get-buffer-window snails-content-buffer) (point))))
(defun snails-header-line-p ()
"Detect whether at header line."
(let ((overlays (overlays-at (point)))
found)
(while overlays
(let ((overlay (car overlays)))
(if (eq (overlay-get overlay 'face) 'snails-header-line-face)
(setq found t)))
(setq overlays (cdr overlays)))
found))
(defun snails-empty-line-p ()
"Detect current line whether empty line."
(= (point-at-eol) (point-at-bol)))
(defun snails-wrap-buffer-icon (buf)
"Wrap display name with buffer icon, use for buffer search backend."
(if (featurep 'all-the-icons)
(format "%s %s"
(with-current-buffer buf
(all-the-icons-icon-for-buffer))
(string-trim-left (buffer-name buf)))