Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #67 from andreyorst/dev
Browse files Browse the repository at this point in the history
Update master to v2019.07.01
  • Loading branch information
andreyorst committed Jul 5, 2019
2 parents f893d0d + 0de1723 commit 2691c61
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 63 deletions.
44 changes: 33 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[vim-plug][5] and [use-package][6]. It helps installing and updating plugins,
can run post-update actions, and isolates plugin configuration within itself.

The release model of **plug.kak** supports two latest releases of Kakoune, and
The release model of **plug.kak** supports one latest stable release of Kakoune, and
the development version. Default branch is always in sync with latest stable
release of Kakoune. If you're using Kakoune builds from GitHub repository,
please switch to [dev][7] branch.
Expand Down Expand Up @@ -95,6 +95,7 @@ These are available keywords:
- [do][12]
- [theme][13]
- [config][14]
- [defer][21]
- [depth-sort][19] and [no-depth-sort][19]
- [domain][20]
- [ensure][15]
Expand Down Expand Up @@ -239,27 +240,47 @@ For example:
```kak
plug "andreyorst/fzf.kak" config %{
map -docstring 'fzf mode' global normal '<c-p>' ': fzf-mode<ret>'
set-option global fzf_preview_width '65%'
evaluate-commands %sh{
if [ ! -z "$(command -v fd)" ]; then
echo "set-option global fzf_file_command 'fd . --no-ignore --type f --follow --hidden'"
fi
}
}
```

In this example I'm setting a <kbd>Ctrl</kbd>+<kbd>p</kbd> mapping that is
meaningful only if the plugin is installed. I've could configure it outside of
`plug` command, but it will fail if I accidentally remove or disable the
plugin. In case of configuring it with `plug` command, I don't need to keep
track of other configuration pieces.

After that I'm setting the `fzf_preview_width` option, and evaluating shell
expansion as usual. Everything within the `config %{ }` block is ordinary kakscript.
track of other configuration pieces. Everything within the `config %{ }` block
is ordinary kakscript.

The `config` keyword is optional, you can skip it if you want. Multiple `config`
blocks are supported as well.

### Deferring plugin configuration
Sometimes it is unnecessary to configure plugin if it isn't loaded. Since
Kakoune added support for module system with `provide-module` and
`require-module` followed by `ModuleLoaded` hook it is possible to defer
configuration until certain module is loaded. For example, let's look on
`fzf.kak` configuration deferred until `fzf` module is required:

```kak
plug "andreyorst/fzf.kak" config %{
map -docstring 'fzf mode' global normal '<c-p>' ': fzf-mode<ret>'
} defer "fzf" %{
set-option global fzf_preview_width '65%'
set-option global fzf_project_use_tilda true
evaluate-commands %sh{
if [ -n "$(command -v fd)" ]; then
echo "set-option global fzf_file_command %{fd . --no-ignore --type f --follow --hidden --exclude .git --exclude .svn}"
else
echo "set-option global fzf_file_command %{find . \( -path '*/.svn*' -o -path '*/.git*' \) -prune -o -type f -follow -print}"
fi
[ -n "$(command -v bat)" ] && echo "set-option global fzf_highlight_cmd bat"
[ -n "${kak_opt_grepcmd}" ] && echo "set-option global fzf_sk_grep_command %{${kak_opt_grepcmd}}"
}
}
```

The `defer "fzf" %{ ... }` block is a configuration block that will be evaluated
only when `fzf` module is loaded.

Since we've touched the configuration of **plug.kak** itself, let's discuss this
topic.

Expand Down Expand Up @@ -372,3 +393,4 @@ And last but not least: `plug`. Load plugin from plugin installation directory b
[18]: #Default-git-domain
[19]: #Depth-sorting-sourced-files
[20]: #Specifying-git-domain-on-per-plugin-basis
[21]: #Deferring-plugin-configuration
119 changes: 67 additions & 52 deletions rc/plug.kak
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@

# Public options
declare-option -docstring \
"path where plugins should be installed.
"Path where plugins should be installed.

Default value: '%val{config}/plugins'" \
str plug_install_dir "%val{config}/plugins"

declare-option -docstring \
"default domain to access git repositories. Can be changed to any preferred domain, like gitlab, bitbucket, gitea, etc.
"Default domain to access git repositories. Can be changed to any preferred domain, like gitlab, bitbucket, gitea, etc.

Default value: 'https://github.com'" \
str plug_git_domain 'https://github.com'

declare-option -docstring \
"sort sourced files by depth" \
"Sort sourced files by depth" \
bool plug_depth_sort false

declare-option -docstring \
Expand All @@ -34,7 +34,7 @@ declare-option -docstring \
int plug_max_active_downloads 10

declare-option -docstring \
"always ensure that all plugins are installed. If this option specified, all uninstalled plugins are being installed when Kakoune starts." \
"Always ensure that all plugins are installed. If this option specified, all uninstalled plugins are being installed when Kakoune starts." \
bool plug_always_ensure false

declare-option -docstring "name of the client in which utilities display information" \
Expand All @@ -54,43 +54,53 @@ str plug_loaded_plugins

declare-option -hidden -docstring \
"List of post update/install hooks to be executed" \
str-list plug_post_hooks ''
str-list plug_post_hooks

declare-option -hidden -docstring \
"List of post update/install hooks to be executed" \
str-list plug_domains ''
str-list plug_domains

# since we want to add highlighters to kak filetype we need to require kak module
require-module kak

# kakrc highlighters
try %<
add-highlighter shared/kakrc/code/plug_keywords regex (^|\h+)\b(plug|do|config|load|domain)\b(\h+)?((?=")|(?=')|(?=%)|(?=\w)) 0:keyword
add-highlighter shared/kakrc/code/plug_attributes regex \b(noload|ensure|branch|tag|commit|theme|(no-)?depth-sort)\b 0:attribute
add-highlighter shared/kakrc/plug_post_hooks region -recurse '\{' '\bdo\h+%\{' '\}' ref sh
> catch %{
echo -debug "plug.kak: Can't declare highlighters for kakrc."
try %{ echo -debug " Detailed error: %val{error}" }
try %[
add-highlighter shared/kakrc/code/plug_keywords regex (^|\h)\b(plug|do|config|load|domain|defer)\b(\h+)?((?=")|(?=')|(?=%)|(?=\w)) 0:keyword
add-highlighter shared/kakrc/code/plug_attributes regex (^|\h)\b(noload|ensure|branch|tag|commit|theme|(no-)?depth-sort)\b 0:attribute
add-highlighter shared/kakrc/plug_post_hooks region -recurse '\{' '\bdo\K\h+%\{' '\}' ref sh
] catch %{
echo -debug "plug.kak: Can't declare highlighters for 'kak' filetype."
echo -debug " Detailed error: %val{error}"
}

# *plug* highlighters
try %<
try %{
add-highlighter shared/plug_buffer group
add-highlighter shared/plug_buffer/done regex [^:]+:\h+(Up\h+to\h+date|Done|Installed)$ 1:string
add-highlighter shared/plug_buffer/update regex [^:]+:\h+(Update\h+available|Deleted)$ 1:keyword
add-highlighter shared/plug_buffer/not_installed regex [^:]+:\h+(Not\h+(installed|loaded)|(\w+\h+)?Error([^\n]+)?)$ 1:red+b
add-highlighter shared/plug_buffer/updating regex [^:]+:\h+(Installing|Updating|Local\h+changes)$ 1:type
add-highlighter shared/plug_buffer/working regex [^:]+:\h+(Running\h+post-update\h+hooks|Waiting[^\n]+)$ 1:attribute
> catch %{
echo -debug "plug.kak: Can't declare highlighters for *plug*."
try %{ echo -debug " Detailed error: %val{error}" }
} catch %{
echo -debug "plug.kak: Can't declare highlighters for *plug* buffer."
echo -debug " Detailed error: %val{error}"
}

hook -group plug-syntax global WinSetOption filetype=plug %{
add-highlighter window/plug_buffer ref plug_buffer
add-highlighter buffer/plug_buffer ref plug_buffer
hook -always -once window WinSetOption filetype=.* %{
remove-highlighter window/plug_buffer
remove-highlighter buffer/plug_buffer
}
}

define-command -override -docstring \
"plug <plugin> [<branch>|<tag>|<commit>] [<noload>|<load> <subset>] [[<config>] <configurations>]: load <plugin> from ""%opt{plug_install_dir}""" \
"plug <plugin> [<switches>]: manage <plugin> from ""%opt{plug_install_dir}""
Switches:
branch (tag, commit) <str> checkout to <str> before loading plugin
noload do not source plugin files
load <subset> source only <subset> of plugin files
defer <module> <configurations> load plugin <configurations> only when <module> is loaded
config <configurations> plugin <configurations>" \
plug -params 1.. -shell-script-candidates %{ ls -1 ${kak_opt_plug_install_dir} } %{ try %{
evaluate-commands %sh{
plugin="${1%%.git}"
Expand All @@ -108,41 +118,49 @@ plug -params 1.. -shell-script-candidates %{ ls -1 ${kak_opt_plug_install_dir} }
printf "%s\n" "set-option -add global plug_plugins %{${plugin} }"
fi
[ "$kak_opt_plug_depth_sort" = "true" ] && depth_sort="true" || depth_sort="false"
[ "${kak_opt_plug_depth_sort}" = "true" ] && depth_sort="true" || depth_sort="false"
while [ $# -gt 0 ]; do
case $1 in
branch|tag|commit)
(branch|tag|commit)
branch_type=$1
shift
checkout="$1" ;;
noload)
(noload)
noload=1 ;;
load)
(load)
shift
load=1
load_files="$1" ;;
do)
(defer)
shift
module="$1"
shift
deferred_conf=$(printf "%s\n" "$1" | sed "s/@/@@/g")
deferred_conf=$(printf "%s\n%s\n" "hook global ModuleLoaded ${module} %@ ${deferred_conf} @")
configurations="${configurations}
${deferred_conf}" ;;
(do)
shift
hooks="${hooks} %{${plugin_name}} %{$1}" ;;
ensure)
(ensure)
ensure=1 ;;
theme)
(theme)
noload=1
theme_hooks="mkdir -p ${kak_config}/colors
find . -type f -name '*.kak' -exec cp {} ${kak_config}/colors/ \;"
hooks="${hooks} %{${plugin_name}} %{${theme_hooks}}" ;;
depth-sort)
(depth-sort)
depth_sort="true" ;;
no-depth-sort)
(no-depth-sort)
depth_sort="false" ;;
domain)
(domain)
shift
domains="${domains} %{${plugin_name}} %{$1}" ;;
config)
(config)
shift
configurations="${configurations} $1" ;;
*)
(*)
configurations="${configurations} $1" ;;
esac
shift
Expand All @@ -152,7 +170,8 @@ plug -params 1.. -shell-script-candidates %{ ls -1 ${kak_opt_plug_install_dir} }
# their configurations are known to `plug.kak', so it can load those after installation
# automatically.
if [ -n "${configurations}" ]; then
printf "%s\n" "declare-option -hidden str plug_${plugin_opt_name}_conf %{${configurations}}"
configurations=$(printf "%s\n" "${configurations}" | sed "s/&/&&/g")
printf "%s\n" "declare-option -hidden str plug_${plugin_opt_name}_conf %&${configurations}&"
else
printf "%s\n" "declare-option -hidden str plug_${plugin_opt_name}_conf"
fi
Expand Down Expand Up @@ -190,7 +209,7 @@ plug -params 1.. -shell-script-candidates %{ ls -1 ${kak_opt_plug_install_dir} }
# trim leading and trailing whitespaces. Looks ugly, but faster than `sed'
file="${file#"${file%%[![:space:]]*}"}"
file="${file%"${file##*[![:space:]]}"}"
if [ "$depth_sort" = "true" ]; then
if [ "${depth_sort}" = "true" ]; then
# performance hungry place.
find -L ${kak_opt_plug_install_dir}/${plugin_name} -path '*/.git' -prune -o -type f -name "${file}" -print | perl -e '
print map { $_->[0] }
Expand All @@ -204,7 +223,7 @@ plug -params 1.. -shell-script-candidates %{ ls -1 ${kak_opt_plug_install_dir} }
fi
done
} fi
printf "%s\n" "evaluate-commands \"%opt{plug_${plugin_opt_name}_conf}\""
printf "%s\n" "evaluate-commands %opt{plug_${plugin_opt_name}_conf}"
printf "%s\n" "set-option -add global plug_loaded_plugins %{${plugin} }"
else
if [ -n "${ensure}" ] || [ "${kak_opt_plug_always_ensure}" = "true" ]; then
Expand All @@ -213,8 +232,8 @@ plug -params 1.. -shell-script-candidates %{ ls -1 ${kak_opt_plug_install_dir} }
fi
}
} catch %{
echo -debug "plug.kak: Error occured while loading %arg{1} plugin:"
try %{ echo -debug " %val{error}" }
echo -debug "plug.kak: Error occured while loading '%arg{1}' plugin:"
echo -debug " %val{error}"
}}

define-command -override -docstring \
Expand Down Expand Up @@ -268,7 +287,7 @@ plug-install -params ..1 %{ nop %sh{ (
plugin_name="${plugin##*/}"
git_domain=${kak_opt_plug_git_domain}
eval "set -- ${kak_opt_plug_domains}"
eval "set -- ${kak_quoted_opt_plug_domains}"
while [ $# -ne 0 ]; do
if [ "$1" = "${plugin_name}" ]; then
git_domain="https://$2"
Expand All @@ -279,9 +298,9 @@ plug-install -params ..1 %{ nop %sh{ (
if [ ! -d "${kak_opt_plug_install_dir}/${plugin_name}" ]; then
case ${plugin} in
http*|git*)
(http*|git*)
git="git clone ${plugin}" ;;
*)
(*)
git="git clone ${git_domain}/${plugin}" ;;
esac
Expand Down Expand Up @@ -413,7 +432,7 @@ define-command -override -hidden \
plug-eval-hooks -params 1 %{ nop %sh{ (
status=0
plugin_name="$1"
eval "set -- ${kak_opt_plug_post_hooks}"
eval "set -- ${kak_quoted_opt_plug_post_hooks}"
while [ $# -gt 0 ]; do
if [ "$1" = "${plugin_name}" ]; then
plugin_name="${1##*/}"
Expand Down Expand Up @@ -457,12 +476,8 @@ plug-list -params ..1 %{ evaluate-commands -try-client %opt{toolsclient} %sh{
mkfifo ${fifo}
printf "%s\n" "edit! -fifo ${fifo} *plug*
set-option window filetype plug
set-option buffer filetype plug
plug-show-help
try %{
remove-highlighter window/wrap
remove-highlighter window/numbers
}
hook -always -once buffer BufCloseFifo .* %{ nop %sh{ rm -rf ${tmp} } }
map buffer normal '<ret>' ':<space>plug-fifo-operate install-update<ret>'
map buffer normal 'H' ':<space>plug-show-help<ret>'
Expand Down Expand Up @@ -535,29 +550,29 @@ plug-fifo-operate -params 1 %{ evaluate-commands -save-regs t %{
evaluate-commands %sh{
plugin="${kak_reg_t%:*}"
case $1 in
install-update)
(install-update)
if [ -d "${kak_opt_plug_install_dir}/${plugin##*/}" ]; then
printf "%s\n" "plug-update ${plugin}'"
else
printf "%s\n" "plug-install ${plugin}'"
fi ;;
update)
(update)
if [ -d "${kak_opt_plug_install_dir}/${plugin##*/}" ]; then
printf "%s\n" "plug-update ${plugin}'"
else
printf "%s\n" "echo -markup %{{Information}${plugin}' is not installed}"
fi ;;
install)
(install)
if [ ! -d "${kak_opt_plug_install_dir}/${plugin##*/}" ]; then
printf "%s\n" "plug-install ${plugin}'"
else
printf "%s\n" "echo -markup %{{Information}${plugin}' already installed}"
fi ;;
clean)
(clean)
printf "%s\n" "plug-clean ${plugin}'" ;;
log)
(log)
printf "%s\n" "plug-display-log ${plugin}'" ;;
*)
(*)
;;
esac
}
Expand Down

0 comments on commit 2691c61

Please sign in to comment.