summaryrefslogtreecommitdiff
path: root/examples/functions/keep
blob: 4433b35318d5637c75331ea5a0204c3fd1d4c474 (plain)
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
# From: Seth Chaiklin <psykseth@aau.dk>
# To: chet@ins.CWRU.Edu
# Subject: bash functions (sorta)

#
# keep:
# usage: keep program
# declare the a program should be "kept".  i.e. try to fg a stopped one
# and only when that fails start a fresh program.
#

keep()
{
	case $# in
	1|2)	;;
	*) echo "usage: keep [alias] program" 1>&2 ; return 1;;
	esac

	# progname
	pn=${1##*/}

	# set up an alias for the kept program
	if [ $# = 1 ]; then
		alias "$pn=fg $1 2>/dev/null || $1"
	else
		alias "$1=fg $2 2>/dev/null || $2"
	fi
}

#
# unkeep:
# usage: unkeep program
# unset the alias set up by the keep function
#

unkeep()
{
	if [ $# != 1 ]; then
		echo "usage: unkeep program"
		return 2
	fi

	# unset the alias for the kept program
	unalias "${1##*/}"
}

#
# kept:
# lists all kept programs in 'alias: program' form
#

kept()
{
	alias | grep "fg.*2>" | sed "s/alias \(.*\)='fg.*||\(.*\)'$/\1:\2/"
}


# some things that should be kept
#keep /usr/local/bin/emacs
#keep e ${EDITOR:-/usr/local/bin/emacs}
#keep edit ${EDITOR:-/usr/local/bin/emacs}
#keep /usr/local/bin/emm