diff --git a/README.md b/README.md index fe37091..26ef923 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ > ⭐ Using Neovim? -> +> > Checkout the Neovim Lua port here - https://github.com/bullets-vim/bullets.nvim 😄 # Description @@ -175,6 +175,21 @@ let g:bullets_auto_indent_after_colon = 1 " default = 1 " i. text ``` +Move the text after the cursor into a new bullet when hitting `` in the middle of a bullet (insert mode): + +```vim +" Example (| is cursor): +" - first |second + +let g:bullets_add_bullet_on_split = 0 " default = 0 +" - first +" |second + +let g:bullets_add_bullet_on_split = 1 +" - first +" - |second +``` + Maximum number of alphabetic characters to use for bullets: ```vim diff --git a/doc/bullets.txt b/doc/bullets.txt index 1dadf33..93765a0 100644 --- a/doc/bullets.txt +++ b/doc/bullets.txt @@ -167,6 +167,23 @@ If you would like to promote bullet instead of deleting it, add the following to `let g:bullets_delete_last_bullet_if_empty = 2` +Bullet on line split +------------------------------- +By default bullets.vim only adds a new bullet when the return key is pressed +at the end of a line. Pressing it in the middle of a bullet splits the line +without a bullet. + +If you would like the text after the cursor to move into a new bullet, add the +following to your .vimrc + + `let g:bullets_add_bullet_on_split = 1` + + - first |second + becomes + - first + - |second + + Maintain right padding on bullets --------------------------------- By default bullets.vim will try to pad bullets with enough spaces to align a diff --git a/plugin/bullets.vim b/plugin/bullets.vim index dcc2ed7..491d088 100644 --- a/plugin/bullets.vim +++ b/plugin/bullets.vim @@ -126,6 +126,12 @@ if !exists('g:bullets_auto_indent_after_colon') let g:bullets_auto_indent_after_colon = 1 endif +if !exists('g:bullets_add_bullet_on_split') + " Should in the middle of a bullet move the text after the cursor into + " a new bullet (1) or split the line without a bullet (0)? + let g:bullets_add_bullet_on_split = 0 +endif + " ------------------------------------------------------ }}} " Parse Bullet Type ------------------------------------------- {{{ @@ -565,23 +571,33 @@ endfun fun! s:insert_new_bullet() let l:curr_line_num = line('.') let l:next_line_num = l:curr_line_num + g:bullets_line_spacing + let l:normal_mode = mode() ==# 'n' + " when splitting, look for the bullet in the text before the cursor and + " carry the text after the cursor into the new bullet + let l:split = !l:normal_mode && !s:is_at_eol() && g:bullets_add_bullet_on_split + let l:text_after_cursor = '' + if l:split + let l:line = getline(l:curr_line_num) + let l:split_col = col('.') + let l:text_after_cursor = substitute(strpart(l:line, l:split_col - 1), '^\s\+', '', '') + call setline(l:curr_line_num, substitute(strpart(l:line, 0, l:split_col - 1), '\s\+$', '', '')) + endif let l:curr_indent = indent(l:curr_line_num) let l:closest_bullet_types = s:closest_bullet_types(l:curr_line_num, l:curr_indent) let l:bullet = s:resolve_bullet_type(l:closest_bullet_types) " need to find which line starts the previous bullet started at and start " searching up from there let l:send_return = 1 - let l:normal_mode = mode() ==# 'n' let l:indent_next = s:line_ends_in_colon(l:curr_line_num) && g:bullets_auto_indent_after_colon " check if current line is a bullet and we are at the end of the line (for " insert mode only) - if l:bullet != {} && (l:normal_mode || s:is_at_eol()) + if l:bullet != {} && (l:normal_mode || l:split || s:is_at_eol()) " was any text entered after the bullet? if l:bullet.text_after_bullet ==# '' " We don't want to create a new bullet if the previous one was not used, " instead we want to delete the empty bullet - like word processors do - if g:bullets_delete_last_bullet_if_empty + if g:bullets_delete_last_bullet_if_empty && !l:split if g:bullets_delete_last_bullet_if_empty == 1 call setline(l:curr_line_num, '') elseif g:bullets_delete_last_bullet_if_empty == 2 @@ -606,10 +622,10 @@ fun! s:insert_new_bullet() " insert next bullet call append(l:curr_line_num, l:next_bullet_list) + call setline(l:next_line_num, getline(l:next_line_num) . l:text_after_cursor) - - " go to next line after the new bullet - let l:col = strlen(getline(l:next_line_num)) + 1 + " go to the start of the new bullet's text + let l:col = strlen(getline(l:next_line_num)) - strlen(l:text_after_cursor) + 1 call setpos('.', [0, l:next_line_num, l:col]) " indent if previous line ended in a colon @@ -617,7 +633,7 @@ fun! s:insert_new_bullet() " demote the new bullet call s:change_line_bullet_level(-1, l:next_line_num) " reset cursor position after indenting - let l:col = strlen(getline(l:next_line_num)) + 1 + let l:col = strlen(getline(l:next_line_num)) - strlen(l:text_after_cursor) + 1 call setpos('.', [0, l:next_line_num, l:col]) elseif g:bullets_renumber_on_change call s:renumber_whole_list() @@ -627,6 +643,12 @@ fun! s:insert_new_bullet() endif endif + if l:split && l:send_return + " no bullet was added, so restore the line and split it as usual + call setline(l:curr_line_num, l:line) + call cursor(l:curr_line_num, l:split_col) + endif + if l:send_return || l:normal_mode " start a new line if l:normal_mode diff --git a/test/bullets_spec.lua b/test/bullets_spec.lua index 6b625fa..2956c14 100644 --- a/test/bullets_spec.lua +++ b/test/bullets_spec.lua @@ -34,6 +34,57 @@ describe("Bullets.vim", function() end) end) + describe("on return key when cursor is not at EOL and g:bullets_add_bullet_on_split is enabled", function() + before_each(function() + vim.g.bullets_add_bullet_on_split = 1 + end) + + it("moves the text after the cursor into a new bullet", function() + helpers.new_buffer({ + "# Hello there", + "- this is the first bullet", + }) + helpers.feedkeys("G0fbisecond ") + assert.are.same({ + "# Hello there", + "- this is the first", + "- second bullet", + }, helpers.get_lines()) + end) + + it("moves the text after the cursor into the next numeric bullet", function() + helpers.new_buffer({ + "# Hello there", + "1. this is the first bullet", + "2. this is the last bullet", + }) + helpers.feedkeys("2G0fbisecond ") + assert.are.same({ + "# Hello there", + "1. this is the first", + "2. second bullet", + "3. this is the last bullet", + }, helpers.get_lines()) + end) + + it("splits a line without a bullet as usual", function() + local saved_fo = vim.o.formatoptions + vim.cmd("set formatoptions-=r") + helpers.new_buffer({ + "# Hello there", + "this is not a bullet", + }) + helpers.feedkeys("G0fbi") + helpers.feedkeys("isecond ") + vim.o.formatoptions = saved_fo + assert.are.same({ + "# Hello there", + "this is not a ", + "second bullet", + }, helpers.get_lines()) + end) + end) + describe("on return key when cursor is at EOL", function() it("adds a new bullet if the previous line had a known bullet type", function() helpers.test_bullet_inserted( diff --git a/test/helpers.lua b/test/helpers.lua index ae1ee52..e761e5b 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -39,6 +39,7 @@ function M.reset_config() vim.g.bullets_custom_mappings = {} vim.g.bullets_max_alpha_characters = 2 vim.g.bullets_auto_indent_after_colon = 1 + vim.g.bullets_add_bullet_on_split = 0 vim.g.bullets_line_spacing = 1 vim.g.bullets_renumber_on_change = 1 vim.g.bullets_nested_checkboxes = 1