1 " Vim filetype plugin file
2 " Language: generic Changelog file
3 " Maintainer: Nikolai Weibull <now@bitwi.se>
4 " Latest Revision: 2007-05-06
6 " g:changelog_timeformat (deprecated: use g:changelog_dateformat instead) -
7 " description: the timeformat used in ChangeLog entries.
9 " g:changelog_dateformat -
10 " description: the format sent to strftime() to generate a date string.
11 " default: "%Y-%m-%d".
12 " g:changelog_username -
13 " description: the username to use in ChangeLog entries
14 " default: try to deduce it from environment variables and system files.
17 " adds a new changelog entry for the current user for the current date.
20 " switches to the ChangeLog buffer opened for the current directory, or
21 " opens it in a new buffer if it exists in the current directory. Then
22 " it does the same as the local <Leader>o described above.
24 " run 'runtime ftplugin/changelog.vim' to enable the global mapping for
27 " should we perhaps open the ChangeLog file even if it doesn't exist already?
28 " Problem is that you might end up with ChangeLog files all over the place.
30 " If 'filetype' isn't "changelog", we must have been to add ChangeLog opener
31 if &filetype == 'changelog'
32 if exists('b:did_ftplugin')
35 let b:did_ftplugin = 1
40 " Set up the format used for dates.
41 if !exists('g:changelog_dateformat')
42 if exists('g:changelog_timeformat')
43 let g:changelog_dateformat = g:changelog_timeformat
45 let g:changelog_dateformat = "%Y-%m-%d"
49 " Try to figure out a reasonable username of the form:
50 " Full Name <user@host>.
51 if !exists('g:changelog_username')
52 if exists('$EMAIL') && $EMAIL != ''
53 let g:changelog_username = $EMAIL
54 elseif exists('$EMAIL_ADDRESS') && $EMAIL_ADDRESS != ''
55 " This is some Debian junk if I remember correctly.
56 let g:changelog_username = $EMAIL_ADDRESS
58 " Get the users login name.
59 let login = system('whoami')
63 let newline = stridx(login, "\n")
65 let login = strpart(login, 0, newline)
69 " Try to get the full name from gecos field in /etc/passwd.
70 if filereadable('/etc/passwd')
71 for line in readfile('/etc/passwd')
72 if line =~ '^' . login
73 let name = substitute(line,'^\%([^:]*:\)\{4}\([^:]*\):.*$','\1','')
74 " Only keep stuff before the first comma.
75 let comma = stridx(name, ',')
77 let name = strpart(name, 0, comma)
79 " And substitute & in the real name with the login of our user.
80 let amp = stridx(name, '&')
82 let name = strpart(name, 0, amp) . toupper(login[0]) .
83 \ strpart(login, 1) . strpart(name, amp + 1)
89 " If we haven't found a name, try to gather it from other places.
91 " Maybe the environment has something of interest.
95 " No? well, use the login name and capitalize first
97 let name = toupper(login[0]) . strpart(login, 1)
102 let hostname = system('hostname')
104 let hostname = 'localhost'
106 let newline = stridx(hostname, "\n")
108 let hostname = strpart(hostname, 0, newline)
112 " And finally set the username.
113 let g:changelog_username = name . ' <' . login . '@' . hostname . '>'
117 " Format used for new date entries.
118 if !exists('g:changelog_new_date_format')
119 let g:changelog_new_date_format = "%d %u\n\n\t* %c\n\n"
122 " Format used for new entries to current date entry.
123 if !exists('g:changelog_new_entry_format')
124 let g:changelog_new_entry_format = "\t* %c"
127 " Regular expression used to find a given date entry.
128 if !exists('g:changelog_date_entry_search')
129 let g:changelog_date_entry_search = '^\s*%d\_s*%u'
132 " Regular expression used to find the end of a date entry
133 if !exists('g:changelog_date_end_entry_search')
134 let g:changelog_date_entry_search = '^\s*$'
138 " Substitutes specific items in new date-entry formats and search strings.
139 " Can be done with substitute of course, but unclean, and need \@! then.
140 function! s:substitute_items(str, date, user)
142 let middles = {'%': '%', 'd': a:date, 'u': a:user, 'c': '{cursor}'}
143 let i = stridx(str, '%')
146 if has_key(middles, str[i + 1])
147 let mid = middles[str[i + 1]]
148 let str = strpart(str, 0, i) . mid . strpart(str, i + 2)
149 let inc = strlen(mid)
151 let i = stridx(str, '%', i + 1 + inc)
156 " Position the cursor once we've done all the funky substitution.
157 function! s:position_cursor()
158 if search('{cursor}') > 0
160 let line = getline(lnum)
161 let cursor = stridx(line, '{cursor}')
162 call setline(lnum, substitute(line, '{cursor}', '', ''))
167 " Internal function to create a new entry in the ChangeLog.
168 function! s:new_changelog_entry()
169 " Deal with 'paste' option.
170 let save_paste = &paste
173 " Look for an entry for today by our user.
174 let date = strftime(g:changelog_dateformat)
175 let search = s:substitute_items(g:changelog_date_entry_search, date,
176 \ g:changelog_username)
177 if search(search) > 0
178 " Ok, now we look for the end of the date entry, and add an entry.
179 call cursor(nextnonblank(line('.') + 1), 1)
180 if search(g:changelog_date_end_entry_search, 'W') > 0
181 let p = line('.') - 1
185 let ls = split(s:substitute_items(g:changelog_new_entry_format, '', ''),
188 call cursor(p + 1, 1)
190 " Flag for removing empty lines at end of new ChangeLogs.
191 let remove_empty = line('$') == 1
193 " No entry today, so create a date-user header and insert an entry.
194 let todays_entry = s:substitute_items(g:changelog_new_date_format,
195 \ date, g:changelog_username)
196 " Make sure we have a cursor positioning.
197 if stridx(todays_entry, '{cursor}') == -1
198 let todays_entry = todays_entry . '{cursor}'
202 call append(0, split(todays_entry, '\n'))
204 " Remove empty lines at end of file.
209 " Reposition cursor once we're done.
213 call s:position_cursor()
215 " And reset 'paste' option
216 let &paste = save_paste
219 if exists(":NewChangelogEntry") != 2
220 map <buffer> <silent> <Leader>o <Esc>:call <SID>new_changelog_entry()<CR>
221 command! -nargs=0 NewChangelogEntry call s:new_changelog_entry()
224 let b:undo_ftplugin = "setl com< fo< et< ai<"
227 setlocal formatoptions+=t
232 setlocal textwidth=78
233 let b:undo_ftplugin .= " tw<"
236 let &cpo = s:cpo_save
239 " Add the Changelog opening mapping
240 nmap <silent> <Leader>o :call <SID>open_changelog()<CR>
242 function! s:open_changelog()
243 if !filereadable('ChangeLog')
246 let buf = bufnr('ChangeLog')
248 if bufwinnr(buf) != -1
249 execute bufwinnr(buf) . 'wincmd w'
251 execute 'sbuffer' buf
257 call s:new_changelog_entry()