/
likelycomplete.vim
1666 lines (1466 loc) · 56.3 KB
/
likelycomplete.vim
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
" @Author: Tom Link (mailto:micathom AT gmail com?subject=[vim])
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Revision: 1646
scriptencoding utf-8
if !exists('g:loaded_tlib') || g:loaded_tlib < 119
runtime plugin/02tlib.vim
if !exists('g:loaded_tlib') || g:loaded_tlib < 119
echoerr 'tlib >= 1.19 is required'
finish
endif
endif
if !exists('g:likelycomplete#experimental')
" If >= 1, enable some experimental options:
" - |g:likelycomplete#set_completefunc|
" - |g:likelycomplete#auto_complete|
" If >= 2, enable even more experimental options:
" - |g:likelycomplete#prgname|
" - |g:likelycomplete#use_syntax|
" If >= 3, enable even more experimental options that are slow:
" - |g:likelycomplete#assess_context|
"
" Some of these experimental options may cause minor interruptions,
" delays, and increased memory usage.
"
" The list of features deemed 'experimental' may change from version
" to version.
let g:likelycomplete#experimental = 0 "{{{2
endif
if !exists('g:likelycomplete#list_set_filter')
" If true, the part of the words matching the the base text will be
" highlighted in |likelycomplete#SelectWord()|.
" If false, the setting of 'smartcase' is ignored.
let g:likelycomplete#list_set_filter = 0 "{{{2
endif
if !exists('g:likelycomplete#select_imap')
" The default map for |:Likelycompletemapselect|.
" If non-empty, enable |:Likelycompletemapselect| for all enabled
" filetypes.
let g:likelycomplete#select_imap = empty(maparg('<C-S-Space>', 'i'))? '<C-S-Space>' : '' "{{{2
endif
if !exists('g:likelycomplete#set_completefunc')
" If true, set 'completefunc' for supported buffers. The results of
" the old completefunc will be incorporated.
" See also |:Likelycompletemapcompletefunc|.
let g:likelycomplete#set_completefunc = g:likelycomplete#experimental >= 1 "{{{2
endif
if !exists('g:likelycomplete#auto_complete')
" The number of characters after which auto-completion should be
" invoked. If 0, auto-completion is disabled.
"
" *i_Ctrl-G_Ctrl-U* *likelycomplete-<c-g><c-u>*
" When auto-completion is enabled, a |imap| <c-g><c-u> is defined
" that can be used to temporarily disable auto-completion in order
" to perform some other kind of |ins-completion|. Auto-completion
" will be re-enabled on the next |CompleteDone| event issued by that
" completion.
"
" This option requires |g:likelycomplete#set_completefunc| to be
" true or |:Likelycompletemapcompletefunc| to be called.
let g:likelycomplete#auto_complete = g:likelycomplete#experimental >= 1 ? 3 : 0 "{{{2
endif
if !exists('g:likelycomplete#auto_complete_defs')
" :read: let g:likelycomplete#auto_complete_defs = {...} "{{{2
let g:likelycomplete#auto_complete_defs = {
\ '*': {'blacklist_syntax_trans_rx': '^\CComment$'},
\ }
endif
if !exists('g:likelycomplete#sources')
" A list of sources that will be used for compiling the list of
" possible completions.
"
" Possible elements:
"
" likelycomplete .. Include information gathered by
" the LikelyComplete plugin
" completefunc .. Include results from 'completefunc'
" omnifunc ...... Include results from 'omnifunc' -- please be
" aware that some implementations for omnifunc
" take their time (at least on first invocation).
" syntaxcomplete .. Use |syntaxcomplete#Complete|.
" words ......... Include the current buffer's keywords
" dictionaries .. Use dictionary files as defined in
" |g:likelycomplete#dictionaries|
" dictionary .... Use dictionary files as defined in
" 'dictionary'
" tags .......... Tags as returned by |taglist()|
" snipMate ...... snipMate snippets
" g:{VAR} ....... A global variable
" b:{VAR} ....... A buffer-local variable
" w:{VAR} ....... A window-local variable
" FUNCTION ...... A function that takes as arguments the filetype,
" the base, a dictionary of filetype-specific
" options and returns a list of possible
" completions
" files ......... Use files
"
" If an entry begins with "?", it is only evaluated when the list of
" possible completions is empty, i.e. none of the previous items
" yielded any completions.
"
" This is only used in conjunction with |:Likelycompletemapselect|
" and |:Likelycompletemapcompletefunc|.
let g:likelycomplete#sources = ['likelycomplete', 'words', 'dictionaries', 'tags'] "{{{2
endif
if !exists('g:likelycomplete#dictionaries')
" A dictionary of 'spelllang' => dictionary file.
"
" CAUTION: Please be aware that the value of spelllang is currently
" ignored and all dictionary files are used.
let g:likelycomplete#dictionaries = {} "{{{2
endif
if !exists('g:likelycomplete#match_beginning')
" If true, matches must match the beginning of a word -- this is
" only relevant to |:Likelycompletemapselect| and
" |:Likelycompletemapcompletefunc|. Matches for |i_Ctrl-P| must
" always match the beginning of a word.
let g:likelycomplete#match_beginning = 0 "{{{2
endif
if !exists('g:likelycomplete#use_fuzzy_matches')
" If true, use fuzzy matches for |:Likelycompletemapselect|
" and |:Likelycompletemapcompletefunc|.
" If you really believe in fuzzy matching, you might want to
" consider also setting |g:tlib#input#filter_mode| to 'fuzzy'.
let g:likelycomplete#use_fuzzy_matches = 1 "{{{2
endif
if !exists('g:likelycomplete#use_completedone')
" If true, add weights for the selected word.
let g:likelycomplete#use_completedone = 1 "{{{2
endif
if !exists('g:likelycomplete#list_picker')
" VIM plugin developers can add support for "list pickers" other
" than tlib by defining the following functions:
"
" likelycomplete#ListPicker(type, prompt, list, base)
"
" with
"
" type .... 's' (select single item) or 'm' (select multiple items)
" prompt .. A string
" list .... A list
" base .... A string that should be selected (may be ignored; see
" also |g:likelycomplete#list_set_filter|)
let g:likelycomplete#list_picker = 'tlib' "{{{2
endif
if !exists('g:likelycomplete#data_cfile')
let g:likelycomplete#data_cfile = tlib#persistent#Filename('likelycomplete', 'data', 1) "{{{2
endif
if !exists('g:likelycomplete#options')
" The following keys are supported:
" exclude_lines_rx ... Exclude lines matching this |regexp|
" strip_comments ..... Remove comments and any trailing text (not
" supported for all filetypes; requires
" cms_rx or 'cms' to be set)
" strip_strings ...... Remove strings from lines
" strip_multiline_strings .. Remove multi-line strings from the text
" strip_numbers ...... Remove numbers from lines
" strip_rx ........... Remove matching text from lines
" cms_rx ............. A |regexp| matching comments (override use
" of 'cms')
"
" The following keys can override global parameters:
" sources ............ Override |g:likelycomplete#sources|
" dictionaries ....... Override |g:likelycomplete#dictionaries|
" set_completefunc ... Override |g:likelycomplete#set_completefunc|
" auto_complete ...... Override |g:likelycomplete#auto_complete|
" auto_complete_defs . Override |g:likelycomplete#auto_complete_defs|
" use_fuzzy_matches .. Override |g:likelycomplete#use_fuzzy_matches|
" use_completedone ... Override |g:likelycomplete#use_completedone|
" use_syntax ......... Override |g:likelycomplete#use_syntax|
" ignore_syntax_rx ... Override |g:likelycomplete#ignore_syntax_rx|
" match_beginning .... Override |g:likelycomplete#match_beginning|
" maxsize ............ Override |g:likelycomplete#maxsize|
" once_per_file ...... Override |g:likelycomplete#once_per_file|
" assess_context ..... Override |g:likelycomplete#assess_context|
" word_minlength ..... Override |g:likelycomplete#word_minlength|
let g:likelycomplete#options = {}
endif
if !exists('g:likelycomplete#maxsize')
" The maximum number of words kept per filetype.
let g:likelycomplete#maxsize = 5000 "{{{2
endif
if !exists('g:likelycomplete#word_minlength')
" Minimum length of words.
let g:likelycomplete#word_minlength = 3 "{{{2
endif
if !exists('g:likelycomplete#once_per_file')
" If true, count a word only once per file. By default, N occurences
" of word X in a buffer will be counted as N times.
let g:likelycomplete#once_per_file = 0 "{{{2
endif
if !exists('g:likelycomplete#assess_context')
" The number of preceding words that should be taken into account
" when sorting the list of possible completions.
" This is only used in conjunction with |:Likelycompletemapselect|
" and |:Likelycompletemapcompletefunc|.
"
" CAUTION: This may cause a slow-down and increased memory use. This
" option only works properly if |g:likelycomplete#once_per_file|
" evaluates to false.
let g:likelycomplete#assess_context = g:likelycomplete#experimental >= 3 && !g:likelycomplete#once_per_file ? 5 : 0 "{{{2
endif
if !exists('g:likelycomplete#context_lines')
" When assessing a word in context also include the previous N
" lines.
let g:likelycomplete#context_lines = 2 "{{{2
endif
if !exists('g:likelycomplete#base')
" The base number of observations.
" Also the number of files, you can edit without removing a word
" that was't included in any of these files.
" If this value is too low, new words won't get a chance to be
" included in the list.
let g:likelycomplete#base = 50 "{{{2
endif
if !exists('g:likelycomplete#max')
" The maximum number of observations taken into account.
let g:likelycomplete#max = 10000 "{{{2
endif
if !exists('g:likelycomplete#base_context')
" Forget a word in context after N files where it was missing.
let g:likelycomplete#base_context = 20 "{{{2
endif
if !exists('g:likelycomplete#max_context')
" The max weight for words in context.
"
" NOTE: The actual max value is |g:likelycomplete#max_context| +
" |g:likelycomplete#base_context| - 1.
let g:likelycomplete#max_context = 1000 "{{{2
endif
if !exists('g:likelycomplete#prgname')
" Non-empty use this program to asynchronously update word lists.
" You should make sure though that your version of vim has
" |+clientserver| support -- this is most likely true for the
" Windows or GTK Version of GVIM.
let g:likelycomplete#prgname = g:likelycomplete#experimental >= 2 && has('clientserver') && !empty(v:servername) ? v:progname : '' "{{{2
endif
if !exists('g:likelycomplete#vimprg')
" If |g:likelycomplete#use_vimproc| is enabled, vim is used to
" update the word-list. By default, we assume, vim is in $PATH.
let g:likelycomplete#vimprg = 'vim' "{{{2
endif
if !exists('g:likelycomplete#run_async')
" How to run |g:likelycomplete#prgname|.
let g:likelycomplete#run_async = has('win16') || has('win32') || has('win64') ? (g:likelycomplete#prgname =~? '\c\<gvim\>' ? '!start %s' : '!start /min cmd /c %s') : '! ( %s > /dev/null ) &' "{{{2
endif
if !exists('g:likelycomplete#use_vimproc')
" Use vimproc[1] in some situations (e.g. when updating the word
" list).
"
" [1] https://github.com/Shougo/vimproc.vim
let g:likelycomplete#use_vimproc = exists('g:loaded_vimproc') && g:loaded_vimproc > 0 "{{{2
endif
if !exists('g:likelycomplete#prg_init_exec')
" An |:exec| command that is run after starting
" |g:likelycomplete#prgname|.
let g:likelycomplete#prg_init_exec = 'set lines=10 ch=5 co='. &co "{{{2
endif
if !exists('g:likelycomplete#max_wait')
let g:likelycomplete#max_wait = 5000 "{{{2
endif
if !exists('g:likelycomplete#use_syntax')
" If > 0, use information about syntax groups for ranking
" completions.
"
" If 1, use |synIDtrans()|. If 2, use |synID()|.
"
" CAUTION: This doesn't work yet reliably. The quality of results
" depends on the quality of the vim syntax file for a given
" filetype.
"
" NOTE: Since this is time-consuming, it is required to enable async
" updates via |g:likelycomplete#prgname| and
" |g:likelycomplete#run_async|.
let g:likelycomplete#use_syntax = g:likelycomplete#experimental >= 2 && !empty(g:likelycomplete#run_async) && !empty(g:likelycomplete#prgname) "{{{2
endif
if !exists('g:likelycomplete#ignore_syntax_rx')
let g:likelycomplete#ignore_syntax_rx = '\%(Comment\|NonText\|String\)' "{{{2
endif
if !exists('g:likelycomplete#use_encoding')
let g:likelycomplete#use_encoding = &enc "{{{2
endif
if !exists('g:likelycomplete#debug')
let g:likelycomplete#debug = 0 "{{{2
endif
function! likelycomplete#LoadData() "{{{3
if g:likelycomplete#debug | echom 'likelycomplete#LoadData' | endif
let s:likelycomplete_data = tlib#persistent#Get(g:likelycomplete#data_cfile, {'version': 1, 'ft': {}, 'ft_options': {}})
return ''
endf
call likelycomplete#LoadData()
function! s:SaveData() "{{{3
Tlibtrace 'likelycomplete', g:likelycomplete#data_cfile
call tlib#persistent#Save(g:likelycomplete#data_cfile, s:likelycomplete_data)
endf
function! s:SetData(filetype, data) "{{{3
let s:likelycomplete_data.ft[a:filetype] = a:data
endf
function! s:GetData(...) "{{{3
let filetype = a:0 >= 1 ? a:1 : s:GetFiletype()
call s:EnsureFiletype(filetype)
return s:likelycomplete_data.ft[filetype]
endf
function! s:FtOptions(filetype) "{{{3
let ft_options = get(s:likelycomplete_data.ft_options, a:filetype, {})
call extend(ft_options, g:likelycomplete#options)
" TLogVAR exists('g:likelycomplete#ft#'. a:filetype .'#options')
if exists('g:likelycomplete#ft#'. a:filetype .'#options')
call extend(ft_options, g:likelycomplete#ft#{a:filetype}#options)
endif
let ft_options.Get = function('likelycomplete#GetOption')
return ft_options
endf
function! likelycomplete#GetOption(name, ...) dict "{{{3
return get(self, a:name, a:0 >= 1 ? a:1 : g:likelycomplete#{a:name})
endf
function! likelycomplete#String2Args(string) "{{{3
let opts = {}
for word in split(a:string, '\s\+')
let m = matchlist(word, '^\([^=]\+\)=\(.*\)$')
if empty(m)
throw "LikelyComplete: Malformed arguments string: ". string(a:string)
endif
let opts[m[1]] = m[2]
endfor
return opts
endf
function! likelycomplete#SetupFiletype(filetype, options) "{{{3
let filetype = s:ValidFiletype(a:filetype)
call s:EnsureFiletype(filetype, a:options)
call likelycomplete#SetupBuffer(filetype, bufnr('%'))
endf
function! likelycomplete#SetupBuffer(filetype, bufnr) "{{{3
let filetype = s:ValidFiletype(a:filetype)
" TLogVAR filetype, a:bufnr
call s:EnsureFiletype(filetype)
call s:SetupComplete(filetype)
if !empty(g:likelycomplete#select_imap)
call likelycomplete#MapSelectWord(g:likelycomplete#select_imap)
endif
" if g:likelycomplete_per_window && exists('w:likelycomplete_filetype')
" echohl WarningMsg
" echom "LikelyComplete: No update when using w:likelycomplete_filetype"
" echohl NONE
" else
exec 'autocmd! LikelyComplete BufDelete,VimLeavePre <buffer='. a:bufnr .'>'
exec 'autocmd LikelyComplete InsertLeave <buffer='. a:bufnr .'> call s:ResetLast()'
exec 'autocmd LikelyComplete BufDelete <buffer='. a:bufnr .'> call s:UpdateWordList('. a:bufnr .','. string(filetype) .','. string(expand('%:p')) .', 1)'
exec 'autocmd LikelyComplete VimLeavePre <buffer='. a:bufnr .'> call s:UpdateWordList('. a:bufnr .','. string(filetype) .','. string(expand('%:p')) .', 0)'
" endif
endf
function! s:SetFiletypeOptions(filetype, options) "{{{3
if !has_key(s:likelycomplete_data.ft_options, a:filetype)
let s:likelycomplete_data.ft_options[a:filetype] = {}
endif
if !empty(a:options)
call extend(s:likelycomplete_data.ft_options[a:filetype], a:options)
endif
" if !has_key(ft_options, '_includes')
" let ft_options._includes = []
" endif
" while has_key(ft_options, 'includes')
" let ft_extended = ft_options.includes
" call add(ft_options._includes, ft_extended)
" call remove(ft_options, 'includes')
" let other_options = s:FtOptions(ft_extended)
" let ft_options = extend(ft_options, other_options, 'keep')
" endwh
endf
function! s:ValidFiletype(filetype) "{{{3
return empty(a:filetype) ? '_' : a:filetype
endf
function! s:GetFiletype() "{{{3
if g:likelycomplete_per_window && exists('w:likelycomplete_filetype')
let ft = w:likelycomplete_filetype
elseif exists('b:likelycomplete_filetype')
let ft = b:likelycomplete_filetype
else
let ft = &l:filetype
endif
return s:ValidFiletype(ft)
endf
function! s:SetDerivedOptions(filetype) "{{{3
if s:GetFiletype() == a:filetype
let ft_options = s:likelycomplete_data.ft_options[a:filetype]
if !has_key(ft_options, 'cms')
let s:likelycomplete_data.ft_options[a:filetype].cms = &l:cms
endif
if !empty(&l:iskeyword) && (!has_key(ft_options, '_split_rx') || get(ft_options, '_iskeyword', '') != &l:iskeyword)
let s:likelycomplete_data.ft_options[a:filetype]._iskeyword = &l:iskeyword
let s:likelycomplete_data.ft_options[a:filetype]._split_rx = s:GetKeywordRx(&l:iskeyword, 1)
endif
endif
endf
function! s:GetKeywordRx(iskeyword, inverse) "{{{3
let parts = map(split(a:iskeyword, ','), 's:KeywordPartRx(split(v:val, ''-''), 0)')
let pos = filter(copy(parts), 'v:val !~ ''^\^''')
let neg = filter(copy(parts), 'v:val =~ ''^\^''')
let neg = map(neg, 'substitute(v:val, ''^\^'', "", "")')
let idash = index(pos, '-')
if idash != -1
call add(pos, remove(pos, idash))
endif
let idash = index(neg, '-')
if idash != -1
call add(neg, remove(neg, idash))
endif
if a:inverse
if !empty(pos)
let pos = insert(pos, '^')
endif
else
if !empty(neg)
let neg = insert(neg, '^')
endif
endif
let poss = join(pos, '')
let negs = join(neg, '')
if !empty(poss)
if !empty(negs)
let rx = printf('\%([%s]\+\|[%s]\+\)\+', poss, negs)
else
let rx = '['. poss .']\+'
endif
elseif !empty(negs)
let rx = '['. negs .']\+'
elseif a:inverse
let rx = '\W\+'
else
let rx = '\w\+'
endif
return rx
endf
function! s:KeywordPartRx(subparts, inverse) "{{{3
if len(a:subparts) > 2
throw "KeywordPartRx: Internal error: ". string(a:subparts)
endif
let subparts = map(a:subparts, 'v:val =~ ''^\d\+$'' ? nr2char(v:val) : v:val')
let inverse = a:inverse
let rv = join(subparts, '-')
if rv =~ '^\^'
let inverse = !inverse
endif
if rv == '@'
let rv = '[:alpha:]'
endif
let pre = inverse ? '^' : ''
return pre . rv
endf
let s:setup = {}
function! s:EnsureFiletype(...) "{{{3
let filetype = a:0 >= 1 ? a:1 : s:GetFiletype()
let options = a:0 >= 2 ? a:2 : {}
" TLogVAR filetype, options
if !has_key(s:setup, filetype)
try
call likelycomplete#ft#{filetype}#Init()
catch /^Vim\%((\a\+)\)\=:E117/
endtry
endif
if !has_key(s:likelycomplete_data.ft, filetype)
call s:SetData(filetype, {})
call s:SetFiletypeOptions(filetype, options)
call LikelycompleteSetupFiletype(filetype)
call s:SaveData()
let rv = 1
else
let rv = 0
endif
if !has_key(s:setup, filetype)
let s:setup[filetype] = 1
call s:SetDerivedOptions(filetype)
endif
return rv
endf
function! likelycomplete#RemoveFiletype(filetype) "{{{3
let filetype = s:ValidFiletype(a:filetype)
unlet! s:likelycomplete_data.ft[filetype]
unlet! s:likelycomplete_data.ft_options[filetype]
let fname = s:WordListFilename(filetype)
if filereadable(fname)
call delete(fname)
endif
call s:SaveData()
echom "LikelyComplete: Removed support for" filetype
endf
function! s:WordListFilename(filetype) "{{{3
let dir = fnamemodify(g:likelycomplete#data_cfile, ':p:h')
let fname = tlib#file#Join([dir, a:filetype .'_words'])
return fname
endf
function! s:SetupComplete(filetype) "{{{3
let fname = s:WordListFilename(a:filetype)
call s:AddDict(fname)
let ft_options = s:FtOptions(a:filetype)
let sources = ft_options.Get('sources')
if index(sources, 'dictionaries') != -1
for [spelllang, dict] in items(ft_options.Get('dictionaries'))
call s:AddDict(dict)
endfor
endif
if ft_options.Get('set_completefunc')
call likelycomplete#SetComleteFunc()
let auto_complete_defs = ft_options.Get('auto_complete_defs')
if ft_options.Get('auto_complete') && (has_key(auto_complete_defs, a:filetype) || has_key(auto_complete_defs, '*'))
autocmd! LikelyComplete CursorMovedI <buffer>
autocmd LikelyComplete CursorMovedI <buffer> if !exists('b:likelycomplete_disable_auto_complete') && !pumvisible() | call s:AutoComplete() | endif
imap <buffer> <silent> <c-g><c-u> <c-\><c-o>:call likelycomplete#EscapeAutoComplete('')<cr>
endif
endif
endf
function! s:AddDict(fname) "{{{3
if filereadable(a:fname)
let opt = 'k'. a:fname
if stridx(&l:complete, opt) == -1
exec 'setl complete+='. escape(opt, ' ,\')
endif
if stridx(&l:dictionary, a:fname) == -1
exec 'setl dictionary+='. escape(a:fname, ' ,\')
endif
endif
endf
if g:likelycomplete#debug
function! likelycomplete#Tokenize(text, ...) "{{{3
let filetype = a:0 >= 1 ? a:1 : &ft
let ft_options = s:FtOptions(filetype)
return s:Tokenize(ft_options, a:text, &l:fenc)
endf
endif
function! s:Tokenize(ft_options, text, fenc) "{{{3
let text = a:text
if has('iconv') && !empty(g:likelycomplete#use_encoding) && a:fenc !=? g:likelycomplete#use_encoding
let text = iconv(text, a:fenc, g:likelycomplete#use_encoding)
endif
if a:ft_options.Get('strip_multiline_strings', 1)
let text = s:RemoveStrings(a:ft_options, text)
endif
let _split_rx = a:ft_options.Get('_split_rx', '\W\+')
return split(text, _split_rx)
endf
function! s:RemoveStrings(ft_options, text) "{{{3
let text = a:text
let text = substitute(text, '"\(\\"\|[^"]\+\)*"', ' ', 'g')
let text = substitute(text, '''\(''''\|\\''\|[^'']\+\)*''', ' ', 'g')
return text
endf
function! likelycomplete#AsyncUpdateWordList(servername, filetype, filename) "{{{3
Tlibtrace 'likelycomplete', a:servername, a:filetype, a:filename
let filetype = s:ValidFiletype(a:filetype)
" set verbosefile=$HOME/tmp/lc.log
" set verbose=12
try
call s:UpdateWordListNow(-1, filetype, a:filename)
if s:ServerExists(a:servername)
call remote_expr(a:servername, 'likelycomplete#LoadData()')
endif
catch
echohl Error
echom v:exception
echohl NONE
endtry
endf
function! s:ServerExists(servername) "{{{3
let servers = split(serverlist(), '\n')
let rv = index(servers, a:servername) != -1
" TLogVAR rv
return rv
endf
function! likelycomplete#EnsureServer(allow_start_server) "{{{3
" let servername = '_LIKELYCOMPLETE_'. v:servername
let servername = '_LIKELYCOMPLETE_'
if servername == v:servername
let servername = ''
elseif !s:ServerExists(servername)
if a:allow_start_server
let cmd = printf('%s -n --servername %s -c "call likelycomplete#InitServer(%s)"',
\ g:likelycomplete#prgname,
\ servername,
\ string(v:servername))
echo 'LikelyComplete: Starting server' servername '...'
call s:Run(cmd)
let s:handshake = 0
let msecs = 0
let step = 500
while msecs < g:likelycomplete#max_wait && !s:handshake
let msecs += step
exec 'sleep' step .'m'
endwh
" TLogVAR msecs
" echom "DBG" s:handshake
if s:handshake
echo
else
let servername = ''
echohl WarningMsg
echom 'LikelyComplete: Timeout when starting server:' servername
echom 'LikelyComplete: Please check the value of g:likelycomplete#prgname:' g:likelycomplete#prgname
echohl NONE
endif
unlet! s:handshake
else
let servername = ''
endif
endif
if !empty(servername)
if remote_expr(servername, 'likelycomplete#RegisterClient('. string(v:servername) .')')
exec 'au LikelyComplete VimLeave * call s:StopServer('. string(servername) .')'
endif
endif
" TLogVAR servername
return servername
endf
function! likelycomplete#Handshake(servername) "{{{3
" TLogVAR a:servername
let s:handshake = 1
return s:handshake
endf
function! s:StopServer(servername) "{{{3
" TLogVAR a:servername
if s:ServerExists(a:servername)
call remote_send(a:servername, ':call likelycomplete#ExitServer('. string(v:servername) .')<CR>')
endif
endf
function! likelycomplete#InitServer(servername) "{{{3
" TLogVAR a:servername
if has('gui_running')
suspend
endif
if s:ServerExists(a:servername)
exec g:likelycomplete#prg_init_exec
call append(0, [
\ 'This instance of '. v:progname .' is managed by LikelyComplete.',
\ 'Please don''t use it for editing files.',
\ 'You can close it if you want to.',
\ ])
0
setlocal nomodified
let expr = 'likelycomplete#Handshake('. string(v:servername) .')'
" TLogVAR expr
call remote_expr(a:servername, expr)
else
qall!
endif
endf
let s:clients = {}
function! likelycomplete#RegisterClient(clientname) "{{{3
let is_new_client = !has_key(s:clients, a:clientname)
if is_new_client
let s:clients[a:clientname] = 1
endif
return is_new_client
endf
function! likelycomplete#ExitServer(clientname) "{{{3
" TLogVAR v:servername, a:clientname
" echom "DBG 1" string(keys(s:clients))
let servers = split(serverlist(), '\n')
let s:clients = filter(s:clients, 'v:key != a:clientname && index(servers, v:key) != -1')
" echom "DBG 2" string(keys(s:clients))
if empty(s:clients)
qall!
endif
endf
function! s:Run(cmd) "{{{3
let run = printf(g:likelycomplete#run_async, a:cmd)
if g:likelycomplete#debug
" TLogVAR run
exec 'silent' escape(run, '#%')
else
exec 'silent!' escape(run, '#%')
endif
endf
let s:sfile = expand('<sfile>:p')
function! s:UpdateWordList(bufnr, filetype, filename, allow_start_server) "{{{3
Tlibtrace 'likelycomplete', a:bufnr, a:filetype, a:filename, a:allow_start_server
let ft_options = s:FtOptions(a:filetype)
if index(ft_options.Get('sources'), 'likelycomplete') == -1
return
endif
let runtype = 'now'
if s:sfile != fnamemodify(a:filename, ':p')
if g:likelycomplete#use_vimproc && a:allow_start_server && !empty('g:likelycomplete#vimprg')
let runtype = 'vimproc'
elseif !empty(g:likelycomplete#prgname) && !empty(g:likelycomplete#run_async)
let runtype = 'async'
endif
endif
if runtype == 'vimproc'
let expr = printf(':call likelycomplete#AsyncUpdateWordList(%s, %s, %s)<CR>',
\ string(v:servername),
\ string(a:filetype),
\ string(fnameescape(a:filename)))
let cmd = printf('%s -c "%s" -c "qa!"', g:likelycomplete#vimprg, expr)
" TLogVAR cmd
call vimproc#system_bg(cmd)
elseif runtype == 'async'
let servername = likelycomplete#EnsureServer(a:allow_start_server)
" TLogVAR servername
if empty(servername)
let runtype = 'now'
else
if s:Getbufvar(a:bufnr, 'likelycomplete_done', 0)
return
endif
let expr = printf(':call likelycomplete#AsyncUpdateWordList(%s, %s, %s)<CR>',
\ string(v:servername),
\ string(a:filetype),
\ string(fnameescape(a:filename)))
" TLogVAR expr
call remote_send(servername, expr)
call setbufvar(a:bufnr, 'likelycomplete_done', 1)
endif
endif
Tlibtrace 'likelycomplete', runtype
if runtype == 'now'
if v:version >= 800
call timer_start(500, {t -> s:UpdateWordListNow(a:bufnr, a:filetype, a:filename)})
else
call s:UpdateWordListNow(a:bufnr, a:filetype, a:filename)
endif
endif
endf
function! s:GetEnrichedBufferWords(bufnr, filetype, filename, ft_options) "{{{3
if a:bufnr == -1 && a:ft_options.Get('use_syntax')
" let rv = s:ScanBufferWords(a:bufnr, a:filetype, a:filename, a:ft_options)
return s:ScanBufferWords(a:bufnr, a:filetype, a:filename, a:ft_options)
" return {'words': s:GetBufferWords(a:bufnr, a:filetype, a:filename, a:ft_options)}
else
return {'words': s:GetBufferWords(a:bufnr, a:filetype, a:filename, a:ft_options)}
endif
endf
function! s:GetSyn(ft_options, lnum, col) "{{{3
" TLogVAR bufnr('%'), a:lnum, a:col
" echom "DBG" string(getline(a:lnum))
let synid = synID(a:lnum, a:col, 1)
if a:ft_options.Get('use_syntax') == 1
let synid = synIDtrans(synid)
endif
let syn = synIDattr(synid, 'name')
" TLogVAR syn, synid
return syn
endf
function! s:ScanBufferWords(bufnr, filetype, filename, ft_options) "{{{3
" TLogVAR a:bufnr, a:filename
exec 'split' fnameescape(a:filename)
norm! gg^
let words = []
let syntax = {}
let ignore_syntax_rx = a:ft_options.Get('ignore_syntax_rx')
let word_minlength = a:ft_options.Get('word_minlength')
let exclude_lines_rx = a:ft_options.Get('exclude_lines_rx', '')
let strip_numbers = a:ft_options.Get('strip_numbers', 1)
" TLogVAR ignore_syntax_rx
let pos = getpos('.')
while search('\k\+', 'Wc')
if getline('.') =~ exclude_lines_rx
norm! +
else
let word = expand('<cword>')
if strwidth(word) >= word_minlength &&
\ !(strip_numbers && word =~ '^-\?\d\+\(\.\d\+\)\?$')
let syn = s:GetSyn(a:ft_options, line('.'), col('.'))
if syn !~ ignore_syntax_rx
" TLogVAR word, syn
call add(words, word)
if !empty(syn)
if !has_key(syntax, word)
let syntax[word] = {}
endif
let syntax[word][syn] = 1
endif
endif
endif
norm! W
endif
let pos1 = getpos('.')
if pos == pos1
break
endif
let pos = pos1
endwh
exec 'bdelete!' fnameescape(a:filename)
return {'words': words, 'syntax': syntax}
endf
function! s:GetBufferWords(bufnr, filetype, filename, ft_options) "{{{3
" TLogVAR a:bufnr, a:filetype, a:filename, a:ft_options, bufnr('%')
if bufnr('%') == a:bufnr
let lines = getline(1, line('$'))
elseif bufloaded(a:bufnr) && exists('*getbufline')
let lines = getbufline(a:bufnr, 1, '$')
elseif filereadable(a:filename)
let lines = readfile(a:filename)
else
return []
endif
let exclude_lines_rx = a:ft_options.Get('exclude_lines_rx', '')
if !empty(exclude_lines_rx)
let lines = filter(lines, 'v:val !~ exclude_lines_rx')
endif
if a:ft_options.Get('strip_strings', 1)
let lines = map(lines, 's:RemoveStrings(a:ft_options, v:val)')
endif
let strip_rx = a:ft_options.Get('strip_rx', '')
if !empty(strip_rx)
let lines = map(lines, 'substitute(v:val, strip_rx, " ", "g")')
endif
if a:ft_options.Get('strip_comments', 1) && has_key(a:ft_options, 'cms')
let cms_rx = a:ft_options.Get('cms_rx', '')
if empty(cms_rx)
let cms_rx = '\V'. substitute(escape(a:ft_options.cms, '\'), '%s', '\\.\\{-}', '')
endif
if cms_rx !~ '\\$\$'
let cms_rx .= '\.\*\$'
endif
let lines = filter(lines, 'v:val !~ cms_rx')
endif
let words = s:Tokenize(a:ft_options, join(lines), getbufvar(a:bufnr, '&fenc'))
let word_minlength = a:ft_options.Get('word_minlength')
let words = filter(words, '!empty(v:val) && strwidth(v:val) >= word_minlength')
if a:ft_options.Get('strip_numbers', 1)
let words = filter(words, 'v:val !~ ''^-\?\d\+\(\.\d\+\)\?$''')
endif
return words
endf
if v:version >= 704
function! s:Getbufvar(bufnr, var, ...) "{{{3
let default = a:0 >= 1 ? a:1 : ''
let val = getbufvar(a:bufnr, a:var, default)
return val
endf
else
function! s:Getbufvar(bufnr, var, ...) "{{{3
let default = a:0 >= 1 ? a:1 : ''
let val = getbufvar(a:bufnr, a:var)
return empty(val) ? default : val
endf
endif
function! s:UpdateWordListNow(bufnr, filetype, filename) "{{{3
Tlibtrace 'likelycomplete', a:bufnr, a:filetype, a:filename
if a:bufnr > 0 && s:Getbufvar(a:bufnr, 'likelycomplete_done', 0)
return
endif
if a:bufnr > 0
echo 'LikelyComplete: Updating' a:filetype 'word list'
endif
let ft_options = s:FtOptions(a:filetype)
let enriched_words = s:GetEnrichedBufferWords(a:bufnr, a:filetype, a:filename, ft_options)
let words = enriched_words.words
let data = s:GetData(a:filetype)
if !empty(words)