]> 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:

Add Toc commands.
authorCiro Santillli <ciro.santilli@gmail.com>
Sat, 29 Mar 2014 20:59:19 +0000 (21:59 +0100)
committerCiro Santillli <ciro.santilli@gmail.com>
Sun, 30 Mar 2014 09:40:13 +0000 (11:40 +0200)
README.md
ftplugin/mkd.vim
test/multiple_headers.md [new file with mode: 0644]

index 9478b5a86e6e549b2b36c775076aa7be7ba11218..843405f0035e32819a0656151ae0746e58406191 100644 (file)
--- a/README.md
+++ b/README.md
@@ -59,6 +59,18 @@ The following work on normal and visual modes:
 - `]c`: go to Current header.
 - `]u`: go to parent header (Up).
 
+## Commands
+
+The following commands currently only work for atx style headers (`#`). Pull request are welcome to extend them to Setext style headers (`===`).
+
+- `:Toc`: create a quickfix vertical window navigable table of contents with the headers.
+
+    Hit `<Enter>` on a line to jump to the corresponding line of the markdown file.
+
+- `:Toch`: Same as `:Toc` but in an horizontal window.
+- `:Toct`: Same as `:Toc` but in a new tab.
+- `:Tocv`: Same as `:Toc` for symmetry with `:Toch` and `Tocv`.
+
 ## Credits
 
 The main contributors of vim-markdown are:
index 4e9f4485833154119dc021d18e836dc4b243a020..27fd855a61e6c5bf834937e1caf127a4a42ae3de 100644 (file)
@@ -294,6 +294,39 @@ function! b:Markdown_MoveToPreviousSiblingHeader()
     endif
 endfunction
 
+function! b:Markdown_Toc(...)
+    if a:0 > 0
+        let l:window_type = a:1
+    else
+        let l:window_type = 'vertical'
+    endif
+    silent vimgrep '^#' %
+    if l:window_type ==# 'horizontal'
+        copen
+    elseif l:window_type ==# 'vertical'
+        vertical copen
+        let &winwidth=(&columns/2)
+    elseif l:window_type ==# 'tab'
+        tab copen
+    else
+        copen
+    endif
+    set modifiable
+    %s/\v^([^|]*\|){2,2} #//
+    for i in range(1, line('$'))
+        let l:line = getline(i)
+        let l:header =  matchstr(l:line, '^#*')
+        let l:length = len(l:header)
+        let l:line = substitute(l:line, '\v^#*[ ]*', '', '')
+        let l:line = substitute(l:line, '\v[ ]*#*$', '', '')
+        let l:line = repeat(' ', (2 * l:length)) . l:line
+        call setline(i, l:line)
+    endfor
+    set nomodified
+    set nomodifiable
+    normal! gg
+endfunction
+
 " Wrapper to do move commands in visual mode.
 "
 function! s:VisMove(f)
@@ -316,3 +349,8 @@ call <sid>MapNormVis('[]', 'b:Markdown_MoveToPreviousSiblingHeader')
 call <sid>MapNormVis(']u', 'b:Markdown_MoveToParentHeader')
 " Menmonic: Current
 call <sid>MapNormVis(']c', 'b:Markdown_MoveToCurHeader')
+
+command! -buffer Toc call b:Markdown_Toc()
+command! -buffer Toch call b:Markdown_Toc('horizontal')
+command! -buffer Tocv call b:Markdown_Toc('vertical')
+command! -buffer Toct call b:Markdown_Toc('tab')
diff --git a/test/multiple_headers.md b/test/multiple_headers.md
new file mode 100644 (file)
index 0000000..b6a403a
--- /dev/null
@@ -0,0 +1,27 @@
+This file is used for tests which require there to be multiple headers in different relative positions to each other.
+
+Each header should have an unique text that identifies it.
+
+# h1 space
+
+#h1 nospace
+
+# h1 trailing hash #
+
+## h2 space
+
+##h2 nospace
+
+## h2 trailing hash ##
+
+### h3 space
+
+###h3 nospace
+
+### h3 trailing hash ###
+
+# h1 before h2
+
+## h2 between h1s
+
+# h1 after h2