7
|
1 " Vim filetype plugin file
|
375
|
2 " Language: generic Changelog file
|
839
|
3 " Maintainer: Nikolai Weibull <now@bitwi.se>
|
|
4 " Latest Revision: 2006-04-19
|
7
|
5 " Variables:
|
839
|
6 " g:changelog_timeformat (deprecated: use g:changelog_dateformat instead) -
|
375
|
7 " description: the timeformat used in ChangeLog entries.
|
|
8 " default: "%Y-%m-%d".
|
839
|
9 " g:changelog_dateformat -
|
|
10 " description: the format sent to strftime() to generate a date string.
|
|
11 " default: "%Y-%m-%d".
|
7
|
12 " g:changelog_username -
|
375
|
13 " description: the username to use in ChangeLog entries
|
|
14 " default: try to deduce it from environment variables and system files.
|
7
|
15 " Local Mappings:
|
|
16 " <Leader>o -
|
375
|
17 " adds a new changelog entry for the current user for the current date.
|
7
|
18 " Global Mappings:
|
|
19 " <Leader>o -
|
375
|
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.
|
7
|
23 " Notes:
|
|
24 " run 'runtime ftplugin/changelog.vim' to enable the global mapping for
|
|
25 " changelog files.
|
|
26 " TODO:
|
|
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.
|
|
29
|
|
30 " If 'filetype' isn't "changelog", we must have been to add ChangeLog opener
|
839
|
31 if &filetype == 'changelog'
|
|
32 if exists('b:did_ftplugin')
|
7
|
33 finish
|
|
34 endif
|
|
35 let b:did_ftplugin = 1
|
|
36
|
375
|
37 let s:cpo_save = &cpo
|
|
38 set cpo&vim
|
7
|
39
|
839
|
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
|
|
44 else
|
|
45 let g:changelog_dateformat = "%Y-%m-%d"
|
|
46 endif
|
7
|
47 endif
|
|
48
|
|
49 " Try to figure out a reasonable username of the form:
|
839
|
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.
|
7
|
56 let g:changelog_username = $EMAIL_ADDRESS
|
|
57 else
|
839
|
58 " Get the users login name.
|
7
|
59 let login = system('whoami')
|
|
60 if v:shell_error
|
375
|
61 let login = 'unknown'
|
7
|
62 else
|
375
|
63 let newline = stridx(login, "\n")
|
|
64 if newline != -1
|
|
65 let login = strpart(login, 0, newline)
|
|
66 endif
|
7
|
67 endif
|
|
68
|
839
|
69 " Try to get the full name from gecos field in /etc/passwd.
|
7
|
70 if filereadable('/etc/passwd')
|
839
|
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, ',')
|
|
76 if comma != -1
|
|
77 let name = strpart(name, 0, comma)
|
|
78 endif
|
|
79 " And substitute & in the real name with the login of our user.
|
|
80 let amp = stridx(name, '&')
|
|
81 if amp != -1
|
|
82 let name = strpart(name, 0, amp) . toupper(login[0]) .
|
|
83 \ strpart(login, 1) . strpart(name, amp + 1)
|
|
84 endif
|
|
85 endif
|
|
86 endfor
|
7
|
87 endif
|
|
88
|
839
|
89 " If we haven't found a name, try to gather it from other places.
|
|
90 if !exists('name')
|
|
91 " Maybe the environment has something of interest.
|
375
|
92 if exists("$NAME")
|
|
93 let name = $NAME
|
|
94 else
|
|
95 " No? well, use the login name and capitalize first
|
839
|
96 " character.
|
375
|
97 let name = toupper(login[0]) . strpart(login, 1)
|
|
98 endif
|
7
|
99 endif
|
|
100
|
839
|
101 " Get our hostname.
|
|
102 let hostname = system('hostname')
|
7
|
103 if v:shell_error
|
839
|
104 let hostname = 'localhost'
|
7
|
105 else
|
375
|
106 let newline = stridx(hostname, "\n")
|
|
107 if newline != -1
|
|
108 let hostname = strpart(hostname, 0, newline)
|
|
109 endif
|
7
|
110 endif
|
|
111
|
839
|
112 " And finally set the username.
|
|
113 let g:changelog_username = name . ' <' . login . '@' . hostname . '>'
|
7
|
114 endif
|
|
115 endif
|
|
116
|
839
|
117 " Format used for new date entries.
|
|
118 if !exists('g:changelog_new_date_format')
|
7
|
119 let g:changelog_new_date_format = "%d %u\n\n\t* %c\n\n"
|
|
120 endif
|
|
121
|
839
|
122 " Format used for new entries to current date entry.
|
|
123 if !exists('g:changelog_new_entry_format')
|
7
|
124 let g:changelog_new_entry_format = "\t* %c"
|
|
125 endif
|
|
126
|
839
|
127 " Regular expression used to find a given date entry.
|
|
128 if !exists('g:changelog_date_entry_search')
|
7
|
129 let g:changelog_date_entry_search = '^\s*%d\_s*%u'
|
|
130 endif
|
|
131
|
839
|
132 " Substitutes specific items in new date-entry formats and search strings.
|
|
133 " Can be done with substitute of course, but unclean, and need \@! then.
|
7
|
134 function! s:substitute_items(str, date, user)
|
|
135 let str = a:str
|
839
|
136 let middles = {'%': '%', 'd': a:date, 'u': a:user, 'c': '{cursor}'}
|
7
|
137 let i = stridx(str, '%')
|
|
138 while i != -1
|
839
|
139 let inc = 0
|
|
140 if has_key(middles, str[i + 1])
|
|
141 let mid = middles[str[i + 1]]
|
|
142 let str = strpart(str, 0, i) . mid . strpart(str, i + 2)
|
|
143 let inc = strlen(mid)
|
7
|
144 endif
|
839
|
145 let i = stridx(str, '%', i + 1 + inc)
|
7
|
146 endwhile
|
|
147 return str
|
|
148 endfunction
|
|
149
|
839
|
150 " Position the cursor once we've done all the funky substitution.
|
7
|
151 function! s:position_cursor()
|
|
152 if search('{cursor}') > 0
|
839
|
153 let lnum = line('.')
|
|
154 let line = getline(lnum)
|
7
|
155 let cursor = stridx(line, '{cursor}')
|
839
|
156 call setline(lnum, substitute(line, '{cursor}', '', ''))
|
7
|
157 endif
|
|
158 startinsert!
|
|
159 endfunction
|
|
160
|
839
|
161 " Internal function to create a new entry in the ChangeLog.
|
7
|
162 function! s:new_changelog_entry()
|
839
|
163 " Deal with 'paste' option.
|
7
|
164 let save_paste = &paste
|
|
165 let &paste = 1
|
839
|
166 call cursor(1, 1)
|
|
167 " Look for an entry for today by our user.
|
|
168 let date = strftime(g:changelog_dateformat)
|
7
|
169 let search = s:substitute_items(g:changelog_date_entry_search, date,
|
839
|
170 \ g:changelog_username)
|
7
|
171 if search(search) > 0
|
839
|
172 " Ok, now we look for the end of the date entry, and add an entry.
|
|
173 call cursor(nextnonblank(line('.') + 1), 1)
|
|
174 if search('^\s*$', 'W') > 0
|
|
175 let p = line('.') - 1
|
|
176 else
|
|
177 let p = line('.')
|
|
178 endif
|
|
179 let ls = split(s:substitute_items(g:changelog_new_entry_format, '', ''),
|
|
180 \ '\n')
|
|
181 call append(p, ls)
|
|
182 call cursor(p + 1, 1)
|
7
|
183 else
|
839
|
184 " Flag for removing empty lines at end of new ChangeLogs.
|
7
|
185 let remove_empty = line('$') == 1
|
|
186
|
839
|
187 " No entry today, so create a date-user header and insert an entry.
|
7
|
188 let todays_entry = s:substitute_items(g:changelog_new_date_format,
|
839
|
189 \ date, g:changelog_username)
|
|
190 " Make sure we have a cursor positioning.
|
7
|
191 if stridx(todays_entry, '{cursor}') == -1
|
839
|
192 let todays_entry = todays_entry . '{cursor}'
|
7
|
193 endif
|
|
194
|
839
|
195 " Now do the work.
|
|
196 call append(0, split(todays_entry, '\n'))
|
|
197
|
|
198 " Remove empty lines at end of file.
|
7
|
199 if remove_empty
|
839
|
200 $-/^\s*$/-1,$delete
|
7
|
201 endif
|
|
202
|
839
|
203 " Reposition cursor once we're done.
|
|
204 call cursor(1, 1)
|
7
|
205 endif
|
|
206
|
|
207 call s:position_cursor()
|
|
208
|
|
209 " And reset 'paste' option
|
|
210 let &paste = save_paste
|
|
211 endfunction
|
|
212
|
|
213 if exists(":NewChangelogEntry") != 2
|
|
214 map <buffer> <silent> <Leader>o <Esc>:call <SID>new_changelog_entry()<CR>
|
|
215 command! -nargs=0 NewChangelogEntry call s:new_changelog_entry()
|
|
216 endif
|
|
217
|
839
|
218 let b:undo_ftplugin = "setl com< fo< et< ai<"
|
7
|
219
|
|
220 setlocal comments=
|
|
221 setlocal formatoptions+=t
|
|
222 setlocal noexpandtab
|
375
|
223 setlocal autoindent
|
7
|
224
|
839
|
225 if &textwidth == 0
|
|
226 setlocal textwidth=78
|
|
227 let b:undo_ftplugin .= " tw<"
|
|
228 endif
|
|
229
|
375
|
230 let &cpo = s:cpo_save
|
|
231 unlet s:cpo_save
|
7
|
232 else
|
|
233 " Add the Changelog opening mapping
|
|
234 nmap <silent> <Leader>o :call <SID>open_changelog()<CR>
|
|
235
|
|
236 function! s:open_changelog()
|
839
|
237 if !filereadable('ChangeLog')
|
|
238 return
|
|
239 endif
|
|
240 let buf = bufnr('ChangeLog')
|
|
241 if buf != -1
|
|
242 if bufwinnr(buf) != -1
|
|
243 execute buf . 'wincmd w'
|
7
|
244 else
|
839
|
245 execute 'bsplit' buf
|
7
|
246 endif
|
839
|
247 else
|
|
248 split ChangeLog
|
|
249 endif
|
7
|
250
|
839
|
251 call s:new_changelog_entry()
|
7
|
252 endfunction
|
|
253 endif
|