~stacyharper/kakoune

9a35e028d37bdab4d49a2f8499d554ed24b52dd1 — Willow Barraco 1 year, 3 months ago 7d218d8
Add kak-connect to connect external programs as file browsers

Kakoune doesn't embark a file browser. It has been designed as client-server,
and this make it easy to connect to external programs.

The problem is that there is no easy way to do so out of the box. The users are
expected to glue this themselves.

This try to help users to connect external softwares to Kakoune.

:connect is a new command to be used with :terminal or :run to start a new
connected terminal, or graphical application.

Those connected software have the corresponding KAKOUNE_SESSION and
KAKOUNE_CLIENT environment variables. It also set kak-connect as EDITOR.

kak-connect is a simple POSIX shell script that send commands to the connected
Kakoune session, to open the files.

The basic usage of all could be:

:connect terminal nnn
:connect terminal ranger

Which open nnn or ranger in another terminal (external, or in a tmux pane), and
to open the files in the Kakoune client.

We can itterate over this:

- Add hooks to open a file browser when the user open a directory, or no file
with :edit.
- Make nnn, or other programs detected and configured by default, as we do in
windowing terminals. And wrap it with a :file-browser command.
3 files changed, 48 insertions(+), 0 deletions(-)

M Makefile
A rc/windowing/connect.kak
A src/kak-connect
M Makefile => Makefile +2 -0
@@ 183,6 183,8 @@ installdirs-debug-yes: installdirs-debug-no
install: src/kak installdirs install-debug-$(debug) install-gzip-man-$(gzip_man)
	cp src/kak$(suffix) $(bindir)
	chmod 0755 $(bindir)/kak
	cp src/kak-connect$(suffix) $(bindir)
	chmod 0755 $(bindir)/kak-connect

	ln -sf ../../bin/kak $(libexecdir)/kak


A rc/windowing/connect.kak => rc/windowing/connect.kak +19 -0
@@ 0,0 1,19 @@
define-command -override connect -params 1.. -command-completion -docstring 'Run a command as <command> sh -c {connect} -- [arguments].  Example: connect terminal sh' %{
  %arg{1} sh -c %{
    export KAKOUNE_SESSION=$1
    export KAKOUNE_CLIENT=$2
    shift 3

    export EDITOR='kak-connect'

    [ $# = 0 ] && set -- "$SHELL"

    "$@"
  } -- %val{session} %val{client} %arg{@}
}

define-command -override run -params 1.. -shell-completion -docstring 'Run a program in a new session' %{
  nop %sh{
    nohup "$@" < /dev/null > /dev/null 2>&1 &
  }
}

A src/kak-connect => src/kak-connect +27 -0
@@ 0,0 1,27 @@
#!/bin/sh

if [ -z "$*" ] ; then
	cat <<EOF >&2
Usage: $(basename "$0") <file> ...

Edit the files in a connected Kakoune session.
EOF
	exit 1
fi

if [ -z "$KAKOUNE_CLIENT" ] || [ -z "$KAKOUNE_SESSION" ]; then
	cat <<EOF >&2
No connected Kakoune session.
EOF
	exit 1
fi

while [ -n "$1" ]; do
	realpath "$1" | \
		tr '\n' '\0' | \
		sed 's| |\\ |g' | \
		xargs -0 printf 'evaluate-commands -try-client %s edit "%s"\n' "$KAKOUNE_CLIENT" | \
		kak -p "$KAKOUNE_SESSION"
	shift
done