blob: a3079d198836a320558565c00cadc2b2d6fed314 [file] [log] [blame]
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001" Vim filetype plugin file (GUI menu and folding)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002" Language: Debian Changelog
3" Maintainer: Michael Piefel <piefel@informatik.hu-berlin.de>
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004" Stefano Zacchiroli <zack@debian.org>
Bram Moolenaar9964e462007-05-05 17:54:07 +00005" Last Change: $LastChangedDate: 2006-08-24 23:41:26 +0200 (gio, 24 ago 2006) $
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +00006" License: GNU GPL, version 2.0 or later
7" URL: http://svn.debian.org/wsvn/pkg-vim/trunk/runtime/ftplugin/debchangelog.vim?op=file&rev=0&sc=0
8
9if exists("b:did_ftplugin")
10 finish
11endif
Bram Moolenaar9964e462007-05-05 17:54:07 +000012let b:did_ftplugin=1
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +000013
14" {{{1 Local settings (do on every load)
15setlocal foldmethod=expr
16setlocal foldexpr=GetDebChangelogFold(v:lnum)
17setlocal foldtext=DebChangelogFoldText()
18
19" Debian changelogs are not supposed to have any other text width,
20" so the user cannot override this setting
21setlocal tw=78
22setlocal comments=f:*
23
24" Clean unloading
25let b:undo_ftplugin = "setlocal tw< comments< foldmethod< foldexpr< foldtext<"
26" }}}1
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
28if exists("g:did_changelog_ftplugin")
29 finish
30endif
31
32" Don't load another plugin (this is global)
33let g:did_changelog_ftplugin = 1
34
Bram Moolenaarf193fff2006-04-27 00:02:13 +000035" {{{1 GUI menu
36
Bram Moolenaar071d4272004-06-13 20:20:40 +000037" Helper functions returning various data.
38" Returns full name, either from $DEBFULLNAME or debianfullname.
39" TODO Is there a way to determine name from anywhere else?
40function <SID>FullName()
41 if exists("$DEBFULLNAME")
42 return $DEBFULLNAME
43 elseif exists("g:debianfullname")
44 return g:debianfullname
45 else
46 return "Your Name"
47 endif
48endfunction
49
50" Returns email address, from $DEBEMAIL, $EMAIL or debianemail.
51function <SID>Email()
52 if exists("$DEBEMAIL")
53 return $DEBEMAIL
54 elseif exists("$EMAIL")
55 return $EMAIL
56 elseif exists("g:debianemail")
Bram Moolenaarae5bce12005-08-15 21:41:48 +000057 return g:debianemail
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 else
59 return "your@email.address"
60 endif
61endfunction
62
63" Returns date in RFC822 format.
64function <SID>Date()
65 let savelang = v:lc_time
66 execute "language time C"
67 let dateandtime = strftime("%a, %d %b %Y %X %z")
68 execute "language time " . savelang
69 return dateandtime
70endfunction
71
72function <SID>WarnIfNotUnfinalised()
73 if match(getline("."), " -- [[:alpha:]][[:alnum:].]")!=-1
74 echohl WarningMsg
75 echo "The entry has not been unfinalised before editing."
76 echohl None
77 return 1
78 endif
79 return 0
80endfunction
81
82function <SID>Finalised()
83 let savelinenum = line(".")
84 normal 1G
85 call search("^ -- ")
86 if match(getline("."), " -- [[:alpha:]][[:alnum:].]")!=-1
87 let returnvalue = 1
88 else
89 let returnvalue = 0
90 endif
91 execute savelinenum
92 return returnvalue
93endfunction
94
95" These functions implement the menus
96function NewVersion()
97 " The new entry is unfinalised and shall be changed
98 amenu disable Changelog.New\ Version
99 amenu enable Changelog.Add\ Entry
100 amenu enable Changelog.Close\ Bug
101 amenu enable Changelog.Set\ Distribution
102 amenu enable Changelog.Set\ Urgency
103 amenu disable Changelog.Unfinalise
104 amenu enable Changelog.Finalise
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +0000105 call append(0, substitute(getline(1), '-\([[:digit:]]\+\))', '-$$\1)', ''))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 call append(1, "")
107 call append(2, "")
108 call append(3, " -- ")
109 call append(4, "")
110 call Distribution("unstable")
111 call Urgency("low")
112 normal 1G
113 call search(")")
114 normal h
115 normal 
Bram Moolenaarbd2ac7e2006-04-28 22:34:45 +0000116 call setline(1, substitute(getline(1), '-\$\$', '-', ''))
117 normal zo
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 call AddEntry()
119endfunction
120
121function AddEntry()
122 normal 1G
123 call search("^ -- ")
124 normal kk
125 call append(".", " * ")
126 normal jjj
127 let warn=<SID>WarnIfNotUnfinalised()
128 normal kk
129 if warn
130 echohl MoreMsg
131 call input("Hit ENTER")
132 echohl None
133 endif
134 startinsert!
135endfunction
136
137function CloseBug()
138 normal 1G
139 call search("^ -- ")
140 let warn=<SID>WarnIfNotUnfinalised()
141 normal kk
142 call append(".", " * (closes: #" . input("Bug number to close: ") . ")")
143 normal j^ll
144 startinsert
145endfunction
146
147function Distribution(dist)
148 call setline(1, substitute(getline(1), ") [[:lower:] ]*;", ") " . a:dist . ";", ""))
149endfunction
150
151function Urgency(urg)
152 call setline(1, substitute(getline(1), "urgency=.*$", "urgency=" . a:urg, ""))
153endfunction
154
155function <SID>UnfinaliseMenu()
156 " This means the entry shall be changed
157 amenu disable Changelog.New\ Version
158 amenu enable Changelog.Add\ Entry
159 amenu enable Changelog.Close\ Bug
160 amenu enable Changelog.Set\ Distribution
161 amenu enable Changelog.Set\ Urgency
162 amenu disable Changelog.Unfinalise
163 amenu enable Changelog.Finalise
164endfunction
165
166function Unfinalise()
167 call <SID>UnfinaliseMenu()
168 normal 1G
169 call search("^ -- ")
170 call setline(".", " -- ")
171endfunction
172
173function <SID>FinaliseMenu()
174 " This means the entry should not be changed anymore
175 amenu enable Changelog.New\ Version
176 amenu disable Changelog.Add\ Entry
177 amenu disable Changelog.Close\ Bug
178 amenu disable Changelog.Set\ Distribution
179 amenu disable Changelog.Set\ Urgency
180 amenu enable Changelog.Unfinalise
181 amenu disable Changelog.Finalise
182endfunction
183
184function Finalise()
185 call <SID>FinaliseMenu()
186 normal 1G
187 call search("^ -- ")
188 call setline(".", " -- " . <SID>FullName() . " <" . <SID>Email() . "> " . <SID>Date())
189endfunction
190
191
192function <SID>MakeMenu()
193 amenu &Changelog.&New\ Version :call NewVersion()<CR>
194 amenu Changelog.&Add\ Entry :call AddEntry()<CR>
195 amenu Changelog.&Close\ Bug :call CloseBug()<CR>
196 menu Changelog.-sep- <nul>
197
198 amenu Changelog.Set\ &Distribution.&unstable :call Distribution("unstable")<CR>
199 amenu Changelog.Set\ Distribution.&frozen :call Distribution("frozen")<CR>
200 amenu Changelog.Set\ Distribution.&stable :call Distribution("stable")<CR>
201 menu Changelog.Set\ Distribution.-sep- <nul>
202 amenu Changelog.Set\ Distribution.frozen\ unstable :call Distribution("frozen unstable")<CR>
203 amenu Changelog.Set\ Distribution.stable\ unstable :call Distribution("stable unstable")<CR>
204 amenu Changelog.Set\ Distribution.stable\ frozen :call Distribution("stable frozen")<CR>
205 amenu Changelog.Set\ Distribution.stable\ frozen\ unstable :call Distribution("stable frozen unstable")<CR>
206
207 amenu Changelog.Set\ &Urgency.&low :call Urgency("low")<CR>
208 amenu Changelog.Set\ Urgency.&medium :call Urgency("medium")<CR>
209 amenu Changelog.Set\ Urgency.&high :call Urgency("high")<CR>
210
211 menu Changelog.-sep- <nul>
212 amenu Changelog.U&nfinalise :call Unfinalise()<CR>
213 amenu Changelog.&Finalise :call Finalise()<CR>
214
215 if <SID>Finalised()
216 call <SID>FinaliseMenu()
217 else
218 call <SID>UnfinaliseMenu()
219 endif
220endfunction
221
222augroup changelogMenu
223au BufEnter * if &filetype == "debchangelog" | call <SID>MakeMenu() | endif
224au BufLeave * if &filetype == "debchangelog" | aunmenu Changelog | endif
225augroup END
226
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000227" }}}
228" {{{1 folding
229
Bram Moolenaar9964e462007-05-05 17:54:07 +0000230" look for an author name in the [zonestart zoneend] lines searching backward
231function! s:getAuthor(zonestart, zoneend)
232 let linepos = a:zoneend
233 while linepos >= a:zonestart
234 let line = getline(linepos)
235 if line =~ '^ --'
236 return substitute(line, '^ --\s*\([^<]\+\)\s*.*', '\1', '')
237 endif
238 let linepos -= 1
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000239 endwhile
Bram Moolenaar9964e462007-05-05 17:54:07 +0000240 return '[unknown]'
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000241endfunction
242
243function! DebChangelogFoldText()
244 if v:folddashes == '-' " changelog entry fold
Bram Moolenaar9964e462007-05-05 17:54:07 +0000245 return foldtext() . ' -- ' . s:getAuthor(v:foldstart, v:foldend) . ' '
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000246 endif
247 return foldtext()
248endfunction
249
250function! GetDebChangelogFold(lnum)
251 let line = getline(a:lnum)
252 if line =~ '^\w\+'
253 return '>1' " beginning of a changelog entry
254 endif
255 if line =~ '^\s\+\[.*\]'
256 return '>2' " beginning of an author-specific chunk
257 endif
258 if line =~ '^ --'
259 return '1'
260 endif
261 return '='
262endfunction
263
Bram Moolenaar9964e462007-05-05 17:54:07 +0000264foldopen! " unfold the entry the cursor is on (usually the first one)
265
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000266" }}}
267
268" vim: set foldmethod=marker: