forked from lenormf/kakoune-extra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectsave.kak
46 lines (37 loc) · 2.01 KB
/
selectsave.kak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
##
## selectsave.kak by lenormf
## Save the selection when writing a buffer, and restore them when opening it later
##
decl -docstring "path to the directory in which the cache is stored" \
str selectsave_path_dir_cache %sh{printf %s "${XDG_CONFIG_HOME:-${HOME}/.config}/kak"}
decl -docstring "full path to the cache file" str selectsave_path_cache "%opt{selectsave_path_dir_cache}/selections_desc.txt"
hook global WinDisplay [^*].* %{ %sh{
if [ -e "${kak_opt_selectsave_path_cache}" ]; then
readonly PATTERN_SELECTIONS='[0-9]+\.[0-9]+,[0-9]+\.[0-9]+(:[0-9]+\.[0-9]+,[0-9]+\.[0-9]+)*'
readonly PATTERN_CURBUF="^${kak_buffile} ${PATTERN_SELECTIONS}\$"
info_curbuf=$(grep -E "${PATTERN_CURBUF}" "${kak_opt_selectsave_path_cache}")
selection_desc=$(expr "${info_curbuf}" : "${kak_buffile} \\(.*\\)\$")
if [ -n "${info_curbuf}" ] && [ -n "${selection_desc}" ]; then
readonly PATH_TMP=$(mktemp)
printf 'eval -client %%{%s} select "%s"\n' "${kak_client}" "${selection_desc}" | kak -p "${kak_session}"
grep -v -E "${PATTERN_CURBUF}" "${kak_opt_selectsave_path_cache}" > "${PATH_TMP}"
mv "${PATH_TMP}" "${kak_opt_selectsave_path_cache}"
fi
fi
} }
hook global BufWritePost [^*].* %{ %sh{
if [ -e "${kak_buffile}" ]; then
{
mkdir -p "${kak_opt_selectsave_path_dir_cache}";
touch "${kak_opt_selectsave_path_cache}";
} || exit
readonly PATTERN_SELECTIONS='[0-9]+\.[0-9]+,[0-9]+\.[0-9]+(:[0-9]+\.[0-9]+,[0-9]+\.[0-9]+)*'
readonly PATTERN_CURBUF="^${kak_buffile} ${PATTERN_SELECTIONS}\$"
if grep -q -E "${PATTERN_CURBUF}" "${kak_opt_selectsave_path_cache}"; then
readonly PATH_TMP=$(mktemp)
grep -v -E "${PATTERN_CURBUF}" "${kak_opt_selectsave_path_cache}" > "${PATH_TMP}"
mv "${PATH_TMP}" "${kak_opt_selectsave_path_cache}"
fi
printf '%s %s\n' "${kak_buffile}" "${kak_selections_desc}" >> "${kak_opt_selectsave_path_cache}"
fi
} }