Karsten Hopp 50ff73
" Plugin to update the %changelog section of RPM spec files
Karsten Hopp 50ff73
" Filename: spec.vim
Karsten Hopp 50ff73
" Maintainer: Igor Gnatenko i.gnatenko.brain@gmail.com
Karsten Hopp 50ff73
" Former Maintainer: Gustavo Niemeyer <niemeyer@conectiva.com> (until March 2014)
Karsten Hopp 50ff73
" Last Change: Mon Jun 01 21:15 MSK 2015 Igor Gnatenko
Karsten Hopp 50ff73
Karsten Hopp 50ff73
if exists("b:did_ftplugin")
Karsten Hopp 50ff73
	finish
Karsten Hopp 50ff73
endif
Karsten Hopp 50ff73
let b:did_ftplugin = 1
Karsten Hopp 50ff73
Karsten Hopp 50ff73
let s:cpo_save = &cpo
Karsten Hopp 50ff73
set cpo&vim
Karsten Hopp 50ff73
Karsten Hopp 50ff73
if !exists("no_plugin_maps") && !exists("no_spec_maps")
Karsten Hopp 50ff73
	if !hasmapto("<Plug>SpecChangelog")
Karsten Hopp 50ff73
		map <buffer> <LocalLeader>c <Plug>SpecChangelog
Karsten Hopp 50ff73
	endif
Karsten Hopp 50ff73
endif
Karsten Hopp 50ff73
Karsten Hopp 50ff73
if !hasmapto("call <SID>SpecChangelog(\"\")<CR>")
Karsten Hopp 50ff73
       noremap <buffer> <unique> <script> <Plug>SpecChangelog :call <SID>SpecChangelog("")<CR>
Karsten Hopp 50ff73
endif
Karsten Hopp 50ff73
Karsten Hopp 50ff73
if !exists("*s:GetRelVer")
Karsten Hopp 50ff73
	function! s:GetRelVer()
Karsten Hopp 50ff73
		if has('python')
Karsten Hopp 50ff73
python << PYEND
Karsten Hopp 50ff73
import sys, datetime, shutil, tempfile
Karsten Hopp 50ff73
import vim
Karsten Hopp 50ff73
Karsten Hopp 50ff73
try:
Karsten Hopp 50ff73
    import rpm
Karsten Hopp 50ff73
except ImportError:
Karsten Hopp 50ff73
    pass
Karsten Hopp 50ff73
else:
Karsten Hopp 50ff73
    specfile = vim.current.buffer.name
Karsten Hopp 50ff73
    if specfile:
Karsten Hopp 50ff73
        rpm.delMacro("dist")
Karsten Hopp 50ff73
        spec = rpm.spec(specfile)
Karsten Hopp 50ff73
        headers = spec.sourceHeader
Karsten Hopp 50ff73
        version = headers["Version"]
Karsten Hopp 50ff73
        release = headers["Release"]
Karsten Hopp 50ff73
        vim.command("let ver = " + version)
Karsten Hopp 50ff73
        vim.command("let rel = " + release)
Karsten Hopp 50ff73
PYEND
Karsten Hopp 50ff73
		endif
Karsten Hopp 50ff73
	endfunction
Karsten Hopp 50ff73
endif
Karsten Hopp 50ff73
Karsten Hopp 50ff73
if !exists("*s:SpecChangelog")
Karsten Hopp 50ff73
	function s:SpecChangelog(format)
Karsten Hopp 50ff73
		if strlen(a:format) == 0
Karsten Hopp 50ff73
			if !exists("g:spec_chglog_format")
Karsten Hopp 50ff73
				let email = input("Name <email address>: ")
Karsten Hopp 50ff73
				let g:spec_chglog_format = "%a %b %d %Y " . l:email
Karsten Hopp 50ff73
				echo "\r"
Karsten Hopp 50ff73
			endif
Karsten Hopp 50ff73
			let format = g:spec_chglog_format
Karsten Hopp 50ff73
		else
Karsten Hopp 50ff73
			if !exists("g:spec_chglog_format")
Karsten Hopp 50ff73
				let g:spec_chglog_format = a:format
Karsten Hopp 50ff73
			endif
Karsten Hopp 50ff73
			let format = a:format
Karsten Hopp 50ff73
		endif
Karsten Hopp 50ff73
		let line = 0
Karsten Hopp 50ff73
		let name = ""
Karsten Hopp 50ff73
		let ver = ""
Karsten Hopp 50ff73
		let rel = ""
Karsten Hopp 50ff73
		let nameline = -1
Karsten Hopp 50ff73
		let verline = -1
Karsten Hopp 50ff73
		let relline = -1
Karsten Hopp 50ff73
		let chgline = -1
Karsten Hopp 50ff73
		while (line <= line("$"))
Karsten Hopp 50ff73
			let linestr = getline(line)
Karsten Hopp 50ff73
			if (name == "" && linestr =~? '^Name:')
Karsten Hopp 50ff73
				let nameline = line
Karsten Hopp 50ff73
				let name = substitute(strpart(linestr,5), '^[	 ]*\([^ 	]\+\)[		]*$','\1','')
Karsten Hopp 50ff73
			elseif (ver == "" && linestr =~? '^Version:')
Karsten Hopp 50ff73
				let verline = line
Karsten Hopp 50ff73
				let ver = substitute(strpart(linestr,8), '^[	 ]*\([^ 	]\+\)[		]*$','\1','')
Karsten Hopp 50ff73
			elseif (rel == "" && linestr =~? '^Release:')
Karsten Hopp 50ff73
				let relline = line
Karsten Hopp 50ff73
				let rel = substitute(strpart(linestr,8), '^[	 ]*\([^ 	]\+\)[		]*$','\1','')
Karsten Hopp 50ff73
			elseif (linestr =~? '^%changelog')
Karsten Hopp 50ff73
				let chgline = line
Karsten Hopp 50ff73
				execute line
Karsten Hopp 50ff73
				break
Karsten Hopp 50ff73
			endif
Karsten Hopp 50ff73
			let line = line+1
Karsten Hopp 50ff73
		endwhile
Karsten Hopp 50ff73
		if (nameline != -1 && verline != -1 && relline != -1)
Karsten Hopp 50ff73
			let include_release_info = exists("g:spec_chglog_release_info")
Karsten Hopp 50ff73
			let name = s:ParseRpmVars(name, nameline)
Karsten Hopp 50ff73
			let ver = s:ParseRpmVars(ver, verline)
Karsten Hopp 50ff73
			let rel = s:ParseRpmVars(rel, relline)
Karsten Hopp 50ff73
		else
Karsten Hopp 50ff73
			let include_release_info = 0
Karsten Hopp 50ff73
		endif
Karsten Hopp 50ff73
Karsten Hopp 50ff73
		call s:GetRelVer()
Karsten Hopp 50ff73
Karsten Hopp 50ff73
		if (chgline == -1)
Karsten Hopp 50ff73
			let option = confirm("Can't find %changelog. Create one? ","&End of file\n&Here\n&Cancel",3)
Karsten Hopp 50ff73
			if (option == 1)
Karsten Hopp 50ff73
				call append(line("$"),"")
Karsten Hopp 50ff73
				call append(line("$"),"%changelog")
Karsten Hopp 50ff73
				execute line("$")
Karsten Hopp 50ff73
				let chgline = line(".")
Karsten Hopp 50ff73
			elseif (option == 2)
Karsten Hopp 50ff73
				call append(line("."),"%changelog")
Karsten Hopp 50ff73
				normal j
Karsten Hopp 50ff73
				chgline = line(".")
Karsten Hopp 50ff73
			endif
Karsten Hopp 50ff73
		endif
Karsten Hopp 50ff73
		if (chgline != -1)
Karsten Hopp 50ff73
			let tmptime = v:lc_time
Karsten Hopp 50ff73
			language time C
Karsten Hopp 50ff73
			let parsed_format = "* ".strftime(format)." - ".ver."-".rel
Karsten Hopp 50ff73
			execute "language time" tmptime
Karsten Hopp 50ff73
			let release_info = "+ ".name."-".ver."-".rel
Karsten Hopp 50ff73
			let wrong_format = 0
Karsten Hopp 50ff73
			let wrong_release = 0
Karsten Hopp 50ff73
			let insert_line = 0
Karsten Hopp 50ff73
			if (getline(chgline+1) != parsed_format)
Karsten Hopp 50ff73
				let wrong_format = 1
Karsten Hopp 50ff73
			endif
Karsten Hopp 50ff73
			if (include_release_info && getline(chgline+2) != release_info)
Karsten Hopp 50ff73
				let wrong_release = 1
Karsten Hopp 50ff73
			endif
Karsten Hopp 50ff73
			if (wrong_format || wrong_release)
Karsten Hopp 50ff73
				if (include_release_info && !wrong_release && !exists("g:spec_chglog_never_increase_release"))
Karsten Hopp 50ff73
					let option = confirm("Increase release? ","&Yes\n&No",1)
Karsten Hopp 50ff73
					if (option == 1)
Karsten Hopp 50ff73
						execute relline
Karsten Hopp 50ff73
						normal ?
Karsten Hopp 50ff73
						let rel = substitute(strpart(getline(relline),8), '^[	 ]*\([^ 	]\+\)[		]*$','\1','')
Karsten Hopp 50ff73
						let release_info = "+ ".name."-".ver."-".rel
Karsten Hopp 50ff73
					endif
Karsten Hopp 50ff73
				endif
Karsten Hopp 50ff73
				let n = 0
Karsten Hopp 50ff73
				call append(chgline+n, parsed_format)
Karsten Hopp 50ff73
				if include_release_info
Karsten Hopp 50ff73
					let n = n + 1
Karsten Hopp 50ff73
					call append(chgline+n, release_info)
Karsten Hopp 50ff73
				endif
Karsten Hopp 50ff73
				let n = n + 1
Karsten Hopp 50ff73
				call append(chgline+n,"- ")
Karsten Hopp 50ff73
				let n = n + 1
Karsten Hopp 50ff73
				call append(chgline+n,"")
Karsten Hopp 50ff73
				let insert_line = chgline+n
Karsten Hopp 50ff73
			else
Karsten Hopp 50ff73
				let line = chgline
Karsten Hopp 50ff73
				if !exists("g:spec_chglog_prepend")
Karsten Hopp 50ff73
					while !(getline(line+2) =~ '^\( *\|\*.*\)$')
Karsten Hopp 50ff73
						let line = line+1
Karsten Hopp 50ff73
					endwhile
Karsten Hopp 50ff73
				endif
Karsten Hopp 50ff73
				call append(line+1,"- ")
Karsten Hopp 50ff73
				let insert_line = line+2
Karsten Hopp 50ff73
			endif
Karsten Hopp 50ff73
			execute insert_line
Karsten Hopp 50ff73
			startinsert!
Karsten Hopp 50ff73
		endif
Karsten Hopp 50ff73
	endfunction
Karsten Hopp 50ff73
endif
Karsten Hopp 50ff73
Karsten Hopp 50ff73
if !exists("*s:ParseRpmVars")
Karsten Hopp 50ff73
    function s:ParseRpmVars(str, strline)
Karsten Hopp 50ff73
	let end = -1
Karsten Hopp 50ff73
	let ret = ""
Karsten Hopp 50ff73
	while (1)
Karsten Hopp 50ff73
		let start = match(a:str, "\%{", end+1)
Karsten Hopp 50ff73
		if (start == -1)
Karsten Hopp 50ff73
			let ret = ret . strpart(a:str, end+1)
Karsten Hopp 50ff73
			break
Karsten Hopp 50ff73
		endif
Karsten Hopp 50ff73
		let ret = ret . strpart(a:str, end+1, start-(end+1))
Karsten Hopp 50ff73
		let end = match(a:str, "}", start)
Karsten Hopp 50ff73
		if (end == -1)
Karsten Hopp 50ff73
			let ret = ret . strpart(a:str, start)
Karsten Hopp 50ff73
			break
Karsten Hopp 50ff73
		endif
Karsten Hopp 50ff73
		let varname = strpart(a:str, start+2, end-(start+2))
Karsten Hopp 50ff73
		execute a:strline
Karsten Hopp 50ff73
		let definestr = "^[ \t]*%(?:global|define)[ \t]\\+" . varname . "[ \t]\\+\\(.*\\)$"
Karsten Hopp 50ff73
		let linenum = search(definestr, "bW")
Karsten Hopp 50ff73
		if (linenum != -1)
Karsten Hopp 50ff73
			let ret = ret .  substitute(getline(linenum), definestr, "\\1", "")
Karsten Hopp 50ff73
		else
Karsten Hopp 50ff73
			let ret = ret . strpart(str, start, end+1-start)
Karsten Hopp 50ff73
		endif
Karsten Hopp 50ff73
	endwhile
Karsten Hopp 50ff73
	return ret
Karsten Hopp 50ff73
    endfunction
Karsten Hopp 50ff73
endif
Karsten Hopp 50ff73
Karsten Hopp 50ff73
" The following lines, along with the macros/matchit.vim plugin,
Karsten Hopp 50ff73
" make it easy to navigate the different sections of a spec file
Karsten Hopp 50ff73
" with the % key (thanks to Max Ischenko).
Karsten Hopp 50ff73
Karsten Hopp 50ff73
let b:match_ignorecase = 0
Karsten Hopp 50ff73
let b:match_words =
Karsten Hopp 50ff73
  \ '^Name:^%description:^%clean:^%(?:auto)?setup:^%build:^%install:^%files:' .
Karsten Hopp 50ff73
  \ '^%package:^%preun:^%postun:^%changelog'
Karsten Hopp 50ff73
Karsten Hopp 50ff73
let &cpo = s:cpo_save
Karsten Hopp 50ff73
unlet s:cpo_save
Karsten Hopp 50ff73
Karsten Hopp 50ff73
let b:undo_ftplugin = "unlet! b:match_ignorecase b:match_words"