robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
26cdff0e92
master
1" Vim filetype plugin file 2" Language: generic Changelog file 3" Maintainer: Martin Florian <marfl@posteo.de> 4" Previous Maintainer: Nikolai Weibull <now@bitwi.se> 5" Latest Revision: 2021-10-17 6" Variables: 7" g:changelog_timeformat (deprecated: use g:changelog_dateformat instead) - 8" description: the timeformat used in ChangeLog entries. 9" default: "%Y-%m-%d". 10" g:changelog_dateformat - 11" description: the format sent to strftime() to generate a date string. 12" default: "%Y-%m-%d". 13" g:changelog_username - 14" description: the username to use in ChangeLog entries 15" default: try to deduce it from environment variables and system files. 16" Local Mappings: 17" <Leader>o - 18" adds a new changelog entry for the current user for the current date. 19" Global Mappings: 20" <Leader>o - 21" switches to the ChangeLog buffer opened for the current directory, or 22" opens it in a new buffer if it exists in the current directory. Then 23" it does the same as the local <Leader>o described above. 24" Notes: 25" run 'runtime ftplugin/changelog.vim' to enable the global mapping for 26" changelog files. 27" TODO: 28" should we perhaps open the ChangeLog file even if it doesn't exist already? 29" Problem is that you might end up with ChangeLog files all over the place. 30 31" If 'filetype' isn't "changelog", we must have been to add ChangeLog opener 32if & filetype == 'changelog' 33if exists ( 'b:did_ftplugin' ) 34finish 35endif 36let b: did_ftplugin = 1 37 38let s: cpo_save = & cpo 39set cpo & vim 40 41" Set up the format used for dates. 42if ! exists ( 'g:changelog_dateformat' ) 43if exists ( 'g:changelog_timeformat' ) 44let g: changelog_dateformat = g: changelog_timeformat 45else 46let g: changelog_dateformat = "%Y-%m-%d" 47endif 48endif 49 50function ! s: username () 51if exists ( 'g:changelog_username' ) 52return g: changelog_username 53elseif $ != "" 54return $ elseif $ EMAIL_ADDRESS != "" 56return $ EMAIL_ADDRESS 57endif 58let s: default_login = 'unknown' 59 60" Disabled by default for security reasons. 61if dist#vim#IsSafeExecutable ( 'changelog' , 'whoami' ) 62let login = s: login () 63else 64let login = s: default_login 65endif 66return printf ( '%s <%s@%s>' , s: name ( login ), login , s: hostname ()) 67endfunction 68 69function ! s: login () 70return s: trimmed_system_with_default ( 'whoami' , s: default_login ) 71endfunction 72 73function ! s: trimmed_system_with_default ( command , default ) 74return s: first_line ( s: system_with_default ( a: command , a: default )) 75endfunction 76 77function ! s: system_with_default ( command , default ) 78let output = system ( a: command ) 79if v: shell_error 80return a: default 81endif 82return output 83endfunction 84 85function ! s: first_line ( string ) 86return substitute ( a: string , '\n.*$' , "" , "" ) 87endfunction 88 89function ! s: name ( login ) 90for name in [ s: gecos_name ( a: login ), $ NAME , s: capitalize ( a: login )] 91if name != "" 92return name 93endif 94endfor 95endfunction 96 97function ! s: gecos_name ( login ) 98for line in s: try_reading_file ( '/etc/passwd' ) 99if line =~ '^' . a: login . ':' 100return substitute ( s: passwd_field ( line , 5 ), '&' , s: capitalize ( a: login ), "" ) 101endif 102endfor 103return "" 104endfunction 105 106function ! s: try_reading_file ( path ) 107try 108return readfile ( a: path ) 109catch 110return [] 111endtry 112endfunction 113 114function ! s: passwd_field ( line , field ) 115let fields = split ( a: line , ':' , 1 ) 116if len ( fields ) < a: field 117return "" 118endif 119return fields [ a: field - 1 ] 120endfunction 121 122function ! s: capitalize ( word ) 123return toupper ( a: word [ 0 ]) . strpart ( a: word , 1 ) 124endfunction 125 126function ! s: hostname () 127return s: trimmed_system_with_default ( 'hostname' , 'localhost' ) 128endfunction 129 130" Format used for new date entries. 131if ! exists ( 'g:changelog_new_date_format' ) 132let g: changelog_new_date_format = "%d %u\n\n\t* %p%c\n\n" 133endif 134 135" Format used for new entries to current date entry. 136if ! exists ( 'g:changelog_new_entry_format' ) 137let g: changelog_new_entry_format = "\t* %p%c" 138endif 139 140" Regular expression used to find a given date entry. 141if ! exists ( 'g:changelog_date_entry_search' ) 142let g: changelog_date_entry_search = '^\s*%d\_s*%u' 143endif 144 145" Regular expression used to find the end of a date entry 146if ! exists ( 'g:changelog_date_end_entry_search' ) 147let g: changelog_date_end_entry_search = '^\s*$' 148endif 149 150 151" Substitutes specific items in new date-entry formats and search strings. 152" Can be done with substitute of course, but unclean, and need \@! then. 153function ! s: substitute_items ( str , date , user , prefix ) 154let str = a: str 155let middles = { '%' : '%' , 'd' : a: date , 'u' : a: user , 'p' : a: prefix , 'c' : '{cursor}' } 156let i = stridx ( str , '%' ) 157while i != -1 158let inc = 0 159if has_key ( middles , str [ i + 1 ]) 160let mid = middles [ str [ i + 1 ]] 161let str = strpart ( str , 0 , i ) . mid . strpart ( str , i + 2 ) 162let inc = strlen ( mid ) - 1 163endif 164let i = stridx ( str , '%' , i + 1 + inc ) 165endwhile 166return str 167endfunction 168 169" Position the cursor once we've done all the funky substitution. 170function ! s: position_cursor () 171if search ( '{cursor}' ) > 0 172let lnum = line ( '.' ) 173let line = getline ( lnum ) 174let cursor = stridx ( line , '{cursor}' ) 175call setline ( lnum , substitute ( line , '{cursor}' , '' , '' )) 176endif 177startinsert 178endfunction 179 180" Internal function to create a new entry in the ChangeLog. 181function ! s: new_changelog_entry ( prefix ) 182" Deal with 'paste' option. 183let save_paste = & paste 184let & paste = 1 185call cursor ( 1 , 1 ) 186" Look for an entry for today by our user. 187let date = strftime ( g: changelog_dateformat ) 188let search = s: substitute_items ( g: changelog_date_entry_search , date , 189 \s: username (), a: prefix ) 190if search ( search ) > 0 191" Ok, now we look for the end of the date entry, and add an entry. 192call cursor ( nextnonblank ( line ( '.' ) + 1 ), 1 ) 193if search ( g: changelog_date_end_entry_search , 'W' ) > 0 194let p = ( line ( '.' ) == line ( '$' )) ? line ( '.' ) : line ( '.' ) - 1 195else 196let p = line ( '.' ) 197endif 198let ls = split ( s: substitute_items ( g: changelog_new_entry_format , '' , '' , a: prefix ), 199 \'\n' ) 200call append ( p , ls ) 201call cursor ( p + 1 , 1 ) 202else 203" Flag for removing empty lines at end of new ChangeLogs. 204let remove_empty = line ( '$' ) == 1 205 206" No entry today, so create a date-user header and insert an entry. 207let todays_entry = s: substitute_items ( g: changelog_new_date_format , 208 \date , s: username (), a: prefix ) 209" Make sure we have a cursor positioning. 210if stridx ( todays_entry , '{cursor}' ) == -1 211let todays_entry = todays_entry . '{cursor}' 212endif 213 214" Now do the work. 215call append ( 0 , split ( todays_entry , '\n' )) 216 217" Remove empty lines at end of file. 218if remove_empty 219$ -/^\s*$/-1,$delete 220endif 221 222" Reposition cursor once we're done. 223call cursor ( 1 , 1 ) 224endif 225 226call s: position_cursor () 227 228" And reset 'paste' option 229let & paste = save_paste 230endfunction 231 232let b: undo_ftplugin = "setl com< fo< et< ai<" 233 234setlocal comments = 235setlocal formatoptions += t 236setlocal noexpandtab 237setlocal autoindent 238 239if & textwidth == 0 240setlocal textwidth = 78 241let b: undo_ftplugin .= " tw<" 242endif 243 244if ! exists ( "no_plugin_maps" ) && ! exists ( "no_changelog_maps" ) && exists ( ":NewChangelogEntry" ) != 2 245nnoremap <buffer> <silent> < Leader > o :< C-u > call< SID > new_changelog_entry('')< CR > 246xnoremap <buffer> <silent> < Leader > o :< C-u > call< SID > new_changelog_entry('')< CR > 247command ! -buffer -nargs = 0 NewChangelogEntry call s:new_changelog_entry('') 248let b: undo_ftplugin .= " | sil! exe 'nunmap <buffer> <Leader>o'" . 249 \" | sil! exe 'vunmap <buffer> <Leader>o'" . 250 \" | sil! delc NewChangelogEntry" 251endif 252 253let & cpo = s: cpo_save 254unlet s: cpo_save 255else 256let s: cpo_save = & cpo 257set cpo & vim 258 259if ! exists ( "no_plugin_maps" ) && ! exists ( "no_changelog_maps" ) 260" Add the Changelog opening mapping 261nnoremap <silent> < Leader > o :call< SID > open_changelog()< CR > 262let b: undo_ftplugin .= " | silent! exe 'nunmap <buffer> <Leader>o" 263endif 264 265function ! s: open_changelog () 266let path = expand ( '%:p:h' ) 267if exists ( 'b:changelog_path' ) 268let changelog = b: changelog_path 269else 270if exists ( 'b:changelog_name' ) 271let name = b: changelog_name 272else 273let name = 'ChangeLog' 274endif 275while isdirectory ( path ) 276let changelog = path . '/' . name 277if filereadable ( changelog ) 278break 279endif 280let parent = substitute ( path , '/\+[^/]*$' , "" , "" ) 281if path == parent 282break 283endif 284let path = parent 285endwhile 286endif 287if ! filereadable ( changelog ) 288return 289endif 290 291if exists ( 'b:changelog_entry_prefix' ) 292let prefix = call ( b: changelog_entry_prefix , []) 293else 294let prefix = substitute ( strpart ( expand ( '%:p' ), strlen ( path )), '^/\+' , "" , "" ) 295endif 296 297let buf = bufnr ( changelog ) 298if buf != -1 299if bufwinnr ( buf ) != -1 300execute bufwinnr ( buf ) . 'wincmd w' 301else 302execute 'sbuffer' buf 303endif 304else 305execute 'split' fnameescape ( changelog ) 306endif 307 308call s: new_changelog_entry ( prefix ) 309endfunction 310 311let & cpo = s: cpo_save 312unlet s: cpo_save 313endif