caio.co/de/jumpnextlongline.vim


Use a more reasonable logic for finding the next line 💬 by Caio 6 years ago (log)
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.

Blob plugin/jumpnextlongline.vim

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
" Jump Next Long Line
" ===================
"
" Vim plugin to jump to the next line that exceed the textwidth setting
" Last Change: 2018 Dec 26
" Maintainer: Caio Romão <caioromao@gmail.com>
" License: This file is placed in the public domain
"
" Mappings:
"   <Leader>l or <Plug>JumpNextLongLine
"       Jumps to the next (too) long line
"
" This plugin doesn't have any settings.

if exists("g:loaded_JumpNextLongLine") || &cp
  finish
endif

let g:loaded_JumpNextLongLine= 1
let s:save_cpo = &cpo
set cpo&vim

if !hasmapto('<Plug>JumpNextLongLine')
    nmap <silent> <unique> <Leader>l <Plug>JumpNextLongLine
endif

noremap <unique> <script> <Plug>JumpNextLongLine :call <SID>JumpNext()<CR>

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

function! s:IsLineTooLong(lineNr)
    let treshold = (&tw ? &tw : 80)
    let spaces = repeat(" ", &ts)

    let len = strlen(substitute(getline(a:lineNr), '\t', spaces, 'g'))
    if len > treshold
        return 1
    endif
endfunction

function! s:NextLongLine()
    let curline = line('.')
    let lastline = line('$')

    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
    " 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

let &cpo = s:save_cpo