summaryrefslogtreecommitdiff
path: root/eclass/shell-completion.eclass
blob: badf3ada57e2f8834989b0ef2179deb196a4d63b (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright 2023-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# @ECLASS: shell-completion.eclass
# @MAINTAINER:
# Jonas Frei <freijon@pm.me>
# Florian Schmaus <flow@gentoo.org>
# mgorny@gentoo.org
# @AUTHOR:
# Alfred Wingate <parona@protonmail.com>
# @SUPPORTED_EAPIS: 7 8 9
# @BLURB: a few quick functions to install various shell completion files
# @DESCRIPTION:
# This eclass provides a standardised way to install shell completions
# for popular shells.

case ${EAPI} in
	7|8|9) ;;
	*) die "${ECLASS}: EAPI ${EAPI:-0} not supported"
esac

if [[ -z ${_SHELL_COMPLETION_ECLASS} ]]; then
_SHELL_COMPLETION_ECLASS=1

if [[ ${EAPI} != 9 ]]; then
inherit toolchain-funcs
fi

# @FUNCTION: _bash-completion-r1_get_bashdir
# @INTERNAL
# @DESCRIPTION:
# First argument is name of the string in bash-completion.pc
# Second argument is the fallback directory if the string is not found
# Note that the first argument is only used on EAPI < 9.
# @EXAMPLE:
# _bash-completion-r1_get_bashdir completionsdir /usr/share/bash-completion
_bash-completion-r1_get_bashdir() {
	debug-print-function ${FUNCNAME} "$@"

	if [[ ${EAPI} != 9 ]] && $(tc-getPKG_CONFIG) --exists bash-completion &>/dev/null; then
		local path
		path=$($(tc-getPKG_CONFIG) --variable="${1}" bash-completion) || die
		# we need to return unprefixed, so strip from what pkg-config returns
		# to us, bug #477692
		echo "${path#"${EPREFIX}"}"
		return
	fi

	echo "${2}"
}

# @FUNCTION: _bash-completion-r1_get_bashcompdir
# @INTERNAL
# @DESCRIPTION:
# Get unprefixed bash-completion completions directory.
_bash-completion-r1_get_bashcompdir() {
	debug-print-function ${FUNCNAME} "$@"

	_bash-completion-r1_get_bashdir completionsdir /usr/share/bash-completion/completions
}

# @FUNCTION: _bash-completion-r1_get_bashhelpersdir
# @INTERNAL
# @DESCRIPTION:
# Get unprefixed bash-completion helpers directory.
_bash-completion-r1_get_bashhelpersdir() {
	debug-print-function ${FUNCNAME} "$@"

	_bash-completion-r1_get_bashdir helpersdir /usr/share/bash-completion/helpers
}

# @FUNCTION: get_bashcompdir
# @DESCRIPTION:
# Get the bash-completion completions directory.
get_bashcompdir() {
	debug-print-function ${FUNCNAME} "$@"

	echo "${EPREFIX}$(_bash-completion-r1_get_bashcompdir)"
}

# @FUNCTION: get_bashhelpersdir
# @INTERNAL
# @DESCRIPTION:
# Get the bash-completion helpers directory.
get_bashhelpersdir() {
	debug-print-function ${FUNCNAME} "$@"

	echo "${EPREFIX}$(_bash-completion-r1_get_bashhelpersdir)"
}

# @FUNCTION: dobashcomp
# @USAGE: <file> [...]
# @DESCRIPTION:
# Install bash-completion files passed as args. Has EAPI-dependent failure
# behavior (like doins).
dobashcomp() {
	debug-print-function ${FUNCNAME} "$@"

	(
		insopts -m 0644
		insinto "$(_bash-completion-r1_get_bashcompdir)"
		doins "${@}"
	)
}

# @FUNCTION: newbashcomp
# @USAGE: <file> <newname>
# @DESCRIPTION:
# Install bash-completion file under a new name. Has EAPI-dependent failure
# behavior (like newins).
newbashcomp() {
	debug-print-function ${FUNCNAME} "$@"

	(
		insopts -m 0644
		insinto "$(_bash-completion-r1_get_bashcompdir)"
		newins "${@}"
	)
}

# @FUNCTION: bashcomp_alias
# @USAGE: <basename> <alias>...
# @DESCRIPTION:
# Alias <basename> completion to one or more commands (<alias>es).
bashcomp_alias() {
	debug-print-function ${FUNCNAME} "$@"

	[[ ${#} -lt 2 ]] && die "Usage: ${FUNCNAME} <basename> <alias>..."
	local base=${1} f
	shift

	for f; do
		dosym "${base}" "$(_bash-completion-r1_get_bashcompdir)/${f}" \
			|| return
	done
}

# @FUNCTION: _shell-completion_get_fishcompdir
# @INTERNAL
# @RETURN: unprefixed fish completions directory
_shell-completion_get_fishcompdir() {
	echo "/usr/share/fish/vendor_completions.d"
}

# @FUNCTION: _shell-completion_get_zshcompdir
# @INTERNAL
# @RETURN: unprefixed zsh completions directory
_shell-completion_get_zshcompdir() {
	echo "/usr/share/zsh/site-functions"
}

# @FUNCTION: get_fishcompdir
# @RETURN: the fish completions directory (with EPREFIX)
get_fishcompdir() {
	debug-print-function ${FUNCNAME} "$@"

	echo "${EPREFIX}$(_shell-completion_get_fishcompdir)"
}

# @FUNCTION: get_zshcompdir
# @RETURN: the zsh completions directory (with EPREFIX)
get_zshcompdir() {
	debug-print-function ${FUNCNAME} "$@"

	echo "${EPREFIX}$(_shell-completion_get_zshcompdir)"
}

# @FUNCTION: dofishcomp
# @USAGE: <file...>
# @DESCRIPTION:
# Install fish completion files passed as args.
dofishcomp() {
	debug-print-function ${FUNCNAME} "$@"

	(
		insopts -m 0644
		insinto "$(_shell-completion_get_fishcompdir)"
		doins "${@}"
	)
}

# @FUNCTION: dozshcomp
# @USAGE: <file...>
# @DESCRIPTION:
# Install zsh completion files passed as args.
dozshcomp() {
	debug-print-function ${FUNCNAME} "$@"

	(
		insopts -m 0644
		insinto "$(_shell-completion_get_zshcompdir)"
		doins "${@}"
	)
}

# @FUNCTION: newfishcomp
# @USAGE: <file> <newname>
# @DESCRIPTION:
# Install fish file under a new name.
newfishcomp() {
	debug-print-function ${FUNCNAME} "$@"

	(
		insopts -m 0644
		insinto "$(_shell-completion_get_fishcompdir)"
		newins "${@}"
	)
}

# @FUNCTION: newzshcomp
# @USAGE: <file> <newname>
# @DESCRIPTION:
# Install zsh file under a new name.
newzshcomp() {
	debug-print-function ${FUNCNAME} "$@"

	(
		insopts -m 0644
		insinto "$(_shell-completion_get_zshcompdir)"
		newins "${@}"
	)
}

fi