<commit_before>if exists('g:loaded_setup_makeprg')
	finish
endif

let s:override_c = { 'makeprg': 'gcc %:p -o %:p:r && %:p:r', 'prefix': 0 }
let s:override_cpp = { 'makeprg': 'g++ %:p -o %:p:r && %:p:r', 'prefix': 0 }
let s:override_pl = { 'makeprg': 'perl' }

fun! s:FindFromModeline(...)
	let maxsearch = a:0 ? a:1 : 10
	let mx = line('$')
	if mx
		let lines = getline(1, min([mx, maxsearch]))
		if mx > maxsearch
			call extend(lines, reverse(getline(max([maxsearch, mx-maxsearch]), mx)))
		endif
		for line in lines
			let m = matchlist(line, 'makeprg=\(.*\)$')
			if len(m)
				return [m[1]]
			endif
		endfor
	endif
	return []
endfun

fun! SetupMakePrg(...)
	if a:0 > 0
		let newprog = input("Make program: ", &l:makeprg, 'shellcmd')
		if strlen(newprog)
			let &l:makeprg = newprog
			return
		endif
		let yn = confirm('Reset to default?', "&Yes\n&No", 2)
		if yn != 1
			return
		endif
	endif
	let found = s:FindFromModeline()
	if len(found)
		let prog = found[0]
		let prefix = get(found,1,0)
	else
		if exists('g:override_makeprg_{&ft}')
			let from = g:override_makeprg_{&ft}
		elseif exists('s:override_{&ft}')
			let from = s:override_{&ft}
		endif
		if !exists('l:from') " && exists('*service#Exists')
			let fn = expand('%:p')
			let svcs = []
			for pat in ['^/etc/\([^/]\+\)/', '\([^/]\+\)\.conf']
				let m = matchlist(fn, pat)
				if !len(m)
					continue
				endif
				let svc = m[1]
				if strlen(service#Exists(svc))
					let from = {}
					let from.makeprg = 'call service#Do("'.svc.'","reload")'
					let from.prefix = 0
					let b:makeprg_is_cmd = 1
					break
				endif
			endfor
		endif
		if !exists('l:from')
			let from = { 'makeprg': &ft, 'prefix': 1 }
		endif
		let prog = get(from, 'makeprg', &ft)
		let prefix = get(from, 'prefix', 1)
	endif
	let &l:makeprg = prog
	if prefix
		let &l:makeprg .= ' %:p'
	endif
endfun

fun! RunMake()
	if !strlen(&l:makeprg)
		return
	endif
	if exists('b:makeprg_is_cmd')
		exe &l:makeprg
		return
	endif
	make
endfun

aug SetupMakePrg
	au!<commit_msg>makeprg: better prefix handling<commit_after>if exists('g:loaded_setup_makeprg')
	finish
endif

let s:override_c = { 'makeprg': 'gcc %:p -o %:p:r && %:p:r' }
let s:override_cpp = { 'makeprg': 'g++ %:p -o %:p:r && %:p:r' }
let s:override_pl = { 'makeprg': 'perl' }

fun! s:FindFromModeline(...)
	let maxsearch = a:0 ? a:1 : 10
	let mx = line('$')
	if mx
		let lines = getline(1, min([mx, maxsearch]))
		if mx > maxsearch
			call extend(lines, reverse(getline(max([maxsearch, mx-maxsearch]), mx)))
		endif
		for line in lines
			let m = matchlist(line, 'makeprg=\(.*\)$')
			if len(m)
				return [m[1]]
			endif
		endfor
	endif
	return []
endfun

fun! SetupMakePrg(...)
	if a:0 > 0
		let newprog = input("Make program: ", &l:makeprg, 'shellcmd')
		if strlen(newprog)
			let &l:makeprg = newprog
			return
		endif
		let yn = confirm('Reset to default?', "&Yes\n&No", 2)
		if yn != 1
			return
		endif
	endif
	let found = s:FindFromModeline()
	if len(found)
		let prog = found[0]
		let prefix = get(found,1,len(matchlist(prog,'%'))?0:1)
	else
		if exists('g:override_makeprg_{&ft}')
			let from = g:override_makeprg_{&ft}
		elseif exists('s:override_{&ft}')
			let from = s:override_{&ft}
		endif
		if !exists('l:from') " && exists('*service#Exists')
			let fn = expand('%:p')
			let svcs = []
			for pat in ['^/etc/\([^/]\+\)/', '\([^/]\+\)\.conf']
				let m = matchlist(fn, pat)
				if !len(m)
					continue
				endif
				let svc = m[1]
				if strlen(service#Exists(svc))
					let from = {}
					let from.makeprg = 'call service#Do("'.svc.'","reload")'
					let from.prefix = 0
					let b:makeprg_is_cmd = 1
					break
				endif
			endfor
		endif
		if !exists('l:from')
			let from = { 'makeprg': &ft, 'prefix': 1 }
		endif
		let prog = get(from, 'makeprg', &ft)
		let prefix = get(from, 'prefix', len(matchlist(prog, '%')) ? 0 : 1)
	endif
	let &l:makeprg = prog
	if prefix
		let &l:makeprg .= ' %:p'
	endif
endfun

fun! RunMake()
	if !strlen(&l:makeprg)
		return
	endif
	if exists('b:makeprg_is_cmd')
		exe &l:makeprg
		return
	endif
	make
endfun

aug SetupMakePrg
	au!
{'fraction_non_alphanumeric': 0.18237454100367198, 'fraction_numerical': 0.009791921664626682, 'mean_word_length': 3.305584826132771, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5910965', 'n_tokens_mistral': 1926, 'n_tokens_neox': 1698, 'n_words': 428}