2017-11-03 08:34:41 +01:00
|
|
|
declare-option -docstring "name of the client in which documentation is to be displayed" \
|
2017-05-16 13:35:43 +02:00
|
|
|
str docsclient
|
2016-11-03 12:57:55 +01:00
|
|
|
|
2017-11-08 09:32:49 +01:00
|
|
|
declare-option -hidden range-specs doc_render_ranges
|
|
|
|
declare-option -hidden range-specs doc_render_links
|
2017-11-06 13:52:24 +01:00
|
|
|
declare-option -hidden range-specs doc_links
|
2017-11-08 09:32:49 +01:00
|
|
|
declare-option -hidden range-specs doc_anchors
|
2016-02-02 20:15:12 +01:00
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
define-command -hidden -params 4 doc-render-regex %{
|
2017-11-03 09:09:45 +01:00
|
|
|
evaluate-commands -draft %{ try %{
|
|
|
|
execute-keys \%s %arg{1} <ret>
|
|
|
|
execute-keys -draft s %arg{2} <ret> d
|
|
|
|
execute-keys "%arg{3}"
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2018-05-31 13:03:35 +02:00
|
|
|
face="$4"
|
|
|
|
eval "set -- $kak_selections_desc"
|
|
|
|
for desc in "$@"; do ranges="$ranges '$desc|$face'"; done
|
2017-11-08 09:32:49 +01:00
|
|
|
echo "update-option buffer doc_render_ranges"
|
2018-05-31 13:03:35 +02:00
|
|
|
echo "set-option -add buffer doc_render_ranges $ranges"
|
2017-11-02 03:03:24 +01:00
|
|
|
}
|
|
|
|
} }
|
|
|
|
}
|
2016-02-02 20:15:12 +01:00
|
|
|
|
2017-11-06 13:52:24 +01:00
|
|
|
define-command -hidden doc-parse-links %{
|
|
|
|
evaluate-commands -draft %{ try %{
|
2017-11-08 09:32:49 +01:00
|
|
|
execute-keys \%s <lt><lt>(.*?),.*?<gt><gt> <ret>
|
2017-11-06 13:52:24 +01:00
|
|
|
execute-keys -draft s <lt><lt>.*,|<gt><gt> <ret> d
|
|
|
|
execute-keys H
|
|
|
|
set-option buffer doc_links %val{timestamp}
|
2017-11-08 09:32:49 +01:00
|
|
|
set-option buffer doc_render_links %val{timestamp}
|
2017-11-06 13:52:24 +01:00
|
|
|
evaluate-commands -itersel %{
|
|
|
|
set-option -add buffer doc_links "%val{selection_desc}|%reg{1}"
|
2017-11-08 09:32:49 +01:00
|
|
|
set-option -add buffer doc_render_links "%val{selection_desc}|default+u"
|
2017-11-06 13:52:24 +01:00
|
|
|
}
|
|
|
|
} }
|
|
|
|
}
|
|
|
|
|
2017-11-08 09:32:49 +01:00
|
|
|
define-command -hidden doc-parse-anchors %{
|
|
|
|
evaluate-commands -draft %{ try %{
|
|
|
|
set-option buffer doc_anchors %val{timestamp}
|
|
|
|
# Find sections as add them as imlicit anchors
|
|
|
|
execute-keys \%s ^={2,}\h+([^\n]+)$ <ret>
|
|
|
|
evaluate-commands -itersel %{
|
2018-06-03 06:46:44 +02:00
|
|
|
set-option -add buffer doc_anchors "%val{selection_desc}|%sh{printf '%s' ""$kak_main_reg_1"" | tr '[A-Z ]' '[a-z-]'}"
|
2017-11-08 09:32:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Parse explicit anchors and remove their text
|
|
|
|
execute-keys \%s \[\[(.*?)\]\]\s* <ret>
|
|
|
|
evaluate-commands -itersel %{
|
|
|
|
set-option -add buffer doc_anchors "%val{selection_desc}|%reg{1}"
|
|
|
|
}
|
|
|
|
execute-keys d
|
|
|
|
update-option buffer doc_anchors
|
|
|
|
} }
|
|
|
|
}
|
|
|
|
|
|
|
|
define-command doc-jump-to-anchor -params 1 %{
|
|
|
|
update-option buffer doc_anchors
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2018-05-31 13:03:35 +02:00
|
|
|
anchor="$1"
|
|
|
|
eval "set -- $kak_opt_doc_anchors"
|
2018-09-30 09:43:09 +02:00
|
|
|
|
|
|
|
shift
|
2018-05-31 13:03:35 +02:00
|
|
|
for range in "$@"; do
|
2018-10-01 07:45:28 +02:00
|
|
|
if [ "${range#*|}" = "$anchor" ]; then
|
2018-05-31 13:03:35 +02:00
|
|
|
printf '%s\n' "select '${range%|*}'; execute-keys vv"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
done
|
2018-09-30 09:43:09 +02:00
|
|
|
printf "echo -markup {Error}No such anchor '%s'" "${anchor}"
|
2017-11-08 09:32:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
define-command doc-follow-link %{
|
2017-11-06 13:52:24 +01:00
|
|
|
update-option buffer doc_links
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2018-05-31 13:03:35 +02:00
|
|
|
eval "set -- $kak_opt_doc_links"
|
|
|
|
for link in "$@"; do
|
|
|
|
printf '%s\n' "$link"
|
|
|
|
done | awk -v FS='[.,|#]' '
|
2017-11-06 13:52:24 +01:00
|
|
|
BEGIN {
|
|
|
|
l=ENVIRON["kak_cursor_line"];
|
|
|
|
c=ENVIRON["kak_cursor_column"];
|
|
|
|
}
|
|
|
|
l >= $1 && c >= $2 && l <= $3 && c <= $4 {
|
2017-11-08 09:32:49 +01:00
|
|
|
if (NF == 6) {
|
|
|
|
print "doc " $5
|
|
|
|
if ($6 != "") {
|
|
|
|
print "doc-jump-to-anchor %{" $6 "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print "doc-jump-to-anchor %{" $5 "}"
|
|
|
|
}
|
2017-11-06 13:52:24 +01:00
|
|
|
exit
|
|
|
|
}
|
|
|
|
'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
define-command -params 1 -hidden doc-render %{
|
2017-11-08 09:32:49 +01:00
|
|
|
edit! -scratch "*doc-%sh{basename $1 .asciidoc}*"
|
2017-11-03 09:09:45 +01:00
|
|
|
execute-keys "!cat %arg{1}<ret>gg"
|
2017-09-29 21:16:31 +02:00
|
|
|
|
2017-11-08 09:32:49 +01:00
|
|
|
doc-parse-anchors
|
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
# Join paragraphs together
|
2018-09-23 09:40:16 +02:00
|
|
|
try %{ execute-keys -draft \%S \n{2,}|(?<=\+)\n|^[^\n]+::\n <ret> <a-K>^\h*-{2,}(\n|\z)<ret> S\n\z<ret> <a-k>\n<ret> <a-j> }
|
2016-02-02 20:15:12 +01:00
|
|
|
|
2017-11-02 03:03:24 +01:00
|
|
|
# Remove some line end markers
|
2017-11-03 09:09:45 +01:00
|
|
|
try %{ execute-keys -draft \%s \h*(\+|:{2,})$ <ret> d }
|
2016-02-02 20:15:12 +01:00
|
|
|
|
2017-11-08 09:32:49 +01:00
|
|
|
# Setup the doc_render_ranges option
|
|
|
|
set-option buffer doc_render_ranges %val{timestamp}
|
2018-02-22 12:22:13 +01:00
|
|
|
doc-render-regex \B(?<!\\)\*(?=\S)[^\n]+?(?<=\S)(?<!\\)\*\B \A|.\z 'H' default+b
|
|
|
|
doc-render-regex \b(?<!\\)_(?=\S)[^\n]+?(?<=\S)(?<!\\)_\b \A|.\z 'H' default+i
|
|
|
|
doc-render-regex \B(?<!\\)`(?=\S)[^\n]+?(?<=\S)(?<!\\)`\B \A|.\z 'H' mono
|
2017-11-02 03:03:24 +01:00
|
|
|
doc-render-regex ^=\h+[^\n]+ ^=\h+ '~' title
|
|
|
|
doc-render-regex ^={2,}\h+[^\n]+ ^={2,}\h+ '' header
|
2018-09-23 14:42:30 +02:00
|
|
|
doc-render-regex ^\h*-{2,}\n\h*.*?^\h*-{2,}\n ^\h*-{2,}\n '' block
|
2017-11-08 09:32:49 +01:00
|
|
|
|
2017-11-06 13:52:24 +01:00
|
|
|
doc-parse-links
|
2017-11-02 03:03:24 +01:00
|
|
|
|
|
|
|
# Remove escaping of * and `
|
2017-11-03 09:09:45 +01:00
|
|
|
try %{ execute-keys -draft \%s \\((?=\*)|(?=`)) <ret> d }
|
2017-11-02 03:03:24 +01:00
|
|
|
|
|
|
|
set-option buffer readonly true
|
2018-06-28 13:08:58 +02:00
|
|
|
add-highlighter buffer/ ranges doc_render_ranges
|
|
|
|
add-highlighter buffer/ ranges doc_render_links
|
|
|
|
add-highlighter buffer/ wrap -word -indent
|
2017-11-08 09:32:49 +01:00
|
|
|
map buffer normal <ret> :doc-follow-link<ret>
|
2016-02-02 20:15:12 +01:00
|
|
|
}
|
|
|
|
|
2017-11-08 09:32:49 +01:00
|
|
|
define-command -params 1..2 \
|
2016-08-30 16:23:42 +02:00
|
|
|
-shell-candidates %{
|
2017-11-08 10:20:37 +01:00
|
|
|
if [ "$kak_token_to_complete" -eq 0 ]; then
|
|
|
|
find "${kak_runtime}/doc/" -type f -name "*.asciidoc" | sed 's,.*/,,; s/\.[^/]*$//'
|
|
|
|
elif [ "$kak_token_to_complete" -eq 1 ]; then
|
|
|
|
readonly page="${kak_runtime}/doc/${1}.asciidoc"
|
|
|
|
if [ -f "${page}" ]; then
|
|
|
|
awk '
|
|
|
|
/^==+ +/ { sub(/^==+ +/, ""); print }
|
|
|
|
/^\[\[[^\]]+\]\]/ { sub(/^\[\[/, ""); sub(/\]\].*/, ""); print }
|
2017-12-28 23:37:04 +01:00
|
|
|
' < $page | tr '[A-Z ]' '[a-z-]'
|
2017-11-08 10:20:37 +01:00
|
|
|
fi
|
|
|
|
fi
|
2016-02-02 20:15:12 +01:00
|
|
|
} \
|
2016-10-11 09:03:41 +02:00
|
|
|
doc -docstring %{doc <topic> [<keyword>]: open a buffer containing documentation about a given topic
|
|
|
|
An optional keyword argument can be passed to the function, which will be automatically selected in the documentation} %{
|
2018-05-06 23:29:52 +02:00
|
|
|
evaluate-commands %sh{
|
2017-11-02 03:03:24 +01:00
|
|
|
readonly page="${kak_runtime}/doc/${1}.asciidoc"
|
|
|
|
if [ -f "${page}" ]; then
|
2017-11-08 09:32:49 +01:00
|
|
|
if [ $# -eq 2 ]; then
|
|
|
|
jump_cmd="doc-jump-to-anchor '$2'"
|
|
|
|
fi
|
|
|
|
printf %s\\n "evaluate-commands -try-client %opt{docsclient} %{ doc-render ${page}; ${jump_cmd} }"
|
2017-05-27 07:14:01 +02:00
|
|
|
else
|
2017-11-02 03:03:24 +01:00
|
|
|
printf %s\\n "echo -markup '{Error}No such doc file: ${page}'"
|
2016-02-02 20:15:12 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
}
|
2016-12-13 19:26:19 +01:00
|
|
|
|
|
|
|
alias global help doc
|