Skip to content

Commit

Permalink
share: Introduce the sh directory, mktemp implementation
Browse files Browse the repository at this point in the history
This commit installs an `sh` directory in the `share` directory, in which
implementations of popular tools that might not be available on a user's
system sit.

In order for a shell scope to use those implementations if the regular
binaries are unavailable, the `PATH` variable has to be modified as follows,
at the beginning of the scope:

```
%sh{
    export PATH="${PATH}:${kak_runtime}/sh"

    <code>
}
```
  • Loading branch information
lenormf committed Jun 10, 2017
1 parent fe46c05 commit 6f0eb00
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions rc/base/lint.kak
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ decl -hidden range-specs lint_errors

def lint -docstring 'Parse the current buffer with a linter' %{
%sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
dir=$(mktemp -d -t kak-lint.XXXXXXXX)
mkfifo "$dir"/fifo
printf '%s\n' "eval -no-hooks write $dir/buf"
Expand Down
3 changes: 3 additions & 0 deletions rc/base/spell.kak
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Formats of language supported:
spell %{
try %{ add-highlighter ranges 'spell_regions' }
%sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
file=$(mktemp -d -t kak-spell.XXXXXXXX)/buffer
printf 'eval -no-hooks write %s\n' "${file}"
printf 'set buffer spell_tmp_file %s\n' "${file}"
Expand Down
3 changes: 3 additions & 0 deletions rc/core/doc.kak
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ decl -docstring "name of the client in which documentation is to be displayed" \

def -hidden -params 1..2 doc-open %{
%sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
manout=$(mktemp /tmp/kak-man-XXXXXX)
# Those options are handled by the `man-db` implementation
Expand Down
3 changes: 3 additions & 0 deletions rc/core/grep.kak
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ def -params .. -file-completion \
-docstring %{grep [<arguments>]: grep utility wrapper
All the optional arguments are forwarded to the grep utility} \
grep %{ %sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
output=$(mktemp -d -t kak-grep.XXXXXXXX)/fifo
mkfifo ${output}
if [ $# -gt 0 ]; then
Expand Down
3 changes: 3 additions & 0 deletions rc/core/make.kak
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def -params .. \
-docstring %{make [<arguments>]: make utility wrapper
All the optional arguments are forwarded to the make utility} \
make %{ %sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
output=$(mktemp -d -t kak-make.XXXXXXXX)/fifo
mkfifo ${output}
( eval ${kak_opt_makecmd} "$@" > ${output} 2>&1 ) > /dev/null 2>&1 < /dev/null &
Expand Down
3 changes: 3 additions & 0 deletions rc/core/man.kak
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ hook global WinSetOption filetype=(?!man).* %{
}

def -hidden -params 1..2 man-impl %{ %sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
manout=$(mktemp /tmp/kak-man-XXXXXX)
colout=$(mktemp /tmp/kak-man-XXXXXX)
MANWIDTH=${kak_window_width} man "$@" > $manout
Expand Down
3 changes: 3 additions & 0 deletions rc/extra/clang.kak
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def -params ..1 \
The syntaxic errors detected during parsing are shown when auto-diagnostics are enabled} \
clang-parse %{
%sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
dir=$(mktemp -d -t kak-clang.XXXXXXXX)
mkfifo ${dir}/fifo
printf %s\\n "set buffer clang_tmp_dir ${dir}"
Expand Down
3 changes: 3 additions & 0 deletions rc/extra/git-tools.kak
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Available commands:\n-add\n-rm\n-blame\n-commit\n-checkout\n-diff\n-hide-blame\n
} \
git %{ %sh{
show_git_cmd_output() {
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
local filetype
case "$1" in
show|diff) filetype=diff ;;
Expand Down
9 changes: 9 additions & 0 deletions rc/extra/go-tools.kak
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ decl -hidden completions gocode_completions

def go-complete -docstring "Complete the current selection with gocode" %{
%sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
dir=$(mktemp -d -t kak-go.XXXXXXXX)
printf %s\\n "set buffer go_complete_tmp_dir ${dir}"
printf %s\\n "eval -no-hooks write ${dir}/buf"
Expand Down Expand Up @@ -56,6 +59,9 @@ decl -hidden str go_format_tmp_dir
def -params ..1 go-format \
-docstring "go-format [-use-goimports]: custom formatter for go files" %{
%sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
dir=$(mktemp -d -t kak-go.XXXXXXXX)
printf %s\\n "set buffer go_format_tmp_dir ${dir}"
printf %s\\n "eval -no-hooks write ${dir}/buf"
Expand Down Expand Up @@ -87,6 +93,9 @@ decl -hidden str go_doc_tmp_dir
# FIXME text escaping
def -hidden -params 1..2 gogetdoc-cmd %{
%sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
dir=$(mktemp -d -t kak-go.XXXXXXXX)
printf %s\\n "set buffer go_doc_tmp_dir ${dir}"
printf %s\\n "eval -no-hooks write ${dir}/buf"
Expand Down
3 changes: 3 additions & 0 deletions rc/extra/jedi.kak
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ decl -docstring "colon separated list of path added to `python`'s $PYTHONPATH en

def jedi-complete -docstring "Complete the current selection" %{
%sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
dir=$(mktemp -d -t kak-jedi.XXXXXXXX)
mkfifo ${dir}/fifo
printf %s\\n "set buffer jedi_tmp_dir ${dir}"
Expand Down
3 changes: 3 additions & 0 deletions rc/extra/racer.kak
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ decl -hidden completions racer_completions

def racer-complete -docstring "Complete the current selection with racer" %{
%sh{
# fallback implementation: mktemp
export PATH="${PATH}:${kak_runtime}/sh"
dir=$(mktemp -d -t kak-racer.XXXXXXXX)
printf %s\\n "set buffer racer_tmp_dir ${dir}"
printf %s\\n "eval -no-hooks %{ write ${dir}/buf }"
Expand Down
68 changes: 68 additions & 0 deletions share/kak/sh/mktemp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/sh
## mktemp fallback implementation
## Options supported:
## -u, --dry-run: only print the path to a temporary file without generating it
## -d, --directory: create a directory
## -p, --tmpdir: use $TMPDIR as base path if possible, /tmp otherwise
## this flag does NOT take any argument

error() {
printf 'mktemp: %s\n' "$*" >&2
exit 1
}

main() {
flag_dry_run=0
flag_directory=0
flag_tmpdir=0

for arg in "$@"; do
case "${arg}" in
-u|--dry-run) flag_dry_run=1;;
-d|--directory) flag_directory=1;;
-p|--tmpdir) flag_tmpdir=1;;
-*) error "Unsupported flag ${arg}";;
*) break;;
esac

shift
done

if [ $# -eq 0 ]; then
template="tmp.XXXXXXXXXX"
flag_tmpdir=1
else
template="${1}"
if ! printf %s\\n "${template}" | grep -q 'XXX$'; then
error "The template must end with at least 3 consecutive 'X's"
fi
fi

if [ ${flag_tmpdir} -ne 0 ]; then
dir="${TMPDIR:-/tmp}/"
fi

template=$(printf %s\\n "${template}" | awk '{
idx = match($0, /X+$/) - 1
printf "%." idx "s", $0
system("cat /dev/urandom | tr -dc a-zA-Z0-9 | fold -w " RLENGTH " | head -n 1")
}')

path="${dir}${template}"
if [ ${flag_dry_run} -eq 0 ]; then
if [ ${flag_directory} -ne 0 ]; then
mkdir -p "${path}"
chmod u+rwx "${path}"
else
if [ -n "${dir}" ]; then
mkdir -p "${dir}"
fi
touch "${path}"
chmod u+rw "${path}"
fi
fi

printf %s\\n "${path}"
}

main "$@"
2 changes: 2 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ distclean: clean

installdirs:
install -d $(bindir) \
$(sharedir)/sh \
$(sharedir)/rc/base \
$(sharedir)/rc/core \
$(sharedir)/rc/extra \
Expand All @@ -117,6 +118,7 @@ installdirs:

install: kak man doc installdirs
install -m 0755 kak $(bindir)
install -m 0755 ../share/kak/sh/* $(sharedir)/sh
install -m 0644 ../share/kak/kakrc $(sharedir)
install -m 0644 ../rc/base/* $(sharedir)/rc/base
install -m 0644 ../rc/core/* $(sharedir)/rc/core
Expand Down

0 comments on commit 6f0eb00

Please # to comment.