~glacambre/firenvim

ref: 076c1a4f04db2173afb395d0af8c63a5bbcab6a8 firenvim/autoload/firenvim.vim -rw-r--r-- 25.1 KiB
076c1a4f — Brian Ryall fixes installing fire nvim on brave 8 months ago
                                                                                
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
let s:firenvim_done = 0
let s:script_dir = expand('<sfile>:p:h:h')

function! firenvim#get_chan() abort
        if (exists('g:last_focused_firenvim_channel'))
                return g:last_focused_firenvim_channel
        endif
        let l:uis = filter(nvim_list_uis(),
                \ {i, ui -> nvim_get_chan_info(ui.chan).client.name ==? 'Firenvim'})
        if len(l:uis) != 1
                if len(l:uis) == 0
                        throw 'firenvim#focus_page(): No firenvim ui found!'
                endif
                throw 'firenvim#focus_page(): Too many UIs found!'
        endif
        return uis[0].chan
endfunction

function! firenvim#eval_js(js, ...) abort
        let callback_name = get(a:, 1, '')
        call rpcnotify(firenvim#get_chan(), 'firenvim_eval_js', a:js, callback_name)
endfunction

" Asks the browser extension to release focus from the frame and focus the
" page instead
function! firenvim#focus_input() abort
        call rpcnotify(firenvim#get_chan(), 'firenvim_focus_input')
endfunction

" Asks the browser extension to release focus from the frame and focus the
" page instead
function! firenvim#focus_page() abort
        call rpcnotify(firenvim#get_chan(), 'firenvim_focus_page')
endfunction

" Asks the browser extension to hide the firenvim frame
function! firenvim#hide_frame() abort
        call rpcnotify(firenvim#get_chan(), 'firenvim_hide_frame')
endfunction

" Asks the browser extension to send one or multiple key events to the
" underlying input field.
function! firenvim#press_keys(...) abort
        if a:0 < 1
                throw 'firenvim#press_keys expects at least one argument'
        endif
        let l:keys = copy(a:000)
        if type(l:keys[0]) == type([])
                if a:0 > 1
                        throw 'firenvim#press_keys expects a single list argument'
                endif
                let l:keys = l:keys[0]
        endif
        if len(filter(copy(l:keys), { key, value -> type(value) == type("") })) != len(l:keys)
                throw 'Key symbols must be strings.'
        endif
        call rpcnotify(firenvim#get_chan(), 'firenvim_press_keys', l:keys)
endfunction

" Simple helper to build the right path depending on the platform.
function! s:build_path(list) abort
        let l:path_separator = '/'
        if has('win32')
                let l:path_separator = "\\"
        endif
        return join(a:list, l:path_separator)
endfunction

" Entry point of the vim-side of the extension.
" This function does the following things:
" - Get a security token from neovim's stdin
" - Bind itself to a TCP port
" - Write the security token + tcp port number to stdout()
" - Take care of forwarding messages received on the TCP port to neovim
function! firenvim#run() abort
        " Write messages to stdout according to the format expected by
        " Firefox's native messaging protocol
        function! WriteStdout(id, data) abort
                " The native messaging protocol expects the message's length
                " to precede the message. It has to use native endianness. We
                " assume big endian.
                let l:len = strlen(a:data)
                let l:lenstr = luaeval('string.char(bit.band(' . l:len . ', 255))'
                                        \. '.. string.char(bit.band(bit.rshift(' . l:len . ', 8), 255))'
                                        \. '.. string.char(bit.band(bit.rshift(' . l:len . ', 16), 255))'
                                        \. '.. string.char(bit.band(bit.rshift(' . l:len . ', 24), 255))')['_VAL']
                call chansend(a:id, [join(l:lenstr) . a:data])
        endfunction
        let s:accumulated_data = ''
        function! OnStdin(id, data, event) abort
                " `:h channel-stdio`: empty a:data means FD closed?
                if a:data == ['']
                        qall!
                end
                if s:firenvim_done
                        return
                endif
                let l:data = s:accumulated_data . a:data[0]
                try
                        let l:params = json_decode(matchstr(l:data[4:], '{[^}]*}'))
                catch
                        let s:accumulated_data = l:data
                        return
                endtry
                let s:firenvim_done = v:true

                let l:package_json = s:build_path([s:script_dir, 'package.json'])
                let l:version = json_decode(join(readfile(l:package_json), "\n"))['version']
                let l:result = { 'version': l:version }

                if exists('g:firenvim_config')
                        let l:result['settings'] = g:firenvim_config
                endif

                if has_key(l:params, 'newInstance') && l:params['newInstance']
                        let l:port = luaeval("require('firenvim').start_server('" .
                                                \ l:params['password'] .
                                                \ "')")
                        let l:result['port'] = l:port
                endif

                call WriteStdout(a:id, json_encode(result))
        endfunction
        let l:chanid = stdioopen({ 'on_stdin': 'OnStdin' })
endfunction

function! s:get_executable_name() abort
        if has('win32')
                return 'firenvim.bat'
        endif
        return 'firenvim'
endfunction

function! s:get_runtime_dir_path() abort
        let l:xdg_runtime_dir = $XDG_RUNTIME_DIR
        if l:xdg_runtime_dir ==# ''
                if has('win32')
                        let l:xdg_runtime_dir = expand('$TEMP')
                        if l:xdg_runtime_dir ==# ''
                                let l:xdg_runtime_dir = expand('$TMP')
                        endif
                        if l:xdg_runtime_dir ==# ''
                                let l:xdg_runtime_dir = expand('$USERPROFILE')
                                if l:xdg_runtime_dir ==# ''
                                        let l:xdg_runtime_dir = fnamemodify(stdpath('data'), ':h')
                                else
                                        let l:xdg_runtime_dir = l:xdg_runtime_dir . '\AppData\Local\Temp'
                                endif
                        endif
                else
                        let l:xdg_runtime_dir = $TMPDIR
                        if l:xdg_runtime_dir ==# ''
                                let l:xdg_runtime_dir = '/tmp/'
                        endif
                endif
        endif
        return s:build_path([l:xdg_runtime_dir, 'firenvim'])
endfunction

function! s:get_data_dir_path() abort
        let l:xdg_data_home = $XDG_DATA_HOME
        if l:xdg_data_home ==# ''
                let l:xdg_data_home = fnamemodify(stdpath('data'), ':h')
        endif
        return s:build_path([l:xdg_data_home, 'firenvim'])
endfunction

function! s:firefox_config_exists() abort
        let l:p = [$HOME, '.mozilla']
        if has('mac')
                let l:p = [$HOME, 'Library', 'Application Support', 'Mozilla']
        elseif has('win32')
                let l:p = [$HOME, 'AppData', 'Roaming', 'Mozilla', 'Firefox']
        end
        return isdirectory(s:build_path(l:p))
endfunction

function! s:get_firefox_manifest_dir_path() abort
        if has('mac')
                return s:build_path([$HOME, 'Library', 'Application Support', 'Mozilla', 'NativeMessagingHosts'])
        elseif has('win32')
                return s:get_data_dir_path()
        end
        return s:build_path([$HOME, '.mozilla', 'native-messaging-hosts'])
endfunction

function! s:brave_config_exists() abort
        let l:p = [$HOME, '.config', 'BraveSoftware']
        if has('mac')
                let l:p = [$HOME, 'Library', 'Application Support', 'BraveSoftware']
        elseif has('win32')
                let l:p = [$HOME, 'AppData', 'Local', 'BraveSoftware']
        end
        return isdirectory(s:build_path(l:p))
endfunction

function! s:opera_config_exists() abort
        let l:p = [$HOME, '.config', 'opera']
        if has('mac')
                let l:p = [$HOME, 'Library', 'Application Support', 'com.operasoftware.Opera']
        elseif has('win32')
                let l:p = [$HOME, 'AppData', 'Local', 'Opera Software']
        end
        return isdirectory(s:build_path(l:p))
endfunction

function! s:vivaldi_config_exists() abort
        let l:p = [$HOME, '.config', 'vivaldi']
        if has('mac')
                let l:p = [$HOME, 'Library', 'Application Support', 'Vivaldi']
        elseif has('win32')
                let l:p = [$HOME, 'AppData', 'Local', 'Vivaldi']
        end
        return isdirectory(s:build_path(l:p))
endfunction

function! s:chrome_config_exists() abort
        let l:p = [$HOME, '.config', 'google-chrome']
        if has('mac')
                let l:p = [$HOME, 'Library', 'Application Support', 'Google', 'Chrome']
        elseif has('win32')
                let l:p = [$HOME, 'AppData', 'Local', 'Google', 'Chrome']
        end
        return isdirectory(s:build_path(l:p))
endfunction

function! s:get_chrome_manifest_dir_path() abort
        if has('mac')
                return s:build_path([$HOME, 'Library', 'Application Support', 'Google', 'Chrome', 'NativeMessagingHosts'])
        elseif has('win32')
                return s:get_data_dir_path()
        end
        return s:build_path([$HOME, '.config', 'google-chrome', 'NativeMessagingHosts'])
endfunction

function! s:get_brave_manifest_dir_path() abort
        if has('mac')
                return s:get_chrome_manifest_dir_path()
        elseif has('win32')
                return s:get_chrome_manifest_dir_path()
        end
        return s:build_path([$HOME, '.config', 'BraveSoftware', 'Brave-Browser', 'NativeMessagingHosts'])
endfunction

function! s:canary_config_exists() abort
        if has('mac')
                let l:p = [$HOME, 'Library', 'Application Support', 'Google', 'Chrome Canary']
        elseif has('win32')
                let l:p = [$HOME, 'AppData', 'Local', 'Google', 'Chrome SxS']
        else
                " Chrome canary doesn't exist on linux
                return v:false
        end
        return isdirectory(s:build_path(l:p))
endfunction

function! s:get_canary_manifest_dir_path() abort
        if has('mac')
                return s:build_path([$HOME, 'Library', 'Application Support', 'Google', 'Chrome Canary', 'NativeMessagingHosts'])
        elseif has('win32')
                return s:get_data_dir_path()
        end
        throw "Chrome Canary doesn't exist on Linux"
endfunction


function! s:chromium_config_exists() abort
        let l:p = [$HOME, '.config', 'chromium']
        if has('mac')
                let l:p = [$HOME, 'Library', 'Application Support', 'Chromium']
        elseif has('win32')
                let l:p = [$HOME, 'AppData', 'Local', 'Chromium']
        end
        return isdirectory(s:build_path(l:p))
endfunction

function! s:get_chromium_manifest_dir_path() abort
        if has('mac')
                return s:build_path([$HOME, 'Library', 'Application Support', 'Chromium', 'NativeMessagingHosts'])
        elseif has('win32')
                return s:get_data_dir_path()
        end
        return s:build_path([$HOME, '.config', 'chromium', 'NativeMessagingHosts'])
endfunction

function! s:get_progpath() abort
        let l:result = v:progpath
        if $APPIMAGE !=# ''
                " v:progpath is different every time you run neovim appimages
                let l:result = $APPIMAGE
        endif
        " Some package managers will install neovim in a version-specific path
        " that v:progpath will point to. This is an issue because the path may
        " break when neovim is updated. Try to detect these cases, work around
        " them if possible and warn the user.
        let l:specific_installs = {
                \ 'homebrew': {
                        \ 'pattern': '^/usr/local/Cellar/',
                        \ 'constant_paths': ['/usr/local/opt/nvim']
                \ },
                \ 'nix': {
                        \ 'pattern': '^/nix/store/',
                        \ 'constant_paths': [
                                \ expand('$HOME/.nix-profile/bin/nvim'),
                                \ '/run/current-system/sw/bin/nvim'
                        \ ]
                \ }
        \ }
        for l:package_manager in keys(l:specific_installs)
                let l:install = l:specific_installs[l:package_manager]
                if match(l:result, l:install['pattern']) == 0
                        let l:warning = 'Warning: ' . l:package_manager . ' path detected. '
                        let l:alternative_found = v:false
                        for l:constant_path in l:install['constant_paths']
                                if executable(l:constant_path)
                                        let l:warning = l:warning .
                                                \ "Using '" . l:constant_path . "'" .
                                                \ "' instead of '" . l:result . "'"
                                        let l:result = l:constant_path
                                        let l:alternative_found = v:true
                                        break
                                endif
                        endfor
                        if !l:alternative_found
                                let l:warning = l:warning .
                                        \ 'Firenvim may break next time you update neovim.'
                        endif
                        echo l:warning
                endif
        endfor
        return l:result
endfunction

function! s:get_executable_content(data_dir, prolog) abort
        if has('win32')
                return  "@echo off\r\n" .
                                        \ "mkdir \"" . a:data_dir . "\" 2>nul\r\n" .
                                        \ "cd \"" . a:data_dir . "\"\r\n" .
                                        \ a:prolog . "\r\n" .
                                        \ "\"" . s:get_progpath() . "\" --headless --cmd \"let g:started_by_firenvim = v:true\" -c \"call firenvim#run()\"\r\n"
        endif
        return "#!/bin/sh\n" .
                                \ 'mkdir -p ' . a:data_dir . "\n" .
                                \ 'chmod 700 ' . a:data_dir . "\n" .
                                \ 'cd ' . a:data_dir . "\n" .
                                \ "unset NVIM_LISTEN_ADDRESS\n" .
                                \ 'export PATH="$PATH:' . $PATH . "\"\n" .
                                \ a:prolog . "\n" .
                                \ "exec '" . s:get_progpath() . "' --headless --cmd 'let g:started_by_firenvim = v:true' -c 'call firenvim#run()'\n"
endfunction

function! s:get_manifest_beginning(execute_nvim_path) abort
        return '{
                                \ "name": "firenvim",
                                \ "description": "Turn Firefox into a Neovim client.",
                                \ "path": "' . substitute(a:execute_nvim_path, '\', '\\\\', 'g') . '",
                                \ "type": "stdio",
                                \'
endfunction

function! s:get_chrome_manifest(execute_nvim_path) abort
        return s:get_manifest_beginning(a:execute_nvim_path) .
                                \' "allowed_origins": [
                                \ "chrome-extension://egpjdkipkomnmjhjmdamaniclmdlobbo/"
                                \ ]
                                \}'
endfunction

function! s:get_firefox_manifest(execute_nvim_path) abort
        return s:get_manifest_beginning(a:execute_nvim_path) .
                                \' "allowed_extensions": ["firenvim@lacamb.re"]
                                \}'
endfunction

function! s:key_to_ps1_str(key, manifest_path) abort
        let l:ps1_content = ''
        let l:key_arr = split(a:key, '\')
        let l:i = 0
        for l:i in range(2, len(l:key_arr) - 1)
                let l:ps1_content = l:ps1_content . "\nNew-Item -Path \"" . join(key_arr[0:i], '\') . '"'
        endfor
        " Then, assign a value to it
        return l:ps1_content . "\nSet-Item -Path \"" .
                                \ a:key .
                                \ '\" -Value "' . a:manifest_path . '"'
endfunction

function! s:get_browser_configuration() abort
        " Brave, Opera and Vivaldi all rely on Chrome's native messenger
        let l:browsers = {
                \'brave': {
                        \ 'has_config': s:brave_config_exists(),
                        \ 'manifest_content': function('s:get_chrome_manifest'),
                        \ 'manifest_dir_path': function('s:get_brave_manifest_dir_path'),
                        \ 'registry_key': 'HKCU:\Software\Google\Chrome\NativeMessagingHosts\firenvim',
                \},
                \'chrome': {
                        \ 'has_config': s:chrome_config_exists(),
                        \ 'manifest_content': function('s:get_chrome_manifest'),
                        \ 'manifest_dir_path': function('s:get_chrome_manifest_dir_path'),
                        \ 'registry_key': 'HKCU:\Software\Google\Chrome\NativeMessagingHosts\firenvim',
                \},
                \'chrome-canary': {
                        \ 'has_config': s:canary_config_exists(),
                        \ 'manifest_content': function('s:get_chrome_manifest'),
                        \ 'manifest_dir_path': function('s:get_canary_manifest_dir_path'),
                        \ 'registry_key': 'HKCU:\Software\Google\Chrome\NativeMessagingHosts\firenvim',
                \},
                \'chromium': {
                        \ 'has_config': s:chromium_config_exists(),
                        \ 'manifest_content': function('s:get_chrome_manifest'),
                        \ 'manifest_dir_path': function('s:get_chromium_manifest_dir_path'),
                        \ 'registry_key': 'HKCU:\Software\Chromium\NativeMessagingHosts\firenvim',
                \},
                \'firefox': {
                        \ 'has_config': s:firefox_config_exists(),
                        \ 'manifest_content': function('s:get_firefox_manifest'),
                        \ 'manifest_dir_path': function('s:get_firefox_manifest_dir_path'),
                        \ 'registry_key': 'HKCU:\Software\Mozilla\NativeMessagingHosts\firenvim',
                \},
                \'opera': {
                        \ 'has_config': s:opera_config_exists(),
                        \ 'manifest_content': function('s:get_chrome_manifest'),
                        \ 'manifest_dir_path': function('s:get_chrome_manifest_dir_path'),
                        \ 'registry_key': 'HKCU:\Software\Google\Chrome\NativeMessagingHosts\firenvim',
                \},
                \'vivaldi': {
                        \ 'has_config': s:vivaldi_config_exists(),
                        \ 'manifest_content': function('s:get_chrome_manifest'),
                        \ 'manifest_dir_path': function('s:get_chrome_manifest_dir_path'),
                        \ 'registry_key': 'HKCU:\Software\Google\Chrome\NativeMessagingHosts\firenvim',
                \}
        \}
        if $TESTING == 1
                call remove(l:browsers, 'brave')
                call remove(l:browsers, 'vivaldi')
                call remove(l:browsers, 'opera')
        endif
        return l:browsers
endfunction

" Installing firenvim requires several steps:
" - Create a batch/shell script that takes care of starting neovim with the
"   right arguments. This is needed because the webextension api doesn't let
"   users specify what arguments programs should be started with
" - Create a manifest file that lets the browser know where the script created
"   can be found
" - On windows, also create a registry key that points to the native manifest
"
" Manifest paths & registry stuff are specified here: 
" https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_manifests#Manifest_location
"
" firenvim#install accepts the following optional arguments:
" a:1: 0 to let firenvim detect what browsers the user wants to use or 1 to
"      force install for every browser.
" a:2: A prologue that should be inserted in the shell/batch script and
"      executed before neovim is ran.
function! firenvim#install(...) abort
        if !has('nvim-0.4.0')
                echoerr 'Error: nvim version >= 0.4.0 required. Aborting.'
                return
        endif

        let l:force_install = 0
        let l:script_prolog = ''
        if a:0 > 0
                let l:force_install = a:1
                if a:0 > 1
                        let l:script_prolog = a:2
                endif
        endif

        " Decide where the script responsible for starting neovim should be
        let l:data_dir = s:get_data_dir_path()
        let l:execute_nvim_path = s:build_path([l:data_dir, s:get_executable_name()])
        " Write said script to said path
        let l:execute_nvim = s:get_executable_content(s:get_runtime_dir_path(), l:script_prolog)

        call mkdir(l:data_dir, 'p', 0700)
        call writefile(split(l:execute_nvim, "\n"), l:execute_nvim_path)
        call setfperm(l:execute_nvim_path, 'rwx------')

        let l:browsers = s:get_browser_configuration()

        let l:powershell_script = ''
        for l:name in keys(l:browsers)
                let l:cur_browser = l:browsers[l:name]
                if !l:cur_browser['has_config'] && !l:force_install
                        echo 'No config detected for ' . l:name . '. Skipping.'
                        continue
                endif

                try
                        let l:manifest_content = l:cur_browser['manifest_content'](l:execute_nvim_path)
                        let l:manifest_dir_path = l:cur_browser['manifest_dir_path']()
                        let l:manifest_path = s:build_path([l:manifest_dir_path, 'firenvim.json'])
                catch /.*/
                        echo 'Aborting installation for ' . l:name . '. ' . v:exception
                        continue
                endtry
                
                if has('win32')
                        let l:manifest_path = s:build_path([l:manifest_dir_path, 'firenvim-' . l:name . '.json'])
                endif

                call mkdir(l:manifest_dir_path, 'p', 0700)
                call writefile([l:manifest_content], l:manifest_path)
                call setfperm(l:manifest_path, 'rw-------')

                echo 'Installed native manifest for ' . l:name . '.'

                if has('win32')
                        " On windows, also create a registry key. We do this
                        " by writing a powershell script to a file and
                        " executing it.
                        let l:ps1_content = s:key_to_ps1_str(l:cur_browser['registry_key'],
                                                \ l:manifest_path)
                        let l:ps1_path = s:build_path([l:manifest_dir_path, l:name . '.ps1'])
                        echo 'Creating registry key for ' . l:name . '. This may take a while. Script: ' . l:ps1_path
                        call writefile(split(l:ps1_content, "\n"), l:ps1_path)
                        call setfperm(l:ps1_path, 'rwx------')
                        let o = system(['powershell', '-Command', '-'], readfile(l:ps1_path))
                        if v:shell_error
                          echo o
                        endif

                        echo 'Created registry key for ' . l:name . '.'
                endif
        endfor
endfunction

" Removes files created by Firenvim during its installation process
function! firenvim#uninstall() abort

        let l:data_dir = s:get_data_dir_path()
        call delete(l:data_dir, 'rf')
        echo 'Removed firenvim data directory.'

        let l:browsers = s:get_browser_configuration()

        for l:name in keys(l:browsers)
                let l:cur_browser = l:browsers[l:name]
                if !l:cur_browser['has_config']
                        continue
                endif

                let l:manifest_dir_path = l:cur_browser['manifest_dir_path']()
                let l:manifest_path = s:build_path([l:manifest_dir_path, 'firenvim.json'])

                if has('win32')
                        let l:manifest_path = s:build_path([l:manifest_dir_path, 'firenvim-' . l:name . '.json'])
                endif

                if has('win32')
                        echo 'Removing registry key for ' . l:name . '. This may take a while.'
                        let l:ps1_content = 'Remove-Item -Path "' . l:cur_browser['registry_key'] . '" -Recurse'
                        let o = system(['powershell', '-Command', '-'], [l:ps1_content])
                        if v:shell_error
                          echo o
                        endif
                        echo 'Removed registry key for ' . l:name . '.'
                endif

                call delete(l:manifest_path)
                echo 'Removed native manifest for ' . l:name . '.'
        endfor
endfunction

function! firenvim#onUIEnter(event) abort
        let l:ui = nvim_get_chan_info(a:event.chan)
        if has_key(l:ui, 'client') && has_key(l:ui.client, 'name') &&
                                \ l:ui.client.name =~? 'Firenvim'
                call map(nvim_list_bufs(), {key, val -> firenvimft#detect(val)})
                augroup FirenvimFtdetectAugroup
                        autocmd!
                        autocmd BufRead,BufNewFile *.txt call firenvimft#detect(str2nr(expand('<abuf>')))
                augroup END
        endif
endfunction