diff options
author | Ashkan Kiani <ashkan.k.kiani@gmail.com> | 2019-10-22 23:59:31 -0700 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2019-11-05 21:50:48 -0800 |
commit | c66297452c523ce599aad79aeea2f510094c93d4 (patch) | |
tree | 3965794dbc103aae540b387295035d3492380222 /runtime/doc/if_lua.txt | |
parent | 3e21d498362f8cfff7ee467be6402fb00a53eeb5 (diff) | |
download | rneovim-c66297452c523ce599aad79aeea2f510094c93d4.tar.gz rneovim-c66297452c523ce599aad79aeea2f510094c93d4.tar.bz2 rneovim-c66297452c523ce599aad79aeea2f510094c93d4.zip |
doc: file-change-detect [ci skip]
Diffstat (limited to 'runtime/doc/if_lua.txt')
-rw-r--r-- | runtime/doc/if_lua.txt | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/runtime/doc/if_lua.txt b/runtime/doc/if_lua.txt index d527a91a93..fd2630d550 100644 --- a/runtime/doc/if_lua.txt +++ b/runtime/doc/if_lua.txt @@ -416,20 +416,46 @@ Example: repeating timer print('sleeping'); +Example: File-change detection *file-change-detect* + 1. Save this code to a file. + 2. Execute it with ":luafile %". + 3. Use ":Watch %" to watch any file. + 4. Try editing the file from another text editor. + 5. Observe that the file reloads in Nvim (because on_change() calls + |:checktime|). > + + local w = vim.loop.new_fs_event() + local function on_change(err, fname, status) + -- Do work... + vim.api.nvim_command('checktime') + -- Debounce: stop/start. + w:stop() + watch_file(fname) + end + function watch_file(fname) + local fullpath = vim.api.nvim_call_function( + 'fnamemodify', {fname, ':p'}) + w:start(fullpath, {}, vim.schedule_wrap(function(...) + on_change(...) end)) + end + vim.api.nvim_command( + "command! -nargs=1 Watch call luaeval('watch_file(_A)', expand('<args>'))") + + Example: TCP echo-server *tcp-server* 1. Save this code to a file. 2. Execute it with ":luafile %". 3. Note the port number. 4. Connect from any TCP client (e.g. "nc 0.0.0.0 36795"): > - local function create_server(host, port, on_connection) + local function create_server(host, port, on_connect) local server = vim.loop.new_tcp() server:bind(host, port) server:listen(128, function(err) assert(not err, err) -- Check for errors. local sock = vim.loop.new_tcp() server:accept(sock) -- Accept client connection. - on_connection(sock) -- Start reading messages. + on_connect(sock) -- Start reading messages. end) return server end |