Skip to content

Commit

Permalink
Merge pull request #106 from SidOfc/feature/gf-like-gx
Browse files Browse the repository at this point in the history
#100 enhance gf
  • Loading branch information
SidOfc authored May 16, 2020
2 parents 7cb638b + 5b507f4 commit 779853a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,7 @@ To prevent mapping of a key from happening, see: [unmapping functionality](#unma
|<kbd>ctrl</kbd>+<kbd>n</kbd> handler|insert|<kbd>ctrl</kbd>+<kbd>n</kbd>|`<Plug>(mkdx-ctrl-n-compl)`|
|<kbd>ctrl</kbd>+<kbd>p</kbd> handler|insert|<kbd>ctrl</kbd>+<kbd>p</kbd>|`<Plug>(mkdx-ctrl-p-compl)`|
|<kbd>#</kbd> handler|insert|<kbd>#</kbd>|`<Plug>(mkdx-link-compl)`|
|Jump to file / URL|normal|<kbd>g</kbd><kbd>f</kbd>|`<Plug>(mkdx-gf)`|
## Remapping functionality
Expand Down
31 changes: 25 additions & 6 deletions autoload/mkdx.vim
Original file line number Diff line number Diff line change
Expand Up @@ -664,16 +664,35 @@ fun! s:util.hlBounds(type)
return [slnum, scol, elnum, ecol]
endfun

fun! s:util.linkUrl(md_link)
let open_paren = match(a:md_link, '](')
let close_paren = match(a:md_link, ')', open_paren)

return a:md_link[(open_paren + 2):(close_paren - 1)]
endfun

fun! mkdx#gf()
if s:util.isAlreadyWrapped('mkdx-text-link-n')
let [slnum, scol, elnum, ecol] = s:util.hlBounds('mkdx-text-link-n')
let destination = s:util.linkUrl(getline(slnum)[scol:])

if destination =~? '^http'
silent! call system('open ' . destination)
else
normal! f(lgf
endif
else
normal! gf
endif
endfun


fun! s:util.unwrap(type, start, end)
let [slnum, scol, elnum, ecol] = s:util.hlBounds(a:type)
let end = a:end

if (a:end == ']()') " end of markdown link, may contain link
let link_start = getline(slnum)[scol:]
let open_paren = match(link_start, '](')
let close_paren = match(link_start, ')', open_paren)
let url = link_start[(open_paren + 2):(close_paren - 1)]
let end = '](' . url . ')'
if (end == ']()') " end of markdown link, may contain link
let end = '](' . s:util.linkUrl(getline(slnum)[scol:]) . ')'
endif
" echom 'slnum:' slnum 'scol:' scol 'elnum:' elnum 'ecol:' ecol

Expand Down
32 changes: 32 additions & 0 deletions doc/mkdx.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ CONTENTS *mkdx-contents*
<Plug>(mkdx-insert-kbd)
<Plug>(mkdx-fence-tilde)
<Plug>(mkdx-fence-backtick)
<Plug>(mkdx-gf)

Mappings
Insert link completion
Expand All @@ -152,6 +153,7 @@ CONTENTS *mkdx-contents*
Toggle checklist
Toggle list
Toggle kbd shortcut
Jump to file or url

Functions
mkdx#ToggleCheckboxState()
Expand Down Expand Up @@ -181,6 +183,7 @@ CONTENTS *mkdx-contents*
mkdx#fold()
mkdx#WrapText()
mkdx#WrapStrike()
mkdx#gf()

Errors
Type
Expand Down Expand Up @@ -343,6 +346,7 @@ for functions. Below is a list of all helptags:
- |mkdx-plug-insert-kbd|
- |mkdx-plug-fence-tilde|
- |mkdx-plug-fence-backtick|
- |mkdx-plug-gf|

- |mkdx-mappings|
- |mkdx-mapping-insert-link-completion|
Expand All @@ -368,6 +372,7 @@ for functions. Below is a list of all helptags:
- |mkdx-mapping-toggle-checklist|
- |mkdx-mapping-toggle-list|
- |mkdx-mapping-toggle-kbd-shortcut|
- |mkdx-mapping-jump-to-file-or-url|

- |mkdx-functions|
- |mkdx-function-toggle-checkbox|
Expand Down Expand Up @@ -396,6 +401,7 @@ for functions. Below is a list of all helptags:
- |mkdx-function-insert-ctrl-p-handler|
- |mkdx-function-complete-link|
- |mkdx-function-fold|
- |mkdx-function-gf|

- |mkdx-errors|
- |mkdx-error-type|
Expand Down Expand Up @@ -1334,6 +1340,11 @@ using {repeat.vim} by Tim Pope (https://github.com/tpope/vim-repeat).

`<C-R>=mkdx#InsertFencedCodeBlock('```')<Cr>kA`

==============================================================================
<Plug>(mkdx-gf) *mkdx-plug-gf*

`:<C-U>call mkdx#gf()<Cr>`

==============================================================================
MAPPINGS *mkdx-mappings*

Expand Down Expand Up @@ -1666,6 +1677,17 @@ kbd expression. In visual mode, the current visual selection will be expanded.

Examples can be found at |mkdx-function-toggle-to-kbd|

==============================================================================
Jump to file or url *mkdx-mapping-jump-to-file-or-url*
gf

This mapping works like regular |gf| unless the cursor is positioned on a
markdown link. When on a link, mkdx will open the file or url inbetween the
parentheses. The cursor can be anywhere on the link and this will work.

When the link is not a file, but a URL, mkdx will use an `open` shell command.
If `open` is not available, it will silently fail.

==============================================================================
FUNCTIONS *mkdx-functions*

Expand Down Expand Up @@ -2358,6 +2380,16 @@ See |mkdx-setting-tokens-stike| for more details. Both arguments are passed to
word under the cursor will be wrapped. When {plug} is omitted, it defaults to
an empty string.

==============================================================================
mkdx#gf() *mkdx-function-gf*

This function is used when positioned within markdown links, it provides
functionality required for |mkdx-mapping-jump-to-file-or-url| (<gf>).

When on a markdown link, will open the location, if it is a file, regular |gf|
is used to open the file, when it is a URL (anything starting with `http`)
it will use `open` to open the URL, fails silently otherwise.

==============================================================================
|ERRORS| *mkdx-errors*

Expand Down
4 changes: 3 additions & 1 deletion ftplugin/markdown/mkdx.vim
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ noremap <silent> <Plug>(mkdx-toggle-to-kbd-n) :call mkdx#ToggleT
noremap <silent> <Plug>(mkdx-toggle-to-kbd-v) :<C-U>call mkdx#ToggleToKbd('v')<Cr>
noremap <silent> <Plug>(mkdx-shift-o) :<C-U>call mkdx#ShiftOHandler()<Cr>
noremap <silent> <Plug>(mkdx-o) :<C-U>call mkdx#OHandler()<Cr>
noremap <silent> <Plug>(mkdx-gf) :<C-U>call mkdx#gf()<Cr>
inoremap <silent> <Plug>(mkdx-enter) <C-R>=mkdx#EnterHandler()<Cr>:setlocal autoindent<Cr>
inoremap <silent> <Plug>(mkdx-shift-enter) <C-R>=mkdx#ShiftEnterHandler()<Cr>
inoremap <silent> <Plug>(mkdx-insert-kbd) <kbd></kbd>F<
Expand Down Expand Up @@ -158,7 +159,8 @@ if g:mkdx#settings.map.enable == 1
\ ['Toggle\ to\ kbd\ tag', 1, 'v', 'k', '<Plug>(mkdx-toggle-to-kbd-v)', ':call mkdx#ToggleToKbd("v")<cr>'],
\ ['Insert\ kbd\ tag', 0, 'i', '<<tab>', '<Plug>(mkdx-insert-kbd)', '<kbd></kbd>2hcit'],
\ ['Backtick\ fenced\ code\ block', 0, 'i', '```', '<Plug>(mkdx-fence-backtick)', '<C-R>=mkdx#FencedCodeBlock("`")<Cr>kA'],
\ ['tilde\ fenced\ code\ block', 0, 'i', '~~~', '<Plug>(mkdx-fence-tilde)', '<C-R>=mkdx#FencedCodeBlock("~")<Cr>kA']
\ ['tilde\ fenced\ code\ block', 0, 'i', '~~~', '<Plug>(mkdx-fence-tilde)', '<C-R>=mkdx#FencedCodeBlock("~")<Cr>kA'],
\ ['Jump to file / open URL', 0, 'n', 'gf', '<Plug>(mkdx-gf)', ':<C-U>call mkdx#gf()<Cr>']
\ ]

if (g:mkdx#settings.links.fragment.complete)
Expand Down

0 comments on commit 779853a

Please sign in to comment.