Skip to content
This repository was archived by the owner on Mar 9, 2020. It is now read-only.

Commit 681e846

Browse files
committed
Added the automatic footnotecounter.
1 parent ee30ba8 commit 681e846

File tree

1 file changed

+54
-32
lines changed

1 file changed

+54
-32
lines changed

plugin/markdownfootnotes.vim

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
" Version: 1.0
44
" URL: https://github.com/vim-pandoc/vim-markdownfootnotes
55
"
6-
" I've taken the original and modified the output to fit the widely
6+
" I've taken the original and modified the output to fit the widely
77
" supported extended markdown format for footnotes.[^note]
88
"
99
" [^note]: Like this.
1010
"
1111
" The original script either puts notes at the end, or before your
1212
" email sig line (i.e., if there is a line that consists of two dashes,
13-
" it puts the notes before that line). This version just puts them at
13+
" it puts the notes before that line). This version just puts them at
1414
" the end.
1515
"
1616
" Based On:
@@ -23,8 +23,8 @@
2323
" URL: http://www.vim.org/scripts/script.php?script_id=431
2424
" Help Part:
2525
" Inspired by Emmanuel Touzery tip:
26-
" http://vim.sourceforge.net/tip_view.php?tip_id=332
27-
" and discussion below (thanks to Luc for pluginization hints)
26+
" http://vim.sourceforge.net/tip_view.php?tip_id=332
27+
" and discussion below (thanks to Luc for pluginization hints)
2828
" I added functions and turned it into vim script.
2929
"
3030
" Installation: Drop it to your plugin directory or use pathogen.
@@ -38,34 +38,34 @@
3838
" alpha - [a] [b] ... [z] [aa] [bb] ... [zz] [a] ...
3939
" Alpha - as above but uppercase [A] ...
4040
" roman - [i] [ii] [iii] displayed properly up to 89
41-
" Roman - as above but uppercase [I] ...
42-
" star - [*] [**] [***] ...
41+
" Roman - as above but uppercase [I] ...
42+
" star - [*] [**] [***] ...
4343
"
4444
" Commands:
45-
"
45+
"
4646
" Those mappings correspond to two commands that you can use in your own
4747
" mappings:
4848
"
4949
" AddVimFootnote
50-
" ~ inserts footnotemark at cursor location, inserts footnotemark on new
50+
" ~ inserts footnotemark at cursor location, inserts footnotemark on new
5151
" line at end of file, opens a split window all ready for you to enter in
5252
" the footnote.
5353

5454
" ReturnFromFootnote
55-
" ~ closes the split window and returns to the text in proper place.
55+
" ~ closes the split window and returns to the text in proper place.
5656
"
5757
" These are mapped to <Leader>f and <Leader>r respectively.
58-
"
58+
"
5959
" FootnoteNumber
6060
" ~ Change the current footnote number (one obligatory argument)
61-
" :FootnoteNumber 5
61+
" :FootnoteNumber 5
6262
"
6363
" FootnoteNumberRestore
64-
" ~ Restore old footnote number
64+
" ~ Restore old footnote number
6565

6666
" FootnoteUndo
6767
" ~ Decrease footnote counter by 1
68-
"
68+
"
6969
" FootnoteMeta
7070
" ~ Change type of the footnotes and restart counter (1, a, A, i, I, *)
7171
" :FootnoteMeta
@@ -76,13 +76,13 @@
7676
" Change footnote type to name_of_the_type. If name_of_the_type is the
7777
" same as your current footnote type nothing would be changed.
7878
" You can change your default type of footnote before inserting first
79-
" footnote.
80-
"
79+
" footnote.
80+
"
8181
" FootnoteRestore
8282
" ~ Restore previous footnote type and counter. Unfortunately there is no easy
8383
" way to sort footnotes at the end of file without handmade :!sort on marked
8484
" lines (it doesn't work for 'star' type).
85-
" :FootnoteRestore
85+
" :FootnoteRestore
8686
"
8787
"""""""""""""""""""""""""""""""""""""""""""""""""""
8888

@@ -95,8 +95,9 @@ set cpo&vim
9595
if !exists("g:vimfootnotetype")
9696
let g:vimfootnotetype = "arabic"
9797
endif
98+
9899
if !exists("g:vimfootnotenumber")
99-
let g:vimfootnotenumber = 0
100+
let g:vimfootnotenumber = 0
100101
endif
101102

102103
" Mappings
@@ -173,7 +174,7 @@ function! s:VimFootnoteRestore()
173174
return 0
174175
endif
175176
endfunction
176-
177+
177178
function! s:VimFootnoteType(footnumber)
178179
if (g:vimfootnotetype =~ "alpha\\|Alpha")
179180
if (g:vimfootnotetype == "alpha")
@@ -254,20 +255,41 @@ function! s:VimFootnoteType(footnumber)
254255
endfunction
255256

256257
function! s:VimFootnotes(appendcmd)
257-
if (g:vimfootnotenumber != 0)
258-
let g:vimfootnotenumber = g:vimfootnotenumber + 1
259-
let g:vimfootnotemark = <sid>VimFootnoteType(g:vimfootnotenumber)
260-
let cr = "\<cr>"
261-
else
262-
let g:vimfootnotenumber = 1
263-
let g:vimfootnotemark = <sid>VimFootnoteType(g:vimfootnotenumber)
264-
let cr = "\<cr>"
265-
endif
266-
exe "normal ".a:appendcmd."[^fn".g:vimfootnotemark."]\<esc>"
267-
:below 4split
268-
normal G
269-
exe "normal o".cr."[^fn".g:vimfootnotemark."]: "
270-
startinsert!
258+
" save current position
259+
let s:cur_pos = getpos(".")
260+
" Define search pattern for footnote definitions
261+
let l:pattern = '\v^\[\^fn(.+)\]:'
262+
let l:flags = 'eW'
263+
call cursor(1,1)
264+
" get first match
265+
let g:vimfootnotenumber = search(l:pattern, l:flags)
266+
if (g:vimfootnotenumber != 0)
267+
let l:temp = 1
268+
" count subsequent matches
269+
while search(l:pattern, l:flags) != 0
270+
let l:temp += 1
271+
if l:temp > 50
272+
redraw | echohl ErrorMsg | echo "Trouble in paradise: the while loop did not work"
273+
break
274+
endif
275+
endwhile
276+
let g:vimfootnotenumber = l:temp + 1
277+
" Return to position
278+
call setpos(".", s:cur_pos)
279+
let g:vimfootnotemark = <sid>VimFootnoteType(g:vimfootnotenumber)
280+
let cr = "\<cr>"
281+
else
282+
let g:vimfootnotenumber = 1
283+
" Return to position
284+
call setpos(".", s:cur_pos)
285+
let g:vimfootnotemark = <sid>VimFootnoteType(g:vimfootnotenumber)
286+
let cr = "\<cr>"
287+
endif
288+
exe "normal ".a:appendcmd."[^fn".g:vimfootnotemark."]\<esc>"
289+
:below 4split
290+
normal G
291+
exe "normal o".cr."[^fn".g:vimfootnotemark."]: "
292+
startinsert!
271293
endfunction
272294

273295
let &cpo = s:cpo_save

0 commit comments

Comments
 (0)