cvsdist 1401a3
" Vim filetype plugin
cvsdist 1401a3
" Language:	spec file
cvsdist 1401a3
" Maintainer:	Guillaume Rousse <rousse@ccr.jussieu.fr>
cvsdist 1401a3
" URL:		http://lis.snv.jussieu.fr/~rousse/linux/spec.vim
Karsten Hopp ef334d
" Version:	$Id: spec.vim,v 1.2 2005/02/10 16:45:35 karsten Exp $
cvsdist 1401a3
cvsdist 1401a3
if exists("b:did_ftplugin")
cvsdist 1401a3
	finish
cvsdist 1401a3
endif
cvsdist 1401a3
let b:did_ftplugin = 1
cvsdist 1401a3
cvsdist 1401a3
" Add mappings, unless user doesn't want
cvsdist 1401a3
if !exists("no_plugin_maps") && !exists("no_spec_maps")
cvsdist 1401a3
	if !hasmapto("<Plug>AddChangelogBlock")
cvsdist 1401a3
		map <buffer> <LocalLeader>ch <Plug>AddChangelogBlock
cvsdist 1401a3
	endif
cvsdist 1401a3
	if !hasmapto("<Plug>AddChangelogEntry")
cvsdist 1401a3
		map <buffer> <LocalLeader>CH <Plug>AddChangelogEntry
cvsdist 1401a3
	endif
cvsdist 1401a3
	noremap <buffer> <unique> <script> <Plug>AddChangelogBlock :call <SID>AddChangelogBlock()<CR> 
cvsdist 1401a3
	noremap <buffer> <unique> <script> <Plug>AddChangelogEntry :call <SID>AddChangelogEntry()<CR>
cvsdist 1401a3
endif
cvsdist 1401a3
cvsdist 1401a3
" compilation option
cvsdist 1401a3
setlocal makeprg=rpm\ -ba\ %
cvsdist 1401a3
setlocal errorformat=error:\ line\ %l:\ %m
cvsdist 1401a3
cvsdist 1401a3
" navigation through sections
cvsdist 1401a3
let b:match_ignorecase = 0
cvsdist 1401a3
let b:match_words =
cvsdist 1401a3
	\ '^Name:^%description:^%clean:^%setup:^%build:^%install:^%files:' .
cvsdist 1401a3
	\ '^%package:^%preun:^%postun:^%changelog'
cvsdist 1401a3
cvsdist 1401a3
if !exists("*s:AddChangelogBlock")
cvsdist 1401a3
	" Adds a changelog block
cvsdist 1401a3
	function s:AddChangelogBlock()
cvsdist 1401a3
		" look for changelog section
cvsdist 1401a3
		let line = <SID>GetFirstLocation(0, '^%changelog')
cvsdist 1401a3
		call <SID>InsertChangelogHeader(line)
cvsdist 1401a3
		call <SID>InsertChangelogEntry(line + 1)
cvsdist 1401a3
	endfunction
cvsdist 1401a3
endif
cvsdist 1401a3
cvsdist 1401a3
if !exists("*s:AddChangelogEntry")
cvsdist 1401a3
	" Adds a changelog entry
cvsdist 1401a3
	function s:AddChangelogEntry()
cvsdist 1401a3
		" look for changelog section
cvsdist 1401a3
		let line = <SID>GetFirstLocation(0, '^%changelog')
cvsdist 1401a3
		" look for last entry
cvsdist 1401a3
		let line = <SID>GetLastLocation(line + 1, '^- ')
cvsdist 1401a3
		call <SID>InsertChangelogEntry(line)
cvsdist 1401a3
	endfunction
cvsdist 1401a3
endif
cvsdist 1401a3
cvsdist 1401a3
if !exists("*s:InsertChangelogHeader")
cvsdist 1401a3
	" Insert a changelog header just after the given line
cvsdist 1401a3
	function s:InsertChangelogHeader(line)
cvsdist 1401a3
		" ensure english locale
cvsdist 1401a3
		language time C
cvsdist 1401a3
		" insert blank line first
cvsdist 1401a3
		call append(a:line, "")
cvsdist 1401a3
		" insert changelog header
cvsdist 1401a3
		call append(a:line,
cvsdist 1401a3
			\ "* " . strftime("%a %b %d %Y") . 
cvsdist 1401a3
			\ " " . <SID>GetTagValue("Packager") .
cvsdist 1401a3
			\ " " . <SID>GetTagValue("Version") .
cvsdist 1401a3
			\ "-" . <SID>GetTagValue("Release")
cvsdist 1401a3
			\)
cvsdist 1401a3
	endfunction
cvsdist 1401a3
endif
cvsdist 1401a3
cvsdist 1401a3
if !exists("*s:InsertChangelogEntry")
cvsdist 1401a3
	" Insert a changelog entry just after the given line
cvsdist 1401a3
	function s:InsertChangelogEntry(line)
cvsdist 1401a3
		" insert changelog entry
Karsten Hopp ef334d
		call append(a:line, "-")
cvsdist 1401a3
		" position cursor here
Karsten Hopp ef334d
		execute a:line +1
cvsdist 1401a3
		" enter insert mode
cvsdist 1401a3
		startinsert!
cvsdist 1401a3
	endfunction
cvsdist 1401a3
endif
cvsdist 1401a3
cvsdist 1401a3
if !exists("*s:GetTagValue")
cvsdist 1401a3
	" Return value of a rpm tag
cvsdist 1401a3
	function s:GetTagValue(tag)
cvsdist 1401a3
		let pattern = '^' . a:tag . ':\s*'
cvsdist 1401a3
		let line = <SID>GetFirstLine(0, pattern)
cvsdist 1401a3
		let value = substitute(line, pattern, "", "")
cvsdist 1401a3
cvsdist 1401a3
		" resolve macros
cvsdist 1401a3
		while (value =~ '%{\?\w\{3,}}\?')
cvsdist 1401a3
			let macro = matchstr(value, '%{\?\w\{3,}}\?')
cvsdist 1401a3
			let macro_name = substitute(macro, '%{\?\(\w\{3,}\)}\?', '\1', "")
cvsdist 1401a3
			let macro_value = <SID>GetMacroValue(macro_name)
cvsdist 1401a3
			let value = substitute(value, '%{\?' . macro_name . '}\?', macro_value, "")
cvsdist 1401a3
		endwhile
cvsdist 1401a3
		
cvsdist 1401a3
		" try to read externaly defined values
cvsdist 1401a3
		if (value == "")
cvsdist 1401a3
			let value = <SID>GetExternalMacroValue(a:tag)
cvsdist 1401a3
		endif
cvsdist 1401a3
cvsdist 1401a3
		return value
cvsdist 1401a3
	endfunction
cvsdist 1401a3
endif
cvsdist 1401a3
cvsdist 1401a3
if !exists("*s:GetMacroValue")
cvsdist 1401a3
	" Return value of a rpm macro
cvsdist 1401a3
	function s:GetMacroValue(macro)
cvsdist 1401a3
		let pattern = '^%define\s*' . a:macro . '\s*'
cvsdist 1401a3
		let line = <SID>GetFirstLine(0, pattern)
cvsdist 1401a3
		return substitute(line, pattern, "", "")
cvsdist 1401a3
	endfunction
cvsdist 1401a3
endif
cvsdist 1401a3
cvsdist 1401a3
if !exists("*s:GetExternalMacroValue")
cvsdist 1401a3
	" Return value of an external rpm macro defined in $HOME/.rpmmacros
cvsdist 1401a3
	function s:GetExternalMacroValue(macro)
cvsdist 1401a3
		if filereadable($HOME . "/.rpmmacros")
cvsdist 1401a3
			let pattern = '^%' . tolower(a:macro) . '\s*'
cvsdist 1401a3
			let line = system("grep '" . pattern . "' $HOME/.rpmmacros")
cvsdist 1401a3
			" get rid of this !#&* trailing <NL>
cvsdist 1401a3
			let line = strpart(line, 0, strlen(line) - 1)
cvsdist 1401a3
			return substitute(line, pattern, "", "")
cvsdist 1401a3
		endif
cvsdist 1401a3
	endfunction
cvsdist 1401a3
endif
cvsdist 1401a3
cvsdist 1401a3
if !exists("*s:GetFirstLocation")
cvsdist 1401a3
	" Return location of first line matching the given pattern after the given line
cvsdist 1401a3
	" Return -1 if not found at the end of the file
cvsdist 1401a3
	function s:GetFirstLocation(from, pattern)
cvsdist 1401a3
		let linenb = a:from
cvsdist 1401a3
		while (linenb <= line("$"))
cvsdist 1401a3
			let linenb = linenb + 1
cvsdist 1401a3
			let linestr = getline(linenb)
cvsdist 1401a3
			if (linestr =~ a:pattern)
cvsdist 1401a3
				return linenb
cvsdist 1401a3
			endif
cvsdist 1401a3
		endwhile
cvsdist 1401a3
		return -1
cvsdist 1401a3
	endfunction
cvsdist 1401a3
endif
cvsdist 1401a3
cvsdist 1401a3
if !exists("*s:GetLastLocation")
cvsdist 1401a3
	" Return location of last line matching the given pattern after the given line
cvsdist 1401a3
	" Return -1 if still found at the end of the file
cvsdist 1401a3
	function s:GetLastLocation(from, pattern)
cvsdist 1401a3
		let linenb = a:from
cvsdist 1401a3
		while (linenb <= line("$"))
cvsdist 1401a3
			let linenb = linenb + 1
cvsdist 1401a3
			let linestr = getline(linenb)
cvsdist 1401a3
			if (linestr !~ a:pattern)
cvsdist 1401a3
				return linenb - 1
cvsdist 1401a3
			endif
cvsdist 1401a3
		endwhile
cvsdist 1401a3
		return -1
cvsdist 1401a3
	endfunction
cvsdist 1401a3
endif
cvsdist 1401a3
cvsdist 1401a3
if !exists("*s:GetFirstLine")
cvsdist 1401a3
	" Return first line matching the given pattern after the given line
cvsdist 1401a3
	" Return "" if not found at the end of the file
cvsdist 1401a3
	function s:GetFirstLine(from, pattern)
cvsdist 1401a3
		let linenb = a:from
cvsdist 1401a3
		while (linenb <= line("$"))
cvsdist 1401a3
			let linenb = linenb + 1
cvsdist 1401a3
			let linestr = getline(linenb)
cvsdist 1401a3
			if (linestr =~ a:pattern)
cvsdist 1401a3
				return linestr
cvsdist 1401a3
			endif
cvsdist 1401a3
		endwhile
cvsdist 1401a3
		return ""
cvsdist 1401a3
	endfunction
cvsdist 1401a3
endif