From 67a7fa2c13d6cee5596a6587cf7caef4ce129815 Mon Sep 17 00:00:00 2001 From: glacambre Date: Sun, 28 Apr 2024 15:37:24 +0200 Subject: [PATCH] Add firenvim#write() function enabling users to write arbitrary data 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. --- autoload/firenvim.vim | 29 +++++++++++++++++++++++++++++ src/frame.ts | 9 +-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/autoload/firenvim.vim b/autoload/firenvim.vim index f70e1d97..4283ab68 100644 --- a/autoload/firenvim.vim +++ b/autoload/firenvim.vim @@ -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 diff --git a/src/frame.ts b/src/frame.ts index 80a10c67..c35db697 100644 --- a/src/frame.ts +++ b/src/frame.ts @@ -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", {