Skip to content

Commit

Permalink
Add firenvim#write() function enabling users to write arbitrary data
Browse files Browse the repository at this point in the history
The goal of this commit is to enable the use case desired by #1589. This
isn't a perfect fit as the user was asking for a way to setup
post-processing of their text instead of having to call firenvim#write,
but as I am on a tight schedule for publishing a new release I do not
have time for further discussions, and following the pattern of
firenvim#hide_frame, firenvim#eval_js, firenvim#focus_* feels like a
very safe choice.
  • Loading branch information
glacambre committed Apr 28, 2024
1 parent b7bab09 commit 8480e4a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
19 changes: 19 additions & 0 deletions autoload/firenvim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ 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: string to write
" 2nd arg: position of the cursor
function! firenvim#write(...) abort
let l:text = ''
let l:cursor = [0, 0]
if len(a:000) == 0
let l:text = nvim_buf_get_lines(0, 0, -1, 0)
let l:cursor = nvim_win_get_cursor(0)
elseif len(a:000) == 2
let l:text = a:0
let l:cursor = a:1
else
throw 'firenvim#write should be called either with 0 arguments or with 2 arguments (string, [number, number])'
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
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

0 comments on commit 8480e4a

Please sign in to comment.