Skip to content

Commit

Permalink
feat: add <plug> mappings
Browse files Browse the repository at this point in the history
* Add “<Plug>…” key maps

* Add support for “g:bullets_custom_mappings” option

* README: Add usage example for “g:bullets_custom_mappings” option

* README: Add clarification comment for g:bullets_custom_mappings option

Co-authored-by: Viacheslav Lotsmanov <[email protected]>
  • Loading branch information
wenzel-hoffman and unclechu authored Sep 10, 2022
1 parent 770a93c commit 643a3c7
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 23 deletions.
42 changes: 36 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Bullets.vim](img/bullets-vim-logo.svg)

[![Build Status](https://travis-ci.org/dkarter/bullets.vim.svg?branch=master)](https://travis-ci.org/dkarter/bullets.vim)
[![Build Status](https://travis-ci.org/dkarter/bullets.vim.svg?branch=master)](https://travis-ci.org/dkarter/bullets.vim)

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors-)
Expand Down Expand Up @@ -79,6 +79,36 @@ Add a leader key before default mappings:
let g:bullets_mapping_leader = '<M-b>' " default = ''
```

Customize key mappings:

```vim
let g:bullets_set_mappings = 0 " disable adding default key mappings, default = 1
" default = []
" N.B. You can set these mappings as-is without using this g:bullets_custom_mappings option but it
" will apply in this case for all file types while when using g:bullets_custom_mappings it would
" take into account file types filter set in g:bullets_enabled_file_types, and also
" g:bullets_enable_in_empty_buffers option.
let g:bullets_custom_mappings = [
\ ['imap', '<cr>', '<Plug>(bullets-newline)'],
\ ['inoremap', '<C-cr>', '<cr>'],
\
\ ['nmap', 'o', '<Plug>(bullets-newline)'],
\
\ ['vmap', 'gN', '<Plug>(bullets-renumber)'],
\ ['nmap', 'gN', '<Plug>(bullets-renumber)'],
\
\ ['nmap', '<leader>x', '<Plug>(bullets-toggle-checkbox)'],
\
\ ['imap', '<C-t>', '<Plug>(bullets-demote)'],
\ ['nmap', '>>', '<Plug>(bullets-demote)'],
\ ['vmap', '>', '<Plug>(bullets-demote)'],
\ ['imap', '<C-d>', '<Plug>(bullets-promote)'],
\ ['nmap', '<<', '<Plug>(bullets-promote)'],
\ ['vmap', '<', '<Plug>(bullets-promote)'],
\ ]
```

Enable/disable deleting the last empty bullet when hitting `<cr>` (insert mode) or `o` (normal mode):

```vim
Expand Down Expand Up @@ -184,7 +214,7 @@ let g:bullets_nested_checkboxes = 1 " default = 1
" - [ ] child bullet [ type <leader>x ]
" - [ ] sub-child
" - [ ] child bullet
"
"
" Result:
" - [o] first bullet [ <- indicates partial completion of sub-tasks ]
" - [X] child bullet
Expand Down Expand Up @@ -225,18 +255,18 @@ let g:bullets_checkbox_partials_toggle = 1 " default = 1
" - [o] partially checked [ type <leader>x ]
" - [x] sub bullet
" - [ ] sub bullet
"
"
" Result:
" - [x] checked
" - [x] sub bullet
" - [x] sub bullet
"
"
" Example 2:
let g:bullets_checkbox_partials_toggle = 0
" - [o] partially checked [ type <leader>x ]
" - [x] sub bullet
" - [ ] sub bullet
"
"
" Result:
" - [ ] checked
" - [ ] sub bullet
Expand Down Expand Up @@ -269,7 +299,7 @@ let g:bullets_set_mappings = 0
Add a leader key before default mappings:

```vim
let g:bullets_mapping_leader = '<M-b>'
let g:bullets_mapping_leader = '<M-b>'
" Set <M-b> to the leader before all default mappings:
" Example: renumbering becomes `<M-b>gN` instead of just `gN`
```
Expand Down
70 changes: 53 additions & 17 deletions plugin/bullets.vim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ if !exists('g:bullets_mapping_leader')
let g:bullets_mapping_leader = ''
end

" Extra key mappings in addition to default ones.
" If you don’t need default mappings set 'g:bullets_set_mappings' to '0'.
" N.B. 'g:bullets_mapping_leader' has no effect on these mappings.
"
" Example:
" let g:bullets_custom_mappings = [
" \ ['imap', '<cr>', '<Plug>(bullets-newline)'],
" \ ]
if !exists('g:bullets_custom_mappings')
let g:bullets_custom_mappings = []
endif

if !exists('g:bullets_delete_last_bullet_if_empty')
let g:bullets_delete_last_bullet_if_empty = 1
end
Expand Down Expand Up @@ -100,7 +112,7 @@ endif

" Parse Bullet Type ------------------------------------------- {{{
fun! s:parse_bullet(line_num, line_text)

let l:bullet = s:match_bullet_list_item(a:line_text)
" Must be a bullet to be a checkbox
let l:check = !empty(l:bullet) ? s:match_checkbox_bullet_item(a:line_text) : {}
Expand Down Expand Up @@ -966,14 +978,34 @@ command! -range=% BulletPromoteVisual call <SID>visual_change_bullet_level(1)
" --------------------------------------------------------- }}}

" Keyboard mappings --------------------------------------- {{{
fun! s:add_local_mapping(mapping_type, mapping, action)

" Automatic bullets
inoremap <silent> <Plug>(bullets-newline) <C-]><C-R>=<SID>insert_new_bullet()<cr>
nnoremap <silent> <Plug>(bullets-newline) :call <SID>insert_new_bullet()<cr>
" Renumber bullet list
vnoremap <silent> <Plug>(bullets-renumber) :RenumberSelection<cr>
nnoremap <silent> <Plug>(bullets-renumber) :RenumberList<cr>
" Toggle checkbox
nnoremap <silent> <Plug>(bullets-toggle-checkbox) :ToggleCheckbox<cr>
" Promote and Demote outline level
inoremap <silent> <Plug>(bullets-demote) <C-o>:BulletDemote<cr>
nnoremap <silent> <Plug>(bullets-demote) :BulletDemote<cr>
vnoremap <silent> <Plug>(bullets-demote) :BulletDemoteVisual<cr>
inoremap <silent> <Plug>(bullets-promote) <C-o>:BulletPromote<cr>
nnoremap <silent> <Plug>(bullets-promote) :BulletPromote<cr>
vnoremap <silent> <Plug>(bullets-promote) :BulletPromoteVisual<cr>
fun! s:add_local_mapping(with_leader, mapping_type, mapping, action)
let l:file_types = join(g:bullets_enabled_file_types, ',')
execute 'autocmd FileType ' .
\ l:file_types .
\ ' ' .
\ a:mapping_type .
\ ' <silent> <buffer> ' .
\ g:bullets_mapping_leader .
\ (a:with_leader ? g:bullets_mapping_leader : '') .
\ a:mapping .
\ ' ' .
\ a:action
Expand All @@ -982,7 +1014,7 @@ fun! s:add_local_mapping(mapping_type, mapping, action)
execute 'autocmd BufEnter * if bufname("") == "" | ' .
\ a:mapping_type .
\ ' <silent> <buffer> ' .
\ g:bullets_mapping_leader .
\ (a:with_leader ? g:bullets_mapping_leader : '') .
\ a:mapping .
\ ' ' .
\ a:action .
Expand All @@ -994,27 +1026,31 @@ augroup TextBulletsMappings
autocmd!

if g:bullets_set_mappings
" automatic bullets
call s:add_local_mapping('inoremap', '<cr>', '<C-]><C-R>=<SID>insert_new_bullet()<cr>')
call s:add_local_mapping('inoremap', '<C-cr>', '<cr>')
" Automatic bullets
call s:add_local_mapping(1, 'imap', '<cr>', '<Plug>(bullets-newline)')
call s:add_local_mapping(1, 'inoremap', '<C-cr>', '<cr>')

call s:add_local_mapping('nnoremap', 'o', ':call <SID>insert_new_bullet()<cr>')
call s:add_local_mapping(1, 'nmap', 'o', '<Plug>(bullets-newline)')

" Renumber bullet list
call s:add_local_mapping('vnoremap', 'gN', ':RenumberSelection<cr>')
call s:add_local_mapping('nnoremap', 'gN', ':RenumberList<cr>')
call s:add_local_mapping(1, 'vmap', 'gN', '<Plug>(bullets-renumber)')
call s:add_local_mapping(1, 'nmap', 'gN', '<Plug>(bullets-renumber)')

" Toggle checkbox
call s:add_local_mapping('nnoremap', '<leader>x', ':ToggleCheckbox<cr>')
call s:add_local_mapping(1, 'nmap', '<leader>x', '<Plug>(bullets-toggle-checkbox)')

" Promote and Demote outline level
call s:add_local_mapping('inoremap', '<C-t>', '<C-o>:BulletDemote<cr>')
call s:add_local_mapping('nnoremap', '>>', ':BulletDemote<cr>')
call s:add_local_mapping('inoremap', '<C-d>', '<C-o>:BulletPromote<cr>')
call s:add_local_mapping('nnoremap', '<<', ':BulletPromote<cr>')
call s:add_local_mapping('vnoremap', '>', ':BulletDemoteVisual<cr>')
call s:add_local_mapping('vnoremap', '<', ':BulletPromoteVisual<cr>')
call s:add_local_mapping(1, 'imap', '<C-t>', '<Plug>(bullets-demote)')
call s:add_local_mapping(1, 'nmap', '>>', '<Plug>(bullets-demote)')
call s:add_local_mapping(1, 'vmap', '>', '<Plug>(bullets-demote)')
call s:add_local_mapping(1, 'imap', '<C-d>', '<Plug>(bullets-promote)')
call s:add_local_mapping(1, 'nmap', '<<', '<Plug>(bullets-promote)')
call s:add_local_mapping(1, 'vmap', '<', '<Plug>(bullets-promote)')
end

for s:custom_key_mapping in g:bullets_custom_mappings
call call('<SID>add_local_mapping', [0] + s:custom_key_mapping)
endfor
augroup END
" --------------------------------------------------------- }}}

Expand Down

0 comments on commit 643a3c7

Please sign in to comment.