aboutsummaryrefslogtreecommitdiffhomepage
path: root/share/functions/funced.fish
diff options
context:
space:
mode:
authorGravatar maxfl <gmaxfl@gmail.com>2012-06-30 10:22:41 +0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2012-07-01 14:20:43 -0700
commitab62fe6496eaf7a56cf30f682dc30c13002ec8aa (patch)
tree62b7c01445ce4dd9baffed817d5027c1df4097c5 /share/functions/funced.fish
parentbc9bae0f7f93385e6a762e1c9f97f679716b34c6 (diff)
Update funced
* Editor mode is no the default * Use -i or --interactive or -e fish to edit function in interactive mode * tmpname is now created with random number added and check that file do not already exist * check $TMPDIR existence and put /tmp if it does not exist * There is an undocumented feature to use functions, started with dash. Introduce necessary changes to funced, functions, def_function() in order to make it work properly. * Delete editor guessing. Use $EDITOR variable or -e key
Diffstat (limited to 'share/functions/funced.fish')
-rw-r--r--share/functions/funced.fish154
1 files changed, 80 insertions, 74 deletions
diff --git a/share/functions/funced.fish b/share/functions/funced.fish
index 985cc4fc..ad56dab0 100644
--- a/share/functions/funced.fish
+++ b/share/functions/funced.fish
@@ -1,85 +1,91 @@
function funced --description 'Edit function definition'
- set -l external ''
- # Default to editing with EDITOR if set
- if set -q EDITOR
- set external $EDITOR
- end
-
- # Allow overriding the editor with a -e flag
- if test (count $argv) -eq 3
- if contains -- $argv[1] -e --editor
- set external $argv[2]
- set -e argv[2]
- set -e argv[1]
- end
- end
-
- # Let an editor of "fish" mean to use the default editor
- if test "$external" = fish
- set -e external
- end
-
- # Make sure any external editor exists
- if set -q external
- if not type -f "$external" >/dev/null
- set -e external
- end
- end
-
- if test (count $argv) = 1
- switch $argv
-
- case '-h' '--h' '--he' '--hel' '--help'
+ set -l editor $EDITOR
+ set -l interactive
+ set -l funcname
+ while set -q argv[1]
+ switch $argv[1]
+ case '---long impossible to match line, because case respects -h option' -h --help
__fish_print_help funced
return 0
+ case -e --editor
+ set editor $argv[2]
+ set -e argv[2]
+
+ case -i --interactive
+ set interactive 1
+
+ case --
+ set funcname $funcname $argv[2]
+ set -e argv[2]
+
case '-*'
- printf (_ "%s: Unknown option %s\n") funced $argv
- return 1
+ set_color red
+ printf (_ "%s: Unknown option %s\n") funced $argv[1]
+ set_color normal
+ return 1
- case '*' '.*'
- set -l init ''
- set -l tmp
+ case '*' '.*'
+ set funcname $funcname $argv[1]
+ end
+ set -e argv[1]
+ end
- if set -q external[1]
- # Generate a temporary filename.
- # mktemp has a different interface on different OSes,
- # or may not exist at all; thus we fake one up from our pid
- set -l tmpname (printf "$TMPDIR/fish_funced_%d.fish" %self)
+ if begin; set -q funcname[2]; or not test "$funcname[1]"; end
+ set_color red
+ _ "funced: You must specify one function name
+"
+ set_color normal
+ return 1
+ end
- if functions -q $argv
- functions $argv > $tmpname
- else
- echo function $argv\n\nend > $tmpname
- end
- if eval $external $tmpname
- . $tmpname
- end
- set -l stat $status
- rm $tmpname >/dev/null
- return $stat
- end
+ set -l init
+ switch $funcname
+ case '-*'
+ set init function -- $funcname\n\nend
+ case '*'
+ set init function $funcname\n\nend
+ end
- set -l IFS
- if functions -q $argv
- # 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
+ if begin; test "$editor" = fish; or set -q interactive[1]; end
+ set -l IFS
+ if functions -q -- $funcname
+ # Shadow IFS here to avoid array splitting in command substitution
+ set init (functions -- $funcname | fish_indent --no-indent)
+ 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
- if read -p $prompt -c "$init" -s cmd
- # Shadow IFS _again_ to avoid array splitting in command substitution
- set -l IFS
- eval (echo -n $cmd | fish_indent)
- end
- return 0
- end
- else
- printf (_ '%s: Expected exactly one argument, got %s.\n\nSynopsis:\n\t%sfunced%s FUNCTION\n') funced (count $argv) (set_color $fish_color_command) (set_color $fish_color_normal)
- end
-end
+ set -l prompt 'printf "%s%s%s> " (set_color green) '$funcname' (set_color normal)'
+ # Unshadow IFS since the fish_title breaks otherwise
+ set -e IFS
+ if read -p $prompt -c "$init" -s cmd
+ # Shadow IFS _again_ to avoid array splitting in command substitution
+ set -l IFS
+ eval (echo -n $cmd | fish_indent)
+ end
+ return 0
+ end
+
+ if not type -f "$editor" >/dev/null
+ set_color red
+ printf (_ "%s: Editor %s is not found\n") funced $editor
+ set_color normal
+ end
+ set -q TMPDIR; or set -l TMPDIR /tmp
+ set -l tmpname (printf "$TMPDIR/fish_funced_%d_%d.fish" %self (random))
+ while test -f $tmpname
+ set tmpname (printf "$TMPDIR/fish_funced_%d_%d.fish" %self (random))
+ end
+
+ if functions -q -- $funcname
+ functions -- $funcname > $tmpname
+ else
+ echo $init > $tmpname
+ end
+ if eval $editor $tmpname
+ . $tmpname
+ end
+ set -l stat $status
+ rm $tmpname >/dev/null
+ return $stat
+end