codekingpro/portable-devtools
114k
1" Vim filetype plugin file (GUI menu, folding and completion)2" Language: Debian Changelog3" Maintainer: Debian Vim Maintainers <team+vim@tracker.debian.org>4" Former Maintainers: Michael Piefel <piefel@informatik.hu-berlin.de>5" Stefano Zacchiroli <zack@debian.org>6" Last Change: 2023 Aug 187" License: Vim License8" URL: https://salsa.debian.org/vim-team/vim-debian/blob/main/ftplugin/debchangelog.vim9 10" Bug completion requires apt-listbugs installed for Debian packages or11" python-launchpadlib installed for Ubuntu packages12 13if exists('b:did_ftplugin')14 finish15endif16let b:did_ftplugin=117 18" {{{1 Local settings (do on every load)19if exists('g:debchangelog_fold_enable')20 setlocal foldmethod=expr21 setlocal foldexpr=DebGetChangelogFold(v:lnum)22 setlocal foldtext=DebChangelogFoldText()23endif24 25" Debian changelogs are not supposed to have any other text width,26" so the user cannot override this setting27setlocal tw=7828setlocal comments=f:* 29 30" Clean unloading31let b:undo_ftplugin = 'setlocal tw< comments< foldmethod< foldexpr< foldtext<'32" }}}133 34if exists('g:did_changelog_ftplugin')35 finish36endif37 38" Don't load another plugin (this is global)39let g:did_changelog_ftplugin = 140 41" Make sure the '<' and 'C' flags are not included in 'cpoptions', otherwise42" <CR> would not be recognized. See ":help 'cpoptions'".43let s:cpo_save = &cpo44set cpo&vim45 46" {{{1 GUI menu47 48" Helper functions returning various data.49" Returns full name, either from $DEBFULLNAME or debianfullname.50" TODO Is there a way to determine name from anywhere else?51function <SID>FullName()52 if exists('$DEBFULLNAME')53 return $DEBFULLNAME54 elseif exists('g:debianfullname')55 return g:debianfullname56 else57 return 'Your Name'58 endif59endfunction60 61" Returns email address, from $DEBEMAIL, $EMAIL or debianemail.62function <SID>Email()63 if exists('$DEBEMAIL')64 return $DEBEMAIL65 elseif exists('$EMAIL')66 return $EMAIL67 elseif exists('g:debianemail')68 return g:debianemail69 else70 return 'your@email.address'71 endif72endfunction73 74" Returns date in RFC822 format.75function <SID>Date()76 let savelang = v:lc_time77 execute 'language time C'78 let dateandtime = strftime('%a, %d %b %Y %X %z')79 execute 'language time ' . savelang80 return dateandtime81endfunction82 83function <SID>WarnIfNotUnfinalised()84 if match(getline('.'), ' -- [[:alpha:]][[:alnum:].]')!=-185 echohl WarningMsg86 echo 'The entry has not been unfinalised before editing.'87 echohl None88 return 189 endif90 return 091endfunction92 93function <SID>Finalised()94 let savelinenum = line('.')95 196 call search('^ -- ')97 if match(getline('.'), ' -- [[:alpha:]][[:alnum:].]')!=-198 let returnvalue = 199 else100 let returnvalue = 0101 endif102 execute savelinenum103 return returnvalue104endfunction105 106" These functions implement the menus107function NewVersion()108 " The new entry is unfinalised and shall be changed109 amenu disable &Changelog.&New\ Version110 amenu enable &Changelog.&Add\ Entry111 amenu enable &Changelog.&Close\ Bug112 amenu enable &Changelog.Set\ &Distribution113 amenu enable &Changelog.Set\ &Urgency114 amenu disable &Changelog.U&nfinalise115 amenu enable &Changelog.&Finalise116 call append(0, substitute(getline(1), '-\([[:digit:]]\+\))', '-$$\1)', ''))117 call append(1, '')118 call append(2, '')119 call append(3, ' -- ')120 call append(4, '')121 call Urgency('low')122 normal! 1G0123 call search(')')124 normal! h125 " ':normal' doesn't support key annotation (<c-a>) directly.126 " Vim's manual recommends using ':exe' to use key annotation indirectly (backslash-escaping needed though).127 exe "normal! \<c-a>"128 call setline(1, substitute(getline(1), '-\$\$', '-', ''))129 if exists('g:debchangelog_fold_enable')130 foldopen131 endif132 call AddEntry()133endfunction134 135function AddEntry()136 1137 call search('^ -- ')138 .-2139 call append('.', ' * ')140 .+3141 let warn=<SID>WarnIfNotUnfinalised()142 .-2143 if warn144 echohl MoreMsg145 call input('Hit ENTER')146 echohl None147 endif148 startinsert!149endfunction150 151function CloseBug()152 1153 call search('^ -- ')154 let warn=<SID>WarnIfNotUnfinalised()155 .-2156 call append('.', ' * (closes: #' . input('Bug number to close: ') . ')')157 normal! j^ll158 startinsert159endfunction160 161function Distribution(dist)162 call setline(1, substitute(getline(1), ') *\%(UNRELEASED\|\l\+\);', ') ' . a:dist . ';', ''))163endfunction164 165function Urgency(urg)166 call setline(1, substitute(getline(1), 'urgency=.*$', 'urgency=' . a:urg, ''))167endfunction168 169function <SID>UnfinaliseMenu()170 " This means the entry shall be changed171 amenu disable &Changelog.&New\ Version172 amenu enable &Changelog.&Add\ Entry173 amenu enable &Changelog.&Close\ Bug174 amenu enable &Changelog.Set\ &Distribution175 amenu enable &Changelog.Set\ &Urgency176 amenu disable &Changelog.U&nfinalise177 amenu enable &Changelog.&Finalise178endfunction179 180function Unfinalise()181 call <SID>UnfinaliseMenu()182 1183 call search('^ -- ')184 call setline('.', ' -- ')185endfunction186 187function <SID>FinaliseMenu()188 " This means the entry should not be changed anymore189 amenu enable &Changelog.&New\ Version190 amenu disable &Changelog.&Add\ Entry191 amenu disable &Changelog.&Close\ Bug192 amenu disable &Changelog.Set\ &Distribution193 amenu disable &Changelog.Set\ &Urgency194 amenu enable &Changelog.U&nfinalise195 amenu disable &Changelog.&Finalise196endfunction197 198function Finalise()199 call <SID>FinaliseMenu()200 1201 call search('^ -- ')202 call setline('.', ' -- ' . <SID>FullName() . ' <' . <SID>Email() . '> ' . <SID>Date())203endfunction204 205 206function <SID>MakeMenu()207 amenu &Changelog.&New\ Version :call NewVersion()<CR>208 amenu &Changelog.&Add\ Entry :call AddEntry()<CR>209 amenu &Changelog.&Close\ Bug :call CloseBug()<CR>210 menu &Changelog.-sep- <nul>211 212 amenu &Changelog.Set\ &Distribution.&unstable :call Distribution("unstable")<CR>213 amenu &Changelog.Set\ &Distribution.&frozen :call Distribution("frozen")<CR>214 amenu &Changelog.Set\ &Distribution.&stable :call Distribution("stable")<CR>215 menu &Changelog.Set\ &Distribution.-sep- <nul>216 amenu &Changelog.Set\ &Distribution.frozen\ unstable :call Distribution("frozen unstable")<CR>217 amenu &Changelog.Set\ &Distribution.stable\ unstable :call Distribution("stable unstable")<CR>218 amenu &Changelog.Set\ &Distribution.stable\ frozen :call Distribution("stable frozen")<CR>219 amenu &Changelog.Set\ &Distribution.stable\ frozen\ unstable :call Distribution("stable frozen unstable")<CR>220 221 amenu &Changelog.Set\ &Urgency.&low :call Urgency("low")<CR>222 amenu &Changelog.Set\ &Urgency.&medium :call Urgency("medium")<CR>223 amenu &Changelog.Set\ &Urgency.&high :call Urgency("high")<CR>224 225 menu &Changelog.-sep- <nul>226 amenu &Changelog.U&nfinalise :call Unfinalise()<CR>227 amenu &Changelog.&Finalise :call Finalise()<CR>228 229 if <SID>Finalised()230 call <SID>FinaliseMenu()231 else232 call <SID>UnfinaliseMenu()233 endif234endfunction235 236augroup changelogMenu237au BufEnter * if &filetype == "debchangelog" | call <SID>MakeMenu() | endif238au BufLeave * if &filetype == "debchangelog" | silent! aunmenu &Changelog | endif239augroup END240 241" }}}242" {{{1 folding243 244" look for an author name in the [zonestart zoneend] lines searching backward245function! s:getAuthor(zonestart, zoneend)246 let linepos = a:zoneend247 while linepos >= a:zonestart248 let line = getline(linepos)249 if line =~# '^ --'250 return substitute(line, '^ --\s*\([^<]\+\)\s*.*', '\1', '')251 endif252 let linepos -= 1253 endwhile254 return '[unknown]'255endfunction256 257" Look for a package source name searching backward from the givenline and258" returns it. Return the empty string if the package name can't be found259function! DebGetPkgSrcName(lineno)260 let lineidx = a:lineno261 let pkgname = ''262 while lineidx > 0263 let curline = getline(lineidx)264 if curline =~# '^\S'265 let pkgname = matchlist(curline, '^\(\S\+\).*$')[1]266 break267 endif268 let lineidx = lineidx - 1269 endwhile270 return pkgname271endfunction272 273function! DebChangelogFoldText()274 if v:folddashes ==# '-' " changelog entry fold275 return foldtext() . ' -- ' . s:getAuthor(v:foldstart, v:foldend) . ' '276 endif277 return foldtext()278endfunction279 280function! DebGetChangelogFold(lnum)281 let line = getline(a:lnum)282 if line =~# '^\w\+'283 return '>1' " beginning of a changelog entry284 endif285 if line =~# '^\s\+\[.*\]'286 return '>2' " beginning of an author-specific chunk287 endif288 if line =~# '^ --'289 return '1'290 endif291 return '='292endfunction293 294if exists('g:debchangelog_fold_enable')295 silent! foldopen! " unfold the entry the cursor is on (usually the first one)296endif297 298" }}}299 300" {{{1 omnicompletion for Closes: #301 302if !exists('g:debchangelog_listbugs_severities')303 let g:debchangelog_listbugs_severities = 'critical,grave,serious,important,normal,minor,wishlist'304endif305 306fun! DebCompleteBugs(findstart, base)307 if a:findstart308 let line = getline('.')309 310 " try to detect whether this is closes: or lp:311 let g:debchangelog_complete_mode = 'debbugs'312 let try_colidx = col('.') - 1313 let colidx = -1 " default to no-completion-possible314 315 while try_colidx > 0 && line[try_colidx - 1] =~# '\s\|\d\|#\|,\|:'316 let try_colidx = try_colidx - 1317 if line[try_colidx] ==# '#' && colidx == -1318 " found hash, where we complete from:319 let colidx = try_colidx320 elseif line[try_colidx] ==# ':'321 if try_colidx > 1 && strpart(line, try_colidx - 2, 3) =~? '\clp:'322 let g:debchangelog_complete_mode = 'lp'323 endif324 break325 endif326 endwhile327 return colidx328 else " return matches:329 let bug_lines = []330 if g:debchangelog_complete_mode ==? 'lp'331 if ! has('python')332 echoerr 'vim must be built with Python support to use LP bug completion'333 return334 endif335 let pkgsrc = DebGetPkgSrcName(line('.'))336 python << EOF337import vim338try:339 from launchpadlib.launchpad import Launchpad340 from lazr.restfulclient.errors import HTTPError341 # login anonymously342 lp = Launchpad.login_anonymously('debchangelog.vim', 'production')343 ubuntu = lp.distributions['ubuntu']344 try:345 sp = ubuntu.getSourcePackage(name=vim.eval('pkgsrc'))346 status = ('New', 'Incomplete', 'Confirmed', 'Triaged',347 'In Progress', 'Fix Committed')348 tasklist = sp.searchTasks(status=status, order_by='id')349 liststr = '['350 for task in tasklist:351 bug = task.bug352 liststr += "'#%d - %s'," % (bug.id, bug.title.replace('\'', '\'\''))353 liststr += ']'354 vim.command('silent let bug_lines = %s' % liststr.encode('utf-8'))355 except HTTPError:356 pass357except ImportError:358 vim.command('echoerr \'python-launchpadlib >= 1.5.4 needs to be installed to use Launchpad bug completion\'')359EOF360 else361 if ! filereadable('/usr/sbin/apt-listbugs')362 echoerr 'apt-listbugs not found, you should install it to use Closes bug completion'363 return364 endif365 let pkgsrc = DebGetPkgSrcName(line('.'))366 let listbugs_output = system('/usr/sbin/apt-listbugs -s ' . g:debchangelog_listbugs_severities . ' list ' . pkgsrc . ' | grep "^ #" 2> /dev/null')367 let bug_lines = split(listbugs_output, '\n')368 endif369 let completions = []370 for line in bug_lines371 let parts = matchlist(line, '^\s*\(#\S\+\)\s*-\s*\(.*\)$')372 " filter only those which match a:base:373 if parts[1] !~ '^' . a:base374 continue375 endif376 let completion = {}377 let completion['word'] = parts[1]378 let completion['menu'] = parts[2]379 let completion['info'] = parts[0]380 let completions += [completion]381 endfor382 return completions383 endif384endfun385 386setlocal omnifunc=DebCompleteBugs387 388" }}}389 390" Restore the previous value of 'cpoptions'.391let &cpo = s:cpo_save392unlet s:cpo_save393 394" vim: set foldmethod=marker:395 