From f2d2201d808c1c263aa9b4d4fefd75afda383f08 Mon Sep 17 00:00:00 2001 From: Alex Leferry 2 Date: Sun, 17 Mar 2019 12:50:57 +0100 Subject: Add [e]dit and [b]uffer commands --- README.md | 6 +++--- rc/aliases/b | 1 + rc/aliases/e | 1 + rc/commands/buffer | 20 ++++++++++++++++++++ rc/commands/edit | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ rc/connect.kak | 21 +++++---------------- rc/connect.sh | 49 ------------------------------------------------- 7 files changed, 79 insertions(+), 68 deletions(-) create mode 120000 rc/aliases/b create mode 120000 rc/aliases/e create mode 100755 rc/commands/buffer create mode 100755 rc/commands/edit delete mode 100755 rc/connect.sh diff --git a/README.md b/README.md index 51006cf..e9cfa2a 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,12 @@ Create a new terminal with `:connect` and edit a file with say, a file explorer. The file will be opened in the client from where `:connect` was executed by using `EDITOR`. -You can also use the `connect` shell command available in `PATH`. +You can also use the `[e]dit` and `[b]uffer` shell commands available in `PATH`. ## Commands -- `connect-shell [program] [arguments]`: Run a shell with `connect` and `EDITOR` connected to the current client -- `connect-terminal [program] [arguments]`: Create a new terminal with `connect` and `EDITOR` connected to the current client +- `connect-shell [program] [arguments]`: Run a shell with `[e]dit`, `[b]uffer` and `EDITOR` connected to the current client +- `connect-terminal [program] [arguments]`: Create a new terminal with `[e]dit`, `[b]uffer` and `EDITOR` connected to the current client ## Aliases diff --git a/rc/aliases/b b/rc/aliases/b new file mode 120000 index 0000000..daf79f2 --- /dev/null +++ b/rc/aliases/b @@ -0,0 +1 @@ +../commands/buffer \ No newline at end of file diff --git a/rc/aliases/e b/rc/aliases/e new file mode 120000 index 0000000..1cb50b6 --- /dev/null +++ b/rc/aliases/e @@ -0,0 +1 @@ +../commands/edit \ No newline at end of file diff --git a/rc/commands/buffer b/rc/commands/buffer new file mode 100755 index 0000000..58c06dd --- /dev/null +++ b/rc/commands/buffer @@ -0,0 +1,20 @@ +#!/bin/sh + +main() { + [ $# = 0 ] && list || edit "$@" +} + +list() { + eval "set -- $KAKOUNE_BUFFERS" + printf '%s\n' "$@" +} + +edit() { + commands= + for buffer do + commands="buffer $buffer; $commands" + done + printf 'evaluate-commands -client "%s" "%s"' "$KAKOUNE_CLIENT" "$commands" | kak -p "$KAKOUNE_SESSION" +} + +main "$@" diff --git a/rc/commands/edit b/rc/commands/edit new file mode 100755 index 0000000..593c90b --- /dev/null +++ b/rc/commands/edit @@ -0,0 +1,49 @@ +#!/bin/sh + +# Simple and naive implementation of realpath for macOS +realpath() { + file=$1 + case "$file" in + /*) + printf '%s' "$file" + ;; + *) + printf '%s' "$PWD/${file#./}" + ;; + esac +} + +if test "$KAKOUNE_SESSION" -a "$KAKOUNE_CLIENT"; then + commands= + # Ignore options + if test "$1" = '--'; then + shift + fi + # Support to target line and column for the first file + # $ kak {file} +{line}:{column} + if test $# -ge 2; then + file=$(realpath "$1") + target=$2 + case "$target" in + +*:*) + line=${target#+}; line=${line%:*} + column=${target#*:} + commands="edit $file $line $column" + shift 2 + ;; + +*) + line=${target#+}; line=${line%:*} + commands="edit $file $line" + shift 2 + ;; + esac + fi + for file do + commands="edit $(realpath "$file"); $commands" + done + printf 'evaluate-commands -client "%s" "%s"' "$KAKOUNE_CLIENT" "$commands" | kak -p "$KAKOUNE_SESSION" +elif test "$KAKOUNE_SESSION"; then + kak -c "$KAKOUNE_SESSION" "$@" +else + kak "$@" +fi diff --git a/rc/connect.kak b/rc/connect.kak index 56af067..6904d2c 100644 --- a/rc/connect.kak +++ b/rc/connect.kak @@ -1,20 +1,11 @@ declare-option -docstring 'Shell program to be used as default entry-point' str connect_shell %sh(echo ${SHELL:-sh}) -declare-option -hidden str connect_editor %sh(printf '%s/connect.sh' "${kak_source%/*}") -declare-option -hidden str connect_cache %sh(printf '%s/kak/connect' "${XDG_CACHE_HOME:-~/.cache}") +declare-option -hidden str connect_source %sh(printf '%s' "${kak_source%/*}") -define-command -hidden connect-configure %{ - nop %sh{ - bin=$kak_opt_connect_cache/bin - mkdir -p "$bin" - cp "$kak_opt_connect_editor" "$bin/connect" - } -} - -define-command connect-shell -params 1.. -shell-completion -docstring 'Run a shell with connect and EDITOR connected to the current client' %{ - nop %sh(PATH=$kak_opt_connect_cache/bin:$PATH EDITOR=connect KAKOUNE_SESSION=$kak_session KAKOUNE_CLIENT=$kak_client $@) +define-command connect-shell -params 1.. -shell-completion -docstring 'Run a shell with [e]dit, [b]uffer and EDITOR connected to the current client' %{ + nop %sh(PATH=$kak_opt_connect_source/commands:$kak_opt_connect_source/aliases:$PATH EDITOR=edit KAKOUNE_SESSION=$kak_session KAKOUNE_CLIENT=$kak_client KAKOUNE_BUFFERS=$kak_buflist $@) } -define-command connect-terminal -params .. -shell-completion -docstring 'Create a new terminal with connect and EDITOR connected to the current client' %{ +define-command connect-terminal -params .. -shell-completion -docstring 'Create a new terminal with [e]dit, [b]uffer and EDITOR connected to the current client' %{ evaluate-commands %sh{ if test $# = 0; then printf 'connect-terminal- %%opt(connect_shell)' @@ -25,9 +16,7 @@ define-command connect-terminal -params .. -shell-completion -docstring 'Create } define-command -hidden connect-terminal- -params .. %{ - terminal sh -c "PATH=%opt(connect_cache)/bin:$PATH EDITOR=connect KAKOUNE_SESSION=%val(session) KAKOUNE_CLIENT=%val(client) %arg(@)" + terminal sh -c "PATH=%opt(connect_source)/commands:%opt(connect_source)/aliases:$PATH EDITOR=edit KAKOUNE_SESSION=%val(session) KAKOUNE_CLIENT=%val(client) KAKOUNE_BUFFERS=""%val(buflist)"" %arg(@)" } alias global connect connect-terminal - -connect-configure diff --git a/rc/connect.sh b/rc/connect.sh deleted file mode 100755 index 3ef0e9c..0000000 --- a/rc/connect.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/sh - -# Simple and naive implementation of realpath for macOS -realpath() { - file=$1 - case "$file" in - /*) - printf '%s' "$file" - ;; - *) - printf '%s' "$PWD/${file#./}" - ;; - esac -} - -if test "$KAKOUNE_SESSION" -a "$KAKOUNE_CLIENT"; then - commands= - # Ignore options - if test "$1" = '--'; then - shift - fi - # Support to target line and column for the first file - # $ kak {file} +{line}:{column} - if test $# -ge 2; then - file=$(realpath "$1") - target=$2 - case "$target" in - +*:*) - line=${target#+}; line=${line%:*} - column=${target#*:} - commands="edit $file $line $column" - shift 2 - ;; - +*) - line=${target#+}; line=${line%:*} - commands="edit $file $line" - shift 2 - ;; - esac - fi - for file in "$@"; do - commands="edit $(realpath "$file"); $commands" - done - printf 'evaluate-commands -client "%s" "%s"' "$KAKOUNE_CLIENT" "$commands" | kak -p "$KAKOUNE_SESSION" -elif test "$KAKOUNE_SESSION"; then - kak -c "$KAKOUNE_SESSION" "$@" -else - kak "$@" -fi -- cgit v1.2.3