Skip to content

Commit

Permalink
Add ability to jump to sections with [[ and ]] (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
SidOfc committed May 1, 2021
1 parent 7fc33a8 commit 99eaf99
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
38 changes: 33 additions & 5 deletions autoload/mkdx.vim
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ fun! s:util.ListHeaders()

for lnum in range(1, line('$'))
let header = getline(lnum)
let current_lnum = lnum
let skip = match(header, '^\(\`\`\`\|\~\~\~\)') > -1 ? !skip : skip
let is_frontmatter = match(
\ get(map(synstack(lnum, 1), 'synIDattr(v:val, "name")'), 0, ''),
Expand All @@ -863,7 +864,8 @@ fun! s:util.ListHeaders()
if (lvl == 0)
let setext_ul = get(matchlist(header, '^\%(-\|=\)\+$'), 0, '')
if !empty(setext_ul)
let header = getline(lnum - 1)
let current_lnum -= 1
let header = getline(lnum - 1)
if !empty(header)
let lvl = setext_ul[0] == '=' ? 1 : 2
endif
Expand All @@ -874,7 +876,7 @@ fun! s:util.ListHeaders()
let hash = s:util.transform(tolower(header), ['clean-header', 'header-to-hash'])
let hashes[hash] = get(hashes, hash, -1) + 1

call add(headers, [lnum, lvl, header, hash, (hashes[hash] > 0 ? '-' . hashes[hash] : '')])
call add(headers, [current_lnum, lvl, header, hash, (hashes[hash] > 0 ? '-' . hashes[hash] : '')])
endif
endif
endfor
Expand Down Expand Up @@ -2223,6 +2225,32 @@ fun! mkdx#gf(...)
endtry
endfun

if $VIM_DEV
let g:mkdx#util = s:util
endif
fun! mkdx#JumpToSection(to) abort
let cursor_lnum = line('.')
let headers = s:util.ListHeaders()
let jumps = v:count1

if a:to ==# 'next'
for header in headers
let header_lnum = header[0]
let jumps -= header_lnum > cursor_lnum ? 1 : 0

if jumps <= 0
call cursor(header_lnum, 1)
break
endif
endfor
else
for header in reverse(headers)
let header_lnum = header[0]
let jumps -= header_lnum < cursor_lnum ? 1 : 0

if jumps <= 0
call cursor(header_lnum, 1)
break
endif
endfor
endif
endfun

let g:mkdx#util = s:util
Expand Down
6 changes: 5 additions & 1 deletion ftplugin/markdown.vim
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ inoremap <silent> <Plug>(mkdx-ctrl-p-compl) <C-R>=mkdx#InsertCtrlPH
inoremap <expr> <silent> <Plug>(mkdx-link-compl) mkdx#CompleteLink()
noremap <silent> <Plug>(mkdx-indent) :call mkdx#IndentHandler(1)<Cr>
noremap <silent> <Plug>(mkdx-unindent) :call mkdx#IndentHandler(0)<Cr>
noremap <Plug>(mkdx-next-section) :call mkdx#JumpToSection('next')<Cr>
noremap <Plug>(mkdx-prev-section) :call mkdx#JumpToSection('prev')<Cr>
if (g:mkdx#settings.links.fragment.complete)
setlocal completefunc=mkdx#Complete
Expand Down Expand Up @@ -121,7 +123,9 @@ if g:mkdx#settings.map.enable == 1
\ ['Jump to file / open URL', 0, 'n', 'gf', '<Plug>(mkdx-gf)', ':<C-U>call mkdx#gf("f")<Cr>'],
\ ['Jump to file / open URL', 0, 'n', 'gx', '<Plug>(mkdx-gx)', ':<C-U>call mkdx#gf("x")<Cr>'],
\ ['Jump to file / open URL', 0, 'v', 'gf', '<Plug>(mkdx-gf-visual)', ':<C-U>call mkdx#gf_visual("f")<Cr>'],
\ ['Jump to file / open URL', 0, 'v', 'gx', '<Plug>(mkdx-gx-visual)', ':<C-U>call mkdx#gf_visual("x")<Cr>']
\ ['Jump to file / open URL', 0, 'v', 'gx', '<Plug>(mkdx-gx-visual)', ':<C-U>call mkdx#gf_visual("x")<Cr>'],
\ ['Jump to next section', 0, 'n', ']]', '<Plug>(mkdx-next-section)', ':call mkdx#JumpToSection("next")<Cr>'],
\ ['Jump to prev section', 0, 'n', '[[', '<Plug>(mkdx-prev-section)', ':call mkdx#JumpToSection("prev")<Cr>'],
\ ]

if (!hasmapto('<Plug>(mkdx-gf)', 'n'))
Expand Down

0 comments on commit 99eaf99

Please sign in to comment.