blob: 6d1f20ee3d67501db2389cc79e406c7c9be13523 [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>
5" Last Change: 25 April 2006
6" License: GNU GPL, version 2.1 or later
Bram Moolenaar071d4272004-06-13 20:20:40 +00007
8if exists("g:did_changelog_ftplugin")
9 finish
10endif
11
12" Don't load another plugin (this is global)
13let g:did_changelog_ftplugin = 1
14
Bram Moolenaarf193fff2006-04-27 00:02:13 +000015" {{{1 GUI menu
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017" Helper functions returning various data.
18" Returns full name, either from $DEBFULLNAME or debianfullname.
19" TODO Is there a way to determine name from anywhere else?
20function <SID>FullName()
21 if exists("$DEBFULLNAME")
22 return $DEBFULLNAME
23 elseif exists("g:debianfullname")
24 return g:debianfullname
25 else
26 return "Your Name"
27 endif
28endfunction
29
30" Returns email address, from $DEBEMAIL, $EMAIL or debianemail.
31function <SID>Email()
32 if exists("$DEBEMAIL")
33 return $DEBEMAIL
34 elseif exists("$EMAIL")
35 return $EMAIL
36 elseif exists("g:debianemail")
Bram Moolenaarae5bce12005-08-15 21:41:48 +000037 return g:debianemail
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 else
39 return "your@email.address"
40 endif
41endfunction
42
43" Returns date in RFC822 format.
44function <SID>Date()
45 let savelang = v:lc_time
46 execute "language time C"
47 let dateandtime = strftime("%a, %d %b %Y %X %z")
48 execute "language time " . savelang
49 return dateandtime
50endfunction
51
52function <SID>WarnIfNotUnfinalised()
53 if match(getline("."), " -- [[:alpha:]][[:alnum:].]")!=-1
54 echohl WarningMsg
55 echo "The entry has not been unfinalised before editing."
56 echohl None
57 return 1
58 endif
59 return 0
60endfunction
61
62function <SID>Finalised()
63 let savelinenum = line(".")
64 normal 1G
65 call search("^ -- ")
66 if match(getline("."), " -- [[:alpha:]][[:alnum:].]")!=-1
67 let returnvalue = 1
68 else
69 let returnvalue = 0
70 endif
71 execute savelinenum
72 return returnvalue
73endfunction
74
75" These functions implement the menus
76function NewVersion()
77 " The new entry is unfinalised and shall be changed
78 amenu disable Changelog.New\ Version
79 amenu enable Changelog.Add\ Entry
80 amenu enable Changelog.Close\ Bug
81 amenu enable Changelog.Set\ Distribution
82 amenu enable Changelog.Set\ Urgency
83 amenu disable Changelog.Unfinalise
84 amenu enable Changelog.Finalise
85 call append(0, substitute(getline(1),'-\([[:digit:]]\+\))', '-Ü\1)', ''))
86 call append(1, "")
87 call append(2, "")
88 call append(3, " -- ")
89 call append(4, "")
90 call Distribution("unstable")
91 call Urgency("low")
92 normal 1G
93 call search(")")
94 normal h
95 normal 
96 call setline(1, substitute(getline(1),'-Ü\([[:digit:]]\+\))', '-\1)', ''))
97 call AddEntry()
98endfunction
99
100function AddEntry()
101 normal 1G
102 call search("^ -- ")
103 normal kk
104 call append(".", " * ")
105 normal jjj
106 let warn=<SID>WarnIfNotUnfinalised()
107 normal kk
108 if warn
109 echohl MoreMsg
110 call input("Hit ENTER")
111 echohl None
112 endif
113 startinsert!
114endfunction
115
116function CloseBug()
117 normal 1G
118 call search("^ -- ")
119 let warn=<SID>WarnIfNotUnfinalised()
120 normal kk
121 call append(".", " * (closes: #" . input("Bug number to close: ") . ")")
122 normal j^ll
123 startinsert
124endfunction
125
126function Distribution(dist)
127 call setline(1, substitute(getline(1), ") [[:lower:] ]*;", ") " . a:dist . ";", ""))
128endfunction
129
130function Urgency(urg)
131 call setline(1, substitute(getline(1), "urgency=.*$", "urgency=" . a:urg, ""))
132endfunction
133
134function <SID>UnfinaliseMenu()
135 " This means the entry shall be changed
136 amenu disable Changelog.New\ Version
137 amenu enable Changelog.Add\ Entry
138 amenu enable Changelog.Close\ Bug
139 amenu enable Changelog.Set\ Distribution
140 amenu enable Changelog.Set\ Urgency
141 amenu disable Changelog.Unfinalise
142 amenu enable Changelog.Finalise
143endfunction
144
145function Unfinalise()
146 call <SID>UnfinaliseMenu()
147 normal 1G
148 call search("^ -- ")
149 call setline(".", " -- ")
150endfunction
151
152function <SID>FinaliseMenu()
153 " This means the entry should not be changed anymore
154 amenu enable Changelog.New\ Version
155 amenu disable Changelog.Add\ Entry
156 amenu disable Changelog.Close\ Bug
157 amenu disable Changelog.Set\ Distribution
158 amenu disable Changelog.Set\ Urgency
159 amenu enable Changelog.Unfinalise
160 amenu disable Changelog.Finalise
161endfunction
162
163function Finalise()
164 call <SID>FinaliseMenu()
165 normal 1G
166 call search("^ -- ")
167 call setline(".", " -- " . <SID>FullName() . " <" . <SID>Email() . "> " . <SID>Date())
168endfunction
169
170
171function <SID>MakeMenu()
172 amenu &Changelog.&New\ Version :call NewVersion()<CR>
173 amenu Changelog.&Add\ Entry :call AddEntry()<CR>
174 amenu Changelog.&Close\ Bug :call CloseBug()<CR>
175 menu Changelog.-sep- <nul>
176
177 amenu Changelog.Set\ &Distribution.&unstable :call Distribution("unstable")<CR>
178 amenu Changelog.Set\ Distribution.&frozen :call Distribution("frozen")<CR>
179 amenu Changelog.Set\ Distribution.&stable :call Distribution("stable")<CR>
180 menu Changelog.Set\ Distribution.-sep- <nul>
181 amenu Changelog.Set\ Distribution.frozen\ unstable :call Distribution("frozen unstable")<CR>
182 amenu Changelog.Set\ Distribution.stable\ unstable :call Distribution("stable unstable")<CR>
183 amenu Changelog.Set\ Distribution.stable\ frozen :call Distribution("stable frozen")<CR>
184 amenu Changelog.Set\ Distribution.stable\ frozen\ unstable :call Distribution("stable frozen unstable")<CR>
185
186 amenu Changelog.Set\ &Urgency.&low :call Urgency("low")<CR>
187 amenu Changelog.Set\ Urgency.&medium :call Urgency("medium")<CR>
188 amenu Changelog.Set\ Urgency.&high :call Urgency("high")<CR>
189
190 menu Changelog.-sep- <nul>
191 amenu Changelog.U&nfinalise :call Unfinalise()<CR>
192 amenu Changelog.&Finalise :call Finalise()<CR>
193
194 if <SID>Finalised()
195 call <SID>FinaliseMenu()
196 else
197 call <SID>UnfinaliseMenu()
198 endif
199endfunction
200
201augroup changelogMenu
202au BufEnter * if &filetype == "debchangelog" | call <SID>MakeMenu() | endif
203au BufLeave * if &filetype == "debchangelog" | aunmenu Changelog | endif
204augroup END
205
206" Debian changelogs are not supposed to have any other text width,
207" so the user cannot override this setting
208setlocal tw=78
209setlocal comments=f:*
210let b:undo_ftplugin = "setlocal tw< comments<"
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000211
212" }}}
213" {{{1 folding
214
215setlocal foldmethod=expr
216set foldexpr=GetDebChangelogFold(v:lnum)
217setlocal foldtext=DebChangelogFoldText()
218
219" look for an author name searching backward from a given line number
220function! s:getAuthor(lnum)
221 let line = getline(a:lnum)
222 let backsteps = 0
223 while line !~ '^ --'
224 let backsteps += 1
225 let line = getline(a:lnum - backsteps)
226 endwhile
227 let author = substitute(line, '^ --\s*\([^<]\+\)\s*.*', '\1', '')
228 return author
229endfunction
230
231function! DebChangelogFoldText()
232 if v:folddashes == '-' " changelog entry fold
233 return foldtext() . ' -- ' . s:getAuthor(v:foldend) . ' '
234 endif
235 return foldtext()
236endfunction
237
238function! GetDebChangelogFold(lnum)
239 let line = getline(a:lnum)
240 if line =~ '^\w\+'
241 return '>1' " beginning of a changelog entry
242 endif
243 if line =~ '^\s\+\[.*\]'
244 return '>2' " beginning of an author-specific chunk
245 endif
246 if line =~ '^ --'
247 return '1'
248 endif
249 return '='
250endfunction
251
252" }}}
253
254" vim: set foldmethod=marker: