| 
									
										
										
										
											2018-07-29 15:07:52 +02:00
										 |  |  | " Author: Antony Lee <anntzer.lee@gmail.com> | 
					
						
							|  |  |  | " Description: Helper functions for reStructuredText syntax folding | 
					
						
							| 
									
										
										
										
											2019-01-01 15:32:17 +01:00
										 |  |  | " Last Modified: 2018-12-29 | 
					
						
							| 
									
										
										
										
											2018-07-29 15:07:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | function s:CacheRstFold() | 
					
						
							| 
									
										
										
										
											2019-01-01 15:32:17 +01:00
										 |  |  |   if !g:rst_fold_enabled | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-29 15:07:52 +02:00
										 |  |  |   let closure = {'header_types': {}, 'max_level': 0, 'levels': {}} | 
					
						
							|  |  |  |   function closure.Process(match) dict | 
					
						
							|  |  |  |     let curline = getcurpos()[1] | 
					
						
							|  |  |  |     if has_key(self.levels, curline - 1) | 
					
						
							|  |  |  |       " For over+under-lined headers, the regex will match both at the | 
					
						
							|  |  |  |       " overline and at the title itself; in that case, skip the second match. | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |     let lines = split(a:match, '\n') | 
					
						
							|  |  |  |     let key = repeat(lines[-1][0], len(lines)) | 
					
						
							|  |  |  |     if !has_key(self.header_types, key) | 
					
						
							|  |  |  |       let self.max_level += 1 | 
					
						
							|  |  |  |       let self.header_types[key] = self.max_level | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |     let self.levels[curline] = self.header_types[key] | 
					
						
							|  |  |  |   endfunction | 
					
						
							|  |  |  |   let save_cursor = getcurpos() | 
					
						
							| 
									
										
										
										
											2019-01-01 15:32:17 +01:00
										 |  |  |   let save_mark = getpos("'[") | 
					
						
							| 
									
										
										
										
											2018-07-29 15:07:52 +02:00
										 |  |  |   silent keeppatterns %s/\v^%(%(([=`:.'"~^_*+#-])\1+\n)?.{1,2}\n([=`:.'"~^_*+#-])\2+)|%(%(([=`:.''"~^_*+#-])\3{2,}\n)?.{3,}\n([=`:.''"~^_*+#-])\4{2,})$/\=closure.Process(submatch(0))/gn | 
					
						
							|  |  |  |   call setpos('.', save_cursor) | 
					
						
							| 
									
										
										
										
											2019-01-01 15:32:17 +01:00
										 |  |  |   call setpos("'[", save_mark) | 
					
						
							| 
									
										
										
										
											2018-07-29 15:07:52 +02:00
										 |  |  |   let b:RstFoldCache = closure.levels | 
					
						
							|  |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function RstFold#GetRstFold() | 
					
						
							| 
									
										
										
										
											2019-01-01 15:32:17 +01:00
										 |  |  |   if !g:rst_fold_enabled | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-29 15:07:52 +02:00
										 |  |  |   if !has_key(b:, 'RstFoldCache') | 
					
						
							|  |  |  |     call s:CacheRstFold() | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  |   if has_key(b:RstFoldCache, v:lnum) | 
					
						
							|  |  |  |     return '>' . b:RstFoldCache[v:lnum] | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     return '=' | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function RstFold#GetRstFoldText() | 
					
						
							| 
									
										
										
										
											2019-01-01 15:32:17 +01:00
										 |  |  |   if !g:rst_fold_enabled | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-29 15:07:52 +02:00
										 |  |  |   if !has_key(b:, 'RstFoldCache') | 
					
						
							|  |  |  |     call s:CacheRstFold() | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  |   let indent = repeat('  ', b:RstFoldCache[v:foldstart] - 1) | 
					
						
							|  |  |  |   let thisline = getline(v:foldstart) | 
					
						
							|  |  |  |   " For over+under-lined headers, skip the overline. | 
					
						
							|  |  |  |   let text = thisline =~ '^\([=`:.''"~^_*+#-]\)\1\+$' ? getline(v:foldstart + 1) : thisline | 
					
						
							|  |  |  |   return indent . text | 
					
						
							|  |  |  | endfunction |