From a2cd8c8a8dfc8b3acc208bb1113260cb69ca03d1 Mon Sep 17 00:00:00 2001 From: axel Date: Sun, 12 Feb 2006 23:14:21 +1000 Subject: [PATCH] Move a few more shellscript functions to their own files darcs-hash:20060212131421-ac50b-861f8941af76cf9a1e6fb7da2ac7f33795fab272.gz --- init/fish_function.fish | 70 ------------------- init/functions/__fish_complete_pids.fish | 8 +++ init/functions/__fish_is_first_token.fish | 15 ++++ init/functions/__fish_list_current_token.fish | 20 ++++++ init/functions/__fish_no_arguments.fish | 15 ++++ init/functions/__fish_print_hostnames.fish | 16 +++++ init/functions/dirs.fish | 9 +++ init/functions/fish_prompt.fish | 20 ++++++ init/functions/popd.fish | 13 ++++ init/functions/prompt_pwd.fish | 21 ++++++ init/functions/pushd.fish | 8 +++ 11 files changed, 145 insertions(+), 70 deletions(-) create mode 100644 init/functions/__fish_complete_pids.fish create mode 100644 init/functions/__fish_is_first_token.fish create mode 100644 init/functions/__fish_list_current_token.fish create mode 100644 init/functions/__fish_no_arguments.fish create mode 100644 init/functions/__fish_print_hostnames.fish create mode 100644 init/functions/dirs.fish create mode 100644 init/functions/fish_prompt.fish create mode 100644 init/functions/popd.fish create mode 100644 init/functions/prompt_pwd.fish create mode 100644 init/functions/pushd.fish diff --git a/init/fish_function.fish b/init/fish_function.fish index 3f84a4954..0c0652732 100644 --- a/init/fish_function.fish +++ b/init/fish_function.fish @@ -4,76 +4,6 @@ # function is used by the default prompt command. # -if test (uname) = Darwin - function prompt_pwd -d "Print the current working directory, shortend to fit the prompt" - if test "$PWD" != "$HOME" - printf "%s" (echo $PWD|sed -e 's|/private||' -e "s|^$HOME|~|" -e 's-/\([^/]\)\([^/]*\)-/\1-g') - echo $PWD|sed -e 's-.*/[^/]\([^/]*$\)-\1-' - else - echo '~' - end - end -else - function prompt_pwd -d "Print the current working directory, shortend to fit the prompt" - if test "$PWD" != "$HOME" - printf "%s" (echo $PWD|sed -e "s|^$HOME|~|" -e 's-/\([^/]\)\([^/]*\)-/\1-g') - echo $PWD|sed -e 's-.*/[^/]\([^/]*$\)-\1-' - else - echo '~' - end - end -end - - -# -# This function is bound to Alt-L, it is used to list the contents of -# the directory under the cursor -# - -function __fish_list_current_token -d "List contents of token under the cursor if it is a directory, otherwise list the contents of the current directory" - set val (eval echo (commandline -t)) - if test -d $val - ls $val - else - set dir (dirname $val) - if test $dir != . -a -d $dir - ls $dir - else - ls - end - end -end - - -function pushd -d "Push directory to stack" - # Comment to avoid set completions - set -g dirstack (command pwd) $dirstack - cd $argv[1] -end - - -function popd -d "Pop dir from stack" - if test $dirstack[1] - cd $dirstack[1] - else - printf (_ "%s: Directory stack is empty...") popd - return 1 - end - - set -e dirstack[1] - -end - - -function dirs -d "Print directory stack" - echo -n (command pwd)" " - for i in $dirstack - echo -n $i" " - end - echo -end - - function prevd-or-backward-word --key-binding if test -z (commandline) prevd diff --git a/init/functions/__fish_complete_pids.fish b/init/functions/__fish_complete_pids.fish new file mode 100644 index 000000000..42fcc5715 --- /dev/null +++ b/init/functions/__fish_complete_pids.fish @@ -0,0 +1,8 @@ +function __fish_complete_pids -d "Print a list of process identifiers along with brief descriptions" + # This may be a bit slower, but it's nice - having the tty displayed is really handy + ps --no-heading -o pid,comm,tty --ppid %self -N | sed -r 's/ *([0-9]+) +([^ ].*[^ ]|[^ ]) +([^ ]+)$/\1\t\2 [\3]/' ^/dev/null + + # If the above is too slow, this is faster but a little less useful + # pgrep -l -v -P %self | sed 's/ /\t/' +end + diff --git a/init/functions/__fish_is_first_token.fish b/init/functions/__fish_is_first_token.fish new file mode 100644 index 000000000..e2ca69b1b --- /dev/null +++ b/init/functions/__fish_is_first_token.fish @@ -0,0 +1,15 @@ + +function __fish_is_first_token -d 'Test if no non-switch argument has been specified yet' + set -- cmd (commandline -poc) + set -e -- cmd[1] + for i in $cmd + switch $i + case '-*' + + case '*' + return 1; + end + end + return 0 +end + diff --git a/init/functions/__fish_list_current_token.fish b/init/functions/__fish_list_current_token.fish new file mode 100644 index 000000000..b31aac402 --- /dev/null +++ b/init/functions/__fish_list_current_token.fish @@ -0,0 +1,20 @@ + +# +# This function is bound to Alt-L, it is used to list the contents of +# the directory under the cursor +# + +function __fish_list_current_token -d "List contents of token under the cursor if it is a directory, otherwise list the contents of the current directory" + set val (eval echo (commandline -t)) + if test -d $val + ls $val + else + set dir (dirname $val) + if test $dir != . -a -d $dir + ls $dir + else + ls + end + end +end + diff --git a/init/functions/__fish_no_arguments.fish b/init/functions/__fish_no_arguments.fish new file mode 100644 index 000000000..e47a42726 --- /dev/null +++ b/init/functions/__fish_no_arguments.fish @@ -0,0 +1,15 @@ + +function __fish_no_arguments -d "Internal fish function" + set -l cmd (commandline -poc) (commandline -tc) + set -e cmd[1] + for i in $cmd + switch $i + case '-*' + + case '*' + return 1 + end + end + return 0 +end + diff --git a/init/functions/__fish_print_hostnames.fish b/init/functions/__fish_print_hostnames.fish new file mode 100644 index 000000000..f6de9784f --- /dev/null +++ b/init/functions/__fish_print_hostnames.fish @@ -0,0 +1,16 @@ + +function __fish_print_hostnames -d "Print a list of known hostnames" + + # Print all hosts from /etc/hosts + if test -f /etc/hosts + sed \n' $USER $__fish_prompt_hostname "$prompt_color" (prompt_pwd) "$__fish_prompt_normal" +end + diff --git a/init/functions/popd.fish b/init/functions/popd.fish new file mode 100644 index 000000000..6aadb7890 --- /dev/null +++ b/init/functions/popd.fish @@ -0,0 +1,13 @@ + +function popd -d "Pop dir from stack" + if test $dirstack[1] + cd $dirstack[1] + else + printf (_ "%s: Directory stack is empty...") popd + return 1 + end + + set -e dirstack[1] + +end + diff --git a/init/functions/prompt_pwd.fish b/init/functions/prompt_pwd.fish new file mode 100644 index 000000000..709ccca6c --- /dev/null +++ b/init/functions/prompt_pwd.fish @@ -0,0 +1,21 @@ + +if test (uname) = Darwin + function prompt_pwd -d (_ "Print the current working directory, shortend to fit the prompt") + if test "$PWD" != "$HOME" + printf "%s" (echo $PWD|sed -e 's|/private||' -e "s|^$HOME|~|" -e 's-/\([^/]\)\([^/]*\)-/\1-g') + echo $PWD|sed -e 's-.*/[^/]\([^/]*$\)-\1-' + else + echo '~' + end + end +else + function prompt_pwd -d (_ "Print the current working directory, shortend to fit the prompt") + if test "$PWD" != "$HOME" + printf "%s" (echo $PWD|sed -e "s|^$HOME|~|" -e 's-/\([^/]\)\([^/]*\)-/\1-g') + echo $PWD|sed -e 's-.*/[^/]\([^/]*$\)-\1-' + else + echo '~' + end + end +end + diff --git a/init/functions/pushd.fish b/init/functions/pushd.fish new file mode 100644 index 000000000..f9d45d640 --- /dev/null +++ b/init/functions/pushd.fish @@ -0,0 +1,8 @@ + + +function pushd -d "Push directory to stack" + # Comment to avoid set completions + set -g dirstack (command pwd) $dirstack + cd $argv[1] +end +