summaryrefslogtreecommitdiff
path: root/dot_config/zsh/functions/elevate-cmd
blob: 6b8b2700a48006857349975319a87f470cce7226 (plain) (blame)
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
# -*- mode: sh -*- vim: ft=zsh

# the de-facto prefix is determined by:
# * zstyle (by widget name) if it's set
# * else the dedicated environment variable
# * else it's detected at runtime
local prefix
if ! zstyle -s :zle:$WIDGET prefix prefix; then
	prefix=$ELEVATECMD
	if [[ -z $prefix ]]; then
		# the runtime detection is uncached in case you install/uninstall
		if   [[ -x =doas ]]; then prefix=doas
		elif [[ -x =sudo ]]; then prefix=sudo
		else return 1 # do nothing
		fi
	fi
fi

# if the prefix is already present, we remove it
if [[ $BUFFER = ${(b)~prefix}\ * ]]; then
	# if the cursor is not inside the prefix
	if [[ $#LBUFFER -gt $#prefix ]]; then
		# then just remove it from the LBUFFER
		LBUFFER=${LBUFFER#$prefix }
	# otherwise, the cursor *is* inside the prefix
	else
		# so remove it from the buffer and move the cursor to start of line
		BUFFER=${BUFFER#$prefix }
		CURSOR=0
	fi
# otherwise we add it
else
	# prefix always goes into LBUFFER since it's not there yet
	LBUFFER="$prefix $LBUFFER"
fi