Deprecate $_ in favor of (status current-command)

Closes #813.
This commit is contained in:
Mahmoud Al-Qudsi 2018-03-24 11:58:19 -05:00
parent 6d80ab8d74
commit 515fc509ec
6 changed files with 21 additions and 8 deletions

View file

@ -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).

View file

@ -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

View file

@ -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);

View file

@ -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_cmd_t> 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;

View file

@ -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);
}

View file

@ -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();