# Sel-editor: editable, live selection-summary buffer

**URL:** <https://discuss.kakoune.com/t/sel-editor-editable-live-selection-summary-buffer/1877>\
**Category:** Plugins\
**Tags:** scripting, language\
**Created:** [September 13, 2021, 11:13am UTC](https://discuss.kakoune.com/t/sel-editor-editable-live-selection-summary-buffer/1877 "2021-09-13T11:13:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![almr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/almr/32/934_2.png) [@almr](https://discuss.kakoune.com/u/almr)\
**Post date:** [September 13, 2021, 11:13am UTC](https://discuss.kakoune.com/t/sel-editor-editable-live-selection-summary-buffer/1877/1 "2021-09-13T11:13:43Z")

</div>

This plugin, [suggested](https://discuss.kakoune.com/t/displaying-all-selections-in-a-single-window/1872) by @Guest0 and @Screwtapello , renders a summary of the current window’s multiple selections (ranges + contents) in a `*selections*` buffer.

![sel-editor-live-2021-09-13](https://discuss.kakoune.com/uploads/default/original/1X/456d78fb5734236a6b31a34a06a5e76ce65d3160.gif)

It can act as a `toolsclient`, and live-update `*selections*` based on the.. selections. You can edit ranges (but not contents), then use `sel-editor-buf2kak` to change the actual selections. E.g. you can delete ranges, modify them, or even enter ranges manually.

I’d like to implement selection-contents editing in the summary buffer, but I need to learn more…

## Installation / usage

Instructions (brief) at the project page

## Notes

I’ve managed to do this in pure kakscript, apparently transcending some language limitations

- looping: using `999q` (macro with count) with a macro calling `: exec iter`. Alternatively, recursion-based looping also works, but hits a 100-level nesting limit (apparently, no tail-call optimization).
- apply lambda to arguments: `def -override lambda-tmp %reg{a}; lambda-tmp args`
- stopping loops: with exceptions
- processing lists / shifting arguments: use `str-list` options, and `set global work %arg{@}; set -add global work %arg{1}`
- checking if arguments exhausted / list empty: `def nop-0_0 -params 0..0 %{}` (call it on any `str-list` option, or on `%arg{@}`; it throws unless there are zero elements in the list). Any such param-count check is possible, either with predefined or on-the-fly checkers.

I’ve been able to implement a `foreach`, zip two lists, concatenate / prefix / suffix a list of strings, and it seems like a lot more is possible…

> **[kstr0k / sel-editor.kak · GitLab](https://gitlab.com/kstr0k/sel-editor.kak/)**
>
> Display selection ranges and contents in Kakoune

---

<div class="post-metadata">

**Author:** ![danr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/danr/32/700_2.png) [@danr](https://discuss.kakoune.com/u/danr)\
**Post date:** [September 15, 2021, 7:34am UTC](https://discuss.kakoune.com/t/sel-editor-editable-live-selection-summary-buffer/1877/2 "2021-09-15T07:34:09Z")

</div>

Looks incredible! But what am I doing wrong? I get:

````auto
~/code/kakconf/sel-editor.kak:16:1: '__k9s0ke_tmp_loader': 3:1: 'eval': no selections in context```
````

---

<div class="post-metadata">

**Author:** ![almr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/almr/32/934_2.png) [@almr](https://discuss.kakoune.com/u/almr)\
**Post date:** [September 15, 2021, 7:43am UTC](https://discuss.kakoune.com/t/sel-editor-editable-live-selection-summary-buffer/1877/3 "2021-09-15T07:43:01Z")

</div>

It’s probably [this](https://discuss.kakoune.com/t/no-in-context-eval-draft-edit-scratch-require-module/1879); but the currently published version should work. Which sel-editor revision are you using (`git log -1 --pretty=%h`)? Is this with the current `kakoune` release, or master?

I’ll probably switch back to a `%sh{}` loader since it seems there are problems.

---

<div class="post-metadata">

**Author:** ![almr](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/almr/32/934_2.png) [@almr](https://discuss.kakoune.com/u/almr)\
**Post date:** [September 16, 2021, 9:08pm UTC](https://discuss.kakoune.com/t/sel-editor-editable-live-selection-summary-buffer/1877/4 "2021-09-16T21:08:39Z")

</div>

I’ll be sure to test in a clean `$HOME` mock-up from now on… Anyway, the bugs are fixed (thanks @danr for alerting); the latest `sel-editor.kak` (see the REAMDE / homepage as well) can…

![sel-editor-2021-09-16 23-34](https://discuss.kakoune.com/uploads/default/original/1X/5aed37d4eeac8bfbb6d5608fd7cb2626c11d27c9.gif)

## Change selection contents

Edit the contents (between `| .. |`) of a selection, press `<ret>w`, confirm, and the text in the original buffer will be updated.

## Easily select / unselect / remove blocks

There’s a custom user mode for `*selections*`, triggered by pressing `<ret>` — the user-mode menu lists all the mappings. E.g. add blocks with `<ret>+`, instead of combining them with `<a-z>`, `Z` as in normal mode, delete blocks with `<ret>d` etc. Some of them have normal-mode correspondents (`>`, `<`, `=`, `<a-ret>` = `).

## Work on specific selections

Press `<ret>=` and only the selected blocks will be applied to the original buffer. Press `>` and all blocks in `*selections*` will be applied.

## Undo

All changes that happen in `*selections*` go into the buffer’s undo history. You can go back in time. However, if the original buffer contents changes, the selections will have incompatible timestamps and some changes won’t be restore-able (via `>` or `=`).

## Highlight

… as can be seen in the preview above

## Auto-infer clients

You can just call `:+=ssl [...]` (or `sel-editor-live-new [...]`); this will call `new` and set up a live `*selections*`.

If you _do_ have a `toolsclient` setup, sel-editor will respect it. If you just call `sel-editor-live-enable`, it will guess which client is the `toolsclient`.

## Notes / TODO

I’ve been bitten by [this](https://discuss.kakoune.com/t/restoring-selections-after-accidental-clear/1366) customization, which pollutes register `z`. I’m going to include selection history (N-levels deep) in `sel-editor`. Whenever there’s a “significant” change in selections (i.e. excluding short, single selections), a NormalIdle hook (if enabled) will add an entry in history.

There’s a bug that prevents selection contents from being made empty. It doesn’t make sense, but I guess new users playing with it could easily trigger it. Just delete unneeded blocks with `<ret>d`.
