events: add "Signal" event #9564

..which gets triggered when SIGUSR1 is sent to the nvim process.

Closes #9562
This commit is contained in:
Marco Hinz
2019-02-04 02:39:05 +01:00
committed by Justin M. Keyes
parent da88278f27
commit 70f6939fd4
6 changed files with 70 additions and 1 deletions

View File

@ -353,6 +353,8 @@ Name triggered by ~
|CompleteDone| after Insert mode completion is done
|User| to be used in combination with ":doautocmd"
|Signal| after the nvim process received a signal
The alphabetical list of autocommand events: *autocmd-events-abc*
@ -914,6 +916,10 @@ ShellCmdPost After executing a shell command with |:!cmd|,
any changed files.
For non-blocking shell commands, see
|job-control|.
*Signal*
Signal After the nvim process received a signal.
The pattern is matched against the name of the
received signal. Only "SIGUSR1" is supported.
*ShellFilterPost*
ShellFilterPost After executing a shell command with
":{range}!cmd", ":w !cmd" or ":r !cmd".

View File

@ -142,6 +142,7 @@ Commands:
Events:
|DirChanged|
|Signal|
|TabNewEntered|
|TermClose|
|TermOpen|

View File

@ -74,6 +74,7 @@ return {
'SessionLoadPost', -- after loading a session file
'ShellCmdPost', -- after ":!cmd"
'ShellFilterPost', -- after ":1,2!cmd", ":w !cmd", ":r !cmd".
'Signal', -- after nvim process received a signal
'SourceCmd', -- sourcing a Vim script using command
'SourcePre', -- before sourcing a Vim script
'SpellFileMissing', -- spell file missing
@ -115,6 +116,7 @@ return {
-- syntax file
nvim_specific = {
DirChanged=true,
Signal=true,
TabClosed=true,
TabNew=true,
TabNewEntered=true,

View File

@ -6917,6 +6917,7 @@ static bool apply_autocmds_group(event_T event, char_u *fname, char_u *fname_io,
|| event == EVENT_REMOTEREPLY
|| event == EVENT_SPELLFILEMISSING
|| event == EVENT_SYNTAX
|| event == EVENT_SIGNAL
|| event == EVENT_TABCLOSED) {
fname = vim_strsave(fname);
} else {

View File

@ -15,6 +15,7 @@
#include "nvim/globals.h"
#include "nvim/memline.h"
#include "nvim/eval.h"
#include "nvim/fileio.h"
#include "nvim/main.h"
#include "nvim/memory.h"
#include "nvim/misc1.h"
@ -22,7 +23,7 @@
#include "nvim/os/signal.h"
#include "nvim/event/loop.h"
static SignalWatcher spipe, shup, squit, sterm;
static SignalWatcher spipe, shup, squit, sterm, susr1;
#ifdef SIGPWR
static SignalWatcher spwr;
#endif
@ -61,6 +62,10 @@ void signal_init(void)
signal_watcher_init(&main_loop, &spwr, NULL);
signal_watcher_start(&spwr, on_signal, SIGPWR);
#endif
#ifdef SIGUSR1
signal_watcher_init(&main_loop, &susr1, NULL);
signal_watcher_start(&susr1, on_signal, SIGUSR1);
#endif
}
void signal_teardown(void)
@ -73,6 +78,9 @@ void signal_teardown(void)
#ifdef SIGPWR
signal_watcher_close(&spwr, NULL);
#endif
#ifdef SIGUSR1
signal_watcher_close(&susr1, NULL);
#endif
}
void signal_stop(void)
@ -84,6 +92,9 @@ void signal_stop(void)
#ifdef SIGPWR
signal_watcher_stop(&spwr);
#endif
#ifdef SIGUSR1
signal_watcher_stop(&susr1);
#endif
}
void signal_reject_deadly(void)
@ -115,6 +126,10 @@ static char * signal_name(int signum)
#endif
case SIGHUP:
return "SIGHUP";
#ifdef SIGUSR1
case SIGUSR1:
return "SIGUSR1";
#endif
default:
return "Unknown";
}
@ -162,6 +177,12 @@ static void on_signal(SignalWatcher *handle, int signum, void *data)
deadly_signal(signum);
}
break;
#ifdef SIGUSR1
case SIGUSR1:
apply_autocmds(EVENT_SIGNAL, (char_u *)"SIGUSR1", curbuf->b_fname, true,
curbuf);
break;
#endif
default:
ELOG("invalid signal: %d", signum);
break;

View File

@ -0,0 +1,38 @@
local helpers = require('test.functional.helpers')(after_each)
local clear = helpers.clear
local command = helpers.command
local eq = helpers.eq
local funcs = helpers.funcs
local next_msg = helpers.next_msg
if helpers.pending_win32(pending) then
-- Only applies to POSIX systems.
return
end
local function posix_kill(signame, pid)
os.execute('kill -s '..signame..' -- '..pid..' >/dev/null')
end
describe('autocmd Signal', function()
before_each(clear)
it('matches *', function()
command('autocmd Signal * call rpcnotify(1, "foo")')
posix_kill('USR1', funcs.getpid())
eq({'notification', 'foo', {}}, next_msg())
end)
it('matches SIGUSR1', function()
command('autocmd Signal SIGUSR1 call rpcnotify(1, "foo")')
posix_kill('USR1', funcs.getpid())
eq({'notification', 'foo', {}}, next_msg())
end)
it('does not match unknown patterns', function()
command('autocmd Signal SIGUSR2 call rpcnotify(1, "foo")')
posix_kill('USR1', funcs.getpid())
eq(nil, next_msg(500))
end)
end)