vim-quicklink icon indicating copy to clipboard operation
vim-quicklink copied to clipboard

Add support for line breaks in links

Open christoomey opened this issue 10 years ago • 0 comments

Given we have a markdown link spanning across more than one line, like:

and the [link definition is broken
accross more than one line][]

...

[link definition is broken accross more than one line]: https://example.com

I still want the fancy link opening to work. Partial solution below works by relying on existing functionality, namely syntax highlighting and text objects, to grab the link text and search for it.

function! s:GetTextObject(text_object_or_motion)
  let l:save_clipboard = &clipboard
  set clipboard= " Avoid clobbering the selection and clipboard registers.
  let l:save_reg = getreg('"')
  let l:save_regmode = getregtype('"')
  execute 'normal! y' . a:text_object_or_motion
  let l:text = @@ " Your text object contents are here.
  call setreg('"', l:save_reg, l:save_regmode)
  let &clipboard = l:save_clipboard
  return l:text
endfunction

function! s:FancyMarkdownOpenLink()
  let save_cursor = getpos(".")
  let raw_link_text = s:GetTextObject('i]')
  let link_text = substitute(raw_link_text, "\n", ' ', 'g')
  let link_target_line = search('^\[' . link_text . '\]', 'n')
  let link_target = matchlist(getline(link_target_line), '\v^\[.*\]:\s(.*)')[1]
  let g:link_target = link_target
  call system("open '". link_target . "'")
  call setpos('.', save_cursor)
endfunction

command! FancyMarkdownOpenLink call <sid>FancyMarkdownOpenLink()
nnoremap <cr> :FancyMarkdownOpenLink<cr>

christoomey avatar Oct 06 '15 18:10 christoomey