Add -e option to funced

New option -e or --editor allows to edit function inside external
editor, rather than in reader.
This commit is contained in:
maxfl 2012-06-27 07:37:48 +04:00 committed by ridiculousfish
parent 40e4f49dbe
commit 85f19f9b8c
3 changed files with 46 additions and 6 deletions

View File

@ -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

View File

@ -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'

View File

@ -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