about summary refs log tree commit diff
path: root/Functions
diff options
context:
space:
mode:
authorTanaka Akira <akr@users.sourceforge.net>1999-04-15 18:05:35 +0000
committerTanaka Akira <akr@users.sourceforge.net>1999-04-15 18:05:35 +0000
commitc175751b501a3a4cb40ad4787340a597ea769be4 (patch)
treef5cd9e9bf7dbfb5b91569181f260965c0a3cb8ad /Functions
downloadzsh-c175751b501a3a4cb40ad4787340a597ea769be4.tar.gz
zsh-c175751b501a3a4cb40ad4787340a597ea769be4.tar.xz
zsh-c175751b501a3a4cb40ad4787340a597ea769be4.zip
Initial revision
Diffstat (limited to 'Functions')
-rw-r--r--Functions/.distfiles5
-rwxr-xr-xFunctions/acx6
-rw-r--r--Functions/cat16
-rwxr-xr-xFunctions/cdmatch23
-rw-r--r--Functions/cdmatch215
-rw-r--r--Functions/checkmail26
-rwxr-xr-xFunctions/cx6
-rwxr-xr-xFunctions/harden6
-rwxr-xr-xFunctions/mere3
-rwxr-xr-xFunctions/multicomp80
-rwxr-xr-xFunctions/proto8
-rw-r--r--Functions/pushd13
-rwxr-xr-xFunctions/randline3
-rwxr-xr-xFunctions/run-help72
-rwxr-xr-xFunctions/yp2
-rwxr-xr-xFunctions/yu2
-rwxr-xr-xFunctions/zed65
-rw-r--r--Functions/zls55
18 files changed, 406 insertions, 0 deletions
diff --git a/Functions/.distfiles b/Functions/.distfiles
new file mode 100644
index 000000000..1aa4fbc89
--- /dev/null
+++ b/Functions/.distfiles
@@ -0,0 +1,5 @@
+DISTFILES_SRC='
+    .distfiles
+    acx cat cdmatch cdmatch2 checkmail cx harden mere multicomp proto
+    pushd randline run-help yp yu zed zls
+'
diff --git a/Functions/acx b/Functions/acx
new file mode 100755
index 000000000..f7c680bb0
--- /dev/null
+++ b/Functions/acx
@@ -0,0 +1,6 @@
+#! /bin/sh
+#
+# zsh shell function to make its arguments 755
+# also works as an sh script
+#
+chmod 755 $*
diff --git a/Functions/cat b/Functions/cat
new file mode 100644
index 000000000..612deac03
--- /dev/null
+++ b/Functions/cat
@@ -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
diff --git a/Functions/cdmatch b/Functions/cdmatch
new file mode 100755
index 000000000..7c4bb9ae7
--- /dev/null
+++ b/Functions/cdmatch
@@ -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.
diff --git a/Functions/cdmatch2 b/Functions/cdmatch2
new file mode 100644
index 000000000..c70357a34
--- /dev/null
+++ b/Functions/cdmatch2
@@ -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
+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
diff --git a/Functions/checkmail b/Functions/checkmail
new file mode 100644
index 000000000..9cc743db4
--- /dev/null
+++ b/Functions/checkmail
@@ -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
diff --git a/Functions/cx b/Functions/cx
new file mode 100755
index 000000000..a0b34a4f0
--- /dev/null
+++ b/Functions/cx
@@ -0,0 +1,6 @@
+#! /bin/sh
+#
+# zsh shell function to make its arguments executable
+# also works as a sh script
+#
+chmod +x $*
diff --git a/Functions/harden b/Functions/harden
new file mode 100755
index 000000000..c02689362
--- /dev/null
+++ b/Functions/harden
@@ -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
+
diff --git a/Functions/mere b/Functions/mere
new file mode 100755
index 000000000..cf8d8ad14
--- /dev/null
+++ b/Functions/mere
@@ -0,0 +1,3 @@
+#! /bin/sh
+# read a man page in the current directory
+nroff -man -Tman $1 | less -s
diff --git a/Functions/multicomp b/Functions/multicomp
new file mode 100755
index 000000000..ab206de0d
--- /dev/null
+++ b/Functions/multicomp
@@ -0,0 +1,80 @@
+# 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
+#
+# Note that exactly matched directories are not expanded, e.g.
+# s/zsh-2.4/s<TAB> will not expand to src/zsh-2.4old/src.
+# 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}"
+  if [[ -n "$pref" && -z $sofar[2] && -d "${sofar}$head" ]]; then
+    # Exactly matched directory: don't try to glob
+    reply=("${sofar}$head")
+  else
+    [[ -z "$pref" ]] && globdir=
+    # if path segment contains wildcards, don't add another.
+    if [[ "$head" = *[\[\(\*\?\$\~]* || -z "$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})
+  fi
+
+  [[ -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})
+
+# }
+
diff --git a/Functions/proto b/Functions/proto
new file mode 100755
index 000000000..df1826506
--- /dev/null
+++ b/Functions/proto
@@ -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
diff --git a/Functions/pushd b/Functions/pushd
new file mode 100644
index 000000000..965c774bf
--- /dev/null
+++ b/Functions/pushd
@@ -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
diff --git a/Functions/randline b/Functions/randline
new file mode 100755
index 000000000..9af714fa2
--- /dev/null
+++ b/Functions/randline
@@ -0,0 +1,3 @@
+# get a random line from a file
+integer z=$(wc -l <$1)
+sed -n $[RANDOM%z+1]p $1
diff --git a/Functions/run-help b/Functions/run-help
new file mode 100755
index 000000000..a8109e3ea
--- /dev/null
+++ b/Functions/run-help
@@ -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
diff --git a/Functions/yp b/Functions/yp
new file mode 100755
index 000000000..7e09613ef
--- /dev/null
+++ b/Functions/yp
@@ -0,0 +1,2 @@
+#! /bin/sh
+ypmatch $1 passwd
diff --git a/Functions/yu b/Functions/yu
new file mode 100755
index 000000000..3c5f170cf
--- /dev/null
+++ b/Functions/yu
@@ -0,0 +1,2 @@
+#! /bin/sh
+ypmatch $1 passwd.byuid
diff --git a/Functions/zed b/Functions/zed
new file mode 100755
index 000000000..e8e7212ef
--- /dev/null
+++ b/Functions/zed
@@ -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
diff --git a/Functions/zls b/Functions/zls
new file mode 100644
index 000000000..da6eff856
--- /dev/null
+++ b/Functions/zls
@@ -0,0 +1,55 @@
+# zls () {
+# simple internal ls using the stat module
+
+zmodload -i stat || return 1
+
+emulate -R zsh
+setopt localoptions
+
+local f stat opts='' L=L mod=: dirs list
+
+dirs=()
+list=()
+
+while getopts ailLFd 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
+
+if ((! ARGC)) then
+    set *
+    opts=d$opts
+fi
+
+for f in $*
+do
+    stat -s$L -A stat -F "%b %e %H:%M" - $f || continue
+    if [[ $opts != *d* && $stat[3] == d* ]] then
+	dirs=( $dirs $f )
+    elif [[ $opts == *l* ]] then
+	[[ $opts == *i* ]] && print -n "${(l:7:)stat[2]} "
+	[[ -n $stat[14] ]] && f=( $f '->' $stat[14] ) || f=( $f($mod) )
+	print -r -- "$stat[3] ${(l:3:)stat[4]} ${(r:8:)stat[5]} " \
+		    "${(r:8:)stat[6]} ${(l:8:)stat[8]} $stat[10] $f"
+    else
+	f=( $f($mod) )
+	list=( "$list[@]" "${${(M)opts:%*i*}:+${(l:7:)stat[2]} }$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
+# }