diff --git a/src/builtins/read.cpp b/src/builtins/read.cpp index 918c0d3c3..179bcc7f7 100644 --- a/src/builtins/read.cpp +++ b/src/builtins/read.cpp @@ -224,6 +224,7 @@ static int read_interactive(parser_t &parser, wcstring &buff, int nchars, bool s conf.left_prompt_cmd = prompt; conf.right_prompt_cmd = right_prompt; + conf.event = L"fish_read"; conf.in = in; @@ -233,7 +234,6 @@ static int read_interactive(parser_t &parser, wcstring &buff, int nchars, bool s commandline_set_buffer(commandline, std::wcslen(commandline)); scoped_push interactive{&parser.libdata().is_interactive, true}; - event_fire_generic(parser, L"fish_read"); auto mline = reader_readline(nchars); interactive.restore(); if (mline) { diff --git a/src/reader.cpp b/src/reader.cpp index eb612815f..f205bbd66 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -2799,6 +2799,7 @@ static int read_i(parser_t &parser) { conf.syntax_check_ok = true; conf.autosuggest_ok = check_autosuggestion_enabled(parser.vars()); conf.expand_abbrev_ok = true; + conf.event = L"fish_prompt"; if (parser.is_breakpoint() && function_exists(DEBUG_PROMPT_FUNCTION_NAME, parser)) { conf.left_prompt_cmd = DEBUG_PROMPT_FUNCTION_NAME; @@ -4009,7 +4010,9 @@ maybe_t reader_data_t::readline(int nchars_or_0) { } first_prompt = false; - event_fire_generic(parser(), L"fish_prompt"); + if (!conf.event.empty()) { + event_fire_generic(parser(), conf.event.c_str()); + } exec_prompt(); /// A helper that kicks off syntax highlighting, autosuggestion computing, and repaints. diff --git a/src/reader.h b/src/reader.h index 1cef1c71b..0a9bde67f 100644 --- a/src/reader.h +++ b/src/reader.h @@ -196,6 +196,9 @@ struct reader_config_t { /// Right prompt command, typically fish_right_prompt. wcstring right_prompt_cmd{}; + /// Name of the event to trigger once we're set up. + wcstring event{}; + /// Whether tab completion is OK. bool complete_ok{false}; diff --git a/tests/pexpects/read.py b/tests/pexpects/read.py index 929f7a796..a539d1251 100644 --- a/tests/pexpects/read.py +++ b/tests/pexpects/read.py @@ -144,3 +144,15 @@ expect_read_prompt() send("jkl\n") expect_str("ghi then jkl\r\n") expect_prompt() + +# Long line so we don't have to count prompts +sendline("""set -g fish_prompt_fired 0; function dontfire --on-event fish_prompt; set -g fish_prompt_fired (math $fish_prompt_fired + 1); end; function dofire --on-event fish_read; set -g fish_read_fired 1; end""") + +expect_prompt() +sendline("read foo") +expect_read_prompt() +sendline("text") +expect_prompt() +# Once for right after setting the listener, another for after the read. +print_var_contents("fish_prompt_fired", "2") +print_var_contents("fish_read_fired", "1")