Repository navigation
Expand file tree
/
Copy pathvimini.vim
More file actions
766 lines (659 loc) · 21.5 KB
/
Copy pathvimini.vim
File metadata and controls
766 lines (659 loc) · 21.5 KB
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
" Vimini: Google Gemini Integration for Vim
" Prevent the script from being loaded more than once.
if exists('g:loaded_vimini')
finish
endif
let g:loaded_vimini = 1
" Configuration: API Key file path
let s:api_key_file = expand('~/.config/gemini.token')
" Configuration: Active Backend name
let g:vimini_backend = get(g:, 'vimini_backend', '')
" Configuration: How to split new windows ('vertical' or 'horizontal').
let g:vimini_split_method = get(g:, 'vimini_split_method', 'vertical')
" Configuration: Log file
let g:vimini_log_file = get(g:, 'vimini_log_file', expand('~/.var/vimini/vimini.log'))
" Configuration: Logging on/off
let g:vimini_logging = get(g:, 'vimini_logging', 'off')
" Configuration: Default path for saved reviews
let g:vimini_review_path = get(g:, 'vimini_review_path', '')
" Configuration: Maximum review tool iterations
let g:vimini_review_max_turns = get(g:, 'vimini_review_max_turns', 30)
" Configuration: Default git remote for PR/MR reviews
let g:vimini_default_remote = get(g:, 'vimini_default_remote', '')
" Configuration: Agent Channel
let g:vimini_channel = get(g:, 'vimini_channel', v:null)
let s:plugin_root_dir = fnamemodify(resolve(expand('<sfile>:p')), ':h')
function! ViminiChannelCallback(channel, msg)
py3 << EOF
try:
from vimini import main
msg = vim.eval('a:msg')
main.handle_channel_message(msg)
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Channel callback error: {error_message}'")
EOF
endfunction
let s:socket_path = ''
py3 << EOF
from os.path import normpath, join
import vim
try:
import sys
import os
plugin_root_dir = vim.eval('s:plugin_root_dir')
python_root_dir = normpath(join(plugin_root_dir, '..', 'python3'))
sys.path.insert(0, python_root_dir)
from vimini import main
api_key_file = vim.eval('s:api_key_file')
backend = vim.eval('g:vimini_backend')
log_file = vim.eval('g:vimini_log_file') if vim.eval('g:vimini_logging') == 'on' else None
main.initialize(api_key_file=api_key_file, logfile=log_file, backend=backend if backend else None)
from vimini import util
if util._BACKEND:
vim.command(f"let g:vimini_backend = '{util._BACKEND}'")
vim.command(f"let g:vimini_model = '{util._MODEL}'")
socket_path = main.start_agent()
if socket_path:
vim.command(f"let s:socket_path = '{socket_path}'")
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
if !empty(s:socket_path)
let g:vimini_channel = ch_open('unix:' . s:socket_path, {'callback': "ViminiChannelCallback"})
endif
py3 << EOF
import vim
try:
main.send_setup()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
" Expose a function to list available models
function! ViminiListModels()
if !exists('g:vimini_channel') || type(g:vimini_channel) != v:t_channel || ch_status(g:vimini_channel) !=# 'open'
echoerr '[Vimini] Error: Agent server channel is not open.'
return
endif
echo '[Vimini] Fetching models...'
try
let l:req = py3eval('main.list_models()')
call ch_sendexpr(g:vimini_channel, l:req)
catch
let l:error_message = substitute(v:exception, "'", "''", 'g')
echoerr '[Vimini] Error: ' . l:error_message
endtry
endfunction
command! ViminiListModels call ViminiListModels()
" Expose a function to switch or display backend
function! ViminiBackend(...)
let l:backend = get(a:, 1, '')
py3 << EOF
try:
from vimini import main
backend_arg = vim.eval('l:backend')
main.backend_command(backend_arg if backend_arg else None)
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
function! ViminiBackendComplete(ArgLead, CmdLine, CursorPos)
return py3eval("main.get_available_backends(vim.eval('a:ArgLead'))")
endfunction
command! -nargs=* -complete=customlist,ViminiBackendComplete ViminiBackend call ViminiBackend(<q-args>)
" Expose a function to Chat with Gemini
function! ViminiChat()
py3 << EOF
try:
from vimini import main
main.chat()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! ViminiChat call ViminiChat()
let g:vimini_thinking = get(g:, 'vimini_thinking', 'on')
" Expose a function to toggle thinking on and off
function! ViminiThinking(...)
let l:option = get(a:, 1, '')
" Handle explicit setting
if !empty(l:option)
if l:option ==# 'on' || l:option ==# 'off'
let g:vimini_thinking = l:option
else
echoerr "[Vimini] Invalid argument for ViminiThinking. Use 'on' or 'off'."
return
endif
" Handle toggling
else
if g:vimini_thinking ==# 'on'
let g:vimini_thinking = 'off'
else
let g:vimini_thinking = 'on'
endif
endif
echo "[Vimini] Thinking is now " . g:vimini_thinking
endfunction
command! -nargs=? ViminiThinking call ViminiThinking(<f-args>)
" Expose a function to toggle logging on and off
function! ViminiToggleLogging(...)
let l:option = get(a:, 1, '')
" Handle explicit setting
if !empty(l:option)
if l:option ==# 'on' || l:option ==# 'off'
let g:vimini_logging = l:option
else
echoerr "[Vimini] Invalid argument for ViminiToggleLogging. Use 'on' or 'off'."
return
endif
" Handle toggling
else
if g:vimini_logging ==# 'on'
let g:vimini_logging = 'off'
else
let g:vimini_logging = 'on'
endif
endif
" Call Python function to update logging state.
py3 << EOF
try:
from vimini import main
log_state = vim.eval('g:vimini_logging')
if log_state == 'on':
log_file = vim.eval('g:vimini_log_file')
main.logging(log_file)
else:
main.logging()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error setting log state: {error_message}'")
EOF
echo "[Vimini] Logging is now " . g:vimini_logging
endfunction
command! -nargs=? ViminiToggleLogging call ViminiToggleLogging(<f-args>)
" Expose a function to generate code with Gemini
function! ViminiCode(prompt)
py3 << EOF
try:
from vimini import main
prompt = vim.eval('a:prompt')
verbose = vim.eval('g:vimini_thinking') == 'on'
main.code(prompt, verbose=verbose)
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiCode call ViminiCode(string(<q-args>))
" Expose a function to apply the generated code
function! ViminiApply(...)
let l:job_id = v:null
if a:0 > 0
let l:job_id = a:1
endif
py3 << EOF
try:
from vimini import main
job_id_arg = vim.eval('l:job_id')
job_id = int(job_id_arg) if job_id_arg is not None else None
main.apply_code(job_id=job_id)
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiApply call ViminiApply(<f-args>)
function! ViminiReview(args)
" Reviews git diffs. Git objects can be specified with "-c <refs>".
" GitHub PRs can be specified with "--pr <number>" (or "--github <number>").
" GitLab MRs can be
8000
specified with "--mr <number>" (or "--gitlab <number>").
" A custom git remote can be specified with "--remote <remote>".
" A security-focused review can be requested with "--security".
" The rest of the arguments are treated as a prompt.
let l:git_objects_arg = v:null
let l:prompt_arg = ''
let l:security_focus = 0
let l:save_review = 0
let l:save_path = ''
let l:pr_arg = v:null
let l:mr_arg = v:null
let l:remote_arg = v:null
let l:args = a:args
" Handle key=value options like --save=path, --pr=123, --mr=123, --remote=origin
let l:idx = 0
while l:idx < len(l:args)
if l:args[l:idx] =~# '^--save='
let l:save_path = substitute(l:args[l:idx], '^--save=', '', '')
call remove(l:args, l:idx)
" Specifying a path implies saving
let l:save_review = 1
continue
elseif l:args[l:idx] =~# '^\%(--pr\|--github\|-pr\)=\d\+$'
let l:pr_arg = substitute(l:args[l:idx], '^\%(--pr\|--github\|-pr\)=', '', '')
call remove(l:args, l:idx)
continue
elseif l:args[l:idx] =~# '^\%(--mr\|--gitlab\|-mr\)=\d\+$'
let l:mr_arg = substitute(l:args[l:idx], '^\%(--mr\|--gitlab\|-mr\)=', '', '')
call remove(l:args, l:idx)
continue
elseif l:args[l:idx] =~# '^--remote='
let l:remote_arg = substitute(l:args[l:idx], '^--remote=', '', '')
call remove(l:args, l:idx)
continue
endif
let l:idx += 1
endwhile
let l:c_idx = index(l:args, '-c')
if l:c_idx != -1
if l:c_idx + 1 < len(l:args)
let l:git_objects_arg = l:args[l:c_idx + 1]
call remove(l:args, l:c_idx, l:c_idx + 1)
else
echoerr "[Vimini] Error: -c option requires an argument."
return
endif
endif
" Handle --pr / --github / -pr <number>
let l:pr_idx = index(l:args, '--pr')
if l:pr_idx == -1
let l:pr_idx = index(l:args, '--github')
endif
if l:pr_idx == -1
let l:pr_idx = index(l:args, '-pr')
endif
if l:pr_idx != -1
if l:pr_idx + 1 < len(l:args)
let l:pr_arg = l:args[l:pr_idx + 1]
call remove(l:args, l:pr_idx, l:pr_idx + 1)
else
echoerr "[Vimini] Error: --pr option requires an argument."
return
endif
endif
" Handle --mr / --gitlab / -mr <number>
let l:mr_idx = index(l:args, '--mr')
if l:mr_idx == -1
let l:mr_idx = index(l:args, '--gitlab')
endif
if l:mr_idx == -1
let l:mr_idx = index(l:args, '-mr')
endif
if l:mr_idx != -1
if l:mr_idx + 1 < len(l:args)
let l:mr_arg = l:args[l:mr_idx + 1]
call remove(l:args, l:mr_idx, l:mr_idx + 1)
else
echoerr "[Vimini] Error: --mr option requires an argument."
return
endif
endif
" Handle --remote <remote>
let l:remote_idx = index(l:args, '--remote')
if l:remote_idx != -1
if l:remote_idx + 1 < len(l:args)
let l:remote_arg = l:args[l:remote_idx + 1]
call remove(l:args, l:remote_idx, l:remote_idx + 1)
else
echoerr "[Vimini] Error: --remote option requires an argument."
return
endif
endif
let l:s_idx = index(l:args, '--security')
if l:s_idx != -1
let l:security_focus = 1
call remove(l:args, l:s_idx)
endif
let l:save_idx = index(l:args, '--save')
if l:save_idx != -1
let l:save_review = 1
call remove(l:args, l:save_idx)
endif
let l:prompt_arg = join(l:args, ' ')
py3 << EOF
try:
from vimini import main
prompt = vim.eval('l:prompt_arg')
git_objects = vim.eval('l:git_objects_arg')
security_focus = bool(int(vim.eval('l:security_focus')))
save_review = bool(int(vim.eval('l:save_review')))
save_path = vim.eval('l:save_path')
pr = vim.eval('l:pr_arg')
mr = vim.eval('l:mr_arg')
remote = vim.eval('l:remote_arg')
verbose = vim.eval('g:vimini_thinking') == 'on'
main.review(prompt, git_objects=git_objects, security_focus=security_focus, verbose=verbose, save=save_review, save_path=save_path, pr=pr, mr=mr, remote=remote)
except Exception as e:
error_message = str(e).replace("
8000
;\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiReview call ViminiReview([<f-args>])
" Expose a function to show git diff
function! ViminiDiff()
py3 << EOF
try:
from vimini import main
main.show_diff()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! ViminiDiff call ViminiDiff()
" Expose a function to generate and execute a git commit
function! ViminiCommit(q_args)
let l:assistant = 1
let l:regenerate = 0
let l:amend = 0
let l:other_args = []
let l:args = split(a:q_args)
for l:arg in l:args
if l:arg ==# '-n'
let l:assistant = 0
elseif l:arg ==# '-r'
let l:regenerate = 1
elseif l:arg ==# '-a'
let l:amend = 1
else
call add(l:other_args, l:arg)
endif
endfor
let l:prompt_refinement = join(l:other_args, ' ')
py3 << EOF
try:
from vimini import main
assistant = bool(int(vim.eval('l:assistant')))
regenerate = bool(int(vim.eval('l:regenerate')))
amend = bool(int(vim.eval('l:amend')))
refinement = vim.eval('l:prompt_refinement')
main.commit(assistant=assistant, regenerate=regenerate, amend=amend, refinement=refinement)
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiCommit call ViminiCommit(<q-args>)
" Expose a function to toggle file selection in commit buffer
function! ViminiCommitToggleFile()
py3 << EOF
try:
from vimini import main
main.toggle_file()
except Exception as e:
pass
EOF
endfunction
" Expose a function to manage uploaded files
function! ViminiFiles()
py3 << EOF
try:
from vimini import main
main.files_command()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=0 ViminiFiles call ViminiFiles()
" Expose a function to manage context files
function! ViminiContextFiles()
py3 << EOF
try:
from vimini import main
main.context_files_command()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=0 ViminiContextFiles call ViminiContextFiles()
" Expose a function to manage project configuration
function! ViminiConfig()
py3 << EOF
try:
from vimini import main
main.config_command()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=0 ViminiConfig call ViminiConfig()
" Expose a function to manage global/backend configuration
function! ViminiGlobalConfig()
py3 << EOF
try:
from vimini import main
main.global_config_command()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=0 ViminiGlobalConfig call ViminiGlobalConfig()
" Expose a function for autocompletion.
" This calls the non-blocking python function that handles the async request.
function! ViminiAutocomplete()
py3 << EOF
try:
from vimini import main
main.autocomplete()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
" Configuration: Autocomplete on/off
let g:vimini_autocomplete = get(g:, 'vimini_autocomplete', 'off')
let s:autocomplete_timer = -1
" Expose a function to toggle autocomplete on and off
function! ViminiToggleAutocomplete(...)
let l:option = get(a:, 1, '')
let l:old_state = g:vimini_autocomplete
" Handle explicit setting
if !empty(l:option)
if l:option ==# 'on' || l:option ==# 'off'
let g:vimini_autocomplete = l:option
else
echoerr "[Vimini] Invalid argument for ViminiToggleAutocomplete. Use 'on' or 'off'."
return
endif
" Handle toggling
else
if g:vimini_autocomplete ==# 'on'
let g:vimini_autocomplete = 'off'
else
let g:vimini_autocomplete = 'on'
endif
endif
echo "[Vimini] Autocomplete is now " . g:vimini_autocomplete
endfunction
command! -nargs=? ViminiToggleAutocomplete call ViminiToggleAutocomplete(<f-args>)
function! s:CancelAutocomplete()
py3 << EOF
try:
from vimini import main
# This signals the python background thread to stop and clears the queue.
main.cancel_autocomplete()
except Exception:
# Fail silently. It's not critical if this fails.
pass
EOF
endfunction
function! s:StopAutocompleteTimer()
if exists('s:autocomplete_timer') && s:autocomplete_timer != -1
call timer_stop(s:autocomplete_timer)
let s:autocomplete_timer = -1
endif
" Cancel any running python autocomplete job.
call s:CancelAutocomplete()
endfunction
function! s:ResetAutocompleteTimer()
" Stop the previous timer and cancel any pending request.
call s:StopAutocompleteTimer()
if g:vimini_autocomplete ==# 'on' && mode() ==# 'i'
let s:autocomplete_timer = timer_start(100, 's:TriggerAutocomplete')
endif
endfunction
function! s:TriggerAutocomplete(timer)
let s:autocomplete_timer = -1
if g:vimini_autocomplete ==# 'on' && mode() ==# 'i'
call ViminiAutocomplete()
endif
endfunction
augroup vimini_autocomplete
autocmd!
autocmd TextChangedI * call s:ResetAutocompleteTimer()
autocmd InsertLeave * call s:StopAutocompleteTimer()
augroup END
" --- Async Job Timers (Generic) ---
let s:job_timer = -1
function! ViminiInternalProcessJobQueue(timer)
py3 << EOF
try:
from vimini import util
util.process_queue()
except Exception:
pass
EOF
endfunction
function! ViminiInternalStartJobTimer()
if s:job_timer != -1
call timer_stop(s:job_timer)
endif
let s:job_timer = timer_start(50, 'ViminiInternalProcessJobQueue', {'repeat': -1})
endfunction
function! ViminiInternalStopJobTimer()
if s:job_timer != -1
call timer_stop(s:job_timer)
let s:job_timer = -1
endif
endfunction
" --- End Async Job Timers ---
" Expose ripgrep search functionality
function! s:ViminiRipGrep(q_args)
py3 << EOF
try:
from vimini import main
arg_string = vim.eval('a:q_args')
main.ripgrep_command(arg_string)
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=* ViminiRipGrep call s:ViminiRipGrep(<q-args>)
" Expose ripgrep apply functionality
function! ViminiRipGrepApply()
py3 << EOF
try:
from vimini import main
main.ripgrep_apply()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=0 ViminiRipGrepApply call ViminiRipGrepApply()
" Expose help functionality
function! ViminiHelp(...)
let l:cmd = get(a:, 1, '')
py3 << EOF
try:
from vimini import main
cmd = vim.eval('l:cmd')
main.help(cmd)
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=? -complete=command ViminiHelp call ViminiHelp(<f-args>)
" --- Status Monitor ---
function! ViminiStatus()
py3 << EOF
try:
from vimini import main
main.status_command()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! ViminiStatus call ViminiStatus()
" Expose a function to reload Vimini code
function! ViminiReload()
if exists('g:vimini_channel') && type(g:vimini_channel) == v:t_channel && ch_status(g:vimini_channel) ==# 'open'
call ch_close(g:vimini_channel)
let g:vimini_channel = v:null
endif
let s:socket_path = ''
py3 << EOF
try:
from vimini import main
main.stop_agent()
main.reload_vimini()
from vimini import main as reloaded_main
socket_path = reloaded_main.start_agent()
if socket_path:
vim.command(f"let s:socket_path = '{socket_path}'")
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error reloading: {error_message}'")
EOF
if !empty(s:socket_path)
let g:vimini_channel = ch_open('unix:' . s:socket_path, {'callback': "ViminiChannelCallback"})
endif
py3 << EOF
import vim
try:
main.send_setup()
except Exception as e:
error_message = str(e).replace("\n", " ").replace("\r", " ").replace("'", "''")
vim.command(f"echoerr '[Vimini] Error: {error_message}'")
EOF
endfunction
command! -nargs=0 ViminiReload call ViminiReload()
let s:status_timer = -1
function! ViminiInternalUpdateStatus(timer)
py3 << EOF
try:
from vimini import util
util.update_status_buffer()
except Exception:
pass
EOF
endfunction
function! ViminiInternalStartStatusTimer()
if s:status_timer == -1
let s:status_timer = timer_start(1000, 'ViminiInternalUpdateStatus', {'repeat': -1})
endif
endfunction
function! ViminiInternalStopStatusTimer()
if s:status_timer != -1
call timer_stop(s:status_timer)
let s:status_timer = -1
endif
endfunction
" --- Shutdown ---
function! s:ViminiShutdown()
if exists('g:vimini_channel') && type(g:vimini_channel) == v:t_channel && ch_status(g:vimini_channel) ==# 'open'
call ch_close(g:vimini_channel)
let g:vimini_channel = v:null
endif
py3 << EOF
try:
from vimini import main
main.stop_agent()
except Exception:
pass
EOF
endfunction
augroup vimini_shutdown
autocmd!
autocmd VimLeavePre * call s:ViminiShutdown()
augroup END