Created
December 27, 2018 18:16
-
-
Save nilium/73c41172184376e5496e2c0a82561c44 to your computer and use it in GitHub Desktop.
Vim plugin to write/update a serial number comment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" serial.vim | |
" Noel Cower | |
" | |
" Plugin to insert serial strings based on a local timestamp. | |
" | |
" For example: | |
" Serial 20181227100403 | |
" | |
" The above is this file's serial, inserted using :SerialUpdate. It can | |
" be updated with subsequent calls to :SerialUpdate. You can create | |
" a serial comment (on the following line, or current line if empty) | |
" using :SerialUpdate!. | |
" | |
" If g:serial_comment_auto_update = 1, then serial comments will | |
" automatically be updated before writing the buffer. | |
" | |
" License: | |
" Copyright 2018 Noel Cower | |
" | |
" Permission is hereby granted, free of charge, to any person obtaining a copy | |
" of this software and associated documentation files (the "Software"), to deal | |
" in the Software without restriction, including without limitation the rights | |
" to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
" copies of the Software, and to permit persons to whom the Software is | |
" furnished to do so, subject to the following conditions: | |
" | |
" The above copyright notice and this permission notice shall be included in all | |
" copies or substantial portions of the Software. | |
" | |
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
" SOFTWARE. | |
if !exists('g:serial_comment_auto_update') | |
" Default to off | |
let g:serial_comment_auto_update = 0 | |
endif | |
function! s:ShouldAutoUpdate() | |
if exists('b:serial_comment_auto_update') | |
return b:serial_comment_auto_update | |
endif | |
return g:serial_comment_auto_update | |
endfunction | |
function! s:Prefix() | |
if exists('b:serial_prefix') | |
return b:serial_prefix | |
elseif exists('g:serial_prefix') | |
return g:serial_prefix | |
endif | |
let l:comment = split(&commentstring, '%s') | |
if len(l:comment) > 0 | |
return trim(l:comment[0]) . ' ' | |
endif | |
return '# ' | |
endfunction | |
function! s:Suffix() | |
if exists('b:serial_suffix') | |
return b:serial_suffix | |
elseif exists('g:serial_suffix') | |
return g:serial_suffix | |
endif | |
let l:comment = split(&commentstring, '%s') | |
if len(l:comment) > 1 | |
return ' ' . trim(l:comment[1]) | |
endif | |
return '' | |
endfunction | |
function! s:GetIndent(line) | |
let l:str = getline(a:line) | |
let l:prefix = match(getline(a:line), '\v\S') | |
if l:prefix == -1 | |
" Whole string is spaces | |
return l:str | |
elseif l:prefix == 0 | |
" No match | |
return '' | |
endif | |
return l:str[:l:prefix-1] | |
endfunction | |
function! s:UpdateSerialConditional() | |
if s:ShouldAutoUpdate() | |
return s:UpdateSerial('?') | |
endif | |
endfunction | |
function! s:UpdateSerial(mode) | |
let l:pos = getpos('.') | |
call setpos('.', getpos('0')) | |
let l:line = search('\V\<Serial \v\d{14}') | |
if l:line == 0 | |
if a:mode != '!' | |
call cursor(l:pos[1], l:pos[2], l:pos[3]) | |
if a:mode != '?' | |
echoerr "No serial line found" | |
endif | |
return | |
end | |
let l:line = l:pos[1] | |
if getline(l:line) != '' | |
call append(l:line, s:GetIndent(l:line)) | |
let l:line += 1 | |
elseif l:line > 1 | |
call setline(l:line, s:GetIndent(l:line-1)) | |
endif | |
endif | |
call s:UpdateSerialLine(l:line) | |
call cursor(l:pos[1], l:pos[2], l:pos[3]) | |
endfunction | |
function! s:UpdateSerialLine(line) | |
let l:serial = strftime('%Y%m%d%H%M%S') | |
let l:prefix = s:Prefix() | |
let l:suffix = s:Suffix() | |
call setline(a:line, s:GetIndent(a:line) . l:prefix . 'Serial ' . l:serial . l:suffix) | |
endfunction | |
augroup grpSerialUpdate | |
autocmd! | |
autocmd BufWritePre * :call <SID>UpdateSerialConditional() | |
augroup END | |
command! -bang SerialUpdate :call <SID>UpdateSerial("<bang>") | |
" vim: ts=8 sw=8 noet: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment