]> git.madduck.net Git - etc/vim.git/blob - ftplugin/mkd.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge branch 'mappings' of https://github.com/cirosantilli/vim-markdown into cirosant...
[etc/vim.git] / ftplugin / mkd.vim
1 "TODO print messages when on visual mode. I only see VISUAL, not the messages.
2
3 "this is how you should view things:
4 "
5 "  |BUFFER
6 "  |
7 "  |outside any header
8 "  |
9 "a-+# a
10 "  |
11 "  |inside a
12 "  |
13 "a-+
14 "b-+## b
15 "  |
16 "  |inside b
17 "  |
18 "b-+
19 "c-+### c
20 "  |
21 "  |inside c
22 "  |
23 "c-+
24 "d-|# d
25 "  |
26 "  |inside d
27 "  |
28 "d-+
29
30 let s:headerExpr = '\v^#'
31
32 "0 if not found
33 fu! b:Markdown_GetLineNumCurHeader()
34     retu search( s:headerExpr, 'bcnW' )
35 endf
36
37 "- if inside a header goes to it
38 "   returns its hashes
39 "- if on top level outside any headers,
40 "   print a warning
41 "   return ''
42 fu! b:Markdown_GoCurHeaderGetHashes()
43     let l:lineNum = b:Markdown_GetLineNumCurHeader()
44     if l:lineNum != 0
45         cal cursor( l:lineNum, 1 )
46         retu matchstr( getline( lineNum ), '\v^#+' )
47     el  
48         retu ''
49     en
50 endf
51
52 "- if inside a header goes to it
53 "   returns its line number
54 "- if on top level outside any headers,
55 "   print a warning
56 "   return 0
57 fu! b:Markdown_GoCurHeader()
58     let l:lineNum = b:Markdown_GetLineNumCurHeader()
59     if l:lineNum != 0
60         cal cursor( l:lineNum, 1 )
61     el
62         ec 'outside any header'
63         "norm! gg
64     en
65     retu l:lineNum
66 endf
67
68 "goes to next header of any level
69 "
70 "if no there are no more headers print a warning
71 fu! b:Markdown_GoNextHeader()
72     if search( s:headerExpr, 'W' ) == 0
73         "norm! G
74         ec 'no next header'
75     en
76 endf
77
78 "goes to previous header of any level
79 "
80 "if it does not exist, print a warning
81 fu! b:Markdown_GoPreviousHeader()
82     let l:oldPos = getpos('.')
83     let l:curHeaderLineNumber = b:Markdown_GoCurHeader()
84     if l:curHeaderLineNumber == 0
85         cal setpos('.',l:oldPos)
86     en
87     if search( s:headerExpr, 'bW' ) == 0
88         "norm! gg
89         cal setpos('.',l:oldPos)
90         ec 'no previous header'
91     en
92 endf
93
94 "goes to previous header of any level
95 "
96 "if it exists, return its lines number
97 "
98 "otherwise, print a warning and return 0
99 fu! b:Markdown_GoHeaderUp()
100     let l:oldPos = getpos('.')
101     let l:hashes = b:Markdown_GoCurHeaderGetHashes()
102     if len( l:hashes ) > 1
103         cal search( '^' . l:hashes[1:] . '[^#]', 'b' )
104     el
105         cal setpos('.',l:oldPos)
106         ec 'already at top level'
107     en
108 endf
109
110 "if no more next siblings, print error message and do nothing.
111 fu! b:Markdown_GoNextSiblingHeader()
112     let l:oldPos = getpos('.')
113     let l:hashes = b:Markdown_GoCurHeaderGetHashes()
114     let l:noSibling = 0
115
116     if l:hashes ==# ''
117         let l:noSibling = 1
118     el
119         let l:nhashes = len(l:hashes)
120         if l:nhashes == 1
121             "special case, just add the largest possible value
122             let l:nextLowerLevelLine  = line('$') + 1
123         el
124             let l:nextLowerLevelLine  = search( '\v^#{1,' . ( l:nhashes - 1 ) . '}[^#]' , 'nW' )
125         en
126
127         let l:nextSameLevelLine   = search( '\v^' . l:hashes . '[^#]', 'nW' )
128         if (
129                 \ l:nextSameLevelLine > 0
130                 \ &&
131                 \ (
132                 \   l:nextLowerLevelLine == 0
133                 \   ||
134                 \   l:nextLowerLevelLine > l:nextSameLevelLine
135                 \ )
136             \ )
137             cal cursor( l:nextSameLevelLine, 1 )
138         el
139             let l:noSibling = 1
140         en
141     en
142
143     if l:noSibling
144         cal setpos('.',l:oldPos)
145         ec 'no next sibling'
146     en
147 endf
148
149 "if no more next siblings, print error message and do nothing.
150 fu! b:Markdown_GoPreviousSiblingHeader()
151     let l:oldPos = getpos('.')
152     let l:hashes = b:Markdown_GoCurHeaderGetHashes()
153     let l:noSibling = 0
154
155     if l:hashes ==# ''
156         let l:noSibling = 1
157     el
158         let l:nhashes = len(l:hashes)
159         if l:nhashes == 1
160             "special case, just add the largest possible value
161             let l:prevLowerLevelLine  = -1
162         el
163             let l:prevLowerLevelLine  = search( '\v^#{1,' . ( l:nhashes - 1 ) . '}[^#]' , 'bnW' )
164         en
165
166         let l:prevSameLevelLine   = search( '\v^' . l:hashes . '[^#]', 'bnW' )
167         if (
168                 \ l:prevSameLevelLine > 0
169                 \ &&
170                 \ (
171                 \   l:prevLowerLevelLine == 0
172                 \   ||
173                 \   l:prevLowerLevelLine < l:prevSameLevelLine
174                 \ )
175             \ )
176             cal cursor( l:prevSameLevelLine, 1 )
177         el
178             let l:noSibling = 1
179         en
180     en
181     
182     if l:noSibling
183         cal setpos('.',l:oldPos)
184         ec 'no previous sibling'
185     en
186
187 endf
188
189 "wrapper to do move commands in visual mode
190 fu! s:VisMove(f)
191     norm! gv
192     cal function(a:f)()
193 endf
194
195 "map in both normal and visual modes
196 fu! s:MapNormVis(rhs,lhs)
197     exe 'nn <buffer><silent> ' . a:rhs . ' :cal ' . a:lhs . '()<cr>'
198     exe 'vn <buffer><silent> ' . a:rhs . ' <esc>:cal <sid>VisMove(''' . a:lhs . ''')<cr>'
199 endf
200
201 cal <sid>MapNormVis( ']]', 'b:Markdown_GoNextHeader' )
202 cal <sid>MapNormVis( '[[', 'b:Markdown_GoPreviousHeader' )
203 cal <sid>MapNormVis( '][', 'b:Markdown_GoNextSiblingHeader' )
204 cal <sid>MapNormVis( '[]', 'b:Markdown_GoPreviousSiblingHeader' )
205 "menmonic: Up
206 cal <sid>MapNormVis( ']u', 'b:Markdown_GoHeaderUp' )
207 "menmonic: Current
208 cal <sid>MapNormVis( ']c', 'b:Markdown_GoCurHeader' )