~stacyharper/kakoune

5474a689ce829bfe39d5b158c82eff215752c3c0 — Willow Barraco 1 year, 3 months ago 9a35e02
Add :explore-files and hook it on :edit errors

This is a follow-up of :connect. We automatically determinate the best
appropriate command to use as file explorer. We hooks this on :edit
errors to open the explorer when used with directory, or no files at
all.
2 files changed, 71 insertions(+), 0 deletions(-)

M rc/windowing/connect.kak
A rc/windowing/explore-files.kak
M rc/windowing/connect.kak => rc/windowing/connect.kak +4 -0
@@ 1,3 1,5 @@
provide-module connect %{

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


@@ 17,3 19,5 @@ define-command -override run -params 1.. -shell-completion -docstring 'Run a pro
    nohup "$@" < /dev/null > /dev/null 2>&1 &
  }
}

}

A rc/windowing/explore-files.kak => rc/windowing/explore-files.kak +67 -0
@@ 0,0 1,67 @@

provide-module explore-files %{

require-module connect

# explorefilescmd should be set such as the next argument is file or
# directory to open
declare-option -docstring %{command run to spawn a file browser} \
    str explorefilescmd %sh{
    for explorefilescmd in 'terminal nnn' \
                   'terminal ranger'; do
        cmd=${explorefilescmd##* }
        if command -v $cmd >/dev/null 2>&1; then
            printf %s\\n "$explorefilescmd"
            exit
        fi
    done
}

define-command -override -params ..1 explore-files %{
    evaluate-commands %sh{
        if [ -z "$kak_opt_explorefilescmd" ]; then
            echo "fail 'explorefilescmd option is not set'"
            exit
        fi
        printf 'connect %s "%s"\n' "$kak_opt_explorefilescmd" "$1"
    }
}
complete-command explore-files file

hook global RuntimeError "\d+:\d+: '(edit|e)':? wrong argument count" %{
  evaluate-commands -save-regs 'd' %{
    set-register d %sh(dirname "$kak_buffile")
    explore-files %reg{d}
    echo "Openned file browser !"
  }
}

hook global RuntimeError "\d+:\d+: '(?:edit|e)':? (.+): is a directory" %{
  explore-files %val{hook_param_capture_1}
  echo "Openned file browser !"
}

hook global RuntimeError "unable to find file '(.+)'" %{
  explore-files %val{hook_param_capture_1}
  echo "Openned file browser !"
}

hook global KakBegin .* %{
  hook -once global ClientCreate .* %{
    try %{
      evaluate-commands -buffer '*debug*' -save-regs '/' %{
        set-register / "^error while opening file '(.+?)':?\n[^\n]+: is a directory$"
        execute-keys '%1s<ret>'
        evaluate-commands -draft -itersel -save-regs 'd' %{
          set-register d %reg{.}
          evaluate-commands -client %val{hook_param} %{
            explore-files %reg{d}
            echo "Openned file browser !"
          }
        }
      }
    }
  }
}

}