| 1 | " Visual Mark |
| 2 | " 2005-10-27, brian wang |
| 3 | " |
| 4 | " Acknowledgements: |
| 5 | " - Thanks to Hari Krishna Dara's genutils.vim (http://vim.sourceforge.net/scripts/script.php?script_id=197) |
| 6 | " - Thanks to Mr. Charles E. Campbell, Jr. for making this script more plugin-like :-) |
| 7 | " - Thanks to Mr. Charles E. Campbell, Jr. for making this script adapt to |
| 8 | " dark/light backgrounds |
| 9 | " - Thanks to Evgeny Filatov for noticing a nasty bug in Vm_get_line_number :-) |
| 10 | |
| 11 | if exists("loaded_VisualMark") |
| 12 | finish |
| 13 | endif |
| 14 | let loaded_VisualMark = 1 |
| 15 | if !has("signs") |
| 16 | echoerr "***sorry*** [".expand("%")."] your vim doesn't support signs" |
| 17 | finish |
| 18 | endif |
| 19 | |
| 20 | " --------------------------------------------------------------------- |
| 21 | " Public Interface: |
| 22 | if !hasmapto('<Plug>Vm_toggle_sign') |
| 23 | if has('mac') |
| 24 | map <unique> <d-F2> <Plug>Vm_toggle_sign |
| 25 | map <silent> <unique> mm <Plug>Vm_toggle_sign |
| 26 | else |
| 27 | map <unique> <c-F2> <Plug>Vm_toggle_sign |
| 28 | map <silent> <unique> mm <Plug>Vm_toggle_sign |
| 29 | endif |
| 30 | endif |
| 31 | nnoremap <silent> <script> <Plug>Vm_toggle_sign :call Vm_toggle_sign()<cr> |
| 32 | |
| 33 | if !hasmapto('<Plug>Vm_goto_next_sign') |
| 34 | map <unique> <F2> <Plug>Vm_goto_next_sign |
| 35 | endif |
| 36 | nnoremap <silent> <script> <Plug>Vm_goto_next_sign :call Vm_goto_next_sign()<cr> |
| 37 | |
| 38 | if !hasmapto('<Plug>Vm_goto_prev_sign') |
| 39 | map <unique> <s-F2> <Plug>Vm_goto_prev_sign |
| 40 | endif |
| 41 | nnoremap <silent> <script> <Plug>Vm_goto_prev_sign :call Vm_goto_prev_sign()<cr> |
| 42 | |
| 43 | " --------------------------------------------------------------------- |
| 44 | " GetVimCmdOutput: |
| 45 | " Stole from Hari Krishna Dara's genutils.vim (http://vim.sourceforge.net/scripts/script.php?script_id=197) |
| 46 | " to ease the scripts dependency issue |
| 47 | fun! s:GetVimCmdOutput(cmd) |
| 48 | " call Dfunc("GetVimCmdOutput(cmd.".a:cmd.">)") |
| 49 | |
| 50 | " Save the original locale setting for the messages |
| 51 | let old_lang = v:lang |
| 52 | |
| 53 | " Set the language to English |
| 54 | exec ":lan mes en_US" |
| 55 | |
| 56 | let v:errmsg = '' |
| 57 | let output = '' |
| 58 | let _z = @z |
| 59 | |
| 60 | try |
| 61 | redir @z |
| 62 | silent exe a:cmd |
| 63 | catch /.*/ |
| 64 | let v:errmsg = substitute(v:exception, '^[^:]\+:', '', '') |
| 65 | finally |
| 66 | redir END |
| 67 | if v:errmsg == '' |
| 68 | let output = @z |
| 69 | endif |
| 70 | let @z = _z |
| 71 | endtry |
| 72 | |
| 73 | " Restore the original locale |
| 74 | exec ":lan mes " . old_lang |
| 75 | |
| 76 | " call Dret("GetVimCmdOutput <".output.">") |
| 77 | return output |
| 78 | endfun |
| 79 | |
| 80 | " --------------------------------------------------------------------- |
| 81 | " Vm_place_sign: |
| 82 | fun! s:Vm_place_sign() |
| 83 | " call Dfunc("Vm_place_sign()") |
| 84 | |
| 85 | if !exists("b:Vm_sign_number") |
| 86 | let b:Vm_sign_number = 1 |
| 87 | endif |
| 88 | |
| 89 | let ln = line(".") |
| 90 | |
| 91 | exe 'sign define SignSymbol linehl=SignColor texthl=SignColor' |
| 92 | exe 'sign place ' . b:Vm_sign_number . ' line=' . ln . ' name=SignSymbol buffer=' . winbufnr(0) |
| 93 | |
| 94 | let vsn = b:Vm_sign_number |
| 95 | let b:Vm_sign_number = b:Vm_sign_number + 1 |
| 96 | |
| 97 | if &bg == "dark" |
| 98 | highlight SignColor ctermfg=white ctermbg=blue guifg=white guibg=RoyalBlue3 |
| 99 | else |
| 100 | highlight SignColor ctermbg=white ctermfg=blue guibg=grey guifg=RoyalBlue3 |
| 101 | endif |
| 102 | |
| 103 | " call Dret("Vm_place_sign : sign#".vsn." line#".ln." buf#".winbufnr(0)) |
| 104 | endfun |
| 105 | |
| 106 | " --------------------------------------------------------------------- |
| 107 | " Vm_remove_sign: |
| 108 | fun! s:Vm_remove_sign(sign_id) |
| 109 | " call Dfunc("Vm_remove_sign(sign_id=".a:sign_id.")") |
| 110 | silent! exe 'sign unplace ' . a:sign_id . ' buffer=' . winbufnr(0) |
| 111 | " call Dret("Vm_remove_sign") |
| 112 | endfun |
| 113 | |
| 114 | " --------------------------------------------------------------------- |
| 115 | " Vm_remove_all_signs: |
| 116 | fun! s:Vm_remove_all_signs() |
| 117 | " call Dfunc("Vm_remove_all_signs()") |
| 118 | silent! exe 'sign unplace *' |
| 119 | " call Dret("Vm_remove_all_signs") |
| 120 | endfun |
| 121 | |
| 122 | " --------------------------------------------------------------------- |
| 123 | " Vm_get_sign_id_from_line: |
| 124 | fun! s:Vm_get_sign_id_from_line(line_number) |
| 125 | " call Dfunc("Vm_get_sign_id_from_line(line_number=".a:line_number.")") |
| 126 | |
| 127 | let sign_list = s:GetVimCmdOutput('sign place buffer=' . winbufnr(0)) |
| 128 | " call Decho(sign_list) |
| 129 | |
| 130 | let line_str_index = match(sign_list, "line=" . a:line_number, 0) |
| 131 | if line_str_index < 0 |
| 132 | " call Dret("Vm_get_sign_id_from_line -1") |
| 133 | return -1 |
| 134 | endif |
| 135 | |
| 136 | let id_str_index = matchend(sign_list, "id=", line_str_index) |
| 137 | " let tmp = strpart(sign_list, id_str_index, 10) "Decho |
| 138 | " call Decho("ID str index: " . tmp) |
| 139 | if id_str_index < 0 |
| 140 | " call Dret("Vm_get_sign_id_from_line -1") |
| 141 | return -1 |
| 142 | endif |
| 143 | |
| 144 | let space_index = match(sign_list, " ", id_str_index) |
| 145 | let id = strpart(sign_list, id_str_index, space_index - id_str_index) |
| 146 | |
| 147 | " call Dret("Vm_get_sign_id_from_line ".id) |
| 148 | return id |
| 149 | endfun |
| 150 | |
| 151 | " --------------------------------------------------------------------- |
| 152 | " Vm_toggle_sign: |
| 153 | fun! Vm_toggle_sign() |
| 154 | " call Dfunc("Vm_toggle_sign()") |
| 155 | |
| 156 | let curr_line_number = line(".") |
| 157 | let sign_id = s:Vm_get_sign_id_from_line(curr_line_number) |
| 158 | |
| 159 | if sign_id < 0 |
| 160 | let is_on = 0 |
| 161 | else |
| 162 | let is_on = 1 |
| 163 | endif |
| 164 | |
| 165 | if (is_on != 0) |
| 166 | call s:Vm_remove_sign(sign_id) |
| 167 | else |
| 168 | call s:Vm_place_sign() |
| 169 | endif |
| 170 | |
| 171 | " call Dret("Vm_toggle_sign") |
| 172 | endfun |
| 173 | |
| 174 | " --------------------------------------------------------------------- |
| 175 | " Vm_get_line_number: |
| 176 | fun! s:Vm_get_line_number(string) |
| 177 | " call Dfunc("Vm_get_line_number(string<".a:string.">)") |
| 178 | |
| 179 | let line_str_index = match(a:string, "line=", b:Vm_start_from) |
| 180 | if line_str_index <= 0 |
| 181 | " call Dret("Vm_get_line_number -1") |
| 182 | return -1 |
| 183 | endif |
| 184 | |
| 185 | let equal_sign_index = match(a:string, "=", line_str_index) |
| 186 | let space_index = match(a:string, " ", equal_sign_index) |
| 187 | let line_number = strpart(a:string, equal_sign_index + 1, space_index - equal_sign_index - 1) |
| 188 | let b:Vm_start_from = space_index |
| 189 | |
| 190 | " call Dret("Vm_get_line_number ".line_number." : =indx:".equal_sign_index." _indx=".space_index) |
| 191 | return line_number + 0 |
| 192 | endfun |
| 193 | |
| 194 | " --------------------------------------------------------------------- |
| 195 | " Vm_get_next_sign_line: |
| 196 | fun! s:Vm_get_next_sign_line(curr_line_number) |
| 197 | " call Dfunc("Vm_get_next_sign_line(curr_line_number=".a:curr_line_number.">)") |
| 198 | |
| 199 | let b:Vm_start_from = 1 |
| 200 | let sign_list = s:GetVimCmdOutput('sign place buffer=' . winbufnr(0)) |
| 201 | " call Decho("sign_list<".sign_list.">") |
| 202 | |
| 203 | let curr_line_number = a:curr_line_number |
| 204 | let line_number = 1 |
| 205 | let is_no_sign = 1 |
| 206 | let min_line_number = -1 |
| 207 | let min_line_number_diff = 0 |
| 208 | |
| 209 | while 1 |
| 210 | let line_number = s:Vm_get_line_number(sign_list) |
| 211 | if line_number < 0 |
| 212 | break |
| 213 | endif |
| 214 | |
| 215 | " Record the very first line that has a sign |
| 216 | if is_no_sign != 0 |
| 217 | let min_line_number = line_number |
| 218 | elseif line_number < min_line_number |
| 219 | let min_line_number = line_number |
| 220 | endif |
| 221 | let is_no_sign = 0 |
| 222 | |
| 223 | " let tmp_diff = curr_line_number - line_number |
| 224 | let tmp_diff = line_number - curr_line_number |
| 225 | if tmp_diff > 0 |
| 226 | " line_number is below curr_line_number |
| 227 | if min_line_number_diff > 0 |
| 228 | if tmp_diff < min_line_number_diff |
| 229 | let min_line_number_diff = tmp_diff |
| 230 | endif |
| 231 | else |
| 232 | let min_line_number_diff = tmp_diff |
| 233 | endif |
| 234 | endif |
| 235 | |
| 236 | " call Decho("[DBG] Line Diff: #" . min_line_number_diff) |
| 237 | endwhile |
| 238 | |
| 239 | let line_number = curr_line_number + min_line_number_diff |
| 240 | " call Decho("[DBG] Line Diff: #" . min_line_number_diff) |
| 241 | " call Decho("[DBG] Line Num: #" . line_number) |
| 242 | |
| 243 | if is_no_sign != 0 || min_line_number_diff <= 0 |
| 244 | let line_number = min_line_number |
| 245 | endif |
| 246 | |
| 247 | " call Dret("Vm_get_next_sign_line ".line_number . " XXX") |
| 248 | return line_number |
| 249 | endfun |
| 250 | |
| 251 | " --------------------------------------------------------------------- |
| 252 | " Vm_get_prev_sign_line: |
| 253 | fun! s:Vm_get_prev_sign_line(curr_line_number) |
| 254 | " call Dfunc("Vm_get_prev_sign_line(curr_line_number=".a:curr_line_number.">)") |
| 255 | |
| 256 | let b:Vm_start_from = 1 |
| 257 | let sign_list = s:GetVimCmdOutput('sign place buffer=' . winbufnr(0)) |
| 258 | " call Decho("sign_list<".sign_list.">") |
| 259 | |
| 260 | let curr_line_number = a:curr_line_number |
| 261 | let line_number = 1 |
| 262 | let is_no_sign = 1 |
| 263 | let max_line_number = -1 |
| 264 | let max_line_number_diff = 0 |
| 265 | |
| 266 | while 1 |
| 267 | let line_number = s:Vm_get_line_number(sign_list) |
| 268 | if line_number < 0 |
| 269 | break |
| 270 | endif |
| 271 | |
| 272 | " Record the very first line that has a sign |
| 273 | if is_no_sign != 0 |
| 274 | let max_line_number = line_number |
| 275 | elseif line_number > max_line_number |
| 276 | let max_line_number = line_number |
| 277 | endif |
| 278 | let is_no_sign = 0 |
| 279 | |
| 280 | let tmp_diff = curr_line_number - line_number |
| 281 | if tmp_diff > 0 |
| 282 | " line_number is below curr_line_number |
| 283 | if max_line_number_diff > 0 |
| 284 | if tmp_diff < max_line_number_diff |
| 285 | let max_line_number_diff = tmp_diff |
| 286 | endif |
| 287 | else |
| 288 | let max_line_number_diff = tmp_diff |
| 289 | endif |
| 290 | endif |
| 291 | |
| 292 | " call Decho("[DBG] Line Diff: #" . max_line_number_diff) |
| 293 | " call Decho("[DBG] Tmp Diff: #" . tmp_diff) |
| 294 | endwhile |
| 295 | |
| 296 | let line_number = curr_line_number - max_line_number_diff |
| 297 | " call Decho("[DBG] Line Diff: #" . max_line_number_diff) |
| 298 | " call Decho("[DBG] Line Num: #" . line_number) |
| 299 | |
| 300 | if is_no_sign != 0 || max_line_number_diff <= 0 |
| 301 | let line_number = max_line_number |
| 302 | endif |
| 303 | |
| 304 | " call Dret("Vm_get_prev_sign_line ".line_number . " XXX") |
| 305 | return line_number |
| 306 | endfun |
| 307 | |
| 308 | " --------------------------------------------------------------------- |
| 309 | " Vm_goto_next_sign: |
| 310 | fun! Vm_goto_next_sign() |
| 311 | " call Dfunc("Vm_goto_next_sign()") |
| 312 | |
| 313 | let curr_line_number = line(".") |
| 314 | let next_sign_line_number = s:Vm_get_next_sign_line(curr_line_number) |
| 315 | |
| 316 | " call Decho("Next sign line #: " . next_sign_line_number) |
| 317 | if next_sign_line_number >= 0 |
| 318 | exe ":" . next_sign_line_number |
| 319 | "call Decho("Going to Line #" . next_sign_line_number) |
| 320 | endif |
| 321 | |
| 322 | " call Dret("Vm_goto_next_sign") |
| 323 | endfun |
| 324 | |
| 325 | " --------------------------------------------------------------------- |
| 326 | " Vm_goto_prev_sign: |
| 327 | fun! Vm_goto_prev_sign() |
| 328 | " call Dfunc("Vm_goto_prev_sign()") |
| 329 | |
| 330 | let curr_line_number = line(".") |
| 331 | let prev_sign_line_number = s:Vm_get_prev_sign_line(curr_line_number) |
| 332 | " call Decho("Previous sign line #: " . prev_sign_line_number) |
| 333 | |
| 334 | if prev_sign_line_number >= 0 |
| 335 | exe prev_sign_line_number |
| 336 | endif |
| 337 | |
| 338 | " call Dret("Vm_goto_prev_sign") |
| 339 | endfun |
| 340 | |
| 341 | " --------------------------------------------------------------------- |
| 342 | |