Context.vim (I want this!)

I wrote a very simple proof of concept bash script that scans up through the given lines on stdin and prints all lines where the indentation decreases with respect to the previous printed line.

The output of this program could be shown in a new window.

#!/bin/bash

function get_indent_level {
    printf "%s" "$1" | grep -Po '^\s+' | tr -d '\n' | wc -c
}

prev_indent=0
IFS=''
tac /dev/stdin | while read line; do
    indent_level=$(get_indent_level "$line")
    if [ $prev_indent -eq 0 ] || [ $indent_level -lt $prev_indent ]; then
        prev_indent=$indent_level
        printf "%s\n" "$line"
    fi
    if [ $indent_level -eq 0 ]; then
        exit
    fi
done | tac

From kakoune you could do something like

exec -draft Gk<a-x><a-|>context.sh<ret>