Merged from the latest developing branch.
[MacVim/jjgod.git] / runtime / ftplugin / debchangelog.vim
bloba3079d198836a320558565c00cadc2b2d6fed314
1 " Vim filetype plugin file (GUI menu and folding)
2 " Language:     Debian Changelog
3 " Maintainer:   Michael Piefel <piefel@informatik.hu-berlin.de>
4 "               Stefano Zacchiroli <zack@debian.org>
5 " Last Change:  $LastChangedDate: 2006-08-24 23:41:26 +0200 (gio, 24 ago 2006) $
6 " 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
9 if exists("b:did_ftplugin")
10   finish
11 endif
12 let b:did_ftplugin=1
14 " {{{1 Local settings (do on every load)
15 setlocal foldmethod=expr
16 setlocal foldexpr=GetDebChangelogFold(v:lnum)
17 setlocal foldtext=DebChangelogFoldText()
19 " Debian changelogs are not supposed to have any other text width,
20 " so the user cannot override this setting
21 setlocal tw=78
22 setlocal comments=f:* 
24 " Clean unloading
25 let b:undo_ftplugin = "setlocal tw< comments< foldmethod< foldexpr< foldtext<"
26 " }}}1
28 if exists("g:did_changelog_ftplugin")
29   finish
30 endif
32 " Don't load another plugin (this is global)
33 let g:did_changelog_ftplugin = 1
35 " {{{1 GUI menu
37 " 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?
40 function <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
48 endfunction
50 " Returns email address, from $DEBEMAIL, $EMAIL or debianemail.
51 function <SID>Email()
52     if exists("$DEBEMAIL")
53         return $DEBEMAIL
54     elseif exists("$EMAIL")
55         return $EMAIL
56     elseif exists("g:debianemail")
57         return g:debianemail
58     else
59         return "your@email.address"
60     endif
61 endfunction
63 " Returns date in RFC822 format.
64 function <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
70 endfunction
72 function <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
80 endfunction
82 function <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
93 endfunction
95 " These functions implement the menus
96 function 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
105     call append(0, substitute(getline(1), '-\([[:digit:]]\+\))', '-$$\1)', ''))
106     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 \x01
116     call setline(1, substitute(getline(1), '-\$\$', '-', ''))
117     normal zo
118     call AddEntry()
119 endfunction
121 function 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!
135 endfunction
137 function 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
145 endfunction
147 function Distribution(dist)
148     call setline(1, substitute(getline(1), ") [[:lower:] ]*;", ") " . a:dist . ";", ""))
149 endfunction
151 function Urgency(urg)
152     call setline(1, substitute(getline(1), "urgency=.*$", "urgency=" . a:urg, ""))
153 endfunction
155 function <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
164 endfunction
166 function Unfinalise()
167     call <SID>UnfinaliseMenu()
168     normal 1G
169     call search("^ -- ")
170     call setline(".", " -- ")
171 endfunction
173 function <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
182 endfunction
184 function Finalise()
185     call <SID>FinaliseMenu()
186     normal 1G
187     call search("^ -- ")
188     call setline(".", " -- " . <SID>FullName() . " <" . <SID>Email() . ">  " . <SID>Date())
189 endfunction
192 function <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>
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>
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>
211     menu Changelog.-sep-                                <nul>
212     amenu Changelog.U&nfinalise                         :call Unfinalise()<CR>
213     amenu Changelog.&Finalise                           :call Finalise()<CR>
215     if <SID>Finalised()
216         call <SID>FinaliseMenu()
217     else
218         call <SID>UnfinaliseMenu()
219     endif
220 endfunction
222 augroup changelogMenu
223 au BufEnter * if &filetype == "debchangelog" | call <SID>MakeMenu() | endif
224 au BufLeave * if &filetype == "debchangelog" | aunmenu Changelog | endif
225 augroup END
227 " }}}
228 " {{{1 folding
230 " look for an author name in the [zonestart zoneend] lines searching backward
231 function! 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
239   endwhile
240   return '[unknown]'
241 endfunction
243 function! DebChangelogFoldText()
244   if v:folddashes == '-'  " changelog entry fold
245     return foldtext() . ' -- ' . s:getAuthor(v:foldstart, v:foldend) . ' '
246   endif
247   return foldtext()
248 endfunction
250 function! 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 '='
262 endfunction
264 foldopen!   " unfold the entry the cursor is on (usually the first one)
266 " }}}
268 " vim: set foldmethod=marker: