diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d3b080fad..2b33c6df5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -106,6 +106,7 @@ Interactive improvements - ``read --help`` and friends no longer ignore redirections. This fixes a regression in version 3.1 (:issue:`10274`). - Command abbreviations (those with ``--position command`` or without a ``--position``) now also expand after decorators like ``command`` (:issue:`10396`). - Abbreviations now expand after process separators like ``;`` and ``|``. This fixes a regression in version 3.6 (:issue:`9730`). +- When exporting interactively defined functions (using ``type``, ``functions`` or ``funcsave``) the function body is now indented, same as in the interactive command line editor (:issue:`8603`). New or improved bindings ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/share/functions/funced.fish b/share/functions/funced.fish index 71342357d..5e2be69f8 100644 --- a/share/functions/funced.fish +++ b/share/functions/funced.fish @@ -47,16 +47,12 @@ function funced --description 'Edit function definition' if test "$editor" = fish if functions -q -- $funcname - command -q fish_indent - and functions --no-details -- $funcname | fish_indent --no-indent | read -z init - or functions --no-details -- $funcname | read -z init + functions --no-details -- $funcname | fish_indent --only-unindent | fish_indent --no-indent | read -z init end set -l prompt 'printf "%s%s%s> " (set_color green) '$funcname' (set_color normal)' if read -p $prompt -c "$init" --shell cmd - command -q fish_indent - and echo -n $cmd | fish_indent | read -lz cmd - or echo -n $cmd | read -lz cmd + echo -n $cmd | fish_indent --only-unindent | read -lz cmd eval "$cmd" end if set -q _flag_save diff --git a/src/builtins/functions.rs b/src/builtins/functions.rs index 543359a7f..01d13aee2 100644 --- a/src/builtins/functions.rs +++ b/src/builtins/functions.rs @@ -8,6 +8,8 @@ use crate::event::{self}; use crate::function; use crate::highlight::colorize; use crate::highlight::highlight_shell; +use crate::parse_util::apply_indents; +use crate::parse_util::parse_util_compute_indents; use crate::parser_keywords::parser_keywords_is_reserved; use crate::termsize::termsize_last; @@ -407,6 +409,10 @@ pub fn functions(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) - def = props.annotated_definition(arg); } + if props.definition_file().is_none() { + def = apply_indents(&def, &parse_util_compute_indents(&def)); + } + if streams.out_is_terminal() { let mut colors = vec![]; highlight_shell(&def, &mut colors, &parser.context(), false, None); diff --git a/src/builtins/type.rs b/src/builtins/type.rs index 7552ba6aa..52a9c996b 100644 --- a/src/builtins/type.rs +++ b/src/builtins/type.rs @@ -3,6 +3,7 @@ use crate::common::str2wcstring; use crate::function; use crate::highlight::{colorize, highlight_shell}; +use crate::parse_util::{apply_indents, parse_util_compute_indents}; use crate::path::{path_get_path, path_get_paths}; #[derive(Default)] @@ -128,6 +129,9 @@ pub fn r#type(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> O comment, props.annotated_definition(arg) )); + if props.definition_file().is_none() { + def = apply_indents(&def, &parse_util_compute_indents(&def)); + } if streams.out_is_terminal() { let mut color = vec![];