118 lines
4.0 KiB
Plaintext
118 lines
4.0 KiB
Plaintext
# lots of this is sourced from Screwtapello's kakoune-ghwiki plugin at
|
|
# https://gitlab.com/Screwtapello/kakoune-ghwiki/-/blob/master/ghwiki.kak.
|
|
# (Licensed MIT.) Thanks for having such good comments!
|
|
declare-option str zettel_all_path zettel-all
|
|
|
|
# where are our zettel names stored?
|
|
declare-option completions zettel_completions
|
|
|
|
# Insert our completion option at the front of the list.
|
|
# Because our completions have non-word characters like spaces and [[]],
|
|
# our completions need to be higher priority to make them show up reliably.
|
|
set-option global completers option=zettel_completions %opt{completers}
|
|
|
|
define-command -docstring "Enable Zettel interactions for this buffer" zettel-enable %{
|
|
map -docstring 'Navigate to linked page' window normal <ret> ': zettel-follow-link<ret>'
|
|
zettel-enable-completions
|
|
}
|
|
|
|
define-command zettel-select-link-target %{
|
|
try %{
|
|
# Do we already have a wiki link completely selected?
|
|
execute-keys s\A\[\[.*\]\]\z<ret>
|
|
|
|
} catch %{
|
|
# No, but maybe if we select the square brackets around us,
|
|
# we'll have a complete wiki link?
|
|
execute-keys <a-a>r
|
|
execute-keys s\A\[\[.*\]\]\z<ret>
|
|
|
|
} catch %{
|
|
# OK, we've selected text inside one set of square brackets,
|
|
# and that's not a wiki link,
|
|
# but maybe the second set of square brackets is around *that*?
|
|
execute-keys <a-a>r
|
|
execute-keys s\A\[\[.*\]\]\z<ret>
|
|
|
|
} catch %{
|
|
# Nope, no idea.
|
|
fail No wiki link found.
|
|
}
|
|
|
|
# We've selected the entire link,
|
|
# now just select the text inside it.
|
|
execute-keys <a-i>r <a-i>r
|
|
}
|
|
|
|
define-command \
|
|
-docstring "Follow the zettel link under the main cursor" \
|
|
zettel-follow-link \
|
|
%{
|
|
# Save the 'a' register so we have a temporary storage space.
|
|
evaluate-commands -save-regs a %{
|
|
|
|
# In a draft context (so we don't mess up the user's selections)
|
|
evaluate-commands -draft %{
|
|
# ...select the target page name of the link surrounding each cursor
|
|
zettel-select-link-target
|
|
|
|
# Copy all the page names to a new scratch buffer
|
|
execute-keys '"ay'
|
|
}
|
|
|
|
# Now all our page names are in register a, so let's open up the
|
|
# corresponding files!
|
|
evaluate-commands %sh{
|
|
printf 'edit "%s"\n' "$(env ZETTEL_SELECT=zettel-select-noninteractive zettel-name $kak_main_reg_a)"
|
|
}
|
|
}
|
|
}
|
|
|
|
define-command zettel-populate-completions %{
|
|
evaluate-commands %sh{
|
|
$kak_opt_zettel_all_path | cut -d: -f2 | fzf --filter "$kak_main_reg_a" | sed 's/"/""/g' | while read -r link; do
|
|
printf 'set-option -add window zettel_completions "%s||%s"\n' "$link" "$link"
|
|
done
|
|
}
|
|
}
|
|
|
|
define-command \
|
|
-docstring "Enable auto-completion of zettel links in the current window." \
|
|
zettel-enable-completions \
|
|
%{
|
|
hook window InsertIdle .* %{
|
|
try %{
|
|
evaluate-commands -draft -save-regs a %{
|
|
# We only care about the main selection for completion purposes.
|
|
execute-keys <space>;
|
|
|
|
try %{
|
|
# Is the cursor somewhere inside two square brackets?
|
|
execute-keys <a-a>r <a-a>r
|
|
|
|
# Then our completions starts inside the square brackets.
|
|
execute-keys <a-i>r <a-i>r <a-semicolon>
|
|
} catch %{
|
|
fail not-in-a-link
|
|
}
|
|
|
|
execute-keys '"ay'
|
|
|
|
# Initialise our completions for the current position...
|
|
set-option window zettel_completions \
|
|
"%val{cursor_line}.%val{cursor_column}@%val{timestamp}"
|
|
|
|
# ...and fill them out!
|
|
zettel-populate-completions
|
|
}
|
|
} catch %{
|
|
evaluate-commands %sh{
|
|
if [ "$kak_error" != "not-in-a-link" ]; then
|
|
# This is some kind of error we care about!
|
|
echo fail "$kak_error"
|
|
fi
|
|
}
|
|
}
|
|
}
|
|
}
|