Skip to content
  • Unwatch
  • Fork

public /vmark Octocat-spinner-32

branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 277 lines (262 sloc) 8.713 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
" File: vmark.vim
" Author: Michael Zhou <michael.zhou@gmail.com>
" Version: 1.2
" Last Modified: Wed Jun 13 17:07:08 EDT 2012
" Description: Toggle visual bookmarks and jump through bookmarks
" Uasge:
" Normal Mode
" mm Toggle visual bookmark current line
" ma Remove all bookmarks
" mn Move to next bookmark below current line.
" Will wrap around at end.
" <F2> Same as above (mn)
" mp Move to previous bookmark above current line.
" Will wrap around at top.
" <F3> Same as above (mp)
"
" Installation:
" Copy vmark.vim to .vim/plugin directory
"
" Configuration:
" g:vm_maxmarks - Max number of bookmarks allowed in a buffer
" g:vm_guifg - Foreground color for bookmarked line in GVIM
" g:vm_guibg - Background color for bookmarked line in GVIM
" g:vm_ctermfg - Foreground color for bookmarked line in VIM
" g:vm_ctermbg - Background color for bookmarked line in VIM
"
" Limitation:
" If syntax highlighting based on keywords is turned on and the line being
" bookmarked starts with a keyword, the keyword may not be highlighted
" but the rest of the line after the keyword should be highlighted normally.
"
" Acknowledgement:
" Took cues from Amit Sethi's highlight.vim
" http://www.vim.org/scripts/script.php?script_id=1599
"

" Toggle bookmark on current line
function! <SID>VMarkToggle()
    call s:InitVMarkVariables()
    let lineno = line(".")
    if count(b:vm_markedlines, lineno) > 0
        call s:VMarkClear(lineno)
    else
        call s:VMarkSet(lineno)
    endif
endfunction

" Set bookmark on current line
function! s:VMarkSet(lineno)
    if count(b:vm_markedlines, a:lineno) > 0
        echo "Line ".line(".")." already bookmarked."
    else
        if empty(b:vm_availablemarks)
            echo "Max number of bookmarks reached. ".
                \"Increase g:vm_maxmarks in plugin/vmark.vim"
        else
            call add(b:vm_markedlines, a:lineno)
            call sort(b:vm_markedlines, "s:NumComparator")
            let c = remove(b:vm_availablemarks, 0)
            let b:vm_linecolormapping[a:lineno] = c
            exec 'syn match '.c.' ".*\%'.line(".").'l.*" containedin=ALL'
        endif
    endif
endfunction

" Clear bookmark on current line
function! s:VMarkClear(lineno)
    if count(b:vm_markedlines, a:lineno) == 0
        echo "Line ".line(".")." not bookmarked."
    else
        let c = remove(b:vm_linecolormapping, a:lineno)
        call add(b:vm_availablemarks, c)
        let idx = 0
        let cnt = len(b:vm_markedlines)
        while idx < cnt
            if a:lineno == get(b:vm_markedlines, idx)
                call remove(b:vm_markedlines, idx)
                break
            endif
            let idx = idx + 1
        endwhile
        echo "Cleared bookmark on line: ".a:lineno
        exec 'syn clear '.c.' ".*\%'.a:lineno.'l.*" containedin=ALL'
    endif
endfunction

" Clear all bookmarks
function! <SID>VMarkClearAll()
    call s:InitVMarkVariables()
    if len(b:vm_markedlines) == 0
        echo "No bookmarks found."
    else
        let idx = 0
        let cnt = len(b:vm_markedlines)
        while idx < cnt
            let lineno = get(b:vm_markedlines, idx)
            let c = remove(b:vm_linecolormapping, lineno)
            call add(b:vm_availablemarks, c)
            exec 'syn clear '.c.' ".*\%'.lineno.'l.*" containedin=ALL'
            let idx = idx + 1
        endwhile
        let b:vm_markedlines = []
        let b:vm_linecolormapping = {}
    endif
endfunction

" Move to next bookmark below current line.
" Will wrap around at last bookmark.
function! <SID>VMarkNext()
    call s:InitVMarkVariables()
    if empty(b:vm_markedlines)
        echo "No bookmarks found."
        return
    endif
    let cnt = len(b:vm_markedlines)
    let idx = 0
    let line1 = 0
    let line2 = 0
    let curline = line(".")
    if curline >= get(b:vm_markedlines, -1) ||
        \ curline < get(b:vm_markedlines, 0)
        let curline = get(b:vm_markedlines, 0)
    else
        while idx < cnt - 1
            let line1 = get(b:vm_markedlines, idx)
            let line2 = get(b:vm_markedlines, idx + 1)
            if line1 <= curline && curline < line2
                let curline = line2
                break
            endif
            let idx = idx + 1
        endwhile
    endif
    echo "Moving to line: ".curline
    call cursor(curline, 1)
endfunction

" Move to previous bookmark.
" Will wrap around at first bookmark.
function! <SID>VMarkPrevious()
    call s:InitVMarkVariables()
    if empty(b:vm_markedlines)
        echo "No bookmarks found."
        return
    endif
    let cnt = len(b:vm_markedlines)
    let idx = cnt - 2
    let line1 = 0
    let line2 = 0
    let curline = line(".")
    if curline > get(b:vm_markedlines, -1) ||
        \ curline <= get(b:vm_markedlines, 0)
        let curline = get(b:vm_markedlines, -1)
    else
        while idx >= 0
            let line1 = get(b:vm_markedlines, idx)
            let line2 = get(b:vm_markedlines, idx + 1)
            if line1 < curline && curline <= line2
                let curline = line1
                break
            endif
            let idx = idx - 1
        endwhile
    endif
    echo "Moving to line: ".curline
    call cursor(curline, 1)
endfunction

" Global init
function! s:GlobalInitVMark()
" Color name prefix
    if !exists("g:vm_colorprefix")
        let g:vm_colorprefix = 'VMarkColor'
    endif
" Max number of bookmarks allowed in a buffer
    if !exists("g:vm_maxmarks")
        let g:vm_maxmarks = 50
    endif
" Foreground color for the bookmarked line in GVIM
    if !exists("g:vm_guifg")
        let g:vm_guifg = 'white'
    endif
" Background color for the bookmarked line in GVIM
    if !exists("g:vm_guibg")
        let g:vm_guibg = 'red'
    endif
" Foreground color for the bookmarked line in VIM
    if !exists("g:vm_ctermfg")
        let g:vm_ctermfg = 'black'
    endif
" Background color for the bookmarked line in VIM
    if !exists("g:vm_ctermbg")
        let g:vm_ctermbg = 'yellow'
    endif
endfunction

" Initialize variables
function! s:InitVMarkVariables()
" Bookmarked line numbers
    if !exists("b:vm_markedlines")
        let b:vm_markedlines = []
    endif
"
" TODO
" Could be improved to have just one data structure
" which is the dictionary to store marked lines
" as well as line number to color mappings.
" Need to take care of sorting dictionary first.
" Could not find a built-in function for the task.
"
" Bookmarked lines to color name mapping
    if !exists("b:vm_linecolormapping")
        let b:vm_linecolormapping = {}
    endif
" Available bookmarks
    if !exists("b:vm_availablemarks")
        let b:vm_availablemarks = []
        let idx = 0
        while idx < g:vm_maxmarks
            call add(b:vm_availablemarks, g:vm_colorprefix.idx)
            exec 'hi '. g:vm_colorprefix . idx .
                \ ' guifg=' . g:vm_guifg . ' guibg=' . g:vm_guibg .
                \ ' ctermfg=' . g:vm_ctermfg . ' ctermbg=' . g:vm_ctermbg
            let idx = idx + 1
        endwhile
    endif
endfunction

" Highlight all marked lines
function! s:VMarkHighlightAll()
    call s:InitVMarkVariables()
    let cnt = len(b:vm_markedlines)
    if cnt != 0
        let idx = 0
        while idx < cnt
            let lineno = b:vm_markedlines[idx]
            let c = b:vm_linecolormapping[lineno]
            exec 'syn match '.c.' ".*\%'.lineno.'l.*" containedin=ALL'
            let idx = idx + 1
        endwhile
    endif
endfunction

" Numeric number comparison function
" Surprised that VIM does not have a built-in
function! s:NumComparator(i1, i2)
    return a:i1 - a:i2
endfunction

" Key mappings
if !hasmapto('<Plug>VMarkToggle')
nmap <silent> <unique> ,mm :call <Plug>VMarkToggle<cr>
endif
if !hasmapto('<Plug>VMarkNext')
nnoremap <silent> <unique> ,mn :call <Plug>VMarkNext<cr>
nnoremap <silent> <unique> <F2> :call <Plug>VMarkNext<cr>
endif
if !hasmapto('<Plug>VMarkPrevious')
nnoremap <silent> <unique> ,mN :call <Plug>VMarkPrevious<cr>
nnoremap <silent> <unique> <F3> :call <Plug>VMarkPrevious<cr>
endif
if !hasmapto('<Plug>VMarkClearAll')
nnoremap <silent> ,mc :call <Plug>VMarkClearAll<cr>
endif

nnoremap <Plug>VMarkToggle :call <sid>VMarkToggle()<cr>
nnoremap <Plug>VMarkNext :call <sid>VMarkNext()<cr>
nnoremap <Plug>VMarkPrevious :call <sid>VMarkPrevious()<cr>
nnoremap <Plug>VMarkClearAll :call <sid>VMarkClearAll()<cr>

" Initialization
call s:GlobalInitVMark()
call s:InitVMarkVariables()

" Highlight all marked lines when a buffer is loaded
autocmd BufEnter * call s:VMarkHighlightAll()
Something went wrong with that request. Please try again.