diff --git a/CHANGELOG.md b/CHANGELOG.md index d2ac6f694..9a7b89471 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ This section is for changes merged to the `major` branch that are not also merge ## Deprecations - The `IFS` variable is deprecated and will be removed in fish 4.0 (#4156). - The `function --on-process-exit` event will be removed in future (#4700). Use the `fish_exit` event instead. +- `$_` is deprecated and will removed in the future (#813). Use `status current-command` in a subshell instead. ## Notable non-backward compatible changes - `.` command no longer exists -- use `source` (#4294). diff --git a/doc_src/index.hdr.in b/doc_src/index.hdr.in index df1d01288..4ebb350c2 100644 --- a/doc_src/index.hdr.in +++ b/doc_src/index.hdr.in @@ -1297,14 +1297,14 @@ To customize the syntax highlighting, you can set the environment variables list \subsection title Programmable title -When using most virtual terminals, it is possible to set the message displayed in the titlebar of the terminal window. This can be done automatically in fish by defining the `fish_title` function. The `fish_title` function is executed before and after a new command is executed or put into the foreground and the output is used as a titlebar message. The $_ environment variable will always contain the name of the job to be put into the foreground (Or 'fish' if control is returning to the shell) when the `fish_prompt` function is called. The first argument to fish_title will contain the most recently executed foreground command as a string, starting with fish 2.2. +When using most virtual terminals, it is possible to set the message displayed in the titlebar of the terminal window. This can be done automatically in fish by defining the `fish_title` function. The `fish_title` function is executed before and after a new command is executed or put into the foreground and the output is used as a titlebar message. The `status current-command` builtin will always return the name of the job to be put into the foreground (or 'fish' if control is returning to the shell) when the `fish_prompt` function is called. The first argument to fish_title will contain the most recently executed foreground command as a string, starting with fish 2.2. Examples: The default `fish` title is \fish function fish_title - echo $_ ' ' + echo (status current-command) ' ' pwd end \endfish diff --git a/src/builtin_fg.cpp b/src/builtin_fg.cpp index df9ce328e..2c838b949 100644 --- a/src/builtin_fg.cpp +++ b/src/builtin_fg.cpp @@ -102,7 +102,8 @@ int builtin_fg(parser_t &parser, io_streams_t &streams, wchar_t **argv) { } const wcstring ft = tok_first(j->command()); - if (!ft.empty()) env_set_one(L"current_cmd", ENV_EXPORT, ft); + //For compatibility with fish 2.0's $_, now replaced with `status current-command` + if (!ft.empty()) env_set_one(L"_", ENV_EXPORT, ft); reader_write_title(j->command()); job_promote(j); diff --git a/src/builtin_status.cpp b/src/builtin_status.cpp index 4b1bea72f..c498dd3bd 100644 --- a/src/builtin_status.cpp +++ b/src/builtin_status.cpp @@ -25,6 +25,7 @@ enum status_cmd_t { STATUS_IS_FULL_JOB_CTRL, STATUS_IS_INTERACTIVE_JOB_CTRL, STATUS_IS_NO_JOB_CTRL, + STATUS_CURRENT_CMD, STATUS_FILENAME, STATUS_FUNCTION, STATUS_LINE_NUMBER, @@ -35,6 +36,7 @@ enum status_cmd_t { // Must be sorted by string, not enum or random. const enum_map status_enum_map[] = { + {STATUS_CURRENT_CMD, L"current-command"}, {STATUS_FILENAME, L"current-filename"}, {STATUS_FUNCTION, L"current-function"}, {STATUS_LINE_NUMBER, L"current-line-number"}, @@ -374,6 +376,12 @@ int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **argv) { streams.out.append(parser.stack_trace()); break; } + case STATUS_CURRENT_CMD: { + CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd) + streams.out.append(program_name); + streams.out.push_back(L'\n'); + break; + } } return retval; diff --git a/src/env.cpp b/src/env.cpp index 2783b2eb0..568d392af 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -322,7 +322,7 @@ bool string_set_contains(const T &set, const wchar_t *val) { /// Check if a variable may not be set using the set command. static bool is_read_only(const wchar_t *val) { - const string_set_t env_read_only = {L"PWD", L"SHLVL", L"history", L"status", L"version", L"fish_pid", L"hostname", L"current_cmd"}; + const string_set_t env_read_only = {L"PWD", L"SHLVL", L"history", L"status", L"version", L"fish_pid", L"hostname", L"_"}; return string_set_contains(env_read_only, val); } diff --git a/src/reader.cpp b/src/reader.cpp index 843530d1b..29cfe81c8 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -99,7 +99,7 @@ #define MODE_PROMPT_FUNCTION_NAME L"fish_mode_prompt" /// The default title for the reader. This is used by reader_readline. -#define DEFAULT_TITLE L"echo $_ \" \"; __fish_pwd" +#define DEFAULT_TITLE L"echo (status current-command) \" \"; __fish_pwd" /// The maximum number of characters to read from the keyboard without repainting. Note that this /// readahead will only occur if new characters are available for reading, fish will never block for @@ -1620,7 +1620,8 @@ static void reader_interactive_init() { invalidate_termsize(); - env_set_one(L"current_cmd", ENV_GLOBAL, L"fish"); + //For compatibility with fish 2.0's $_, now replaced with `status current-command` + env_set_one(L"_", ENV_GLOBAL, L"fish"); } /// Destroy data for interactive use. @@ -1897,7 +1898,8 @@ void reader_run_command(parser_t &parser, const wcstring &cmd) { wcstring ft = tok_first(cmd); - if (!ft.empty()) env_set_one(L"current_cmd", ENV_GLOBAL, ft); + //For compatibility with fish 2.0's $_, now replaced with `status current-command` + if (!ft.empty()) env_set_one(L"_", ENV_GLOBAL, ft); reader_write_title(cmd); @@ -1913,7 +1915,8 @@ void reader_run_command(parser_t &parser, const wcstring &cmd) { term_steal(); - env_set_one(L"current_cmd", ENV_GLOBAL, program_name); + //For compatibility with fish 2.0's $_, now replaced with `status current-command` + env_set_one(L"_", ENV_GLOBAL, program_name); #ifdef HAVE__PROC_SELF_STAT proc_update_jiffies();