From 45bb8f535bb0258353b0d1de6dff5b6bfa1a06f6 Mon Sep 17 00:00:00 2001 From: Saeed M Rad Date: Thu, 7 Aug 2025 10:19:42 +0000 Subject: [PATCH] funced: pretend copied functions are defined interactively After #9542, the format for `functions -Dv` was changed for copied functions. ```diff -stdin -n/a +[path to copy location] +[path to original definition] [and a few more lines] ``` Some components were (and perhaps are) still expecting the old format, however. After a search, it looks like `funced` and `fish_config` are the only two functions using `functions -D` and `functions -Dv` in this repo (none are using `type -p`). As noted in issue #11614, `funced` currently edits the file which copies the given copied function. Another option was to make `funced` edit the file which originally defined the function. Since the copied function would not have been updated either way, I modified `funced` so it would pretend that the copied function was defined interactively, like it was before. I did not modify `fish_config`, since it was only used for preset prompts in the web config, none of which used `functions --copy`. (Moreover, I believe it would have behaved correctly, since the preset would not have had to define the function, only copy it.) Fixes issue #11614 --- CHANGELOG.rst | 1 + share/functions/funced.fish | 5 +++++ tests/checks/funced.fish | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 tests/checks/funced.fish diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 166dfc1af..0812462e8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -51,6 +51,7 @@ Interactive improvements - Instead of flashing all the text to the left of the cursor, fish now flashes the matched token during history token search, the completed token during completion (:issue:`11050`), the autosuggestion when deleting it, and the full command line in all other cases. - Pasted commands are now stripped of any ``$`` prefix. - The :kbd:`alt-s` binding will now also use ``run0`` if available. +- ``funced`` will now edit copied functions directly, instead of the file where ``function --copy`` was invoked. (:issue:`11614`) New or improved bindings ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/share/functions/funced.fish b/share/functions/funced.fish index bcff40cc2..32e767ce1 100644 --- a/share/functions/funced.fish +++ b/share/functions/funced.fish @@ -71,6 +71,11 @@ function funced --description 'Edit function definition' if not functions -q -- $funcname echo $init >$tmpname + else if not string match --quiet --regex '^(?:n/a|not-autoloaded|autoloaded)$' -- (functions --details --verbose -- $funcname)[2] + # Pretend this copied function does not have a definition file. Editing the file which + # originally defined it would not update this copy, and the file which copied it + # would not have a function body to edit. (issue #11614) + functions -- $funcname >$tmpname else if functions --details -- $funcname | string match --invert --quiet --regex '^(?:-|stdin|embedded:.*)$' set writepath (functions --details -- $funcname) # Use cat here rather than cp to avoid copying permissions diff --git a/tests/checks/funced.fish b/tests/checks/funced.fish new file mode 100644 index 000000000..183b2b6b5 --- /dev/null +++ b/tests/checks/funced.fish @@ -0,0 +1,23 @@ +#RUN: %fish %s + +function my-src + echo hello +end + +echo "functions --copy my-src my-dst" >my-copy-function.fish +source my-copy-function.fish +rm my-copy-function.fish # Cleanup + +functions --details --verbose my-dst +# CHECK: my-copy-function.fish +# CHECK: {{.*}}tests/checks/funced.fish +# CHECK: 3 +# CHECK: scope-shadowing + +VISUAL=cat EDITOR=cat funced my-dst +# CHECK: # Defined in {{.*}}/tests/checks/funced.fish @ line 3, copied in my-copy-function.fish @ line 1 +# CHECK: function my-dst +# CHECK: echo hello +# CHECK: end +# CHECK: Editor exited but the function was not modified +# CHECK: If the editor is still running, check if it waits for completion, maybe a '--wait' option?