|
| 1 | +" Author: Mikolaj Machowski <[email protected]> |
| 2 | +" Version: 0.3 |
| 3 | +" Description: Footnotes in Vim |
| 4 | +" Installation: See below |
| 5 | +" Last change: czw wrz 26 09:00 2002 C |
| 6 | +" |
| 7 | +" Inspired by Emmanuel Touzery tip: |
| 8 | +" http://vim.sourceforge.net/tip_view.php?tip_id=332 |
| 9 | +" and discussion below (thanks to Luc for pluginization hints) |
| 10 | +" I added functions and turned it into vim script. |
| 11 | +" |
| 12 | +" Installation: Drop it to your plugin directory but you can declare your |
| 13 | +" favorite types of footnotes in your ftplugins. |
| 14 | +" |
| 15 | +" |
| 16 | +" Commands: |
| 17 | +" <Leader>f (in insert mode) - inserts footnotemark, opens at bottom window where |
| 18 | +" footnotemark is also inserted and you are ready to type in your |
| 19 | +" footnotetext. |
| 20 | +" <Leader>r (in insert mode) - closes footnote window and returns to the text in |
| 21 | +" proper place. |
| 22 | +" |
| 23 | +" You can change them by placing in your vimrc: |
| 24 | +" imap your_map <Plug>AddVimFootnote |
| 25 | +" imap your_map <Plug>ReturnFromFootnote |
| 26 | +" |
| 27 | +" |
| 28 | +" Footnotes are placed at the end of the file but above signature delimiter |
| 29 | +" (is one exists). |
| 30 | +" |
| 31 | +" Settings: |
| 32 | +" g:vimfootnotetype - possible values: |
| 33 | +" arabic (default) - [1] [2] [3] ... |
| 34 | +" alpha - [a] [b] ... [z] [aa] [bb] ... [zz] [a] ... |
| 35 | +" Alpha - as above but uppercase [A] ... |
| 36 | +" star - [*] [**] [***] ... |
| 37 | +" |
| 38 | +" Additional commands: |
| 39 | +" FootnoteNumber: |
| 40 | +" You can change current footnote number (one obligatory argument) |
| 41 | +" :FootnoteNumber 5 |
| 42 | +" FootnoteNumberRestore: |
| 43 | +" You can restore old footnote number |
| 44 | +" :FootnoteNumberRestore |
| 45 | +" FootnoteUndo: |
| 46 | +" Decrease footnote counter by 1 |
| 47 | +" :FootnoteUndo |
| 48 | +" FootnoteMeta: |
| 49 | +" Change type of the footnotes and restart counter (1, a, A, *) |
| 50 | +" :FootnoteMeta |
| 51 | +" If your previous footnote type was alpha, Alpha or star new type will |
| 52 | +" be arabic. |
| 53 | +" If your previous footnote type was arabic new type will be alpha. |
| 54 | +" :FootnoteMeta name_of_the_type |
| 55 | +" Change footnote type to name_of_the_type. If name_of_the_type is the |
| 56 | +" same as your current footnote type nothing would be changed. |
| 57 | +" FootnoteRestore: |
| 58 | +" Restore previous footnote type and counter. Unfortunately there is no easy |
| 59 | +" way to sort footnotes at the end of file without handmade :!sort on marked |
| 60 | +" lines (it doesn't work for 'star' type). |
| 61 | +" :FootnoteRestore |
| 62 | +" |
| 63 | +" For easier work with this commands I would suggest place this lines in your |
| 64 | +" vimrc (they offer very nice competion of Vim commands): |
| 65 | +" set laststatus=2 |
| 66 | +" set wildmode=longest,list |
| 67 | +" set wildmenu |
| 68 | +" |
| 69 | +" And/or map :FootnoteComs for something you like. |
| 70 | +" |
| 71 | +""""""""""""""""""""""""""""""""""""""""""""""""""" |
| 72 | +if exists("g:loaded_footnote_vim") | finish | endif |
| 73 | + let g:loaded_footnote_vim = 1 |
| 74 | + |
| 75 | +if !hasmapto('<Plug>AddVimFootnote', 'i') |
| 76 | + imap <Leader>f <Plug>AddVimFootnote |
| 77 | +endif |
| 78 | +if !hasmapto('<Plug>ReturnFromFootnote', 'i') |
| 79 | + imap <leader>r <Plug>ReturnFromFootnote |
| 80 | +endif |
| 81 | + |
| 82 | +imap <Plug>AddVimFootnote <C-O>:call <SID>VimFootnotes()<CR> |
| 83 | +imap <Plug>ReturnFromFootnote <C-O>:q<CR><Right> |
| 84 | +
|
| 85 | +" :Footnote commands |
| 86 | +command! -nargs=1 FootnoteNumber call <sid>VimFootnoteNumber(<q-args>) |
| 87 | +command! -nargs=0 FootnoteNumberRestore call <sid>VimFootnoteNumberRestore() |
| 88 | +command! -nargs=0 FootnoteUndo |
| 89 | + \ | let g:vimfootnotenumber = g:vimfootnotenumber - 1 |
| 90 | +command! -nargs=? FootnoteMeta call <sid>VimFootnoteMeta(<f-args>) |
| 91 | +command! -nargs=0 FootnoteRestore call <sid>VimFootnoteRestore() |
| 92 | + |
| 93 | +function! s:VimFootnoteNumber(newnumber) |
| 94 | + let g:oldvimfootnotenumber = g:vimfootnotenumber |
| 95 | + let g:vimfootnotenumber = a:newnumber |
| 96 | +endfunction |
| 97 | + |
| 98 | +function! s:VimFootnoteNumberRestore() |
| 99 | + if exists(g:oldvimfootnotenumber) |
| 100 | + let g:vimfootnotenumber = g:oldvimfootnotenumber |
| 101 | + else |
| 102 | + return 0 |
| 103 | + endif |
| 104 | +endfunction |
| 105 | + |
| 106 | +function! s:VimFootnoteMeta(...) |
| 107 | + let g:oldvimfootnotetype = g:vimfootnotetype |
| 108 | + let g:oldvimfootnotenumber = g:vimfootnotenumber |
| 109 | + if a:0 == "0" |
| 110 | + if (g:vimfootnotetype == "arabic") |
| 111 | + let g:vimfootnotetype = "alpha" |
| 112 | + else |
| 113 | + let g:vimfootnotetype = "arabic" |
| 114 | + endif |
| 115 | + else |
| 116 | + if (a:1 == g:vimfootnotetype) |
| 117 | + echomsg "You have chosen the same footnote type! Command won't affect." |
| 118 | + return 0 |
| 119 | + else |
| 120 | + let g:vimfootnotetype = a:1 |
| 121 | + endif |
| 122 | + endif |
| 123 | + let g:vimfootnotenumber = 0 |
| 124 | +endfunction |
| 125 | + |
| 126 | +function! s:VimFootnoteRestore() |
| 127 | + if exists("g:oldvimfootnotenumber") |
| 128 | + let oldvimfootnotetype2 = g:vimfootnotetype |
| 129 | + let oldvimfootnotenumber2 = g:vimfootnotenumber |
| 130 | + let g:vimfootnotetype = g:oldvimfootnotetype |
| 131 | + let g:vimfootnotenumber = g:oldvimfootnotenumber |
| 132 | + let g:oldvimfootnotetype = oldvimfootnotetype2 |
| 133 | + let g:oldvimfootnotenumber = oldvimfootnotenumber2 |
| 134 | + else |
| 135 | + echomsg "You didn't chang footnote type. Yet." |
| 136 | + return 0 |
| 137 | + endif |
| 138 | +endfunction |
| 139 | + |
| 140 | +function! s:VimFootnoteType(footnumber) |
| 141 | + if !exists("g:vimfootnotetype") |
| 142 | + let g:vimfootnotetype = "arabic" |
| 143 | + endif |
| 144 | + if (g:vimfootnotetype =~ "alpha\\|Alpha") |
| 145 | + if (g:vimfootnotetype == "alpha") |
| 146 | + let upper = "0" |
| 147 | + else |
| 148 | + let upper = "-32" |
| 149 | + endif |
| 150 | + if (a:footnumber <= 26) |
| 151 | + let ftnumber = nr2char(a:footnumber+96+upper) |
| 152 | + elseif (a:footnumber <= 52) |
| 153 | + let ftnumber = nr2char(a:footnumber+70+upper).nr2char(a:footnumber+70+upper) |
| 154 | + else |
| 155 | + let g:vimfootnotenumber = 1 |
| 156 | + let ftnumber = nr2char(97+upper) |
| 157 | + endif |
| 158 | + elseif (g:vimfootnotetype == "star") |
| 159 | + let starnumber = 1 |
| 160 | + let ftnumber = "" |
| 161 | + while starnumber <= a:footnumber |
| 162 | + let ftnumber = ftnumber."*" |
| 163 | + let starnumber = starnumber+1 |
| 164 | + endwhile |
| 165 | + else |
| 166 | + let ftnumber = a:footnumber |
| 167 | + endif |
| 168 | + return ftnumber |
| 169 | +endfunction |
| 170 | + |
| 171 | +function! s:VimFootnotes() |
| 172 | + if exists("g:vimfootnotenumber") |
| 173 | + let g:vimfootnotenumber = g:vimfootnotenumber + 1 |
| 174 | + let b:vimfootnotemark = <sid>VimFootnoteType(g:vimfootnotenumber) |
| 175 | + let cr = "" |
| 176 | + else |
| 177 | + let g:vimfootnotenumber = 1 |
| 178 | + let b:vimfootnotemark = <sid>VimFootnoteType(g:vimfootnotenumber) |
| 179 | + let cr = "\<cr>" |
| 180 | + endif |
| 181 | + exe "normal a[".b:vimfootnotemark."]\<esc>" |
| 182 | + :below 4split |
| 183 | + normal G |
| 184 | + if search("^-- $", "bW") |
| 185 | + exe "normal O".cr."[".b:vimfootnotemark."] " |
| 186 | + else |
| 187 | + exe "normal o".cr."[".b:vimfootnotemark."] " |
| 188 | + endif |
| 189 | + startinsert! |
| 190 | +endfunction |
| 191 | + |
0 commit comments