|
| 1 | +" ============================================================================== |
| 2 | +" Name: ShowMarks |
| 3 | +" Description: Visually displays the location of marks local to a buffer. |
| 4 | +" Authors: Anthony Kruize <[email protected]> |
| 5 | +" Michael Geddes <[email protected]> |
| 6 | +" Version: 1.0 |
| 7 | +" Modified: 21 November 2001 |
| 8 | +" License: Released into the public domain. |
| 9 | +" Usage: Copy this file into the plugins directory so it will be |
| 10 | +" automatically sourced. |
| 11 | +" |
| 12 | +" Default keymappings are: |
| 13 | +" <Leader>mt - Toggles ShowMarks on and off. |
| 14 | +" <Leader>mh - Hides a mark. |
| 15 | +" |
| 16 | +" Hiding a mark doesn't actually remove it, it simply moves it to |
| 17 | +" line 1 and hides it visually. |
| 18 | +" |
| 19 | +" ============================================================================== |
| 20 | + |
| 21 | +" Check if we should continue loading |
| 22 | +if exists( "loaded_showmarks" ) |
| 23 | + finish |
| 24 | +endif |
| 25 | +let loaded_showmarks = 1 |
| 26 | + |
| 27 | +" Mappings |
| 28 | +if !hasmapto( '<Plug>ShowmarksShowMarksToggle') |
| 29 | + map <unique> <leader>mt <Plug>ShowmarksShowMarksToggle |
| 30 | +endif |
| 31 | +if !hasmapto( '<Plug>ShowmarksHideMark') |
| 32 | + map <unique> <leader>mh <Plug>ShowmarksHideMark |
| 33 | +endif |
| 34 | + |
| 35 | +noremap <unique> <script> <Plug>ShowmarksShowMarksToggle :call <SID>ShowMarksToggle()<CR> |
| 36 | +noremap <unique> <script> <Plug>ShowmarksHideMark :call <SID>HideMark()<CR> |
| 37 | +
|
| 38 | +" AutoCommands: This will check the marks and set the signs |
| 39 | +aug ShowMarks |
| 40 | + autocmd CursorHold * call s:ShowMarks() |
| 41 | +aug END |
| 42 | + |
| 43 | +" Toggle whether we display marks |
| 44 | +function! s:ShowMarksToggle() |
| 45 | + if !exists("b:ShowMarks_Enabled") |
| 46 | + let b:ShowMarks_Enabled = 1 |
| 47 | + endif |
| 48 | + |
| 49 | + if b:ShowMarks_Enabled == 0 |
| 50 | + let b:ShowMarks_Enabled = 1 |
| 51 | + aug ShowMarks |
| 52 | + autocmd CursorHold * call s:ShowMarks() |
| 53 | + aug END |
| 54 | + else |
| 55 | + let b:ShowMarks_Enabled = 0 |
| 56 | + let n = 0 |
| 57 | + while n < 26 |
| 58 | + let c = nr2char(char2nr('a') + n) |
| 59 | + let id = n * 26 + winbufnr(0) |
| 60 | + if exists('b:placed_'.c) |
| 61 | + let b:placed_{c} = 1 |
| 62 | + exe 'sign unplace '.id.' buffer='.winbufnr(0) |
| 63 | + endif |
| 64 | + let n = n + 1 |
| 65 | + endwhile |
| 66 | + aug ShowMarks |
| 67 | + autocmd! |
| 68 | + aug END |
| 69 | + endif |
| 70 | +endfunction |
| 71 | + |
| 72 | +" Highlighting: Setup some nice colours to show the mark position. |
| 73 | +hi default ShowMarksHL ctermfg=blue ctermbg=lightblue cterm=bold guifg=blue guibg=lightblue gui=bold |
| 74 | + |
| 75 | +" Setup the sign definitions for each mark |
| 76 | +function! s:ShowMarksSetup() |
| 77 | + let n = 0 |
| 78 | + while n < 26 |
| 79 | + let c = nr2char(char2nr('a') + n) |
| 80 | + exe 'sign define ShowMark'.c.' text='.c.'> texthl=ShowMarksHL' |
| 81 | + let n = n + 1 |
| 82 | + endwhile |
| 83 | +endfunction |
| 84 | + |
| 85 | +call s:ShowMarksSetup() |
| 86 | + |
| 87 | +" This function is called on the CursorHold autocommand. |
| 88 | +" It runs through all the marks and displays or removes signs as appropriate. |
| 89 | +function! s:ShowMarks() |
| 90 | + let n = 0 |
| 91 | + while n < 26 |
| 92 | + let c = nr2char(char2nr('a') + n) |
| 93 | + let id = n * 26 + winbufnr(0) |
| 94 | + let curline = line("'".c) |
| 95 | + if curline != 0 && (!exists('b:placed_'.c) || b:placed_{c} != curline ) |
| 96 | + exe 'sign unplace '.id.' buffer='.winbufnr(0) |
| 97 | + exe 'sign place '.id.' name=ShowMark'.c.' line='.line("'".c).' buffer='.winbufnr(0) |
| 98 | + endif |
| 99 | + let b:placed_{c} = curline |
| 100 | + let n = n + 1 |
| 101 | + endwhile |
| 102 | +endfunction |
| 103 | + |
| 104 | +" Hide the mark at the current line. |
| 105 | +" This simply moves the mark to line 1 and hides the sign. |
| 106 | +function! s:HideMark() |
| 107 | + let curline = line(".") |
| 108 | + let n = 0 |
| 109 | + while n < 26 |
| 110 | + let c = nr2char(char2nr('a') + n) |
| 111 | + let markline = line("'".c) |
| 112 | + if curline == markline |
| 113 | + let id = n * 26 + winbufnr(0) |
| 114 | + exe 'sign unplace '.id.' buffer='.winbufnr(0) |
| 115 | + exe '1 mark '.c |
| 116 | + let b:placed_{c} = 1 |
| 117 | + endif |
| 118 | + let n = n + 1 |
| 119 | + endwhile |
| 120 | +endfunction |
0 commit comments