|
6 | 6 | " Note: This file is encoded in UTF-8 including a byte order mark so |
7 | 7 | " that Vim loads the script using the right encoding transparently. |
8 | 8 |
|
9 | | -let g:xolox#notes#version = '0.15.5' |
| 9 | +let g:xolox#notes#version = '0.16' |
10 | 10 |
|
11 | 11 | function! xolox#notes#shortcut() " {{{1 |
12 | 12 | " The "note:" pseudo protocol is just a shortcut for the :Note command. |
@@ -770,23 +770,71 @@ function! xolox#notes#get_bullet(chr) |
770 | 770 | return xolox#notes#unicode_enabled() ? '•' : a:chr |
771 | 771 | endfunction |
772 | 772 |
|
773 | | -function! xolox#notes#indent_list(command, line1, line2) " {{{3 |
| 773 | +function! xolox#notes#indent_list(direction, line1, line2) " {{{3 |
774 | 774 | " Change indent of list items from {line1} to {line2} using {command}. |
| 775 | + let indentstr = repeat(' ', &tabstop) |
775 | 776 | if a:line1 == a:line2 && getline(a:line1) == '' |
776 | | - call setline(a:line1, repeat(' ', &tabstop)) |
| 777 | + call setline(a:line1, indentstr) |
777 | 778 | else |
778 | | - execute a:line1 . ',' . a:line2 . 'normal' a:command |
779 | | - if getline('.') =~ '\(•\|\*\)$' |
| 779 | + " Regex to match a leading bullet. |
| 780 | + let leading_bullet = xolox#notes#leading_bullet_pattern() |
| 781 | + for lnum in range(a:line1, a:line2) |
| 782 | + let line = getline(lnum) |
| 783 | + " Calculate new nesting level, should not result in < 0. |
| 784 | + let level = max([0, xolox#notes#get_list_level(line) + a:direction]) |
| 785 | + if a:direction == 1 |
| 786 | + " Indent the line. |
| 787 | + let line = indentstr . line |
| 788 | + else |
| 789 | + " Unindent the line. |
| 790 | + let line = substitute(line, '^' . indentstr, '', '') |
| 791 | + endif |
| 792 | + " Replace the bullet. |
| 793 | + let bullet = g:notes_list_bullets[level % len(g:notes_list_bullets)] |
| 794 | + call setline(lnum, substitute(line, leading_bullet, xolox#misc#escape#substitute(bullet), '')) |
| 795 | + endfor |
| 796 | + " Regex to match a trailing bullet. |
| 797 | + if getline('.') =~ xolox#notes#trailing_bullet_pattern() |
780 | 798 | " Restore trailing space after list bullet. |
781 | 799 | call setline('.', getline('.') . ' ') |
782 | 800 | endif |
783 | 801 | endif |
784 | 802 | normal $ |
785 | 803 | endfunction |
786 | 804 |
|
| 805 | +function! xolox#notes#leading_bullet_pattern() |
| 806 | + " Return a regular expression pattern that matches any leading list bullet. |
| 807 | + let escaped_bullets = copy(g:notes_list_bullets) |
| 808 | + call map(escaped_bullets, 'xolox#misc#escape#pattern(v:val)') |
| 809 | + return '\(\_^\s*\)\@<=\(' . join(escaped_bullets, '\|') . '\)' |
| 810 | +endfunction |
| 811 | + |
| 812 | +function! xolox#notes#trailing_bullet_pattern() |
| 813 | + " Return a regular expression pattern that matches any trailing list bullet. |
| 814 | + let escaped_bullets = copy(g:notes_list_bullets) |
| 815 | + call map(escaped_bullets, 'xolox#misc#escape#pattern(v:val)') |
| 816 | + return '\(' . join(escaped_bullets, '\|') . '\|\*\)$' |
| 817 | +endfunction |
| 818 | + |
| 819 | +function! xolox#notes#get_comments_option() |
| 820 | + " Get the value for the &comments option including user defined list bullets. |
| 821 | + let items = copy(g:notes_list_bullets) |
| 822 | + call map(items, '": " . v:val . " "') |
| 823 | + call add(items, ':> ') " <- e-mail style block quotes. |
| 824 | + return join(items, ',') |
| 825 | +endfunction |
| 826 | + |
| 827 | +function! xolox#notes#get_list_level(line) |
| 828 | + " Get the nesting level of the list item on the given line. This will only |
| 829 | + " work with the list item indentation style expected by the notes plug-in |
| 830 | + " (that is, top level list items are indented with one space, each nested |
| 831 | + " level below that is indented by pairs of three spaces). |
| 832 | + return (len(matchstr(a:line, '^\s*')) - 1) / 3 |
| 833 | +endfunction |
| 834 | + |
787 | 835 | function! xolox#notes#cleanup_list() " {{{3 |
788 | 836 | " Automatically remove empty list items on Enter. |
789 | | - if getline('.') =~ '^\s*\' . xolox#notes#get_bullet('*') . '\s*$' |
| 837 | + if getline('.') =~ (xolox#notes#leading_bullet_pattern() . '\s*$') |
790 | 838 | let s:sol_save = &startofline |
791 | 839 | setlocal nostartofline " <- so that <C-u> clears the complete line |
792 | 840 | return "\<C-o>0\<C-o>d$\<C-o>o" |
|
0 commit comments