caio.co/de/jumpnextlongline.vim

Use a more reasonable logic for finding the next line

I was going to archive this repo but then I looked at the code and
found it too naive :-) This patch changes the logic to scan on
request instead of building a list of long lines just to immediately
discard them.

Now I can archive this with a peaceful mind.
Id
e6a1abc58f1b5a32b5bda97d2bb5611c08af8c8b
Author
Caio
Commit time
2018-12-26T12:52:14+01:00

Modified plugin/jumpnextlongline.vim

@@ -1,8 +1,8
" Jump Next Long Line
" ===================
"
" Vim plugin to jump to the next line that exceed the textwidth setting
-" Last Change: 2010 Dec 24
+" Last Change: 2018 Dec 26
" Maintainer: Caio Romão <caioromao@gmail.com>
" License: This file is placed in the public domain
"
@@ -28,49 +28,42

function! s:JumpNext()
let nline = s:NextLongLine()
- execute "normal! " . nline . "gg"
+ if nline > 0
+ execute "normal! " . nline . "gg"
+ endif
endfunction

-function! s:ListLongLines()
+function! s:IsLineTooLong(lineNr)
let treshold = (&tw ? &tw : 80)
let spaces = repeat(" ", &ts)
- let longlines = []

- let i = 1
- while i <= line("$")
- " Respect user's &ts setting
- let len = strlen(substitute(getline(i), '\t', spaces, 'g'))
- if len > treshold
- call add(longlines, i)
- endif
- let i += 1
- endwhile
-
- return longlines
+ let len = strlen(substitute(getline(a:lineNr), '\t', spaces, 'g'))
+ if len > treshold
+ return 1
+ endif
endfunction

function! s:NextLongLine()
- if !exists("b:long_lines_list")
- let b:long_lines_list = s:ListLongLines()
- endif
-
let curline = line('.')
- let listsize = len(b:long_lines_list)
+ let lastline = line('$')

- let i = 0
- while i < listsize
- let nextline = get(b:long_lines_list, i)
- if nextline > curline
- return nextline
+ let i = curline + 1
+ " Scan to the end of the file
+ while i <= lastline
+ if s:IsLineTooLong(i)
+ return i
endif
let i += 1
endwhile
- " allow wrapping to the beginning of the buffer
- return get(b:long_lines_list, 0, curline)
+ " Nothing found: scan from the beginning
+ let i = 1
+ while i <= curline
+ if s:IsLineTooLong(i)
+ return i
+ endif
+ let i += 1
+ endwhile
+ return 0
endfunction
-
-" Delete list of long lines when idle and when writing
-" to trigger it's re-creation
-autocmd cursorhold,bufwritepost * unlet! b:long_lines_list

let &cpo = s:save_cpo