diff --git a/README.adoc b/README.adoc index da9fc64..761d9aa 100644 --- a/README.adoc +++ b/README.adoc @@ -359,6 +359,42 @@ There are also two shorthands for adding the attributes `options=""` and `cols=" With the cursor inside the table, kbd:[+,cols+] adds the latter and kbd:[+,opts+] the former. +==== Aligning + +When the https://github.com/godlygeek/tabular[Tabular Plugin] is +installed (which is highly recommended when working with tables), +alignment of tables is possible via kbd:[+,|+]. This applies only to tables +that use the Bar kbd:[+|+] as separator between cells. + +Be aware that this only works for basic tables and certain features like +cell spanning do not work well with it together. It won't break the table, +but the cell spanning will not be visualized in the asciidoc sourcecode. + +.Before +.... +[%header] +|=== +|Animal|Color|Food +|Chicken|white|mostlycrop +|Pig|rose|everything +|=== +.... + +.After +.... +[%header] +|=== +| Animal | Color | Food +| Chicken | white | mostly crop +| Pig | rose | everything +|=== +.... + +If the option `g:asciidoc_table_autoalign` is set to 1 (which is set to 0 +by default) a function will be enabled that autoaligns a table each time +the Bar kbd:[+|+] is entered inside the table. + + [[editing-motions]] === Motions diff --git a/doc/asciidoc.txt b/doc/asciidoc.txt index 8089397..dd157df 100644 --- a/doc/asciidoc.txt +++ b/doc/asciidoc.txt @@ -279,6 +279,9 @@ Move forward to end of section with |][| and backward with |[]|. " Table attributes nnoremap cols :call asciidoc#table#insert_attributes('cols') nnoremap opts :call asciidoc#table#insert_attributes('options') + " Table alignment (requires Tabular vim plugin) + nnoremap :Tabularize /\(===\)\@! + vnoremap :Tabularize /\(===\)\@! " End.Table }}} diff --git a/ftplugin/asciidoc.vim b/ftplugin/asciidoc.vim index fc3e738..48b1a51 100644 --- a/ftplugin/asciidoc.vim +++ b/ftplugin/asciidoc.vim @@ -72,6 +72,11 @@ if -1 < match(g:asciidoc_use_defaults, 'options') let g:asciidoc_debug_level = 0 endif + if !exists('g:asciidoc_table_autoalign') + " do not autoalign table cells by default + let g:asciidoc_table_autoalign = 0 + endif + setlocal commentstring=//\ %s setlocal comments=fl:////,://,fn:*,fn:. setlocal formatoptions=tcqjnro @@ -375,4 +380,26 @@ nnoremap dsb :call asciidoc#experimental#delete_s " }}} +" Tabular ================================================== {{{ +if exists(':Tabularize') + " Use | to realign tables with the Tabuliarize plugin + nnoremap :Tabularize /\(===\)\@! " we need a negative lookahead to avoid breaking the block delimiters + vnoremap :Tabularize /\(===\)\@! " we need a negative lookahead to avoid breaking the block delimiters + + " Realign table when entering a | + if g:asciidoc_table_autoalign == 1 + inoremap :call align()a + function! s:align() + if exists(':Tabularize') && getline('.') =~# '^\s*|' && (getline(line('.')-1) =~# '^\s*|' || getline(line('.')+1) =~# '^\s*|') + let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g')) + let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*')) + Tabularize /|\(=\)\@! + normal! 0 + call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.')) + endif + endfunction + endif +endif +" }}} + " vim: set fdm=marker: