From 85f19f9b8c74239d22bbe34cdf338598f89f41b6 Mon Sep 17 00:00:00 2001 From: maxfl Date: Wed, 27 Jun 2012 07:37:48 +0400 Subject: [PATCH] Add -e option to funced New option -e or --editor allows to edit function inside external editor, rather than in reader. --- doc_src/funced.txt | 4 ++- share/completions/funced.fish | 1 + 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 715babeb1..91087c405 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 - funced NAME + funced [-e] NAME \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. + +- -e or --editor open function body inside external editor diff --git a/share/completions/funced.fish b/share/completions/funced.fish index 2e9f66a4e..2c2580d21 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 5bd54acdd..13ee44385 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