comparison runtime/ftplugin/changelog.vim @ 839:1f3b1021f002 v7.0e05

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