diff --git a/plugin/bullets.vim b/plugin/bullets.vim index fd0560e..8124bec 100644 --- a/plugin/bullets.vim +++ b/plugin/bullets.vim @@ -649,6 +649,7 @@ fun! s:line_ends_in_colon(lnum) return l:line[strlen(l:line)-1:] ==# ':' endif endfun + " --------------------------------------------------------- }}} " Checkboxes ---------------------------------------------- {{{ @@ -715,11 +716,10 @@ fun! s:set_checkbox(lnum, marker) endif endfun -fun! s:toggle_checkboxes_nested() +fun! s:toggle_checkboxes_nested(lnum) " toggle checkbox on the current line, as well as its parents and children - let l:lnum = line('.') - let l:indent = indent(l:lnum) - let l:bullet = s:closest_bullet_types(l:lnum, l:indent) + let l:indent = indent(a:lnum) + let l:bullet = s:closest_bullet_types(a:lnum, l:indent) let l:bullet = s:resolve_bullet_type(l:bullet) " Is this a checkbox? Do nothing if it's not, otherwise toggle the checkbox @@ -727,16 +727,16 @@ fun! s:toggle_checkboxes_nested() return endif - let l:checked = s:toggle_checkbox(l:lnum) + let l:checked = s:toggle_checkbox(a:lnum) if g:bullets_nested_checkboxes " Toggle children and parents - let l:completion_marker = s:sibling_checkbox_status(l:lnum) - call s:set_parent_checkboxes(l:lnum, l:completion_marker) + let l:completion_marker = s:sibling_checkbox_status(a:lnum) + call s:set_parent_checkboxes(a:lnum, l:completion_marker) " Toggle children if l:checked >= 0 - call s:set_child_checkboxes(l:lnum, l:checked) + call s:set_child_checkboxes(a:lnum, l:checked) endif endif endfun @@ -777,9 +777,25 @@ fun! s:set_child_checkboxes(lnum, checked) endif endfun +fun! s:visual_toggle_checkboxes() + let l:selection_lines = s:get_visual_selection_lines() + for l:line in l:selection_lines + " call s:toggle_checkboxes_nested(l:line.nr) + let l:checked = s:toggle_checkbox(l:line.nr) + + if g:bullets_nested_checkboxes + " Toggle children and parents + let l:completion_marker = s:sibling_checkbox_status(l:line.nr) + call s:set_parent_checkboxes(l:line.nr, l:completion_marker) + endif + + endfor +endfun + command! SelectCheckboxInside call select_checkbox(1) command! SelectCheckbox call select_checkbox(0) -command! ToggleCheckbox call toggle_checkboxes_nested() +command! ToggleCheckbox call toggle_checkboxes_nested(line('.')) +command! -range=% ToggleCheckboxVisual call visual_toggle_checkboxes() " Checkboxes ---------------------------------------------- }}} " Roman numerals --------------------------------------------- {{{ @@ -1071,6 +1087,13 @@ fun! s:change_line_bullet_level(direction, lnum) call setline(a:lnum, l:next_bullet_str) endfun +fun! s:change_bullet_level_and_renumber(direction) + " Calls change_bullet_level and then renumber_whole_list if required + call s:change_bullet_level(a:direction) + if g:bullets_renumber_on_change + call s:renumber_whole_list() + endif +endfun fun! s:change_bullet_level(direction, is_visual) " Changes the bullet level for each of the selected lines @@ -1104,6 +1127,7 @@ nnoremap (bullets-renumber) :RenumberList " Toggle checkbox nnoremap (bullets-toggle-checkbox) :ToggleCheckbox +vnoremap (bullets-toggle-checkbox) :ToggleCheckboxVisual " Promote and Demote outline level inoremap (bullets-demote) :BulletDemote @@ -1153,6 +1177,7 @@ augroup TextBulletsMappings " Toggle checkbox call s:add_local_mapping(1, 'nmap', 'x', '(bullets-toggle-checkbox)') + call s:add_local_mapping(1, 'vmap', 'x', '(bullets-toggle-checkbox)') " Promote and Demote outline level call s:add_local_mapping(1, 'imap', '', '(bullets-demote)') diff --git a/spec/checkboxes_spec.rb b/spec/checkboxes_spec.rb index 814e354..56eae0f 100644 --- a/spec/checkboxes_spec.rb +++ b/spec/checkboxes_spec.rb @@ -248,4 +248,29 @@ TEXT end + + it 'visual toggle a bullet' do + filename = "#{SecureRandom.hex(6)}.txt" + write_file(filename, <<-TEXT) + # Hello there + - [ ] first bullet + - [ ] second bullet + - [ ] third bullet + TEXT + + vim.edit filename + vim.normal 'jV2j' + vim.command 'ToggleCheckboxVisual' + vim.write + + file_contents = IO.read(filename) + + expect(file_contents).to eq normalize_string_indent(<<-TEXT) + # Hello there + - [X] first bullet + - [X] second bullet + - [X] third bullet + + TEXT + end end