vim72-20100325-kaoriya-w64j.zip
[MacVim/KaoriYa.git] / runtime / plugin / autodate.vim
blob0d8ab4562defcfe46b28b4a96d3e11cca3b49fe0
1 " vi:set ts=8 sts=2 sw=2 tw=0:
3 " autodate.vim - A plugin to update time stamps automatically
5 " Maintainer:   MURAOKA Taro <koron@tka.att.ne.jp>
6 " Last Change:  06-Feb-2006.
8 " Description:
9 " Command:
10 "   :Autodate       Manually autodate.
11 "   :AutodateON     Turn on autodate in current buffer (default).
12 "   :AutodateOFF    Turn off autodate in current buffer.
14 " Options:
15 "   Each global variable (option) is overruled by buffer variable (what
16 "   starts with "b:").
18 "   'autodate_format'
19 "       Format string used for time stamps.  See |strftime()| for details.
20 "       See MonthnameString() for special extension of format.
21 "       Default: '%d-%3m-%Y'
23 "   'autodate_lines'
24 "       The number of lines searched for the existence of a time stamp when
25 "       writing a buffer.  The search range will be from top of buffer (or
26 "       line 'autodate_start_line') to 'autodate_lines' lines below.  The
27 "       bigger value you have, the longer it'll take to search words.  You
28 "       can expect to improve performance by decreasing the value of this
29 "       option.
30 "       Default: 50
32 "   'autodate_start_line'
33 "       Line number to start searching for time stamps when writing buffer
34 "       to file.
35 "       If minus, line number is counted from the end of file.
36 "       Default: 1
38 "   'autodate_keyword_pre'
39 "       A prefix pattern (see |pattern| for details) which denotes time
40 "       stamp's location.  If empty, default value will be used.
41 "       Default: '\cLast Change:'
43 "   'autodate_keyword_post'
44 "       A postfix pattern which denotes time stamp's location.  If empty,
45 "       default value will be used.
46 "       Default: '\.'
48 " Usage:
49 "   Write a line as below (case ignored) somewhere in the first 50 lines of
50 "   a buffer:
51 "       Last Change: .
52 "   When writing the buffer to a file, this line will be modified and a time
53 "   stamp will be inserted automatically.  Example:
54 "       Last Change: 11-May-2002.
56 "   You can execute :Autodate command to update time stamps manually.  The
57 "   range of lines which looks for a time stamp can also be specified.  When
58 "   no range is given, the command will be applied to the current line.
59 "   Example:
60 "       :%Autodate              " Whole file
61 "       :\<,\>Autodate          " Range selected by visual mode
62 "       :Autodate               " Current cursor line
64 "   The format of the time stamp to insert can be specified by
65 "   'autodate_format' option.  See |strftime()| (vim script function) for
66 "   details.  Sample format settings and the corresponding outputs are show
67 "   below.
68 "       FORMAT: %Y/%m/%d        OUTPUT: 2001/12/24
69 "       FORMAT: %H:%M:%S        OUTPUT: 10:06:32
70 "       FORMAT: %y%m%d-%H%M     OUTPUT: 011224-1006
72 "   Autodate.vim determines where to insert a time stamp by looking for a
73 "   KEYWORD.  A keyword consists of a PREFIX and a POSTFIX part, and they
74 "   can be set by 'autodate_keyword_pre' and 'autodate_keyword_post'
75 "   options, respectively.  If you set these values as below in your .vimrc:
76 "       :let autodate_format = ': %Y/%m/%d %H:%M:%S '
77 "       :let autodate_keyword_pre  = '\$Date'
78 "       :let autodate_keyword_post = '\$'
79 "   They will function like $Date$ in cvs.  Example:
80 "       $Date: 2001/12/24 10:06:32 $
82 "   Just another application. To insert a time stamp between '<!--DATE-->'
83 "   when writing HTML, try below:
84 "       :let b:autodate_keyword_pre = '<!--DATE-->'
85 "       :let b:autodate_keyword_post = '<!--DATE-->'
86 "   It will be useful if to put these lines in your ftplugin/html.vim.
87 "   Example:
88 "       <!--DATE-->24-Dec-2001<!--DATE-->
90 "   In addition, priority is given to a buffer local option (what starts in
91 "   b:) about all the options of autodate.
94 " To make vim NOT TO LOAD this plugin, write next line in your .vimrc:
95 "       :let plugin_autodate_disable = 1
97 " Japanese Description:
98 " ƒRƒ}ƒ“ƒh:
99 "   :Autodate       Žè“®‚Ń^ƒCƒ€ƒXƒ^ƒ“ƒv�X�V
100 "   :AutodateON     Œ»�݂̃oƒbƒtƒ@‚ÌŽ©“®�X�V‚ð—LŒø‰»
101 "   :AutodateOFF    Œ»�݂̃oƒbƒtƒ@‚ÌŽ©“®�X�V‚𖳌ø‰»
103 " ƒIƒvƒVƒ‡ƒ“: (‚»‚ꂼ‚ê‚̃IƒvƒVƒ‡ƒ“‚̓oƒbƒtƒ@”Å(b:)‚ª—D�悳‚ê‚é)
105 "   'autodate_format'
106 "       ƒ^ƒCƒ€ƒXƒ^ƒ“ƒv‚ÉŽg—p‚³‚ê‚éƒtƒH�[ƒ}ƒbƒg•¶Žš—ñ�BƒtƒH�[ƒ}ƒbƒg‚Ì�Ú�ׂÍ
107 "       |strftime()|‚ðŽQ�Æ�BƒtƒH�[ƒ}ƒbƒg‚Ö‚Ì“ÆŽ©Šg’£‚ɂ‚¢‚Ä‚ÍMonthnameString()
108 "       ‚ðŽQ�Æ�B�È—ª’l: '%d-%3m-%Y'
110 "   'autodate_lines'
111 "       •Û‘¶Žž‚Ƀ^ƒCƒ€ƒXƒ^ƒ“ƒv‚Ì‘¶�Ý‚ðƒ`ƒFƒbƒN‚·‚é�s�”�B‘�‚₹‚Α�‚â‚·‚Ù‚Ç
112 "       ƒL�[ƒ��[ƒh‚ðŒŸ�õ‚·‚邽‚ß‚ÉŽžŠÔ‚ª‚©‚©‚è“®�삪’x‚­‚È‚é�B‹t‚É’x‚¢‚ÆŠ´‚¶
113 "       ‚½‚Æ‚«‚É‚Í�¬‚³‚È’l‚ð�Ý’è‚·‚ê‚΃pƒtƒH�[ƒ}ƒ“ƒX‚̉ü‘P‚ªŠú‘Ò‚Å‚«‚é�B
114 "       �È—ª’l: 50
116 "   'autodate_keyword_pre'
117 "       ƒ^ƒCƒ€ƒXƒ^ƒ“ƒv‚Ì‘¶�Ý‚ðŽ¦‚·‘O’uƒL�[ƒ��[ƒh(�³‹K•\Œ»)�B•K�{�B‹ó•¶Žš—ñ‚ð
118 "       Žw’è‚·‚é‚Æ�È—ª’l‚ªŽg‚í‚ê‚é�B�È—ª’l: '\cLast Change:'
120 "   'autodate_keyword_post'
121 "       ƒ^ƒCƒ€ƒXƒ^ƒ“ƒv‚Ì‘¶�Ý‚ðŽ¦‚·Œã’uƒL�[ƒ��[ƒh(�³‹K•\Œ»)�B•K�{�B‹ó•¶Žš—ñ‚ð
122 "       Žw’è‚·‚é‚Æ�È—ª’l‚ªŽg‚í‚ê‚é�B�È—ª’l: '\.'
124 " Žg—p–@:
125 "   ƒtƒ@ƒCƒ‹‚Ì�擪‚©‚ç50�sˆÈ“à‚É
126 "       Last Change: .
127 "   ‚Æ�‘‚¢‚½�s(‘啶Žš�¬•¶Žš‚Í‹æ•Ê‚µ‚Ü‚¹‚ñ)‚ð—pˆÓ‚·‚é‚Æ�Aƒtƒ@ƒCƒ‹‚Ì•Û‘¶(:w)Žž
128 "   ‚ÉŽ©“®“I‚É‚»‚ÌŽž��(ƒ^ƒCƒ€ƒXƒ^ƒ“ƒv)‚ª‘}“ü‚³‚ê‚Ü‚·�BŒ‹‰Ê—á:
129 "       Last Change: 11-May-2002.
131 "   ExƒRƒ}ƒ“ƒh‚Ì:Autodate‚ðŽÀ�s‚·‚邱‚Æ‚ÅŽè“®‚Ń^ƒCƒ€ƒXƒ^ƒ“ƒv‚Ì�X�V‚ª�s‚È‚¦
132 "   ‚Ü‚·�B‚»‚Ì�ۂɃ^ƒCƒ€ƒXƒ^ƒ“ƒv‚ð’T‚·”͈͂ðŽw’è‚·‚邱‚Æ‚à‚Å‚«‚Ü‚·�B“Á‚ɔ͈Í
133 "   ‚ðŽw’肵‚È‚¯‚ê‚΃J�[ƒ\ƒ‹‚Ì‚ ‚é�s‚ª‘Î�Û‚É‚È‚è‚Ü‚·�B—á:
134 "       :%Autodate              " ƒtƒ@ƒCƒ‹‘S‘Ì
135 "       :\<,\>Autodate          " ƒrƒWƒ…ƒAƒ‹‘I‘ð—̈æ
136 "       :Autodate               " Œ»�݃J�[ƒ\ƒ‹‚Ì‚ ‚é�s
138 "   ‘}“ü‚·‚éƒ^ƒCƒ€ƒXƒ^ƒ“ƒv‚Ì�‘Ž®‚̓IƒvƒVƒ‡ƒ“'autodate_format'‚ÅŽw’è‚·‚邱‚Æ
139 "   ‚ª‚Å‚«‚Ü‚·�B�Ú�ׂÍVimƒXƒNƒŠƒvƒgŠÖ�”|strftime()|‚Ì�à–¾‚É�]‚¢‚Ü‚·�BˆÈ‰º‚É
140 "   �‘Ž®‚Æ‚»‚Ì�o—Í‚Ì—á‚ðŽ¦‚µ‚Ü‚·:
141 "       �‘Ž®: %Y/%m/%d          �o—Í: 2001/12/24
142 "       �‘Ž®: %H:%M:%S          �o—Í: 10:06:32
143 "       �‘Ž®: %y%m%d-%H%M       �o—Í: 011224-1006
145 "   autodate.vim‚̓L�[ƒ��[ƒh‚ð’T‚·‚±‚ƂŃ^ƒCƒ€ƒXƒ^ƒ“ƒv‚ð‘}“ü‚·‚ׂ«ˆÊ’u‚ðŒˆ’è
146 "   ‚µ‚Ä‚¢‚Ü‚·�BƒL�[ƒ��[ƒh‚Í‘O’u•”‚ÆŒã’u•”‚©‚ç‚È‚è�A‚»‚ꂼ‚êƒIƒvƒVƒ‡ƒ“‚Ì
147 "   'autodate_keyword_pre'‚Æ'autodate_keyword_post'‚ð�Ý’è‚·‚邱‚Æ‚Å•Ï�X‚Å‚«
148 "   ‚Ü‚·�BŒÂ�l�Ý’èƒtƒ@ƒCƒ‹(_vimrc)‚ÅŽŸ‚̂悤‚É�Ý’è‚·‚é‚Æ:
149 "       :let autodate_format = ': %Y/%m/%d %H:%M:%S '
150 "       :let autodate_keyword_pre  = '\$Date'
151 "       :let autodate_keyword_post = '\$'
152 "   cvs‚É‚¨‚¯‚é$Date$‚̂悤‚É“®�삵‚Ü‚·�B—á:
153 "       $Date: 2001/12/24 10:06:32 $
155 "   ‰ž—p‚Æ‚µ‚ÄHTML‚ð‹L�q‚·‚é�Û‚É<!--DATE-->‚ň͂܂ꂽ’†‚Ƀ^ƒCƒ€ƒXƒ^ƒ“ƒv‚ð‘}
156 "   “ü‚³‚¹‚½‚¢�ê�‡‚É‚Í:
157 "       :let b:autodate_keyword_pre = '<!--DATE-->'
158 "       :let b:autodate_keyword_post = '<!--DATE-->'
159 "   ‚ÆŽw’肵‚Ü‚·�Bftplugin/html.vim‚Å�Ý’è‚·‚é‚Æ•Ö—˜‚Å‚µ‚傤�B—á:
160 "       <!--DATE-->24-Dec-2001<!--DATE-->
162 "   ‚È‚¨autodate‚Ì‘�‚ẴIƒvƒVƒ‡ƒ“‚ɂ‚¢‚Ä�Aƒoƒbƒtƒ@ƒ��[ƒJƒ‹ƒIƒvƒVƒ‡ƒ“(b:‚Å
163 "   Žn‚Ü‚é‚à‚Ì)‚ª—D�悳‚ê‚Ü‚·�B
165 " ‚±‚̃vƒ‰ƒOƒCƒ“‚ð“Ç�ž‚Ý‚½‚­‚È‚¢Žž‚Í.vimrc‚ÉŽŸ‚̂悤‚É�‘‚­‚±‚Æ:
166 "       :let plugin_autodate_disable = 1
168 if exists('plugin_autodate_disable')
169   finish
170 endif
171 let s:debug = 0
173 "---------------------------------------------------------------------------
174 "                                   Options
177 " 'autodate_format'
179 if !exists('autodate_format')
180   let g:autodate_format = '%d-%3m-%Y'
181 endif
184 " 'autodate_lines'
186 if !exists('autodate_lines')
187   let g:autodate_lines = 50
188 endif
191 " 'autodate_start_line'
193 if !exists('autodate_start_line')
194   let g:autodate_start_line = 1
195 endif
198 " 'autodate_keyword_pre'
200 if !exists('autodate_keyword_pre')
201   let g:autodate_keyword_pre = '\cLast Change:'
202 endif
205 " 'autodate_keyword_post'
207 if !exists('autodate_keyword_post')
208   let g:autodate_keyword_post = '\.'
209 endif
211 "---------------------------------------------------------------------------
212 "                                   Mappings
214 command! -range Autodate call <SID>Autodate(<line1>, <line2>)
215 command! AutodateOFF let b:autodate_disable = 1
216 command! AutodateON let b:autodate_disable = 0
217 if has("autocmd")
218   augroup Autodate
219     au!
220     autocmd BufUnload,FileWritePre,BufWritePre * call <SID>Autodate()
221   augroup END
222 endif " has("autocmd")
224 "---------------------------------------------------------------------------
225 "                                Implementation
228 " Autodate([{firstline} [, {lastline}]])
230 "   {firstline}‚Æ{lastline}‚ÅŽw’肳‚ꂽ”͈͂ɂ‚¢‚Ä�Aƒ^ƒCƒ€ƒXƒ^ƒ“ƒv‚ÌŽ©“®
231 "   ƒAƒbƒvƒf�[ƒg‚ð�s‚È‚¤�B{firstline}‚ª�È—ª‚³‚ꂽ�ê�‡‚̓tƒ@ƒCƒ‹‚Ì�擪�A
232 "   {lastline}‚ª�È—ª‚³‚ꂽ�ê�‡‚Í{firstline}‚©‚ç'autodate_lines'�s‚͈̔͂ª‘Î
233 "   �Û‚É‚È‚é�B
235 function! s:Autodate(...)
236   " Check enable
237   if (exists('b:autodate_disable') && b:autodate_disable != 0) || &modified == 0
238     return
239   endif
241   " Verify {firstline}
242   if a:0 > 0 && a:1 > 0
243     let firstline = a:1
244   else
245     let firstline = s:GetAutodateStartLine()
246   endif
248   " Verify {lastline}
249   if a:0 > 1 && a:2 <= line('$')
250     let lastline = a:2
251   else
252     let lastline = firstline + s:GetAutodateLines() - 1
253     " Range check
254     if lastline > line('$')
255       let lastline = line('$')
256     endif
257   endif
259   if firstline <= lastline
260     call s:AutodateStub(firstline, lastline)
261   endif
262 endfunction
265 " GetAutodateStartLine()
267 "   ’T�õŠJŽn“_‚ðŽæ“¾‚·‚é
269 function! s:GetAutodateStartLine()
270   let retval = 1
271   if exists('b:autodate_start_line')
272     let retval = b:autodate_start_line
273   elseif exists('g:autodate_start_line')
274     let retval = g:autodate_start_line
275   endif
277   if retval < 0
278     let retval = retval + line('$') + 1
279   endif
280   if retval <= 0
281     let retval = 1
282   endif
283   return retval
284 endfunction
287 " GetAutodateLines()
288 "   
289 "   autodate‘Î�۔͈͂ðŽæ“¾‚·‚é
291 function! s:GetAutodateLines()
292   if exists('b:autodate_lines') && b:autodate_lines > 0
293     return b:autodate_lines
294   elseif exists('g:autodate_lines') && g:autodate_lines > 0
295     return g:autodate_lines
296   else
297     return 50
298   endif
299 endfunction
302 " AutodateStub(first, last)
304 "   Žw’肳‚ꂽ”͈͂ɂ‚¢‚ă^ƒCƒ€ƒXƒ^ƒ“ƒv‚ÌŽ©“®ƒAƒbƒvƒf�[ƒg‚ð�s‚È‚¤�B
306 function! s:AutodateStub(first, last)
308   " Verify pre-keyword.
309   if exists('b:autodate_keyword_pre') && b:autodate_keyword_pre != ''
310     let pre = b:autodate_keyword_pre
311   else
312     if exists('g:autodate_keyword_pre') && g:autodate_keyword_pre != ''
313       let pre = g:autodate_keyword_pre
314     else
315       let pre = '\cLast Change:'
316     endif
317   endif
319   " Verify post-keyword.
320   if exists('b:autodate_keyword_post') && b:autodate_keyword_post != ''
321     let post = b:autodate_keyword_post
322   else
323     if exists('g:autodate_keyword_post') && g:autodate_keyword_post != ''
324       let post = g:autodate_keyword_post
325     else
326       let post = '\.'
327     endif
328   endif
330   " Verify format.
331   if exists('b:autodate_format') && b:autodate_format != ''
332     let format = b:autodate_format
333   else
334     if exists('g:autodate_format') && g:autodate_format != ''
335       let format = g:autodate_format
336     else
337       let format = '%d-%3m-%Y'
338     endif
339   endif
341   " Generate substitution pattern
342   let pat = '\('.pre.'\s*\)\(\S.*\)\?\('.post.'\)'
343   let sub = Strftime2(format)
344   " For debug
345   if s:debug
346     echo "range=".a:first."-".a:last
347     echo "pat= ".pat
348     echo "sub= ".sub
349   endif
351   " Process
352   let i = a:first
353   while i <= a:last
354     let curline = getline(i)
355     if curline =~ pat
356       let newline = substitute(curline, pat, '\1' . sub . '\3', '')
357       if curline !=# newline
358         call setline(i, newline)
359       endif
360     endif
361     let i = i + 1
362   endwhile
363 endfunction
366 " Strftime2({format} [, {time}])
367 "   Enchanced version of strftime().
369 "   strftime()‚̃tƒH�[ƒ}ƒbƒgŠg’£ƒo�[ƒWƒ‡ƒ“�BƒtƒH�[ƒ}ƒbƒg�‘Ž®‚Í‚Ù‚Æ‚ñ‚ǃIƒŠƒW
370 "   ƒiƒ‹‚ƈê���B‚µ‚©‚µŒŽ‚̉pŒê–¼‚É’uŠ·‚í‚é“Á•Ê‚È�‘Ž® %{n}m ‚ªŽg—p‰Â”\�B{n}‚É
371 "   ‚͉pŒê–¼‚Ì•¶Žš—ñ‚Ì’·‚³‚ðŽw’è‚·‚é(�Å‘å9)�B0‚ðŽw’è‚·‚ê‚Η]•ª‚È‹ó”’‚Í•t‰Á‚³
372 "   ‚ê‚È‚¢�B
373 "       —á:
374 "           :echo Strftime2("%d-%3m-%Y")
375 "           07-Oct-2001
376 "           :echo Strftime2("%d-%0m-%Y")
377 "           07-October-2001
379 function! Strftime2(...)
380   if a:0 > 0
381     " Get {time} argument.
382     if a:0 > 1
383       let time = a:2
384     else
385       let time = localtime()
386     endif
387     " Parse special format.
388     let format = a:1
389     let format = substitute(format, '%\(\d\+\)m', '\=MonthnameString(-1, submatch(1), time)', 'g')
390     let format = substitute(format, '%\(\d\+\)a', '\=DaynameString(-1, submatch(1), time)', 'g')
391     return strftime(format, time)
392   endif
393   " Genrate error!
394   return strftime()
395 endfunction
398 " MonthnameString([{month} [, {length} [, {time}]]])
399 "   Get month name string in English with first specified letters.
401 "   ‰pŒê‚ÌŒŽ–¼‚ðŽw’肵‚½’·‚³‚Å•Ô‚·�B{month}‚ð�È—ª‚µ‚½�ê�‡‚É‚ÍŒ»�Ý‚ÌŒŽ–¼‚ª•Ô
402 "   ‚³‚ê‚é�B{month}‚É–³Œø‚ÈŽw’è(1�`12ˆÈŠO)‚ª�s‚È‚í‚ꂽ�ê�‡‚Í{time}‚ÅŽ¦‚³‚ê‚é
403 "   ŒŽ–¼‚ª•Ô‚³‚ê‚é�B{time}‚ð�È—ª‚µ‚½�ê�‡‚É‚Í‘ã‚í‚è‚É|localtime()|‚ªŽg—p‚³‚ê
404 "   ‚é�B{length}‚É‚Í•Ô‚·–¼‘O‚Ì’·‚³‚ðŽw’è‚·‚邪�È—ª‚·‚é‚Æ”CˆÓ’·‚É‚È‚é�B
405 "       —á:
406 "           :echo MonthnameString(8) . " 2001"
407 "           August 2001
408 "           :echo MonthnameString(8,3) . " 2001"
409 "           Aug 2001
411 function! MonthnameString(...)
412   " Get {time} argument.
413   if a:0 > 2
414     let time = a:3
415   else
416     let time = localtime()
417   endif
418   " Verify {month}.
419   if a:0 > 0 && (a:1 >= 1 && a:1 <= 12)
420     let month = a:1
421   else
422     let month = substitute(strftime('%m', time), '^0\+', '', '')
423   endif
424   " Verify {length}.
425   if a:0 > 1 && (a:2 >= 1 && a:2 <= 9)
426     let length = a:2
427   else
428     let length = strpart('785534469788', month - 1, 1)
429   endif
430   " Generate string of month name.
431   return strpart('January  February March    April    May      June     July     August   SeptemberOctober  November December ', month * 9 - 9, length)
432 endfunction
435 " DaynameString([{month} [, {length} [, {time}]]])
436 "   Get day name string in English with first specified letters.
438 "   ‰pŒê‚Ì—j“ú–¼‚ðŽw’肵‚½’·‚³‚Å•Ô‚·�B{day}‚ð�È—ª‚µ‚½�ê�‡‚É‚Í–{“ú‚Ì—j“ú–¼‚ª
439 "   •Ô‚³‚ê‚é�B{day}‚É–³Œø‚ÈŽw’è(0�`6ˆÈŠO)‚ª�s‚È‚í‚ꂽ�ê�‡‚Í{time}‚ÅŽ¦‚³‚ê‚é
440 "   —j“ú–¼‚ª•Ô‚³‚ê‚é�B{time}‚ð�È—ª‚µ‚½�ê�‡‚É‚Í‘ã‚í‚è‚É|localtime()|‚ªŽg—p‚³
441 "   ‚ê‚é�B{length}‚É‚Í•Ô‚·–¼‘O‚Ì’·‚³‚ðŽw’è‚·‚邪�È—ª‚·‚é‚Æ”CˆÓ’·‚É‚È‚é�B
442 "       —á:
443 "           :echo DaynameString(0)
444 "           Sunday
445 "           :echo DaynameString(5,3).', 13th'
446 "           Fri, 13th
448 function! DaynameString(...)
449   " Get {time} argument.
450   if a:0 > 2
451     let time = a:3
452   else
453     let time = localtime()
454   endif
455   " Verify {day}.
456   if a:0 > 0 && (a:1 >= 0 && a:1 <= 6)
457     let day = a:1
458   else
459     let day = strftime('%w', time) + 0
460   endif
461   " Verify {length}.
462   if a:0 > 1 && (a:2 >= 1 && a:2 <= 9)
463     let length = a:2
464   else
465     let length = strpart('6798686', day, 1)
466   endif
467   " Generate string of day name.
468   return strpart('Sunday   Monday   Tuesday  WednesdayThursday Friday   Saturday ', day * 9, length)
469 endfunction