gabrielelana / vim-markdown

Markdown for Vim: a complete environment to create Markdown files with a syntax highlight that doesn't suck!
MIT License
740 stars 59 forks source link

JumpToHeader should use avoid modifying search history and status #39

Closed muru closed 8 years ago

muru commented 8 years ago

In a Vi and Vim Stack Exchange post, @romainl posted a function to jump to headers (for mapping to [[, ]], etc.):

function! CustomJump(motion) range
    let cnt = v:count1
    let save = @/
    mark '
    while cnt > 0
        silent! execute a:motion
        let cnt = cnt - 1
    endwhile
    call histdel('/', -1)
    let @/ = save
endfunction

While I prefer the current s:JumpToHeader(), I feel it could be improved to avoid modifying search history:

function! s:JumpToHeader(forward, visual)
  let save = @/
  let pattern = '\v^#{1,6}.*$|^.+\n%(\-+|\=+)$' 
  " snip  
  execute 'silent normal! ' . direction . pattern . "\n"
  call histdel('/', -1)
  let @/ = save
endfunction

Another useful bit would the while loop, which lets @romainl's mapping respect a count.

gabrielelana commented 8 years ago

@murukeshm nice! What about a pull request?