Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!-- ALL-CONTRIBUTORS-BADGE:END -->

> ⭐ Using Neovim?
>
>
> Checkout the Neovim Lua port here - https://github.com/bullets-vim/bullets.nvim 😄

# Description
Expand Down Expand Up @@ -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 `<cr>` 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
Expand Down
17 changes: 17 additions & 0 deletions doc/bullets.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 29 additions & 7 deletions plugin/bullets.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cr> 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 ------------------------------------------- {{{
Expand Down Expand Up @@ -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
Expand All @@ -606,18 +622,18 @@ 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
if l:indent_next
" 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()
Expand All @@ -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
Expand Down
51 changes: 51 additions & 0 deletions test/bullets_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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("G0fbi<CR>second <Esc>")
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("2G0fbi<CR>second <Esc>")
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<CR>")
helpers.feedkeys("isecond <Esc>")
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(
Expand Down
1 change: 1 addition & 0 deletions test/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down