From 5b82dfb18ce6570049275ae4d8a9b362a1090c2f Mon Sep 17 00:00:00 2001 From: Thomas Torsney-Weir Date: Mon, 30 Sep 2013 21:15:22 +0200 Subject: [PATCH 1/9] add basic mmd table editing support --- README.md | 19 ++++- ftplugin/mmd/tables.vim | 172 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 ftplugin/mmd/tables.vim diff --git a/README.md b/README.md index 679dfeb..e064bf4 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,21 @@ An extension of Ben Williams's markdown syntax file to accommodate Fletcher Penney's multimarkdown. -Still in early, halfhearted development. +## Table support + +This plugin allows you to add columns and rows to a table, change the +justification, and format the table so all columns have the same width. +The functions for this are as follows: + +* `g:GotoCell(row, col)` +* `g:AddTableRowBelow()` +* `g:AddTableRowAbove()` +* `g:AddTableColumnBefore()` +* `g:AddTableColumnAfter()` +* `g:FormatTable()` +* `g:LeftifyColumn()` +* `g:CenterColumn()` +* `g:RightifyColumn()` + + + diff --git a/ftplugin/mmd/tables.vim b/ftplugin/mmd/tables.vim new file mode 100644 index 0000000..2325c72 --- /dev/null +++ b/ftplugin/mmd/tables.vim @@ -0,0 +1,172 @@ + +" --------------------- +" internal module stuff +" --------------------- + +" clears out the contents of a row without affecting the columns +function! s:ClearRow(line) + return substitute(a:line, '[^|]', ' ', 'g') +endfunction + +" returns the line of the header of the table +function! s:HeaderLine() + let winview = winsaveview() + normal! { + let linenum = line(".") + 1 + call winrestview(winview) + return linenum +endfunction + +" returns the last line of the table +function! s:TableEndLine() + let winview = winsaveview() + normal! } + let linenum = line(".") - 1 + call winrestview(winview) + return linenum +endfunction + +function! s:TableNumRows() + return s:TableEndLine() - s:HeaderLine() + 1 +endfunction + +function! s:TableNumCols() + return strlen(substitute(getline('.'), '[^|]', '', 'g')) - 1 +endfunction + +" returns the current row relative to the beginning of the table +function! s:CurrentRow() + return line(".") - s:HeaderLine() + 1 +endfunction + +function! s:CurrentCol() + return strlen(substitute(getline('.')[0:col('.')], '[^|]', '', 'g')) +endfunction + +" returns the width of the column we are on +function! s:ColumnWidth() + let winview = winsaveview() + if getline('.')[col('.')-1] !=? '|' + normal! F| + endif + let colstart = col('.') + normal! f| + let colend = col('.') + call winrestview(winview) + return colend - colstart - 1 +endfunction + +" 1=left, 2=center, 3=right +function! s:ChangeJustification(col, justcode) + let winview = winsaveview() + call g:GotoCell(2, a:col) + let markerwidth = s:ColumnWidth() - 2 + if a:justcode == 1 + let juststr = ":" . repeat('-', markerwidth-1) + elseif a:justcode == 2 + let juststr = ":" . repeat('-', markerwidth-2) . ":" + elseif a:justcode == 3 + let juststr = repeat('-', markerwidth-1) . ":" + endif + execute "normal! ct| " . juststr . " " + call winrestview(winview) +endfunction + +" --------------------- +" public stuff +" --------------------- + +" goes to a specific cell +function! g:GotoCell(row, col) + " first move to the correct line + let destline = a:row + s:HeaderLine() - 1 + execute "normal! " . destline . "G" + normal! 0 + + " now we move to the correct cell + if a:col > 1 + execute "normal! " . (a:col-1) . "f|" + endif + normal! l +endfunction + +function! g:AddTableRowBelow() + normal! yyp + call setline('.', s:ClearRow(getline('.'))) +endfunction + +function! g:AddTableRowAbove() + normal! kyyp + call setline('.', s:ClearRow(getline('.'))) +endfunction + +function! g:AddTableColumnBefore() + let new_col = '|' . repeat(' ', s:ColumnWidth()) + let curcol = s:CurrentCol() + let curline = 1 + while curline <= s:TableNumRows() + call g:GotoCell(curline, curcol) + normal! F| + execute "normal! i" . new_col + let curline += 1 + endwhile +endfunction + +function! g:AddTableColumnAfter() + let new_col = repeat(' ', s:ColumnWidth()) . '|' + let curcol = s:CurrentCol() + let curline = 1 + while curline <= s:TableNumRows() + call g:GotoCell(curline, curcol) + normal! f| + execute "normal! a" . new_col + let curline += 1 + endwhile +endfunction + +function! g:FormatTable() + " we need to figure out how many spaces are in each column + " and adjust all rows to contain that many spaces + let col = 1 + while col <= s:TableNumCols() + " first figure out the largest column + let line = 1 + let maxcolwidth = 0 + while line <= s:TableNumRows() + call g:GotoCell(line, col) + let w = s:ColumnWidth() + if w > maxcolwidth + let maxcolwidth = w + endif + let line += 1 + endwhile + " now pad each row in the column + let line = 1 + while line <= s:TableNumRows() + call g:GotoCell(line, col) + let padlen = maxcolwidth - s:ColumnWidth() + 1 + if padlen > 0 + normal! t| + execute "normal! ct|" . repeat(' ', padlen) + endif + let line += 1 + endwhile + let col += 1 + endwhile +endfunction + +" left justify the column we're in +function! g:LeftifyColumn() + call s:ChangeJustification(s:CurrentCol(), 1) +endfunction + +" center justify the column we're in +function! g:CenterColumn() + call s:ChangeJustification(s:CurrentCol(), 2) +endfunction + +" right justify the column we're in +function! g:RightifyColumn() + call s:ChangeJustification(s:CurrentCol(), 3) +endfunction + From a4fa4d6c82d78d54012c8a2e4ab8f79e1edeba83 Mon Sep 17 00:00:00 2001 From: Thomas Torsney-Weir Date: Tue, 1 Oct 2013 18:28:03 +0200 Subject: [PATCH 2/9] safety check to make sure we're actually in a table row --- ftplugin/mmd/tables.vim | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/ftplugin/mmd/tables.vim b/ftplugin/mmd/tables.vim index 2325c72..b62b441 100644 --- a/ftplugin/mmd/tables.vim +++ b/ftplugin/mmd/tables.vim @@ -3,6 +3,10 @@ " internal module stuff " --------------------- +function! s:IsTableRow(line) + return a:line =~# "^\|.*\|$" +endfunction + " clears out the contents of a row without affecting the columns function! s:ClearRow(line) return substitute(a:line, '[^|]', ' ', 'g') @@ -78,6 +82,10 @@ endfunction " goes to a specific cell function! g:GotoCell(row, col) + if !s:IsTableRow(getline('.')) + return + endif + " first move to the correct line let destline = a:row + s:HeaderLine() - 1 execute "normal! " . destline . "G" @@ -91,16 +99,28 @@ function! g:GotoCell(row, col) endfunction function! g:AddTableRowBelow() + if !s:IsTableRow(getline('.')) + return + endif + normal! yyp call setline('.', s:ClearRow(getline('.'))) endfunction function! g:AddTableRowAbove() + if !s:IsTableRow(getline('.')) + return + endif + normal! kyyp call setline('.', s:ClearRow(getline('.'))) endfunction function! g:AddTableColumnBefore() + if !s:IsTableRow(getline('.')) + return + endif + let new_col = '|' . repeat(' ', s:ColumnWidth()) let curcol = s:CurrentCol() let curline = 1 @@ -113,6 +133,10 @@ function! g:AddTableColumnBefore() endfunction function! g:AddTableColumnAfter() + if !s:IsTableRow(getline('.')) + return + endif + let new_col = repeat(' ', s:ColumnWidth()) . '|' let curcol = s:CurrentCol() let curline = 1 @@ -125,6 +149,9 @@ function! g:AddTableColumnAfter() endfunction function! g:FormatTable() + if !s:IsTableRow(getline('.')) + return + endif " we need to figure out how many spaces are in each column " and adjust all rows to contain that many spaces let col = 1 From e1c516740e2d012d06971c92b703b47138c96320 Mon Sep 17 00:00:00 2001 From: Thomas Torsney-Weir Date: Sun, 13 Oct 2013 16:00:03 -0400 Subject: [PATCH 3/9] add info on default keybindings --- README.md | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e064bf4..59177cd 100644 --- a/README.md +++ b/README.md @@ -9,15 +9,31 @@ This plugin allows you to add columns and rows to a table, change the justification, and format the table so all columns have the same width. The functions for this are as follows: -* `g:GotoCell(row, col)` -* `g:AddTableRowBelow()` -* `g:AddTableRowAbove()` -* `g:AddTableColumnBefore()` -* `g:AddTableColumnAfter()` -* `g:FormatTable()` -* `g:LeftifyColumn()` -* `g:CenterColumn()` -* `g:RightifyColumn()` +| Operation | Function name | Default keybinding | +| :-------- | :------------ | :----------------- | +| Go to a cell | `g:GotoCell(row, col)` | None | +| Add a row above the current position | `g:AddTableRowBelow()` | `to` | +| Add a row below the current position | `g:AddTableRowAbove()` | `tO` | +| Add a column before the current position | `g:AddTableColumnBefore()` | `ti` | +| Add a column after the current position | `g:AddTableColumnAfter()` | `ta` | +| format the entire table | `g:FormatTable()` | `tgq` | +| set column to justify left | `g:LeftifyColumn()` | `tjl` | +| set column to justify center | `g:CenterColumn()` | `tjc` | +| set column to justify right | `g:RightifyColumn()` | `tjr` | +## Keybindings +Because of the number of functions this plugin provides default keybindings. +If you don't want to use this then you can disable key mappings in your +`.vimrc` file like so: + +```VimL +let g:vim_multimarkdown_map_keys = 0 +``` + +and then map the keys yourself, for example: + +```VimL +nnoremap addcol :call g:AddTableColumnBefore() +``` From 9d83630f7c28dd3aeab828106b57b804dab1a67f Mon Sep 17 00:00:00 2001 From: Thomas Torsney-Weir Date: Sun, 13 Oct 2013 15:59:34 -0400 Subject: [PATCH 4/9] add keybindings for table functions --- ftplugin/mmd/keybindings.vim | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 ftplugin/mmd/keybindings.vim diff --git a/ftplugin/mmd/keybindings.vim b/ftplugin/mmd/keybindings.vim new file mode 100644 index 0000000..4b9ed8b --- /dev/null +++ b/ftplugin/mmd/keybindings.vim @@ -0,0 +1,23 @@ + +if !exists('g:vim_multimarkdown_map_keys') + let g:vim_multimarkdown_map_keys = 1 +endif + +" --------------------- +" table stuff +" --------------------- + +if g:vim_multimarkdown_map_keys + " adding/removing cells + nnoremap ti :call g:AddTableColumnBefore() + nnoremap ta :call g:AddTableColumnAfter() + nnoremap tO :call g:AddTableRowAbove() + nnoremap to :call g:AddTableRowBelow() + + " table formatting + nnoremap tgq :call g:FormatTable() + nnoremap tjl :call g:LeftifyColumn() + nnoremap tjc :call g:CenterColumn() + nnoremap tjr :call g:RightifyColumn() +endif + From 03947e131aee1aba28a24094fbbf82896c7b584c Mon Sep 17 00:00:00 2001 From: Thomas Torsney-Weir Date: Tue, 29 Oct 2013 10:32:15 +0100 Subject: [PATCH 5/9] use commands instead of functions for the table stuff --- ftplugin/mmd/keybindings.vim | 16 ++++++------- ftplugin/mmd/tables.vim | 45 +++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/ftplugin/mmd/keybindings.vim b/ftplugin/mmd/keybindings.vim index 4b9ed8b..75d937d 100644 --- a/ftplugin/mmd/keybindings.vim +++ b/ftplugin/mmd/keybindings.vim @@ -9,15 +9,15 @@ endif if g:vim_multimarkdown_map_keys " adding/removing cells - nnoremap ti :call g:AddTableColumnBefore() - nnoremap ta :call g:AddTableColumnAfter() - nnoremap tO :call g:AddTableRowAbove() - nnoremap to :call g:AddTableRowBelow() + nnoremap ti :MmdTableAddColBefore + nnoremap ta :MmdTableAddColAfter + nnoremap tO :MmdTableAddRowAbove + nnoremap to :MmdTableAddRowBelow " table formatting - nnoremap tgq :call g:FormatTable() - nnoremap tjl :call g:LeftifyColumn() - nnoremap tjc :call g:CenterColumn() - nnoremap tjr :call g:RightifyColumn() + nnoremap tgq :MmdTableFormat + nnoremap tjl :MmdLeftJustifyCol + nnoremap tjc :MmdCenterCol + nnoremap tjr :MmdRightJustifyCol endif diff --git a/ftplugin/mmd/tables.vim b/ftplugin/mmd/tables.vim index b62b441..73b08a5 100644 --- a/ftplugin/mmd/tables.vim +++ b/ftplugin/mmd/tables.vim @@ -63,7 +63,7 @@ endfunction " 1=left, 2=center, 3=right function! s:ChangeJustification(col, justcode) let winview = winsaveview() - call g:GotoCell(2, a:col) + call s:GotoCell(2, a:col) let markerwidth = s:ColumnWidth() - 2 if a:justcode == 1 let juststr = ":" . repeat('-', markerwidth-1) @@ -81,7 +81,7 @@ endfunction " --------------------- " goes to a specific cell -function! g:GotoCell(row, col) +function! s:GotoCell(row, col) if !s:IsTableRow(getline('.')) return endif @@ -98,7 +98,7 @@ function! g:GotoCell(row, col) normal! l endfunction -function! g:AddTableRowBelow() +function! s:AddTableRowBelow() if !s:IsTableRow(getline('.')) return endif @@ -107,7 +107,7 @@ function! g:AddTableRowBelow() call setline('.', s:ClearRow(getline('.'))) endfunction -function! g:AddTableRowAbove() +function! s:AddTableRowAbove() if !s:IsTableRow(getline('.')) return endif @@ -116,7 +116,7 @@ function! g:AddTableRowAbove() call setline('.', s:ClearRow(getline('.'))) endfunction -function! g:AddTableColumnBefore() +function! s:AddTableColumnBefore() if !s:IsTableRow(getline('.')) return endif @@ -125,14 +125,14 @@ function! g:AddTableColumnBefore() let curcol = s:CurrentCol() let curline = 1 while curline <= s:TableNumRows() - call g:GotoCell(curline, curcol) + call s:GotoCell(curline, curcol) normal! F| execute "normal! i" . new_col let curline += 1 endwhile endfunction -function! g:AddTableColumnAfter() +function! s:AddTableColumnAfter() if !s:IsTableRow(getline('.')) return endif @@ -141,14 +141,14 @@ function! g:AddTableColumnAfter() let curcol = s:CurrentCol() let curline = 1 while curline <= s:TableNumRows() - call g:GotoCell(curline, curcol) + call s:GotoCell(curline, curcol) normal! f| execute "normal! a" . new_col let curline += 1 endwhile endfunction -function! g:FormatTable() +function! s:FormatTable() if !s:IsTableRow(getline('.')) return endif @@ -160,7 +160,7 @@ function! g:FormatTable() let line = 1 let maxcolwidth = 0 while line <= s:TableNumRows() - call g:GotoCell(line, col) + call s:GotoCell(line, col) let w = s:ColumnWidth() if w > maxcolwidth let maxcolwidth = w @@ -170,7 +170,7 @@ function! g:FormatTable() " now pad each row in the column let line = 1 while line <= s:TableNumRows() - call g:GotoCell(line, col) + call s:GotoCell(line, col) let padlen = maxcolwidth - s:ColumnWidth() + 1 if padlen > 0 normal! t| @@ -183,17 +183,34 @@ function! g:FormatTable() endfunction " left justify the column we're in -function! g:LeftifyColumn() +function! s:LeftifyColumn() call s:ChangeJustification(s:CurrentCol(), 1) endfunction " center justify the column we're in -function! g:CenterColumn() +function! s:CenterColumn() call s:ChangeJustification(s:CurrentCol(), 2) endfunction " right justify the column we're in -function! g:RightifyColumn() +function! s:RightifyColumn() call s:ChangeJustification(s:CurrentCol(), 3) endfunction + +" --------------------- +" commands +" --------------------- + +"command! -nargs=2 MmdTableCell call s:GotoCell() +command! MmdTableFormat call s:FormatTable() + +command! MmdLeftJustifyCol call s:LeftifyColumn() +command! MmdRightJustifyCol call s:RightifyColumn() +command! MmdCenterCol call s:CenterColumn() + +command! MmdTableAddRowBelow call s:AddTableRowBelow() +command! MmdTableAddRowAbove call s:AddTableRowAbove() +command! MmdTableAddColBefore call s:AddTableColumnBefore() +command! MmdTableAddColAfter call s:AddTableColumnAfter() + From 3214f919c5677a98a87733aca982dc086176bff8 Mon Sep 17 00:00:00 2001 From: Thomas Torsney-Weir Date: Tue, 29 Oct 2013 10:44:22 +0100 Subject: [PATCH 6/9] can delete table rows and cols now --- ftplugin/mmd/keybindings.vim | 10 ++++++---- ftplugin/mmd/tables.vim | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/ftplugin/mmd/keybindings.vim b/ftplugin/mmd/keybindings.vim index 75d937d..887be47 100644 --- a/ftplugin/mmd/keybindings.vim +++ b/ftplugin/mmd/keybindings.vim @@ -9,10 +9,12 @@ endif if g:vim_multimarkdown_map_keys " adding/removing cells - nnoremap ti :MmdTableAddColBefore - nnoremap ta :MmdTableAddColAfter - nnoremap tO :MmdTableAddRowAbove - nnoremap to :MmdTableAddRowBelow + nnoremap ti :MmdTableAddColBefore + nnoremap ta :MmdTableAddColAfter + nnoremap tO :MmdTableAddRowAbove + nnoremap to :MmdTableAddRowBelow + nnoremap tdd :MmdTableDeleteRow + nnoremap tx :MmdTableDeleteCol " table formatting nnoremap tgq :MmdTableFormat diff --git a/ftplugin/mmd/tables.vim b/ftplugin/mmd/tables.vim index 73b08a5..97697f5 100644 --- a/ftplugin/mmd/tables.vim +++ b/ftplugin/mmd/tables.vim @@ -148,6 +148,29 @@ function! s:AddTableColumnAfter() endwhile endfunction +function! s:DeleteTableRow() + if !s:IsTableRow(getline('.')) + return + endif + + normal! dd +endfunction + +function! s:DeleteTableColumn() + if !s:IsTableRow(getline('.')) + return + endif + + let curcol = s:CurrentCol() + let curline = 1 + while curline <= s:TableNumRows() + call s:GotoCell(curline, curcol) + normal! F| + normal! dt| + let curline += 1 + endwhile +endfunction + function! s:FormatTable() if !s:IsTableRow(getline('.')) return @@ -214,3 +237,6 @@ command! MmdTableAddRowAbove call s:AddTableRowAbove() command! MmdTableAddColBefore call s:AddTableColumnBefore() command! MmdTableAddColAfter call s:AddTableColumnAfter() +command! MmdTableDeleteRow call s:DeleteTableRow() +command! MmdTableDeleteCol call s:DeleteTableColumn() + From 369e53271d8528c2e1cf4c7295bf83fe9d824172 Mon Sep 17 00:00:00 2001 From: Thomas Torsney-Weir Date: Tue, 29 Oct 2013 10:51:53 +0100 Subject: [PATCH 7/9] update readme for new table functions --- README.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 59177cd..232aa20 100644 --- a/README.md +++ b/README.md @@ -9,17 +9,18 @@ This plugin allows you to add columns and rows to a table, change the justification, and format the table so all columns have the same width. The functions for this are as follows: -| Operation | Function name | Default keybinding | -| :-------- | :------------ | :----------------- | -| Go to a cell | `g:GotoCell(row, col)` | None | -| Add a row above the current position | `g:AddTableRowBelow()` | `to` | -| Add a row below the current position | `g:AddTableRowAbove()` | `tO` | -| Add a column before the current position | `g:AddTableColumnBefore()` | `ti` | -| Add a column after the current position | `g:AddTableColumnAfter()` | `ta` | -| format the entire table | `g:FormatTable()` | `tgq` | -| set column to justify left | `g:LeftifyColumn()` | `tjl` | -| set column to justify center | `g:CenterColumn()` | `tjc` | -| set column to justify right | `g:RightifyColumn()` | `tjr` | +| Operation | Command name | Default keybinding | +| :-------- | :------------ | :----------------- | +| Add a row above the current position | `:MmdTableAddRowAbove` | `to` | +| Add a row below the current position | `:MmdTableAddRowBelow` | `tO` | +| Add a column before the current position | `:MmdTableAddColBefore` | `ti` | +| Add a column after the current position | `:MmdTableAddColAfter` | `ta` | +| Remove the current table row | `:MmdTableDeleteRow` | `tdd` | +| Remove the current table column | `:MmdTableDeleteCol` | `tx` | +| Format the entire table | `:MmdTableFormat` | `tgq` | +| Set column to justify left | `:MmdLeftJustifyCol` | `tjl` | +| Set column to justify center | `:MmdCenterCol` | `tjc` | +| Set column to justify right | `:MmdRightJustifyCol` | `tjr` | ## Keybindings From c66cd5850c55febbb4599dcd88bb24e0a37bc440 Mon Sep 17 00:00:00 2001 From: Thomas Torsney-Weir Date: Tue, 4 Feb 2014 19:57:14 +0100 Subject: [PATCH 8/9] playing with syntax highlighting of tables --- syntax/mmd.vim | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/syntax/mmd.vim b/syntax/mmd.vim index 1347072..4208152 100644 --- a/syntax/mmd.vim +++ b/syntax/mmd.vim @@ -63,6 +63,9 @@ syn match mmdFootnoteMarker "\[^\S\+\]" syn match mmdFootnoteIdentifier "\[^.\+\]:" contained syn region mmdFootnoteText start="^\s\{0,3\}\[^.\+\]:[ \t]" end="^$" contains=mmdFootnoteIdentifier +" mmd table support +syn region mmdTable start=/\v^\|.+\|$/ end=/\v^$/ + " Link definitions: [id]: URL (Optional Title) " TODO handle automatic links without colliding with htmlTag () "syn region mkdLinkDef matchgroup=mkdDelimiter start="^ \{,3}\zs\[[^^#]" end="]:" oneline nextgroup=mkdLinkDefTarget skipwhite @@ -162,6 +165,15 @@ HtmlHiLink mkdSourceDef Statement HtmlHiLink mkdSource String HtmlHiLink mkdLinkAttrib Function +<<<<<<< HEAD +======= +HtmlHiLink mmdEquation1 mmdMath +HtmlHiLink mmdEquation2 mmdMath +HtmlHiLink mmdMath Special + +HtmlHiLink mmdTable Comment + +>>>>>>> 1429db9... playing with syntax highlighting of tables HtmlHiLink mkdDelimiter Delimiter let b:current_syntax = "mmd" From ae3bf2b501d4c6f772170da35434c6acc9455c38 Mon Sep 17 00:00:00 2001 From: Thomas Torsney-Weir Date: Wed, 5 Feb 2014 10:04:31 +0100 Subject: [PATCH 9/9] command to create a table of the specified size --- README.md | 28 +++++++++++++++------------- ftplugin/mmd/tables.vim | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 232aa20..0781025 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # vim-multimarkdown An extension of Ben Williams's markdown syntax file to accommodate Fletcher -Penney's multimarkdown. +Penney's [multimarkdown](http://fletcherpenney.net/multimarkdown/). ## Table support @@ -9,18 +9,20 @@ This plugin allows you to add columns and rows to a table, change the justification, and format the table so all columns have the same width. The functions for this are as follows: -| Operation | Command name | Default keybinding | -| :-------- | :------------ | :----------------- | -| Add a row above the current position | `:MmdTableAddRowAbove` | `to` | -| Add a row below the current position | `:MmdTableAddRowBelow` | `tO` | -| Add a column before the current position | `:MmdTableAddColBefore` | `ti` | -| Add a column after the current position | `:MmdTableAddColAfter` | `ta` | -| Remove the current table row | `:MmdTableDeleteRow` | `tdd` | -| Remove the current table column | `:MmdTableDeleteCol` | `tx` | -| Format the entire table | `:MmdTableFormat` | `tgq` | -| Set column to justify left | `:MmdLeftJustifyCol` | `tjl` | -| Set column to justify center | `:MmdCenterCol` | `tjc` | -| Set column to justify right | `:MmdRightJustifyCol` | `tjr` | +| Operation | Command name | Default keybinding | +| :-------- | :------------ | :----------------- | +| Add a row above the current position | `:MmdTableAddRowAbove` | `to` | +| Add a row below the current position | `:MmdTableAddRowBelow` | `tO` | +| Add a column before the current position | `:MmdTableAddColBefore` | `ti` | +| Add a column after the current position | `:MmdTableAddColAfter` | `ta` | +| Remove the current table row | `:MmdTableDeleteRow` | `tdd` | +| Remove the current table column | `:MmdTableDeleteCol` | `tx` | +| Format the entire table | `:MmdTableFormat` | `tgq` | +| Set column to justify left | `:MmdLeftJustifyCol` | `tjl` | +| Set column to justify center | `:MmdCenterCol` | `tjc` | +| Set column to justify right | `:MmdRightJustifyCol` | `tjr` | +| Create a new table of the specified size | `:MmdTableCreate ` | none | +| Go to a specific cell | `:MmdTableCell ` | none | ## Keybindings diff --git a/ftplugin/mmd/tables.vim b/ftplugin/mmd/tables.vim index 97697f5..e713f1f 100644 --- a/ftplugin/mmd/tables.vim +++ b/ftplugin/mmd/tables.vim @@ -80,6 +80,23 @@ endfunction " public stuff " --------------------- +" creates a new table of the specified size +" there is 1 header row +function! s:CreateTable(numrows, numcols) + " create the header row + execute "normal! i" . repeat('| ', a:numcols) . "|" + normal! o + " just make everything left justified + execute "normal! i" . repeat('| :-- ', a:numcols) . "|" + "normal! o + " put in the rows + let i = 0 + while i < a:numrows + call s:AddTableRowBelow() + let i += 1 + endwhile +endfunction + " goes to a specific cell function! s:GotoCell(row, col) if !s:IsTableRow(getline('.')) @@ -225,7 +242,8 @@ endfunction " commands " --------------------- -"command! -nargs=2 MmdTableCell call s:GotoCell() +command! -nargs=+ MmdTableCreate call s:CreateTable() +command! -nargs=+ MmdTableCell call s:GotoCell() command! MmdTableFormat call s:FormatTable() command! MmdLeftJustifyCol call s:LeftifyColumn()