]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Follow named anchors with ge
authormemeplex <carlosjosepita@gmail.com>
Wed, 22 Feb 2017 16:52:36 +0000 (13:52 -0300)
committermemeplex <carlosjosepita@gmail.com>
Wed, 22 Feb 2017 17:15:05 +0000 (14:15 -0300)
This commit allows ge to follow named anchors in links of the form
file#anchor, where file may omit the .md extension as usual.

Two global variables control the operation of this feature:

    vim_markdown_follow_anchor: a flag that tells vim-markdown whether
    to attempt to follow a named anchor in a link or not. When this is
    1, and only if a link can be split in exactly two parts by the
    pattern '#', then the first part is interpreted as the file and the
    second one as the named anchor. After the file is opened, the anchor
    will be searched.

    vim_markdown_anchorexpr: if available this expression will be
    evaluated substituting v:anchor with a quoted string that contains
    the anchor to visit. The result of the evaluation will become the
    real anchor to search in the target file. This is useful in order to
    convert anchors of the form my-section-title to searches of the form
    My Section Title. It needs to be generic since every markdown
    renderer follows its own conventions to generate "auto-anchors".
    A dummy example: let vim_markdown_anchorexpr = "'<<'.v:anchor"

Note: I've also reformatted EditUrlUnderCursor a bit since it was
violating the 4-space tab and single-quoted string conventions that most
of the rest of the code seems to follow.

ftplugin/markdown.vim

index 21c9442c09eed2ef84ad7011f74ba482a0116eae..35c8a63ac5dc62412611e40159df0013627d876d 100644 (file)
@@ -578,22 +578,37 @@ endfunction
 
 " We need a definition guard because we invoke 'edit' which will reload this
 " script while this function is running. We must not replace it.
-if !exists("*s:EditUrlUnderCursor")
-  function s:EditUrlUnderCursor()
-      let l:url = s:Markdown_GetUrlForPosition(line('.'), col('.'))
-      if l:url != ''
-          if get(g:, 'vim_markdown_autowrite', 0)
-            write
-          endif
-          if get(g:, 'vim_markdown_no_extensions_in_markdown', 0)
-              execute 'edit' fnamemodify(expand('%:~'), ':p:h').'/'.l:url.'.md'
-          else
-              execute 'edit' l:url 
-          endif
-      else
-          echomsg 'The cursor is not on a link.'
-      endif
-  endfunction
+if !exists('*s:EditUrlUnderCursor')
+    function s:EditUrlUnderCursor()
+        let l:url = s:Markdown_GetUrlForPosition(line('.'), col('.'))
+        let l:anchor = ''
+        if l:url != ''
+            if get(g:, 'vim_markdown_autowrite', 0)
+                write
+            endif
+            if get(g:, 'vim_markdown_follow_anchor', 0)
+                let l:parts = split(l:url, '#')
+                if len(l:parts) == 2
+                    let [l:url, l:anchor] = parts
+                    let l:anchorexpr = get(g:, 'vim_markdown_anchorexpr', '')
+                    if l:anchorexpr != ''
+                        let l:anchor = eval(substitute(
+                            \ l:anchorexpr, 'v:anchor',
+                            \ escape('"'.l:anchor.'"', '"'), ''))
+                    endif
+                endif
+            endif
+            if get(g:, 'vim_markdown_no_extensions_in_markdown', 0)
+                let l:url = fnamemodify(expand('%:~'), ':p:h').'/'.l:url.'.md'
+            endif
+            execute 'edit' l:url
+            if l:anchor != ''
+                silent! execute '/'.l:anchor
+            endif
+        else
+            echomsg 'The cursor is not on a link.'
+        endif
+    endfunction
 endif
 
 function! s:VersionAwareNetrwBrowseX(url)