1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-10-22 16:20:23 +02:00

Initial revision

This commit is contained in:
Tanaka Akira 1999-06-08 09:26:01 +00:00
parent b39cafaa22
commit fcd7cd1cfa
25 changed files with 728 additions and 0 deletions

View file

@ -0,0 +1,6 @@
DISTFILES_SRC='
.distfiles
acx allopt cat cdmatch cdmatch2 checkmail cx harden mere multicomp
proto pushd randline run-help yp yu zed zless zls
'

6
Functions/Misc/acx Executable file
View file

@ -0,0 +1,6 @@
#! /bin/sh
#
# zsh shell function to make its arguments 755
# also works as an sh script
#
chmod 755 $*

29
Functions/Misc/allopt Normal file
View file

@ -0,0 +1,29 @@
# This function lists options with the no's in front removed for
# improved comprehension, i.e. `norcs off' becomes `rcs on'.
# The format is otherwise like that with `kshoptionprint' set,
# i.e. you can see all options whether on or off.
# It can take a list of option names or parts thereof to search for
# via egrep.
#
# Written by Sweth Chandramouli with hacks by Bart Schaefer.
listalloptions () {
emulate -R zsh
builtin setopt localoptions kshoptionprint
local OPT_NAME OPT_PAIR OPT_VALUE
for OPT_PAIR in "${(f)$(builtin setopt)}" ; do
OPT_VALUE=${OPT_PAIR##* }
OPT_NAME=${OPT_PAIR%% *}
if [[ ${OPT_NAME#no} != ${OPT_NAME} ]] ; then
OPT_VALUE=${(L)${${OPT_VALUE:s/on/OFF}:s/off/on}} &&
OPT_NAME=${OPT_NAME#no};
fi;
echo ${(r:21:)OPT_NAME} ${OPT_VALUE}
done
}
if [[ -n $@ ]]; then
listalloptions | egrep "${(j.|.)@}"
else
listalloptions
fi

16
Functions/Misc/cat Normal file
View file

@ -0,0 +1,16 @@
#! /usr/local/bin/zsh -f
local file
if ((! ARGC)) then
set -- -
fi
for file
do
if [[ "$file" == - ]] then
while read -u0ek 4096; do ; done
else
while read -u0ek 4096; do ; done < "$file"
fi
done

23
Functions/Misc/cdmatch Executable file
View file

@ -0,0 +1,23 @@
# Start of cdmatch.
# Save in your functions directory and autoload, then do
# compctl -x 'S[/][~][./][../]' -g '*(-/)' - \
# 'n[-1,/], s[]' -K cdmatch -S '/' -- cd pushd
#
# Completes directories for cd, pushd, ... anything which knows about cdpath.
# You do not have to include `.' in your cdpath.
#
# It works properly only if $ZSH_VERSION > 3.0-pre4. Remove `emulate -R zsh'
# for all other values of $ZSH_VERSION > 2.6-beta2. For earlier versions
# it still works if RC_EXPAND_PARAM is not set or when cdpath is empty.
emulate -R zsh
setopt localoptions
local narg pref cdp
read -nc narg
read -Ac pref
cdp=(. $cdpath)
reply=( ${^cdp}/${pref[$narg]%$2}*$2(-/DN^M:t) )
return
# End of cdmatch.

15
Functions/Misc/cdmatch2 Normal file
View file

@ -0,0 +1,15 @@
# This function should be called from compctl to complete the
# second argument of cd and pushd.
emulate -R zsh # Requires zsh 3.0-pre4 or later
setopt localoptions extendedglob
local from
read -Ac from
from="${from[2]}"
eval "reply=( \${PWD:s@$from@$1*$2@}~$PWD(ND-/:) )"
reply=( "${${reply[@]#${PWD%%$from*}}%${PWD#*$from}}" )
[[ ${#reply[(r),-1]} != 0 ]] && reply[(r)]="''"
return

26
Functions/Misc/checkmail Normal file
View file

@ -0,0 +1,26 @@
#! /usr/local/bin/zsh
#
# This autoloadable function checks the folders specified as arguments
# for new mails. The arguments are interpeted in exactly the same way
# as the mailpath special zsh parameter (see zshparam(1)).
#
# If no arguments are given mailpath is used. If mailpath is empty, $MAIL
# is used and if that is also empty, /var/spool/mail/$LOGNAME is used.
# This function requires zsh-3.0.1 or newer.
#
local file message
for file in "${@:-${mailpath[@]:-${MAIL:-/var/spool/mail/$LOGNAME}}}"
do
message="${${(M)file%%\?*}#\?}"
file="${file%%\?*}"
if [[ -d "$file" ]] then
file=( "$file"/**/*(.ND) )
if (($#file)) then
checkmail "${^file}\?$message"
fi
elif test -s "$file" -a -N "$file"; then # this also sets $_ to $file
print -r -- "${(e)message:-You have new mail.}"
fi
done

6
Functions/Misc/cx Executable file
View file

@ -0,0 +1,6 @@
#! /bin/sh
#
# zsh shell function to make its arguments executable
# also works as a sh script
#
chmod +x $*

6
Functions/Misc/harden Normal file
View file

@ -0,0 +1,6 @@
#! /bin/sh
# harden a link (convert it to a singly linked file)
cp $1 $1.foo
rm $1
mv $1.foo $1

3
Functions/Misc/mere Normal file
View file

@ -0,0 +1,3 @@
#! /bin/sh
# read a man page in the current directory
nroff -man -Tman $1 | less -s

72
Functions/Misc/multicomp Executable file
View file

@ -0,0 +1,72 @@
# multicomp() {
# Completes all manner of files given prefixes for each path segment.
# e.g. s/z/s -> src/zsh-2.4/src
#
# Usage: e.g.
# compctl -D -f + -U -Q -S '' -K multicomp
#
# Will expand glob patterns already in the word, but use complete-word,
# not TAB (expand-or-complete), or you will get ordinary glob expansion.
# Requires the -U option to compctl.
# Menucompletion is highly recommended for ambiguous matches.
# Liable to screw up escaped metacharacters royally.
# $fignore is not used: feel free to add your own bit.
emulate -R zsh # Requires zsh 3.0-pre4 or later
local pref head sofar origtop newtop globdir="(-/)" wild
setopt localoptions nullglob rcexpandparam globdots
unsetopt markdirs globsubst shwordsplit nounset
pref="${1}$2"
# Hack to allow programmable completion to select multicomp after a :
# (e.g.
# compctl -D -f -x 's[:]' -U -Q -S '' -K multicomp
# )
pref="${pref#:}"
sofar=('')
reply=('')
if [[ "$pref" = \~* ]]; then
# If the string started with ~, save the head and what it will become.
origtop="${pref%%/*}"
eval "newtop=$origtop"
# Save the expansion as the bit matched already
sofar=($newtop)
pref="${pref#$origtop}"
fi
while [[ -n "$pref" ]]; do
[[ "$pref" = /* ]] && sofar=(${sofar}/) && pref="${pref#/}"
head="${pref%%/*}"
pref="${pref#$head}"
[[ -z "$pref" ]] && globdir=
# if path segment contains wildcards, don't add another.
if [[ "$head" = *[\[\(\*\?\$\~]* ]]; then
wild=$head
else
# Simulate case-insensitive globbing for ASCII characters
wild="[${(j(][))${(s())head:l}}]*" # :gs/a/[a]/ etc.
# The following could all be one expansion, but for readability:
wild=$wild:gs/a/aA/:gs/b/bB/:gs/c/cC/:gs/d/dD/:gs/e/eE/:gs/f/fF/
wild=$wild:gs/g/gG/:gs/h/hH/:gs/i/iI/:gs/j/jJ/:gs/k/kK/:gs/l/lL/
wild=$wild:gs/m/mM/:gs/n/nN/:gs/o/oO/:gs/p/pP/:gs/q/qQ/:gs/r/rR/
wild=$wild:gs/s/sS/:gs/t/tT/:gs/u/uU/:gs/v/vV/:gs/w/wW/:gs/x/xX/
wild=$wild:gs/y/yY/:gs/z/zZ/:gs/-/_/:gs/_/-_/:gs/[]//
# Expand on both sides of '.' (except when leading) as for '/'
wild="${${wild:gs/[.]/*.*/}#\*}"
fi
reply=(${sofar}"${wild}${globdir}")
reply=(${~reply})
[[ -z $reply[1] ]] && reply=() && break
[[ -n $pref ]] && sofar=($reply)
done
# Restore ~'s in front if there were any.
# There had better not be anything funny in $newtop.
[[ -n "$origtop" ]] && reply=("$origtop"${reply#$newtop})
# }

8
Functions/Misc/proto Executable file
View file

@ -0,0 +1,8 @@
#! /bin/sh
# generate prototypes, if your style is the same as mine
for i
do
rm $i:r.pro 2>/dev/null
grep -v '[{};:#]' $i | grep '^[A-Za-z]' |
grep -v static | sed 's/$/;/' >! $i:r.pro
done

13
Functions/Misc/pushd Normal file
View file

@ -0,0 +1,13 @@
# pushd function to emulate the old zsh behaviour. With this function
# pushd +/-n just lifts the selected element to the top of the stack
# instead of just cycling the stack.
emulate -R zsh
setopt localoptions
if [[ ARGC -eq 1 && "$1" == [+-]<-> ]] then
setopt pushdignoredups
builtin pushd ~$1
else
builtin pushd "$@"
fi

3
Functions/Misc/randline Executable file
View file

@ -0,0 +1,3 @@
# get a random line from a file
integer z="$(wc -l <$1)"
sed -n $[RANDOM%z+1]p $1

72
Functions/Misc/run-help Normal file
View file

@ -0,0 +1,72 @@
#!/usr/local/bin/zsh
#
# Figure out where to get the best help, and get it.
#
# Install this function by placing it in your FPATH and then
# adding to your .zshrc the lines:
# unalias run-help
# autoload run-help
#
emulate -R zsh
setopt localoptions
# Check whether Util/helpfiles has been used to generate zsh help
if [[ $1 == "-l" ]]
then
if [[ -n "${HELPDIR:-}" ]]
then
echo 'Here is a list of topics for which help is available:'
echo ""
print -rc $HELPDIR/*(:t)
else
echo 'There is no list of help topics available at this time'
fi
return 0
elif [[ -n "${HELPDIR:-}" && -r $HELPDIR/$1 && $1 != compctl ]]
then
${=PAGER:-more} $HELPDIR/$1
return $?
fi
# No zsh help, use "whence" to figure out where else we might look
local what places newline='
'
integer i=0 didman=0
places=( "${(@f)$(builtin whence -va $1)}" )
while ((i++ < $#places))
do
what=$places[$i]
builtin print -r $what
case $what in
(*( is an alias)*)
[[ ${what[(w)6]:t} != ${what[(w)1]} ]] && run-help ${what[(w)6]:t}
;;
(*( is a * function))
builtin functions ${what[(w)1]} | ${=PAGER:-more}
;;
(*( is a * builtin))
case ${what[(w)1]} in
(compctl) man zshcompctl;;
(bindkey) man zshzle;;
(*setopt) man zshoptions;;
(*) man zshbuiltins;;
esac
;;
(*( is hashed to *))
man ${what[(w)-1]:t}
;;
(*)
((! didman++)) && man $1
;;
esac
if ((i < $#places && ! didman))
then
builtin print -nP "%SPress any key for more help or q to quit%s"
builtin read -k what
[[ $what != $newline ]] && echo
[[ $what == [qQ] ]] && break
fi
done

2
Functions/Misc/yp Executable file
View file

@ -0,0 +1,2 @@
#! /bin/sh
ypmatch $1 passwd

2
Functions/Misc/yu Executable file
View file

@ -0,0 +1,2 @@
#! /bin/sh
ypmatch $1 passwd.byuid

65
Functions/Misc/zed Normal file
View file

@ -0,0 +1,65 @@
#
# zed
#
# No other shell could do this.
# Edit small files with the command line editor.
# Use ^X^W to save, ^C to abort.
# Option -f: edit shell functions. (Also if called as fned.)
#
# Completion: use
# compctl -f -x 'w[1,-f]' -F -- zed
#
local var fun cleanup
# We do not want timeout while we are editing a file
integer TMOUT=0
[[ $1 = -f || $0 = fned ]] && fun=1
[[ $1 = -(|-|f) ]] && shift
[[ -z "$1" ]] && echo 'Usage: "zed filename" or "zed -f function"' && return 1
# catch interrupts
cleanup="$(bindkey -L "^M"; bindkey -L -M emacs "^X^W"; bindkey -aL "ZZ"
echo "trap - INT EXIT"; trap)"
trap "return 130" INT
trap "$cleanup" EXIT
# don't mangle !'s
setopt localoptions nobanghist
bindkey "^M" self-insert-unmeta
# Depending on your stty's, you may be able to use ^J as accept-line, else:
bindkey -M emacs "^X^W" accept-line
bindkey -a "ZZ" accept-line
if ((fun)) then
var="$(functions $1)"
# If function is undefined but autoloadable, load it
if [[ $var = undefined* ]] then
local dir
for dir in $fpath; do
if [[ -f $dir/$1 ]] then
var="$1() {
$(<$dir/$1)
}"
break
fi
done
elif [[ -z $var ]] then
var="$1() {
}"
fi
vared var && eval function "$var"
else
[[ -f $1 ]] && var="$(<$1)"
while vared var
do
(print -r -- "$var" >| $1) && break
echo -n -e '\a'
done
fi
return 0
# End of zed

37
Functions/Misc/zless Normal file
View file

@ -0,0 +1,37 @@
#!/usr/bin/zsh -f
#
# zsh function script to run less on various inputs, decompressing as required.
# Author: Phil Pennock. zsh-hacks@athenaeum.demon.co.uk
# Modified by Bart Schaefer.
# Thanks to zefram@fysh.org for a great deal of help in sorting this out,
# ie wrt syntax for unsetting members of arrays and eval "$(...)" when I
# asked for something better than . =(...)
#
# Use -zforce to pass through a display-formatting command
# zless -zforce 'bzip2 -dc' foo-no-dotbz2
# zless -zforce 'od -hc' foo-binfile
#
# If you can understand all of this without reference to zshexpn(1)
# and zshparam(1) then you either have a photographic memory or you
# need to get out more.
#
emulate -R zsh
setopt localoptions
[[ $# -ge 1 ]] || return
local lessopts
set -A lessopts
integer i=1 loi=1
while ((i <= $#))
do
case $argv[i] in
-zforce) argv[i,i+2]=("=($argv[i+1] \"$argv[i+2]\")"); ((++i));;
-*) lessopts[loi++]=\"$argv[i]\"; argv[i]=(); continue;;
*.(gz|Z)) argv[i]="=(zcat \"$argv[i]\")";;
*.bz2) argv[i]="=(bzip2 -dc \"$argv[i]\")";;
*.bz) argv[i]="=(bzip -dc \"$argv[i]\")";;
esac
((++i))
done
eval command less $lessopts $*

72
Functions/Misc/zls Normal file
View file

@ -0,0 +1,72 @@
# zls () {
# simple internal ls using the stat module
zmodload -i stat || return 1
emulate -R zsh
setopt localoptions
local f opts='' L=L mod=: dirs list
typeset -A stat
dirs=()
list=()
while getopts ailLFdtuc f
do
opts=$opts$f
if [[ $f == '?' ]] then
echo Usage: $0 [ -ailLFd ] [ filename ... ]
return 1
fi
done
shift OPTIND-1
[[ $opts == *L* ]] && L=''
[[ $opts == *F* ]] && mod=T$mod
[[ $opts == *a* ]] && setopt globdots
local time=mtime tmod=m
[[ $opts == *u* ]] && time=atime tmod=a
[[ $opts == *c* ]] && time=ctime tmod=c
if ((! ARGC)) then
if [[ $opts = *t* ]]; then
set *(o$tmod)
else
set *
fi
opts=d$opts
elif [[ $opts = *t* && $ARGC -gt 1 ]]; then
# another glaringly obvious zsh trick: reorder the argv list
# by time, without messing up metacharacters inside
local n='$1'
for (( f = 2; f <= $ARGC; f++ )); do
n="$n|\$$f"
done
eval "argv=(($n)(o$tmod))"
fi
for f in $*
do
stat -s$L -H stat -F "%b %e %H:%M" - $f || continue
if [[ $opts != *d* && $stat[mode] == d* ]] then
dirs=( $dirs $f )
elif [[ $opts == *l* ]] then
[[ $opts == *i* ]] && print -n "${(l:7:)stat[inode]} "
[[ -n $stat[link] ]] && f=( $f '->' $stat[link] ) || f=( $f($mod) )
print -r -- "$stat[mode] ${(l:3:)stat[nlink]} ${(r:8:)stat[uid]} " \
"${(r:8:)stat[gid]} ${(l:8:)stat[size]} $stat[$time] $f"
else
f=( $f($mod) )
list=( "$list[@]" "${${(M)opts:%*i*}:+${(l:7:)stat[inode]} }$f" )
fi
done
(($#list)) && print -cr -- "$list[@]"
while (($#dirs)) do
((ARGC > $#dirs)) && echo
((ARGC > 1)) && echo $dirs[1]:
(cd $dirs[1] && $0 -d$opts)
shift dirs
done
# }