vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

spec.vim (5830B)


      1 " Plugin to update the %changelog section of RPM spec files
      2 " Filename: spec.vim
      3 " Maintainer: Igor Gnatenko i.gnatenko.brain@gmail.com
      4 " Former Maintainer: Gustavo Niemeyer <niemeyer@conectiva.com> (until March 2014)
      5 " Last Change: 2015 Jun 01
      6 "  Update by Zdenek Dohnal, 2022 May 17
      7 "  2024 Sep 10 by Vim Project: add epoch support for spec changelog, #15537
      8 "  2024 Oct 07 by Vim Project: add comment support, #15817
      9 "  2026 May 25 by Vim Project: drop the python GetRelVer() func
     10 
     11 if exists("b:did_ftplugin")
     12 	finish
     13 endif
     14 let b:did_ftplugin = 1
     15 
     16 let s:cpo_save = &cpo
     17 set cpo&vim
     18 
     19 setlocal comments=b:#
     20 setlocal commentstring=#\ %s
     21 
     22 let b:undo_ftplugin = "setlocal comments< commentstring<"
     23 
     24 if !exists("no_plugin_maps") && !exists("no_spec_maps")
     25 	if !hasmapto("<Plug>SpecChangelog")
     26 		map <buffer> <LocalLeader>c <Plug>SpecChangelog
     27 	endif
     28 endif
     29 
     30 if !hasmapto("call <SID>SpecChangelog(\"\")<CR>")
     31 	noremap <buffer> <unique> <script> <Plug>SpecChangelog :call <SID>SpecChangelog("")<CR>
     32 endif
     33 
     34 if !exists("*s:SpecChangelog")
     35 	function s:SpecChangelog(format)
     36 		if strlen(a:format) == 0
     37 			if !exists("g:spec_chglog_format")
     38 				let email = input("Name <email address>: ")
     39 				let g:spec_chglog_format = "%a %b %d %Y " . l:email
     40 				echo "\r"
     41 			endif
     42 			let format = g:spec_chglog_format
     43 		else
     44 			if !exists("g:spec_chglog_format")
     45 				let g:spec_chglog_format = a:format
     46 			endif
     47 			let format = a:format
     48 		endif
     49 		let line = 0
     50 		let name = ""
     51 		let epoch = ""
     52 		let ver = ""
     53 		let rel = ""
     54 		let nameline = -1
     55 		let epochline = -1
     56 		let verline = -1
     57 		let relline = -1
     58 		let chgline = -1
     59 		while (line <= line("$"))
     60 			let linestr = getline(line)
     61 			if name == "" && linestr =~? '^Name:'
     62 				let nameline = line
     63 				let name = substitute(strpart(linestr,5), '^[	 ]*\([^ 	]\+\)[		]*$','\1','')
     64 			elseif epoch == "" && linestr =~? '^Epoch:'
     65 				let epochline = line
     66 				let epoch = substitute(strpart(linestr,6), '^[     ]*\([^         ]\+\)[          ]*$','\1','')
     67 			elseif ver == "" && linestr =~? '^Version:'
     68 				let verline = line
     69 				let ver = substitute(strpart(linestr,8), '^[	 ]*\([^ 	]\+\)[		]*$','\1','')
     70 			elseif rel == "" && linestr =~? '^Release:'
     71 				let relline = line
     72 				let rel = substitute(strpart(linestr,8), '^[	 ]*\([^ 	]\+\)[		]*$','\1','')
     73 			elseif linestr =~? '^%changelog'
     74 				let chgline = line
     75 				execute line
     76 				break
     77 			endif
     78 			let line = line+1
     79 		endwhile
     80 		if nameline != -1 && verline != -1 && relline != -1
     81 			let include_release_info = exists("g:spec_chglog_release_info")
     82 			let name = s:ParseRpmVars(name, nameline)
     83 			let epoch = s:ParseRpmVars(epoch, epochline)
     84 			let ver = s:ParseRpmVars(ver, verline)
     85 			let rel = s:ParseRpmVars(rel, relline)
     86 		else
     87 			let include_release_info = 0
     88 		endif
     89 
     90 		if chgline == -1
     91 			let option = confirm("Can't find %changelog. Create one? ","&End of file\n&Here\n&Cancel",3)
     92 			if option == 1
     93 				call append(line("$"),"")
     94 				call append(line("$"),"%changelog")
     95 				execute line("$")
     96 				let chgline = line(".")
     97 			elseif option == 2
     98 				call append(line("."),"%changelog")
     99 				normal j
    100 				let chgline = line(".")
    101 			endif
    102 		endif
    103 		if chgline != -1
    104 			let tmptime = v:lc_time
    105 			language time C
    106 			if strlen(epoch)
    107 				let ver = epoch.":".ver
    108 			endif
    109 			let parsed_format = "* ".strftime(format)." - ".ver."-".rel
    110 			execute "language time" tmptime
    111 			let release_info = "+ ".name."-".ver."-".rel
    112 			let wrong_format = 0
    113 			let wrong_release = 0
    114 			let insert_line = 0
    115 			if getline(chgline+1) != parsed_format
    116 				let wrong_format = 1
    117 			endif
    118 			if include_release_info && getline(chgline+2) != release_info
    119 				let wrong_release = 1
    120 			endif
    121 			if wrong_format || wrong_release
    122 				if include_release_info && !wrong_release && !exists("g:spec_chglog_never_increase_release")
    123 					let option = confirm("Increase release? ","&Yes\n&No",1)
    124 					if option == 1
    125 						execute relline
    126 						normal 
    127 						let rel = substitute(strpart(getline(relline),8), '^[	 ]*\([^ 	]\+\)[		]*$','\1','')
    128 						let release_info = "+ ".name."-".ver."-".rel
    129 					endif
    130 				endif
    131 				let n = 0
    132 				call append(chgline+n, parsed_format)
    133 				if include_release_info
    134 					let n = n + 1
    135 					call append(chgline+n, release_info)
    136 				endif
    137 				let n = n + 1
    138 				call append(chgline+n,"- ")
    139 				let n = n + 1
    140 				call append(chgline+n,"")
    141 				let insert_line = chgline+n
    142 			else
    143 				let line = chgline
    144 				if !exists("g:spec_chglog_prepend")
    145 					while !(getline(line+2) =~ '^\( *\|\*.*\)$')
    146 						let line = line+1
    147 					endwhile
    148 				endif
    149 				call append(line+1,"- ")
    150 				let insert_line = line+2
    151 			endif
    152 			execute insert_line
    153 			startinsert!
    154 		endif
    155 	endfunction
    156 endif
    157 
    158 if !exists("*s:ParseRpmVars")
    159     function s:ParseRpmVars(str, strline)
    160 	let end = -1
    161 	let ret = ""
    162 	while (1)
    163 		let start = match(a:str, "\%{", end+1)
    164 		if start == -1
    165 			let ret = ret . strpart(a:str, end+1)
    166 			break
    167 		endif
    168 		let ret = ret . strpart(a:str, end+1, start-(end+1))
    169 		let end = match(a:str, "}", start)
    170 		if end == -1
    171 			let ret = ret . strpart(a:str, start)
    172 			break
    173 		endif
    174 		let varname = strpart(a:str, start+2, end-(start+2))
    175 		execute a:strline
    176 		let definestr = "^[ \t]*%\\(define\\|global\\)[ \t]\\+".varname."[ \t]\\+\\(.*\\)$"
    177 		let linenum = search(definestr, "bW")
    178 		if linenum != 0
    179 			let ret = ret .  substitute(getline(linenum), definestr, "\\2", "")
    180 		endif
    181 	endwhile
    182 	return ret
    183     endfunction
    184 endif
    185 
    186 " The following lines, along with the macros/matchit.vim plugin,
    187 " make it easy to navigate the different sections of a spec file
    188 " with the % key (thanks to Max Ischenko).
    189 
    190 let b:match_ignorecase = 0
    191 let b:match_words =
    192   \ '^Name:^%description:^%clean:^%(?:auto)?setup:^%build:^%install:^%files:' .
    193   \ '^%package:^%preun:^%postun:^%changelog'
    194 
    195 let &cpo = s:cpo_save
    196 unlet s:cpo_save
    197 
    198 let b:undo_ftplugin ..= " | unlet! b:match_ignorecase b:match_words"