fish-shell/doc_src/cmds
Fabian Homborg 733114fefb
Add set --function (#8145)
* Add `set --function`

This makes the function's scope available, even inside of blocks. Outside of blocks it's the toplevel local scope.

This removes the need to declare variables locally before use, and will probably end up being the main way variables get set.

E.g.:

```fish
set -l thing
if condition
    set thing one
else
    set thing two
end
```

could be written as

```fish
if condition
    set -f thing one
else
    set -f thing two
end
```

Note: Many scripts shipped with fish use workarounds like `and`/`or`
instead of `if`, so it isn't easy to find good examples.

Also, if there isn't an else-branch in that above, just with

```fish
if condition
    set -f thing one
end
```

that means something different from setting it before! Now, if
`condition` isn't true, it would use a global (or universal) variable of
te same name!

Some more interesting parts:

Because it *is* a local scope, setting a variable `-f` and
`-l` in the toplevel of a function ends up the same:

```fish
function foo2
    set -l foo bar
    set -f foo baz # modifies the *same* variable!
end
```

but setting it locally inside a block creates a new local variable
that shadows the function-scoped variable:

```fish
function foo3
    set -f foo bar
    begin
        set -l foo banana
        # $foo is banana
    end
    # $foo is bar again
end
```

This is how local variables already work. "Local" is actually "block-scoped".

Also `set --show` will only show the closest local scope, so it won't
show a shadowed function-level variable. Again, this is how local
variables already work, and could be done as a separate change.

As a fun tidbit, functions with --no-scope-shadowing can now use this to set variables in the calling function. That's probably okay given that it's already an escape hatch (but to be clear: if it turns out to problematic I reserve the right to remove it).

Fixes #565
2021-08-01 20:08:12 +02:00
..
_.rst Add an "_" builtin to call into gettext (#7036) 2020-05-29 20:53:44 +02:00
abbr.rst Update docs for multi-erase in abbr and set 2020-10-04 12:39:32 +02:00
alias.rst docs: Links for the function-related commands 2021-05-08 12:12:56 +02:00
and.rst doc: add interlinks between true ←→ false and and ←→ or cmds 2020-02-23 23:41:16 -08:00
argparse.rst docs: Replace all internal links with :ref:s 2021-03-26 19:32:14 +01:00
begin.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
bg.rst docs: note job expansion in bg/fg/jobs arguments 2021-02-28 20:56:23 +08:00
bind.rst Provide functions to toggle commandline prefix/suffix 2021-06-23 20:51:20 +02:00
block.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
break.rst doc: add interlinks between break and continue commands 2020-03-09 19:24:38 +01:00
breakpoint.rst doc: homogenize commands titles 2020-04-04 10:44:53 +02:00
builtin.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
case.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
cd.rst docs: Reword cd a bit 2021-07-23 18:00:57 +02:00
cdh.rst doc: add section about directory history / stack 2020-04-18 10:40:48 +02:00
command.rst Let command, jobs and type take --query instead of --quiet 2020-09-04 16:55:09 +02:00
commandline.rst commandline: allow to get/set cursor position relative to token/process/job 2021-06-23 20:51:20 +02:00
complete.rst docs: Document how complete groups options 2021-07-23 19:29:16 +02:00
contains.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
continue.rst doc: add interlinks between break and continue commands 2020-03-09 19:24:38 +01:00
count.rst docs: Make more code lines shorter 2021-02-02 08:35:38 +01:00
dirh.rst doc: add section about directory history / stack 2020-04-18 10:40:48 +02:00
dirs.rst doc: add section about directory history / stack 2020-04-18 10:40:48 +02:00
disown.rst docs: Link builtins 2020-03-21 15:31:25 +01:00
echo.rst docs: Use \ instead of \\ in examples (#7286) 2020-08-26 18:29:03 +02:00
else.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
emit.rst doc: homogenize commands titles 2020-04-04 10:44:53 +02:00
end.rst doc: homogenize commands titles 2020-04-04 10:44:53 +02:00
eval.rst doc: add links to 'source' command from 'eval' and 'functions' 2020-03-21 13:21:38 +01:00
exec.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
exit.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
false.rst doc: add interlinks between true ←→ false and and ←→ or cmds 2020-02-23 23:41:16 -08:00
fg.rst docs: note job expansion in bg/fg/jobs arguments 2021-02-28 20:56:23 +08:00
fish.rst document --no-config 2021-07-27 23:00:23 +02:00
fish_add_path.rst docs: Fix markup for code blocks 2020-10-10 21:49:33 +02:00
fish_breakpoint_prompt.rst docs: Less "the user", more "you" 2020-09-18 16:53:59 +02:00
fish_command_not_found.rst Call "fish_command_not_found" if a command wasn't found 2020-09-06 11:15:54 +02:00
fish_config.rst fish_config: Add CLI-based theme selector 2021-07-14 18:56:19 +02:00
fish_git_prompt.rst docs: use monospace for inline code snippets more consistently 2020-10-26 19:25:41 +01:00
fish_greeting.rst docs: Add fish_title and fish_greeting 2020-10-28 17:44:45 +01:00
fish_hg_prompt.rst Fixed sentence in fish_hg_prompt docs 2020-12-28 19:39:27 +01:00
fish_indent.rst fish_indent: Change --debug-level to --debug with flog categories 2020-12-14 19:36:18 +01:00
fish_is_root_user.rst Fix paste-o that duplicated documentation header. 2020-06-30 23:45:41 +02:00
fish_key_reader.rst docs: Add completions for fish_key_reader 2020-04-19 07:06:31 +02:00
fish_mode_prompt.rst docs: link and explicit instructions on creating a blank fish_mode_prompt 2020-06-06 22:52:13 +08:00
fish_opt.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
fish_prompt.rst docs: Update example prompt in fish_prompt docs 2020-10-02 19:02:10 +02:00
fish_right_prompt.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
fish_status_to_signal.rst Remove dunderscores from __fish_status_to_signal (#7597) 2021-01-03 15:15:57 +01:00
fish_svn_prompt.rst Synopses examples for vcs services now function as intended 2020-10-02 23:45:38 +02:00
fish_title.rst docs: Add fish_title and fish_greeting 2020-10-28 17:44:45 +01:00
fish_update_completions.rst doc: homogenize commands titles 2020-04-04 10:44:53 +02:00
fish_vcs_prompt.rst Synopses examples for vcs services now function as intended 2020-10-02 23:45:38 +02:00
for.rst doc: homogenize commands titles 2020-04-04 10:44:53 +02:00
funced.rst docs: Fix spacing on key combinations 2020-03-21 13:20:34 +01:00
funcsave.rst docs: Don't speak of "initialization files" 2021-05-28 20:49:57 +02:00
function.rst function: note limits on signal triggers in documentation 2021-07-11 23:03:39 +08:00
functions.rst docs: Remove obsolete part from functions 2021-05-19 19:09:46 +02:00
help.rst docs: Link builtins 2020-03-21 15:31:25 +01:00
history.rst history.rst: clarify that "history search" is the default command 2020-08-04 21:44:26 +02:00
if.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
isatty.rst docs/isatty: Mention default value for FILE DESCRIPTOR 2020-09-05 15:54:48 +02:00
jobs.rst docs: note job expansion in bg/fg/jobs arguments 2021-02-28 20:56:23 +08:00
math.rst math: Make function parentheses optional (#7877) 2021-03-30 17:21:28 +02:00
nextd.rst doc: add section about directory history / stack 2020-04-18 10:40:48 +02:00
not.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
open.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
or.rst doc: add interlinks between true ←→ false and and ←→ or cmds 2020-02-23 23:41:16 -08:00
popd.rst doc: add section about directory history / stack 2020-04-18 10:40:48 +02:00
prevd.rst doc: add section about directory history / stack 2020-04-18 10:40:48 +02:00
printf.rst docs: Document that the man pages are for our builtins 2021-07-16 18:21:41 +02:00
prompt_login.rst Add a "prompt_login" helper function 2021-04-30 17:07:54 +02:00
prompt_pwd.rst doc: homogenize commands titles 2020-04-04 10:44:53 +02:00
psub.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
pushd.rst Improve the documentation for pushd 2021-05-04 13:21:46 -07:00
pwd.rst docs: Document that the man pages are for our builtins 2021-07-16 18:21:41 +02:00
random.rst docs: Reword random 2020-09-10 20:48:13 +02:00
read.rst docs: Remove references to read history 2021-02-02 09:42:57 +01:00
realpath.rst docs: Document that the man pages are for our builtins 2021-07-16 18:21:41 +02:00
return.rst Let "return" exit a script (#8148) 2021-07-21 22:33:39 +02:00
set.rst Add set --function (#8145) 2021-08-01 20:08:12 +02:00
set_color.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
source.rst docs: don't quote code snippets 2020-04-13 22:56:22 +02:00
status.rst docs: Update status docs 2021-04-14 21:46:51 +02:00
string-collect.rst Docs for "$(cmd)" and $(cmd) 2021-07-13 21:33:42 +02:00
string-escape.rst docs: Use \ instead of \\ in examples (#7286) 2020-08-26 18:29:03 +02:00
string-join.rst Fix a Sphinx warning 2021-05-04 13:50:09 -07:00
string-join0.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
string-length.rst Fix example in string length docs 2020-09-27 21:59:15 +02:00
string-lower.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
string-match.rst string: Add "--groups-only" to match 2021-07-16 20:27:54 +02:00
string-pad.rst Improve string pad examples and add a cross-reference 2020-09-28 18:42:02 +02:00
string-repeat.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
string-replace.rst docs: Use \ instead of \\ in examples (#7286) 2020-08-26 18:29:03 +02:00
string-split.rst docs: Use \ instead of \\ in examples (#7286) 2020-08-26 18:29:03 +02:00
string-split0.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
string-sub.rst Add string sub --end (#6765) 2020-03-22 15:53:09 +01:00
string-trim.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
string-unescape.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
string-upper.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
string.rst string: Quit early if --quiet is satisfied 2020-12-01 18:55:01 +01:00
suspend.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
switch.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
test.rst docs: Document that the man pages are for our builtins 2021-07-16 18:21:41 +02:00
time.rst docs: Document that the man pages are for our builtins 2021-07-16 18:21:41 +02:00
trap.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
true.rst doc: add interlinks between true ←→ false and and ←→ or cmds 2020-02-23 23:41:16 -08:00
type.rst Let command, jobs and type take --query instead of --quiet 2020-09-04 16:55:09 +02:00
ulimit.rst docs: Link builtins 2020-03-21 15:31:25 +01:00
umask.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
vared.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
wait.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
while.rst docs: Make more code lines shorter 2021-02-02 08:35:38 +01:00