タイムスタンプを自動更新

ファイル保存時にタイムスタンプを自動で更新する関数を
vim tips 97を参考にして(パクって)書いてみました.

(autodate.vimというプラグインがありますが,何故か上手く動かなかったので・・・)

保存した際に,ファイルの先頭から20行以内に"LastModified: "という文字列があれば,タイムスタンプを更新します.

" update last modified
"-----------------------------------------------------------
" http://vim.wikia.com/wiki/Insert_current_date_or_time
" If buffer modified, update any 'Last Modified: ' in the first 20 lines.
" 'Last Modified: Thu Jun 25 15:37:27 2009 JST
" Restores position using s mark.
function! s:eDate()
  let language =  v:lc_time
  try
    execute ":silent! language time " . "C"
    return strftime("%c %Z")
  finally
    execute ":silent! language time " . language
  endtry
endfun

function! s:LastModified()
  if &modified
    let pos = getpos('.')
    let n = min([20, line("$")])
    exe '1,' . n . 's#^\(.\{,10}Last Modified: \).*#\1' .
          \ s:eDate() . '#e'
    call setpos('.',pos)
  endif
endfun
autocmd BufWritePre * call s:LastModified()
  • tipsとの変更点1

ロケールが標準だとja_JP.〜になっているので,そのまま使うと

2009年06月25日 18時15分19秒 JST

こんな感じになってしまいます.そこでs:eDateを介してCロケールでstrftimeを返してます.

  • tipsとの変更点2

normal 〜の挙動が怪しかったので,getpos,setposにしてみました.