File: zed

package info (click to toggle)
zsh 3.1.2-8
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 3,628 kB
  • ctags: 3,582
  • sloc: ansic: 39,480; sh: 2,393; makefile: 1,189; perl: 329; awk: 257; sed: 25
file content (62 lines) | stat: -rwxr-xr-x 1,408 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# zed():  Peter Stephenson <pws@s-a.amtp.liv.ac.uk>
# 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