From 908e0c1f8c5e395bde75809310de493451e5319c Mon Sep 17 00:00:00 2001 From: Nicholas Ochiel Date: Fri, 17 Nov 2017 22:40:42 +0300 Subject: [PATCH 1/3] * Crude implementation of focus: use the current region to switch to the desired window. - The proper way to do this would be to switch focus to the target client's region but GNU Screen offers no obvious way to do that. - Remove screen region after client is closed. - Assumed: Kak server runs in screen. --- rc/base/screen.kak | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/rc/base/screen.kak b/rc/base/screen.kak index 157d38d09..f474fb8fa 100644 --- a/rc/base/screen.kak +++ b/rc/base/screen.kak @@ -1,9 +1,9 @@ # http://gnu.org/software/screen/ # ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ -hook global KakBegin .* %{ +hook -group GNUscreen global KakBegin .* %{ %sh{ - [ -z "${kak_client_env_STY}" ] && exit + [ -z "${STY}" ] && exit echo " alias global focus screen-focus alias global new screen-new-vertical @@ -15,14 +15,24 @@ hook global KakBegin .* %{ define-command screen-new-vertical -params .. -command-completion -docstring "Create a new vertical region" %{ %sh{ tty="$(ps -o tty ${kak_client_pid} | tail -n 1)" - screen -X eval 'split -h' 'focus down' "screen kak -c \"${kak_session}\" -e \"$*\"" < "/dev/$tty" + screen -X eval \ + 'split -h' \ + 'focus down' \ + "screen sh -c 'kak -c \"${kak_session}\" -e \"$*\" ; + screen -X remove'" \ + < "/dev/$tty" } } define-command screen-new-horizontal -params .. -command-completion -docstring "Create a new horizontal region" %{ %sh{ tty="$(ps -o tty ${kak_client_pid} | tail -n 1)" - screen -X eval 'split -v' 'focus right' "screen kak -c \"${kak_session}\" -e \"$*\"" < "/dev/$tty" + screen -X eval \ + 'split -v' \ + 'focus right' \ + "screen sh -c 'kak -c \"${kak_session}\" -e \"$*\" ; + screen -X remove'" \ + < "/dev/$tty" } } @@ -39,11 +49,10 @@ If no client is passed then the current one is used} \ screen-focus %{ %sh{ if [ $# -eq 1 ]; then printf %s\\n " - evaluate-commands -client '$1' %{ %sh{ - screen -X focus - }}" + evaluate-commands -client '$1' focus + " elif [ -n "${kak_client_env_STY}" ]; then tty="$(ps -o tty ${kak_client_pid} | tail -n 1)" - screen -X select "$kak_client_env_WINDOW" < "/dev/$tty" + screen -X select "${kak_client_env_WINDOW}" < "/dev/$tty" fi } } From 494d046fb2de078fc3715e806553556dc9a897ae Mon Sep 17 00:00:00 2001 From: Nicholas Ochiel Date: Fri, 17 Nov 2017 22:40:42 +0300 Subject: [PATCH 2/3] * Integrate ranger with GNU Screen. - Ensure ranger opens with the currently focused client tty if multiple screen clients are connected to the same session. - Ensure args are passed to ranger correctly. - Ensure command-line args are passed to ranger correctly. (Only strict long format works e.g. `ranger --cmd="echo foo"`) - Change the current directory because `screen -X` uses screen's cwd not kakoune's. - Use heredoc to make escaping and quoting easier to debug and slightly less unpredictable. - Use ranger's `--choosefiles` option instead of mapping keys and having to do multiple levels of escaping and quoting of args esp. command-line args which were unusable in `screen -X eval`. - Remove screen region when user quits ranger. --- rc/extra/ranger.kak | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rc/extra/ranger.kak b/rc/extra/ranger.kak index 4ba2a64fd..48eb0c998 100644 --- a/rc/extra/ranger.kak +++ b/rc/extra/ranger.kak @@ -23,6 +23,29 @@ All the optional arguments are forwarded to the ranger utility} \ kak -p $kak_session; \ tmux select-pane -t $kak_client_env_TMUX_PANE'.format(file=fm.thisfile.path)) \ if fm.thisfile.is_file else fm.execute_console('move right=1')" + + elif [ -n "${STY}" ]; then + + script="/tmp/kak-ranger-${kak_client}-${kak_session}.sh" + selections="/tmp/kak-ranger-${kak_client}-${kak_session}.txt" + cat > "$script" << EOF +#! /usr/bin/env sh +cd "$PWD" +ranger --choosefiles="$selections" $@ +while read -r f; do + printf %s "evaluate-commands -client '${kak_client}' edit '\"\$f\"'" | kak -p '${kak_session}' +done < "$selections" +screen -X remove +rm -f "$selections" "$script" +EOF + + tty="$(ps -o tty ${kak_client_pid} | tail -n 1)" + screen -X eval \ + 'split -h' \ + 'focus down' \ + "screen sh '$script'" \ + < "/dev/$tty" + elif [ -n "$WINDOWID" ]; then setsid $kak_opt_termcmd " \ ranger $@ --cmd "'"'" \ From e5742fd6b1bb84f0da1625c2300b25d369f4ba89 Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Wed, 29 Nov 2017 20:17:45 +0300 Subject: [PATCH 3/3] - Fix: proper escaping of kak_{session, client} --- rc/extra/ranger.kak | 58 ++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/rc/extra/ranger.kak b/rc/extra/ranger.kak index 48eb0c998..76e014533 100644 --- a/rc/extra/ranger.kak +++ b/rc/extra/ranger.kak @@ -4,31 +4,31 @@ define-command ranger-open-on-edit-directory \ -docstring 'Start the ranger file system explorer when trying to edit a directory' %{ hook global RuntimeError "\d+:\d+: '\w+' (.*): is a directory" %{ %sh{ - directory=$kak_hook_param_capture_1 - echo ranger $directory + directory=$kak_hook_param_capture_1 + echo ranger $directory }} } define-command \ - -params .. -file-completion \ - -docstring %{ranger []: open the file system explorer to select buffers to open -All the optional arguments are forwarded to the ranger utility} \ - ranger %{ %sh{ - if [ -n "$TMUX" ]; then - tmux split-window -h \ - ranger $@ --cmd " \ - map eval \ - fm.execute_console('shell \ - echo evaluate-commands -client $kak_client edit {file} | \ - kak -p $kak_session; \ - tmux select-pane -t $kak_client_env_TMUX_PANE'.format(file=fm.thisfile.path)) \ - if fm.thisfile.is_file else fm.execute_console('move right=1')" + -params .. -file-completion \ + -docstring %{ranger []: open the file system explorer to select buffers to open + All the optional arguments are forwarded to the ranger utility} \ + ranger %{ %sh{ + if [ -n "${TMUX}" ]; then + tmux split-window -h \ + ranger $@ --cmd " \ + map eval \ + fm.execute_console('shell \ + echo evaluate-commands -client ' + ranger.ext.shell_escape.shell_escape('$kak_client') + ' edit {file} | \ + kak -p '.format(file=fm.thisfile.path) + ranger.ext.shell_escape.shell_escape('$kak_session') + '; \ + tmux select-pane -t $kak_client_env_TMUX_PANE') \ + if fm.thisfile.is_file else fm.execute_console('move right=1')" - elif [ -n "${STY}" ]; then + elif [ -n "${STY}" ]; then - script="/tmp/kak-ranger-${kak_client}-${kak_session}.sh" - selections="/tmp/kak-ranger-${kak_client}-${kak_session}.txt" - cat > "$script" << EOF + script="/tmp/kak-ranger-${kak_client}-${kak_session}.sh" + selections="/tmp/kak-ranger-${kak_client}-${kak_session}.txt" + cat > "$script" << EOF #! /usr/bin/env sh cd "$PWD" ranger --choosefiles="$selections" $@ @@ -46,14 +46,14 @@ EOF "screen sh '$script'" \ < "/dev/$tty" - elif [ -n "$WINDOWID" ]; then - setsid $kak_opt_termcmd " \ - ranger $@ --cmd "'"'" \ - map eval \ - fm.execute_console('shell \ - echo evaluate-commands -client $kak_client edit {file} | \ - kak -p $kak_session; \ - xdotool windowactivate $kak_client_env_WINDOWID'.format(file=fm.thisfile.path)) \ - if fm.thisfile.is_file else fm.execute_console('move right=1')"'"' < /dev/null > /dev/null 2>&1 & - fi + elif [ -n "$WINDOWID" ]; then + setsid $kak_opt_termcmd " \ + ranger $@ --cmd "'"'" \ + map eval \ + fm.execute_console('shell \ + echo evaluate-commands -client ' + ranger.ext.shell_escape.shell_escape('$kak_client') + ' edit {file} | \ + kak -p '.format(file=fm.thisfile.path) + ranger.ext.shell_escape.shell_escape('$kak_session') + '; \ + xdotool windowactivate $kak_client_env_WINDOWID') \ + if fm.thisfile.is_file else fm.execute_console('move right=1')"'"' < /dev/null > /dev/null 2>&1 & + fi }}