Port most kak scripts to be POSIX shell compliant

This commit is contained in:
Maxime Coste 2014-03-06 03:35:38 +00:00
parent 8e0b5d67aa
commit f733007a7b
8 changed files with 38 additions and 33 deletions

View File

@ -16,7 +16,7 @@ def clang-complete %{
( (
pos=-:${kak_cursor_line}:${kak_cursor_column} pos=-:${kak_cursor_line}:${kak_cursor_column}
cd $(dirname ${kak_buffile}) cd $(dirname ${kak_buffile})
output=$(clang++ -x c++ -fsyntax-only ${kak_opt_clang_options} -Xclang -code-completion-at=${pos} - < ${kak_opt_clang_filename} |& tee /tmp/kak-clang-out | output=$(clang++ -x c++ -fsyntax-only ${kak_opt_clang_options} -Xclang -code-completion-at=${pos} - < ${kak_opt_clang_filename} 2>&1 | tee /tmp/kak-clang-out |
grep -E "^COMPLETION:[^:]+:" | perl -pe 's/^COMPLETION:[^:]+: +//; s/:/\\:/g; s/\[#.*?#\]|<#.*?#>(, *|\))?|\{#.*?#\}\)?//g') grep -E "^COMPLETION:[^:]+:" | perl -pe 's/^COMPLETION:[^:]+: +//; s/:/\\:/g; s/\[#.*?#\]|<#.*?#>(, *|\))?|\{#.*?#\}\)?//g')
rm -r $(dirname ${kak_opt_clang_filename}) rm -r $(dirname ${kak_opt_clang_filename})
completions="${kak_cursor_line}.${kak_cursor_column}@${kak_timestamp}" completions="${kak_cursor_line}.${kak_cursor_column}@${kak_timestamp}"
@ -24,7 +24,7 @@ def clang-complete %{
completions="${completions}:${cmp}" completions="${completions}:${cmp}"
done done
echo "eval -client $kak_client %[ echo completed; set buffer completions '${completions}' ]" | kak -p ${kak_session} echo "eval -client $kak_client %[ echo completed; set buffer completions '${completions}' ]" | kak -p ${kak_session}
) >& /dev/null < /dev/null & ) > /dev/null 2>&1 < /dev/null &
} }
} }

View File

@ -1,5 +1,5 @@
decl str termcmd %sh{ decl str termcmd %sh{
if [[ -n "$TMUX" ]]; then if [ -n "$TMUX" ]; then
echo "'tmux split-window -h'" echo "'tmux split-window -h'"
else else
echo "'urxvt -e sh -c'" echo "'urxvt -e sh -c'"
@ -8,6 +8,6 @@ decl str termcmd %sh{
def new -docstring 'create a new kak client for current session' \ def new -docstring 'create a new kak client for current session' \
-shell-params %{ nop %sh{ -shell-params %{ nop %sh{
if (( $# != 0 )); then kakoune_params="-e '$@'"; fi if [ $# -ne 0 ]; then kakoune_params="-e '$@'"; fi
setsid ${kak_opt_termcmd} "kak -c ${kak_session} ${kakoune_params}" < /dev/null >& /dev/null & setsid ${kak_opt_termcmd} "kak -c ${kak_session} ${kakoune_params}" < /dev/null > /dev/null 2>&1 &
}} }}

View File

@ -75,26 +75,32 @@ hook global BufNew .*\.(h|hh|hpp|hxx|H) _cpp_insert_include_guards
decl str-list alt_dirs ".;.." decl str-list alt_dirs ".;.."
def alt -docstring "Jump to the alternate file (header/implementation)" %{ %sh{ def alt -docstring "Jump to the alternate file (header/implementation)" %{ %sh{
shopt -s extglob alt_dirs=$(echo ${kak_opt_alt_dirs} | sed -e 's/;/ /g')
alt_dirs=${kak_opt_alt_dirs//;/ }
file=$(basename ${kak_buffile}) file=$(basename ${kak_buffile})
dir=$(dirname ${kak_buffile}) dir=$(dirname ${kak_buffile})
case ${file} in case ${file} in
*.c|*.cc|*.cpp|*.cxx|*.C) *.c|*.cc|*.cpp|*.cxx|*.C)
for alt_dir in ${alt_dirs}; do for alt_dir in ${alt_dirs}; do
altname=$(ls -1 "${dir}/${alt_dir}/${file%.*}".@(h|hh|hpp|hxx|H) 2> /dev/null | head -n 1) for ext in h hh hpp hxx H; do
[[ -e ${altname} ]] && break altname="${dir}/${alt_dir}/${file%.*}.${ext}"
[ -f ${altname} ] && break
done
[ -f ${altname} ] && break
done done
;; ;;
*.h|*.hh|*.hpp|*.hxx|*.H) *.h|*.hh|*.hpp|*.hxx|*.H)
for alt_dir in ${alt_dirs}; do for alt_dir in ${alt_dirs}; do
altname=$(ls -1 "${dir}/${alt_dir}/${file%.*}".@(c|cc|cpp|cxx|C) 2> /dev/null | head -n 1) for ext in c cc cpp cxx C; do
[[ -e ${altname} ]] && break altname="${dir}/${alt_dir}/${file%.*}.${ext}"
[ -f ${altname} ] && break
done
[ -f ${altname} ] && break
done done
;; ;;
esac esac
if [[ -e ${altname} ]]; then echo debug ${altname}
if [ -f ${altname} ]; then
echo edit "'${altname}'" echo edit "'${altname}'"
else else
echo echo "'alternative file not found'" echo echo "'alternative file not found'"

View File

@ -8,20 +8,20 @@ def -shell-params \
-docstring 'jump to tag definition' \ -docstring 'jump to tag definition' \
tag \ tag \
%{ %sh{ %{ %sh{
if [[ -z "$1" ]]; then tagname=${kak_selection}; else tagname=$1; fi if [ -z "$1" ]; then tagname=${kak_selection}; else tagname="$1"; fi
matches=$(readtags ${tagname}) matches=$(readtags ${tagname})
if [[ -z "${matches}" ]]; then if [ -z "${matches}" ]; then
echo "echo tag not found ${tagname}" echo "echo tag not found ${tagname}"
else else
menuparam=$(readtags ${tagname} | perl -i -ne ' menuparam=$(readtags ${tagname} | perl -i -ne '
/([^\t]+)\t([^\t]+)\t\/\^([^{}]*).*\$\// and print "%{$2 [$3]} %{try %{ edit %{$2}; exec %{/\\Q$3<ret>vc} } catch %{ echo %{unable to find tag} } } "; /([^\t]+)\t([^\t]+)\t\/\^([^{}]*).*\$\// and print "%{$2 [$3]} %{try %{ edit %{$2}; exec %{/\\Q$3<ret>vc} } catch %{ echo %{unable to find tag} } } ";
/([^\t]+)\t([^\t]+)\t(\d+)/ and print "%{$2:$3} %{edit %{$2} %{$3}}"; /([^\t]+)\t([^\t]+)\t(\d+)/ and print "%{$2:$3} %{edit %{$2} %{$3}}";
') ' | sed -e 's/\n/ /g')
if [[ -z "${menuparam}" ]]; then if [ -z "${menuparam}" ]; then
echo "echo no such tag ${tagname}"; echo "echo no such tag ${tagname}";
else else
echo "menu -auto-single ${menuparam//$'\n'/ }"; echo "menu -auto-single ${menuparam}";
fi fi
fi fi
}} }}
@ -29,12 +29,10 @@ def -shell-params \
def tag-complete %{ eval -draft %{ def tag-complete %{ eval -draft %{
exec <space>hb<a-k>^\w+$<ret> exec <space>hb<a-k>^\w+$<ret>
%sh{ ( %sh{ (
compl=$(readtags -p "$kak_selection" | cut -f 1 | sort | uniq) compl=$(readtags -p "$kak_selection" | cut -f 1 | sort | uniq | sed -e 's/:/\\:/g' | sed -e 's/\n/:/g' )
compl=${compl//:/\\:}
compl=${compl//$'\n'/:}
compl="${kak_cursor_line}.${kak_cursor_column}+${#kak_selection}@${kak_timestamp}:${compl}" compl="${kak_cursor_line}.${kak_cursor_column}+${#kak_selection}@${kak_timestamp}:${compl}"
echo "set buffer=$kak_bufname completions '${compl}'" | kak -p ${kak_session} echo "set buffer=$kak_bufname completions '${compl}'" | kak -p ${kak_session}
) >& /dev/null < /dev/null & } ) > /dev/null 2>&1 < /dev/null & }
}} }}
def funcinfo %{ def funcinfo %{
@ -42,7 +40,7 @@ def funcinfo %{
exec [(<space>B<a-k>[a-zA-Z_]+\(<ret> exec [(<space>B<a-k>[a-zA-Z_]+\(<ret>
%sh{ %sh{
sigs=$(readtags -e ${kak_selection%(} | grep kind:f | sed -re 's/^(\S+).*(class|struct|namespace):(\S+).*signature:(.*)$/\4 [\3::\1]/') sigs=$(readtags -e ${kak_selection%(} | grep kind:f | sed -re 's/^(\S+).*(class|struct|namespace):(\S+).*signature:(.*)$/\4 [\3::\1]/')
if [[ -n "$sigs" ]]; then if [ -n "$sigs" ]; then
echo "eval -client ${kak_client} %{info -anchor right '$sigs'}" echo "eval -client ${kak_client} %{info -anchor right '$sigs'}"
fi fi
} }
@ -70,5 +68,5 @@ def gentags -docstring 'generate tag file asynchronously' %{
msg="tags generation failed" msg="tags generation failed"
fi fi
echo "eval -client $kak_client echo -color Information '${msg}'" | kak -p ${kak_session} echo "eval -client $kak_client echo -color Information '${msg}'" | kak -p ${kak_session}
) >& /dev/null < /dev/null & } ) > /dev/null 2>&1 < /dev/null & }
} }

View File

@ -22,14 +22,14 @@ def -shell-params git %{ %sh{
esac esac
tmpfile=$(mktemp /tmp/kak-git-XXXXXX) tmpfile=$(mktemp /tmp/kak-git-XXXXXX)
if git "$@" > ${tmpfile}; then if git "$@" > ${tmpfile}; then
[[ -n "$kak_opt_docsclient" ]] && echo "eval -client '$kak_opt_docsclient' %{" [ -n "$kak_opt_docsclient" ] && echo "eval -client '$kak_opt_docsclient' %{"
echo "edit! -scratch *git* echo "edit! -scratch *git*
exec |cat<space>${tmpfile}<ret>gk exec |cat<space>${tmpfile}<ret>gk
nop %sh{rm ${tmpfile}} nop %sh{rm ${tmpfile}}
set buffer filetype '${filetype}'" set buffer filetype '${filetype}'"
[[ -n "$kak_opt_docsclient" ]] && echo "}" [ -n "$kak_opt_docsclient" ] && echo "}"
else else
echo "echo %{git $@ failed, see *debug* buffer}" echo "echo %{git $@ failed, see *debug* buffer}"
rm ${tmpfile} rm ${tmpfile}
@ -45,7 +45,7 @@ def -shell-params git %{ %sh{
declare -A authors declare -A authors
declare -A dates declare -A dates
send_flags() { send_flags() {
if [[ -z "$line" ]]; then return; fi if [ -z "$line" ]; then return; fi
text=$(echo "${sha:0:8} ${dates[$sha]} ${authors[$sha]}" | sed -e 's/:/\\:/g') text=$(echo "${sha:0:8} ${dates[$sha]} ${authors[$sha]}" | sed -e 's/:/\\:/g')
flag="$line|black|$text" flag="$line|black|$text"
for (( i=1; $i < $count; i++ )); do for (( i=1; $i < $count; i++ )); do

View File

@ -5,10 +5,10 @@ def -shell-params -file-completion \
grep %{ %sh{ grep %{ %sh{
output=$(mktemp -d -t kak-grep.XXXXXXXX)/fifo output=$(mktemp -d -t kak-grep.XXXXXXXX)/fifo
mkfifo ${output} mkfifo ${output}
if (( $# > 0 )); then if [ $# -gt 0 ]; then
( ${kak_opt_grepcmd} "$@" | tr -d '\r' >& ${output} ) >& /dev/null < /dev/null & ( ${kak_opt_grepcmd} "$@" | tr -d '\r' > ${output} 2>&1 ) > /dev/null 2>&1 < /dev/null &
else else
( ${kak_opt_grepcmd} "${kak_selection}" | tr -d '\r' >& ${output} ) >& /dev/null < /dev/null & ( ${kak_opt_grepcmd} "${kak_selection}" | tr -d '\r' > ${output} 2>&1 ) > /dev/null 2>&1 < /dev/null &
fi fi
echo "eval -try-client '$kak_opt_toolsclient' %{ echo "eval -try-client '$kak_opt_toolsclient' %{

View File

@ -4,7 +4,7 @@ decl str toolsclient
def -shell-params make %{ %sh{ def -shell-params make %{ %sh{
output=$(mktemp -d -t kak-make.XXXXXXXX)/fifo output=$(mktemp -d -t kak-make.XXXXXXXX)/fifo
mkfifo ${output} mkfifo ${output}
( eval ${kak_opt_makecmd} $@ >& ${output} ) >& /dev/null < /dev/null & ( eval ${kak_opt_makecmd} "$@" > ${output} 2>&1 ) > /dev/null 2>&1 < /dev/null &
echo "eval -try-client '$kak_opt_toolsclient' %{ echo "eval -try-client '$kak_opt_toolsclient' %{
edit! -fifo ${output} *make* edit! -fifo ${output} *make*

View File

@ -16,8 +16,9 @@ hook global WinSetOption filetype=(?!man).* %{
def -hidden -shell-params _man %{ %sh{ def -hidden -shell-params _man %{ %sh{
tmpfile=$(mktemp /tmp/kak-man-XXXXXX) tmpfile=$(mktemp /tmp/kak-man-XXXXXX)
MANWIDTH=${kak_window_width} man "$@" | col -b > ${tmpfile} output=$(MANWIDTH=${kak_window_width} man "$@")
if (( ${PIPESTATUS[0]} == 0 )); then if [ $? -eq 0 ]; then
echo "${output}" | col -b > ${tmpfile}
echo "edit! -scratch '*man*' echo "edit! -scratch '*man*'
exec |cat<space>${tmpfile}<ret>gk exec |cat<space>${tmpfile}<ret>gk
nop %sh{rm ${tmpfile}} nop %sh{rm ${tmpfile}}
@ -29,6 +30,6 @@ def -hidden -shell-params _man %{ %sh{
} } } }
def -shell-params man %{ %sh{ def -shell-params man %{ %sh{
[[ -z "$@" ]] && set -- "$kak_selection" [ -z "$@" ] && set -- "$kak_selection"
echo "eval -try-client %opt{docsclient} _man $@" echo "eval -try-client %opt{docsclient} _man $@"
} } } }