Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable arbitrary writes #1606

Merged
merged 2 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion autoload/firenvim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ function! firenvim#hide_frame() abort
call rpcnotify(firenvim#get_chan(), 'firenvim_hide_frame')
endfunction

" Asks the browser extension to write to the text area. Either two or no
" arguments.
" 1st arg: list of strings to write, one string per line, empty strings for
" empty lines
" 2nd arg: position of the cursor
function! firenvim#write(...) abort
let l:text = ''
let l:cursor = [0, 0]
if a:0 == 0
let l:text = nvim_buf_get_lines(0, 0, -1, 0)
let l:cursor = nvim_win_get_cursor(0)
elseif a:0 == 2
let l:text = a:1
let l:cursor = a:2
if type(l:text) != v:t_list || (len(l:text) > 0 && type(l:text[0]) != v:t_string)
throw "firenvim#write's first argument must be a list of strings"
endif
if type(l:text) != v:t_list
\ || len(l:cursor) != 2
\ || type(l:cursor[0]) != v:t_number
\ || type(l:cursor[1]) != v:t_number
throw "firenvim#write's second argument must be a list of two numbers"
endif
else
throw 'firenvim#write should be called either with 0 or 2 arguments'
endif
call rpcnotify(firenvim#get_chan(), 'firenvim_bufwrite', {'text': l:text, 'cursor': l:cursor})
endfunction

" Asks the browser extension to send one or multiple key events to the
" underlying input field.
function! firenvim#press_keys(...) abort
Expand Down Expand Up @@ -201,7 +230,18 @@ function! firenvim#run() abort
let l:result['port'] = l:port
endif

call WriteStdout(a:id, json_encode(result))
let l:response = ''
try
let l:response = json_encode(result)
catch /E474/
call remove(result, 'settings')
if !has_key(result, 'messages')
let result['messages'] = []
endif
call add(result['messages'], 'Error serializing settings:' . v:exception)
let l:response = json_encode(result)
endtry
call WriteStdout(a:id, l:response)
endfunction
if exists('g:firenvim_c')
for data in g:firenvim_i
Expand Down
9 changes: 1 addition & 8 deletions src/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,7 @@ export const isReady = browser
group = group,
pattern = filename,
callback = function(ev)
vim.fn.rpcnotify(
channel,
"firenvim_bufwrite",
{
text = vim.api.nvim_buf_get_lines(0, 0, -1, 0),
cursor = vim.api.nvim_win_get_cursor(0)
}
)
vim.fn["firenvim#write"]()
end
})
vim.api.nvim_create_autocmd("VimLeave", {
Expand Down
Loading