robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
d3509eac6a
master
1" Vim filetype plugin file (GUI menu, folding and completion) 2" Language: Debian Changelog 3" 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: 2026 Jun 29 7" License: Vim License 8" URL: https://salsa.debian.org/vim-team/vim-debian/blob/main/ftplugin/debchangelog.vim 9 10" Bug completion requires apt-listbugs installed for Debian packages or 11" python-launchpadlib installed for Ubuntu packages 12 13if exists ( 'b:did_ftplugin' ) 14finish 15endif 16let b: did_ftplugin = 1 17 18" {{{1 Local settings (do on every load) 19if exists ( 'g:debchangelog_fold_enable' ) 20setlocal foldmethod = expr 21setlocal foldexpr = DebGetChangelogFold (v:lnum) 22setlocal foldtext = DebChangelogFoldText () 23endif 24 25" Debian changelogs are not supposed to have any other text width, 26" so the user cannot override this setting 27setlocal tw = 78 28setlocal comments = f:* 29 30" Clean unloading 31let b: undo_ftplugin = 'setlocal tw< comments< foldmethod< foldexpr< foldtext<' 32" }}}1 33 34if exists ( 'g:did_changelog_ftplugin' ) 35finish 36endif 37 38" Don't load another plugin (this is global) 39let g: did_changelog_ftplugin = 1 40 41" Make sure the '<' and 'C' flags are not included in 'cpoptions', otherwise 42" <CR> would not be recognized. See ":help 'cpoptions'". 43let s: cpo_save = & cpo 44set cpo & vim 45 46" {{{1 GUI menu 47 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 () 52if exists ( '$DEBFULLNAME' ) 53return $ DEBFULLNAME 54elseif exists ( 'g:debianfullname' ) 55return g: debianfullname 56else 57return 'Your Name' 58endif 59endfunction 60 61" Returns email address, from $DEBEMAIL, $EMAIL or debianemail. 62function <SID> () 63if exists ( '$DEBEMAIL' ) 64return $ DEBEMAIL 65elseif exists ( '$EMAIL' ) 66return $ elseif exists ( 'g:debianemail' ) 68return g: debianemail 69else 70return 'your@email.address' 71endif 72endfunction 73 74" Returns date in RFC822 format. 75function <SID> Date () 76let savelang = v: lc_time 77execute 'language time C' 78let dateandtime = strftime ( '%a, %d %b %Y %X %z' ) 79execute 'language time ' . savelang 80return dateandtime 81endfunction 82 83function <SID> WarnIfNotUnfinalised () 84if match ( getline ( '.' ), ' -- [[:alpha:]][[:alnum:].]' ) != -1 85echohl WarningMsg 86echo 'The entry has not been unfinalised before editing.' 87echohl None 88return 1 89endif 90return 0 91endfunction 92 93function <SID> Finalised () 94let savelinenum = line ( '.' ) 951 96call search ( '^ -- ' ) 97if match ( getline ( '.' ), ' -- [[:alpha:]][[:alnum:].]' ) != -1 98let returnvalue = 1 99else 100let returnvalue = 0 101endif 102execute savelinenum 103return returnvalue 104endfunction 105 106" These functions implement the menus 107function NewVersion () 108" The new entry is unfinalised and shall be changed 109amenu disable &Changelog.&New\ Version 110amenu enable &Changelog.&Add\ Entry 111amenu enable &Changelog.&Close\ Bug 112amenu enable &Changelog.Set\ &Distribution 113amenu enable &Changelog.Set\ &Urgency 114amenu disable &Changelog.U&nfinalise 115amenu enable &Changelog.&Finalise 116call append ( 0 , substitute ( getline ( 1 ), '-\([[:digit:]]\+\))' , '-$$\1)' , '' )) 117call append ( 1 , '' ) 118call append ( 2 , '' ) 119call append ( 3 , ' -- ' ) 120call append ( 4 , '' ) 121call Urgency ( 'low' ) 122normal ! 1G0 123call search ( ')' ) 124normal ! h 125" ':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). 127exe "normal! \<c-a>" 128call setline ( 1 , substitute ( getline ( 1 ), '-\$\$' , '-' , '' )) 129if exists ( 'g:debchangelog_fold_enable' ) 130foldopen 131endif 132call AddEntry () 133endfunction 134 135function AddEntry () 1361 137call search ( '^ -- ' ) 138 .-2 139call append ( '.' , ' * ' ) 140 .+3 141let warn = <SID> WarnIfNotUnfinalised () 142 .-2 143if warn 144echohl MoreMsg 145call input ( 'Hit ENTER' ) 146echohl None 147endif 148startinsert ! 149endfunction 150 151function CloseBug () 1521 153call search ( '^ -- ' ) 154let warn = <SID> WarnIfNotUnfinalised () 155 .-2 156call append ( '.' , ' * (closes: #' . input ( 'Bug number to close: ' ) . ')' ) 157normal ! j^ll 158startinsert 159endfunction 160 161function Distribution ( dist ) 162call setline ( 1 , substitute ( getline ( 1 ), ') *\%(UNRELEASED\|\l\+\);' , ') ' . a: dist . ';' , '' )) 163endfunction 164 165function Urgency ( urg ) 166call setline ( 1 , substitute ( getline ( 1 ), 'urgency=.*$' , 'urgency=' . a: urg , '' )) 167endfunction 168 169function <SID> UnfinaliseMenu () 170" This means the entry shall be changed 171amenu disable &Changelog.&New\ Version 172amenu enable &Changelog.&Add\ Entry 173amenu enable &Changelog.&Close\ Bug 174amenu enable &Changelog.Set\ &Distribution 175amenu enable &Changelog.Set\ &Urgency 176amenu disable &Changelog.U&nfinalise 177amenu enable &Changelog.&Finalise 178endfunction 179 180function Unfinalise () 181call <SID> UnfinaliseMenu () 1821 183call search ( '^ -- ' ) 184call setline ( '.' , ' -- ' ) 185endfunction 186 187function <SID> FinaliseMenu () 188" This means the entry should not be changed anymore 189amenu enable &Changelog.&New\ Version 190amenu disable &Changelog.&Add\ Entry 191amenu disable &Changelog.&Close\ Bug 192amenu disable &Changelog.Set\ &Distribution 193amenu disable &Changelog.Set\ &Urgency 194amenu enable &Changelog.U&nfinalise 195amenu disable &Changelog.&Finalise 196endfunction 197 198function Finalise () 199call <SID> FinaliseMenu () 2001 201call search ( '^ -- ' ) 202call setline ( '.' , ' -- ' . <SID> FullName () . ' <' . <SID> () . '> ' . <SID> Date ()) 203endfunction 204 205 206function <SID> MakeMenu () 207amenu &Changelog.&New\ Version :call NewVersion()<CR> 208amenu &Changelog.&Add\ Entry :call AddEntry()<CR> 209amenu &Changelog.&Close\ Bug :call CloseBug()<CR> 210menu &Changelog.-sep- <nul> 211 212amenu &Changelog.Set\ &Distribution.&unstable :call Distribution("unstable")<CR> 213amenu &Changelog.Set\ &Distribution.&frozen :call Distribution("frozen")<CR> 214amenu &Changelog.Set\ &Distribution.&stable :call Distribution("stable")<CR> 215menu &Changelog.Set\ &Distribution.-sep- <nul> 216amenu &Changelog.Set\ &Distribution.frozen\ unstable :call Distribution("frozen unstable")<CR> 217amenu &Changelog.Set\ &Distribution.stable\ unstable :call Distribution("stable unstable")<CR> 218amenu &Changelog.Set\ &Distribution.stable\ frozen :call Distribution("stable frozen")<CR> 219amenu &Changelog.Set\ &Distribution.stable\ frozen\ unstable :call Distribution("stable frozen unstable")<CR> 220 221amenu &Changelog.Set\ &Urgency.&low :call Urgency("low")<CR> 222amenu &Changelog.Set\ &Urgency.&medium :call Urgency("medium")<CR> 223amenu &Changelog.Set\ &Urgency.&high :call Urgency("high")<CR> 224 225menu &Changelog.-sep- <nul> 226amenu &Changelog.U&nfinalise :call Unfinalise()<CR> 227amenu &Changelog.&Finalise :call Finalise()<CR> 228 229if <SID> Finalised () 230call <SID> FinaliseMenu () 231else 232call <SID> UnfinaliseMenu () 233endif 234endfunction 235 236augroup changelogMenu 237au BufEnter * if & filetype == "debchangelog" |call <SID> MakeMenu () |endif 238au BufLeave * if & filetype == "debchangelog" |silent ! aunmenu &Changelog |endif 239augroup END 240 241" }}} 242" {{{1 folding 243 244" look for an author name in the [zonestart zoneend] lines searching backward 245function ! s: getAuthor ( zonestart , zoneend ) 246let linepos = a: zoneend 247while linepos >= a: zonestart 248let line = getline ( linepos ) 249if line =~# '^ --' 250return substitute ( line , '^ --\s*\([^<]\+\)\s*.*' , '\1' , '' ) 251endif 252let linepos -= 1 253endwhile 254return '[unknown]' 255endfunction 256 257" Look for a package source name searching backward from the givenline and 258" returns it. Return the empty string if the package name can't be found 259function ! DebGetPkgSrcName ( lineno ) 260let lineidx = a: lineno 261let pkgname = '' 262while lineidx > 0 263let curline = getline ( lineidx ) 264if curline =~# '^\S' 265let pkgname = matchlist ( curline , '^\(\S\+\).*$' )[ 1 ] 266break 267endif 268let lineidx = lineidx - 1 269endwhile 270return pkgname 271endfunction 272 273function ! DebChangelogFoldText () 274if v: folddashes ==# '-' " changelog entry fold 275return foldtext () . ' -- ' . s: getAuthor ( v: foldstart , v: foldend ) . ' ' 276endif 277return foldtext () 278endfunction 279 280function ! DebGetChangelogFold ( lnum ) 281let line = getline ( a: lnum ) 282if line =~# '^\w\+' 283return '>1' " beginning of a changelog entry 284endif 285if line =~# '^\s\+\[.*\]' 286return '>2' " beginning of an author-specific chunk 287endif 288if line =~# '^ --' 289return '1' 290endif 291return '=' 292endfunction 293 294if exists ( 'g:debchangelog_fold_enable' ) 295silent ! foldopen ! " unfold the entry the cursor is on (usually the first one) 296endif 297 298" }}} 299 300" {{{1 omnicompletion for Closes: # 301 302if ! exists ( 'g:debchangelog_listbugs_severities' ) 303let g: debchangelog_listbugs_severities = 'critical,grave,serious,important,normal,minor,wishlist' 304endif 305 306fun ! DebCompleteBugs ( findstart , base ) 307if a: findstart 308let line = getline ( '.' ) 309 310" try to detect whether this is closes: or lp: 311let g: debchangelog_complete_mode = 'debbugs' 312let try_colidx = col ( '.' ) - 1 313let colidx = -1 " default to no-completion-possible 314 315while try_colidx > 0 && line [ try_colidx - 1 ] =~# '\s\|\d\|#\|,\|:' 316let try_colidx = try_colidx - 1 317if line [ try_colidx ] ==# '#' && colidx == -1 318" found hash, where we complete from: 319let colidx = try_colidx 320elseif line [ try_colidx ] ==# ':' 321if try_colidx > 1 && strpart ( line , try_colidx - 2 , 3 ) =~? '\clp:' 322let g: debchangelog_complete_mode = 'lp' 323endif 324break 325endif 326endwhile 327return colidx 328else " return matches: 329let bug_lines = [] 330if g: debchangelog_complete_mode ==? 'lp' 331if ! has ( 'python' ) 332echoerr 'vim must be built with Python support to use LP bug completion' 333return 334endif 335let pkgsrc = DebGetPkgSrcName ( line ( '.' )) 336python << EOF 337import vim 338try : 339from launchpadlib .launchpad import Launchpad 340from lazr .restfulclient .errors import HTTPError 341# login anonymously 342lp = Launchpad .login_anonymously ('debchangelog.vim' ,'production' ) 343ubuntu = lp .distributions ['ubuntu' ] 344try : 345sp = ubuntu .getSourcePackage (name = vim .eval ('pkgsrc' )) 346status = ('New' ,'Incomplete' ,'Confirmed' ,'Triaged' , 347'In Progress' ,'Fix Committed' ) 348tasklist = sp .searchTasks (status = status ,order_by = 'id' ) 349liststr = '[' 350for task in tasklist : 351bug = task .bug 352liststr += "'#%d - %s'," % (bug .id ,bug .title .replace ('\'' ,'\'\'' )) 353liststr += ']' 354vim .command ('silent let bug_lines = %s' % liststr .encode ('utf-8' )) 355except HTTPError : 356pass 357except ImportError : 358vim .command ('echoerr \'python-launchpadlib >= 1.5.4 needs to be installed to use Launchpad bug completion\'' ) 359EOF 360else 361if ! filereadable ( '/usr/sbin/apt-listbugs' ) 362echoerr 'apt-listbugs not found, you should install it to use Closes bug completion' 363return 364endif 365let pkgsrc = DebGetPkgSrcName ( line ( '.' )) 366let listbugs_output = system ( '/usr/sbin/apt-listbugs -s ' . g: debchangelog_listbugs_severities . ' list ' . shellescape ( pkgsrc ) . ' | grep "^ #" 2> /dev/null' ) 367let bug_lines = split ( listbugs_output , '\n' ) 368endif 369let completions = [] 370for line in bug_lines 371let parts = matchlist ( line , '^\s*\(#\S\+\)\s*-\s*\(.*\)$' ) 372" filter only those which match a:base: 373if parts [ 1 ] !~ '^' . a: base 374continue 375endif 376let completion = {} 377let completion [ 'word' ] = parts [ 1 ] 378let completion [ 'menu' ] = parts [ 2 ] 379let completion [ 'info' ] = parts [ 0 ] 380let completions += [ completion ] 381endfor 382return completions 383endif 384endfun 385 386setlocal omnifunc = DebCompleteBugs 387 388" }}} 389 390" Restore the previous value of 'cpoptions'. 391let & cpo = s: cpo_save 392unlet s: cpo_save 393 394" vim: set foldmethod=marker: