diff options
author | maxfl <gmaxfl@gmail.com> | 2012-06-27 07:37:48 +0400 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2012-06-29 16:42:34 -0700 |
commit | 85f19f9b8c74239d22bbe34cdf338598f89f41b6 (patch) | |
tree | 6ec3665a9eb2d2db6ea6d24157a7b84e684c91ee | |
parent | 40e4f49dbe483cef84edbea5e7a723e21c0f5e2e (diff) |
Add -e option to funced
New option -e or --editor allows to edit function inside external
editor, rather than in reader.
-rw-r--r-- | doc_src/funced.txt | 4 | ||||
-rw-r--r-- | share/completions/funced.fish | 1 | ||||
-rw-r--r-- | share/functions/funced.fish | 47 |
3 files changed, 46 insertions, 6 deletions
diff --git a/doc_src/funced.txt b/doc_src/funced.txt index 715babeb..91087c40 100644 --- a/doc_src/funced.txt +++ b/doc_src/funced.txt @@ -1,9 +1,11 @@ \section funced funced - edit a function interactively \subsection funced-synopsis Synopsis - <code>funced NAME</code> + <code>funced [-e] NAME</code> \subsection funced-description Description Use the funced command to interactively edit the definition of a function. If there is no function with the name specified, a skeleton function is inserted, if a function exist, the definion will be shown on the command line. + +- <code>-e</code> or <code>--editor</code> open function body inside external editor diff --git a/share/completions/funced.fish b/share/completions/funced.fish index 2e9f66a4..2c2580d2 100644 --- a/share/completions/funced.fish +++ b/share/completions/funced.fish @@ -1 +1,2 @@ complete -c funced -xa "(functions -na)" --description "Save function" +complete -c funced -s e -l editor -d 'Open function in external editor' diff --git a/share/functions/funced.fish b/share/functions/funced.fish index 5bd54acd..13ee4438 100644 --- a/share/functions/funced.fish +++ b/share/functions/funced.fish @@ -1,5 +1,26 @@ +function funced --description 'Edit function definition' + set -l external + if test (count $argv) = 2 + for i in (seq 2) + switch $argv[$i] + case -e --editor + if set -q EDITOR + set external $EDITOR + end -function funced --description "Edit function definition" + if not type -f "$external" >/dev/null + for e in edit nano pico joe mcedit vim vi + if type -f $e >/dev/null + set external $e + break + end + end + end + set -e argv[$i] + break + end + end + end if test (count $argv) = 1 switch $argv @@ -15,15 +36,31 @@ function funced --description "Edit function definition" set -l init '' set -l tmp - # Shadow IFS here to avoid array splitting in command substitution - set -l IFS + if set -q external[1] + set -l tmpname (mktemp --suffix=.fish) + + if functions -q $argv + functions $argv > $tmpname + else + echo "function $argv +end +" > $tmpname + end + eval $external $tmpname + . $tmpname + set -l stat $status + rm $tmpname >/dev/null + return $stat + end + + set -l IFS if functions -q $argv - set init (functions $argv | fish_indent --no-indent) + # Shadow IFS here to avoid array splitting in command substitution + set init (functions $argv | fish_indent --no-indent) else set init function $argv\nend end - set -l prompt 'printf "%s%s%s> " (set_color green) '$argv' (set_color normal)' # Unshadow IFS since the fish_title breaks otherwise set -e IFS |