Skip to content

funced: pretend copied functions are defined interactively #11691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2025

Conversation

esdmr
Copy link
Contributor

@esdmr esdmr commented Jul 29, 2025

Description

After #9542, the format for functions -Dv was changed for copied functions.

-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 find any preexisting test file for funced, so I did not write regressions test. Please tell me if I should.)

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. (This grammar is making me dizzy.))

Fixes issue #11614

TODOs:

  • Changes to fish usage are reflected in user documentation/manpages.
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.rst

CHANGELOG.rst Outdated
@@ -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 it was copied. (:issue:`11614`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically correct but maybe it's clearer to say
maybe « ... where "functions --copy" was invoked » ?
I'm fine eitehr way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed it to instead of the file where ``function --copy`` was invoked.

@@ -71,6 +71,11 @@ function funced --description 'Edit function definition'

if not functions -q -- $funcname
echo $init >$tmpname
else if string match --invert --quiet --regex '^(?:n/a|not-autoloaded|autoloaded)$' -- (functions --details --verbose -- $funcname)[2]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not string match would be a little bit more idiomatic than string match --invert.

It's a shame that we don't have an easier / more robust way of figuring out that a function was copied from somewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line later had that --invert so I had kept it. changed it to not.

# 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, after testing this, I think I get it. LGTM, thanks for fixing this.
Your PR description makes sense - maybe put it into the commit message?

Here's a quick test, run with

cargo b && tests/test_driver.py target/debug tests/checks/funced.fish

(we could definitely add more tests)

diff --git a/tests/checks/funced.fish b/tests/checks/funced.fish
new file mode 100644
index 0000000000..a285635334
--- /dev/null
+++ b/tests/checks/funced.fish
@@ -0,0 +1,22 @@
+#RUN: %fish %s
+
+function my-src
+    echo hello
+end
+
+echo >my-copy-function.fish "functions --copy my-src my-dst"
+source my-copy-function.fish
+
+functions --details --verbose my-dst
+# CHECK: my-copy-function.fish
+# CHECK: {{.*}}tests/checks/funced.fish
+# CHECK: 3
+# CHECK: scope-shadowing
+
+EDITOR=cat funced my-dst
+# CHECK: # Defined in {{.*}}/tests/checks/funced.fish @ line 3, copied in my-copy-function.fish @ line 2
+# 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I have added the test.

I have also rebased the commit and updated the commit message.

@krobelus krobelus added this to the fish 4.1 milestone Aug 6, 2025
@esdmr esdmr force-pushed the esdmr/funced-copied branch from 5caaf86 to 1b1df8c Compare August 7, 2025 10:03
After fish-shell#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 fish-shell#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 fish-shell#11614
@esdmr esdmr force-pushed the esdmr/funced-copied branch from 1b1df8c to 45bb8f5 Compare August 7, 2025 10:22
@krobelus krobelus merged commit c123c4e into fish-shell:master Aug 10, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants