bash中Makefile变量的自动完成
假设我的bash中Makefile变量的自动完成,bash,variables,autocomplete,makefile,Bash,Variables,Autocomplete,Makefile,假设我的Makefile类似于: DIR :=# foobar: ls ${DIR} 我打字的时候 mak[tab] f[tab] 它给出了正确的答案 make foobar 但是 没有魔法 make foobar DIR= 所以我的问题是:有没有一种方法可以在bash中自动完成Makefile变量(除了目标之外)?这个答案还远远没有完成。要grep Makefile中的所有变量,我们使用make-p打印Makefile数据库: 在下一步中,我们删除除变量名以外的所有内容(在:
Makefile
类似于:
DIR :=#
foobar:
ls ${DIR}
我打字的时候
mak[tab] f[tab]
它给出了正确的答案
make foobar
但是
没有魔法
make foobar DIR=
所以我的问题是:有没有一种方法可以在bash中自动完成Makefile变量(除了目标之外)?这个答案还远远没有完成。要grep Makefile中的所有变量,我们使用
make-p
打印Makefile数据库:
在下一步中,我们删除除变量名以外的所有内容(在:=
之后的所有内容):
以下几行说明了如何实现这一点:
_make_variables()
{
# get current completion
local cur=${COMP_WORDS[COMP_CWORD]}
# get list of possible makefile variables
local var=$(make -p Makefile | sed -n '/# makefile (from/ {n; s/^\([^.#:= ]\+\) *:\?=.*$/\1=/p;}')
# don't add a space after completion
compopt -o nospace
# find possible matches
COMPREPLY=( $(compgen -W "${var}" -- ${cur}) )
}
# use _make_variables to complete make arguments
complete -F _make_variables make
现在maked[tab]
结果是makedir=
遗憾的是,使用这种方法,您将丢失所有文件和目标完成。此外,从完成输出中删除一些其他变量(例如,MAKEFILE\u LIST
)也很有用
为了添加此功能,可能需要填写一份愿望/错误报告。为此,您需要修改make bash completion函数,该函数提取目标名称,同时提取变量名称。这应该是可能的,尽管我不能说需要做多少工作。我几乎可以肯定bash正在完成“foobar”,不是因为“foobar”是makefile中的目标,而是因为您有一个名为
foobar
@Beta的文件,谢谢您的反馈!不,出于测试目的,我在Makefile dir中创建了两个subdir(命名为“asd”和“qwe”)。顺便说一句,我正在使用Debian测试,Make4.0和Bash4.3。24@Beta ... 我的dir中没有更多的文件(特别是名为“foobar”)了(但是Makefile)谢谢,+1。在bash完成项目时填写了愿望列表项。
# GNU Make 3.81
# Copyright (C) 2006 Free Software Foundation, Inc.
# This is free software; see the source for copying conditions.
# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
# This program built for x86_64-pc-linux-gnu
# Make data base, printed on Mon Oct 13 13:36:12 2014
# Variables
# automatic
<D = $(patsubst %/,%,$(dir $<))
# automatic
?F = $(notdir $?)
# environment
DESKTOP_SESSION = kde-plasma
# ...
# makefile (from `Makefile', line 1)
DIR :=
$ make -p | sed -n '/# makefile (from/ {n; p;}'
MAKEFILE_LIST := Makefile
DIR :=
$ make -p Makefile | sed -n '/# makefile (from/ {n; s/^\([^.#:= ]\+\) *:\?=.*$/\1/p;}'
MAKEFILE_LIST
DIR
_make_variables()
{
# get current completion
local cur=${COMP_WORDS[COMP_CWORD]}
# get list of possible makefile variables
local var=$(make -p Makefile | sed -n '/# makefile (from/ {n; s/^\([^.#:= ]\+\) *:\?=.*$/\1=/p;}')
# don't add a space after completion
compopt -o nospace
# find possible matches
COMPREPLY=( $(compgen -W "${var}" -- ${cur}) )
}
# use _make_variables to complete make arguments
complete -F _make_variables make