├── UNLICENSE ├── README.md └── buffer-switcher.kak /UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kakoune-buffer-switcher 2 | 3 | **Warning**: For kakoune version 2021.11.08 and earlier, the master branch of this plugin will not work. The latest commit compatible with these versions is 216bf7e70a3944ddd684b278a43c1dd5759a5b85. 4 | 5 | [Kakoune](http://kakoune.org) plugin to navigate between open buffers, and manipulate the buffers-list. 6 | 7 | ## Setup 8 | 9 | Add `buffer-switcher.kak` to your `autoload` directory: `~/.config/kak/autoload/`, or source it manually. 10 | 11 | ## Usage 12 | 13 | This plugin can be used with the `buffer-switcher` command. When it is invoked, a special-purpose `*buffer-switcher*` buffer is created. It lists all currently opened buffers (except for itself and `*debug*`), one per line. 14 | Pressing `` will change the current buffer to the one under the cursor, and close the switcher. In addition, if a line was removed its buffer is removed as well (unless it has unsaved changes). The remaining buffers are re-ordered according to the order of the lines. 15 | Pressing `` will close the switcher, without applying any of the changes. 16 | 17 | Note: the experience is likely suboptimal when using multiple clients, suggestions for improvements on that front are welcome. 18 | 19 | ## Customization 20 | 21 | The face `BufferSwitcherCurrent` is used to indicate the buffer that was previously active. 22 | 23 | ## License 24 | 25 | [Unlicense](http://unlicense.org) 26 | -------------------------------------------------------------------------------- /buffer-switcher.kak: -------------------------------------------------------------------------------- 1 | # we assume that a buffer name will never contain newlines (not exactly true, but who cares) 2 | 3 | face global BufferSwitcherCurrent black,green 4 | 5 | define-command buffer-switcher %{ 6 | try %{ 7 | b *buffer-switcher* 8 | } catch %{ 9 | eval -save-regs '"/' %{ 10 | reg / "^\Q%val{bufname}\E$" 11 | reg dquote %val{buflist} 12 | 13 | edit -scratch *buffer-switcher* 14 | exec ')i' 15 | exec '%' 16 | # remove *debug* buffer 17 | exec -draft '^\*debug\*$d' 18 | try %{ 19 | # select current one 20 | exec '' 21 | # also highlight it in green 22 | addhl buffer/ regex "%reg{/}" 0:BufferSwitcherCurrent 23 | } catch %{ 24 | exec gg 25 | } 26 | map buffer normal ': buffer-switcher-switch' 27 | map buffer normal ': delete-buffer *buffer-switcher*' 28 | hook global WinDisplay -once .* %{ try %{ delete-buffer *buffer-switcher* } } 29 | } 30 | } 31 | } 32 | 33 | define-command -hidden buffer-switcher-switch %{ 34 | try buffer-switcher-delete-buffers 35 | try buffer-switcher-sort-buffers 36 | exec ',;xH' 37 | buffer %val{selection} 38 | try %{ delete-buffer *buffer-switcher* } 39 | } 40 | 41 | # delete all buffers whose lines were removed 42 | define-command -hidden buffer-switcher-delete-buffers %{ 43 | # print buflist, and all lines 44 | # everything that appears only once gets removed 45 | eval -buffer *buffer-switcher* %{ 46 | exec '%H' 47 | eval %sh{ 48 | { 49 | eval set -- "$kak_quoted_buflist" 50 | for buf do 51 | # ignore self and debug 52 | if [ "$buf" = '*buffer-switcher*' ]; then 53 | : 54 | elif [ "$buf" = '*debug*' ]; then 55 | : 56 | else 57 | printf '%s\n' "$buf" 58 | fi 59 | done 60 | eval set -- "$kak_quoted_selections" 61 | for buf do 62 | printf '%s\n' "$buf" 63 | done 64 | } | awk " 65 | // { 66 | line=\$0 67 | if (line in line_count) 68 | line_count[line] = line_count[line] + 1; 69 | else 70 | line_count[line] = 1; 71 | } 72 | END { 73 | for (line in line_count) 74 | if (line_count[line] == 1) 75 | { 76 | gsub(\"'\", \"''''\", line); 77 | print(\"try 'delete-buffer ''\" line \"'' '\"); 78 | } 79 | }" 80 | } 81 | } 82 | } 83 | 84 | # re-arrange the buflist according to the order in the *buffer-switcher* 85 | define-command -hidden buffer-switcher-sort-buffers %{ 86 | eval -buffer *buffer-switcher* %{ 87 | exec '%H' 88 | arrange-buffers %val{selections} 89 | } 90 | } 91 | --------------------------------------------------------------------------------