Skip to content

Instantly share code, notes, and snippets.

@icarofreire
Last active July 14, 2017 19:00
Show Gist options
  • Select an option

  • Save icarofreire/07628bdb45f8bed0442de69bbc83d001 to your computer and use it in GitHub Desktop.

Select an option

Save icarofreire/07628bdb45f8bed0442de69bbc83d001 to your computer and use it in GitHub Desktop.
similar to acejump, but in lines.
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
highlight AceJumpGreyLinhas ctermfg=darkgrey guifg=lightgrey
highlight AceJumpRedLinhas ctermfg=darkred guibg=NONE guifg=black gui=NONE
function! AceJumpLinhas ()
let origPos = getpos('.')
let origSearch = @/
" row/col positions of words beginning with user's chosen letter
let pos = []
" monotone all text in visible part of window (dark grey by default)
call matchadd('AceJumpGreyLinhas', '\%'.line('w0').'l\_.*\%'.line('w$').'l', 50)
let col = 0
" loop over every line on the screen (just the visible lines)
for row in range(line('w0'), line('w$'))
" store any matching row/col positions
if( len(getline(row)) > 0 ) "colocará as letras nas linhas não vazias;
call add(pos, [row, 0])
endif
endfor
if len(pos) > 1
" jump characters used to mark found words (user-editable)
let chars = 'abcdefghijlkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.;"[]<>{}|\\'
if len(pos) > len(chars)
" TODO add groupings here if more pos matches than jump characters
endif
" trim found positions list; cannot be longer than jump markers list
let pos = pos[:len(chars)]
" jumps list to pair jump characters with found word positions
let jumps = {}
" change each found word's first letter to a jump character
for [r,c] in pos
" stop marking words if there are no more jump characters
if len(chars) == 0
break
endif
" 'pop' the next jump character from the list
let char = chars[0]
let chars = chars[1:]
" move cursor to the next found word
call setpos('.', [0,r,c+1,0])
" create jump character key to hold associated found word position
let jumps[char] = [0,r,c+1,0]
" replace first character in word with current jump character
exe 'norm r'.char
" change syntax on the jump character to make it highly visible
call matchadd('AceJumpRedLinhas', '\%'.r.'l\%'.(c+1).'c', 50)
endfor
call setpos('.', origPos)
" this redraw is critical to syntax highlighting
redraw
" prompt user again for the jump character to jump to
echo 'Letra da linha que deseja saltar:'
let jumpChar = nr2char(getchar())
" get rid of our syntax search highlighting
call clearmatches()
" clear out the status line
echo ""
redraw
" restore previous search register value
let @/ = origSearch
" undo all the jump character letter replacement
norm u
" if the user input a proper jump character, jump to it
if has_key(jumps, jumpChar)
call setpos('.', jumps[jumpChar])
else
" if it didn't work out, restore original cursor position
call setpos('.', origPos)
endif
elseif len(pos) == 1
" if we only found one match, just jump to it without prompting
" set position to the one match
let [r,c] = pos[0]
call setpos('.', [0,r,c+1,0])
elseif len(pos) == 0
" no matches; set position back to start
call setpos('.', origPos)
endif
" turn off all search highlighting
call clearmatches()
" clean up the status line and return
echo ""
redraw
return
endfunction
nnoremap <M-s> :call AceJumpLinhas()<CR>
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment