kakoune/rc/windowing/detection.kak
Johannes Altmanninger 746e0d032f rc windowing: allow to configure windowing system and window placement in new/terminal commands
Today I can control "terminal" and "new" by changing the terminal
alias but I always need to choose a concrete implementation, like
"tmux-terminal-horizontal", even when there is otherwise no need to
mention tmux in my config.

Allow to configure windowing system and window placement independently
by introducing dedicated options.

This allows to create mappings that work in any windowing system like

	map global user c   %{:with-option windowing_placement window new<ret>}
	map global user '"' %{:with-option windowing_placement vertical new<ret>}
	map global user '%' %{:with-option windowing_placement horizontal new<ret>}

For windowing systems that don't support all placements, you can wrap
the above in try/catch to fall back on the "window" variant which is
defined for all windowing systems.

When using multiple (nested) windowing systems, you might want to
add mappings like

	map global user t %{:with-option windowing_module tmux new<ret>}
	map global user T %{:with-option windowing_module wayland new<ret>}

---

This changes the default "terminal" alias for some modules. In
particular, instead of delegating to

    iterm-terminal-vertical
    screen-terminal-vertical
    tmux-terminal-horizontal
    wezterm-terminal-vertical

it will now by default delegate to the respective "-window" variant.
We could maintain backwards compatiblity here by setting the
"windowing_placement" option accordingly, but the new behavior seems
more logical?

Also, this removes the "terminal-tab" alias which was only defined
by the kitty module.

We could try to keep the alias approach and implement a "with-alias"
command, however that approach can only capture both dimensions
(windowing system and placement) if we add tons of commands
like "terminal-horizontal" (with implied windowing system) and
"tmux-terminal" (with implied placement).

Side thought: we could also get rid of the "focus" alias and instead
define

    define-command focus %{
        "%opt{windowing_module}-focus"
    }

Closes #3943, #4425
2023-11-13 22:54:49 +01:00

90 lines
3.3 KiB
Plaintext

# Attempt to detect the windowing environment we're operating in
#
# We try to load modules from the windowing_modules str-list option in order,
# stopping when one of the modules loads successfully. This ensures that only
# a single module is loaded by default.
#
# On load each module must attempt to detect the environment it's appropriate
# for, and if the environment isn't appropriate it must fail with an error.
# In addition, each module must check for the length of the windowing_modules
# str-list option defined below, and must /not/ check for an appropriate
# environment if the list is empty. An example of this test:
#
# evaluate-commands %sh{
# [ -z "${kak_opt_windowing_modules}" ] || [ -n "$TMUX" ] || echo 'fail tmux not detected'
# }
#
# Each module is expected to define at least two aliases:
# * terminal - create a new terminal with sensible defaults
# * focus - focus the specified client, defaulting to the current client
#
declare-option -docstring \
"Ordered list of windowing modules to try and load. An empty list disables
both automatic module loading and environment detection, enabling complete
manual control of the module loading." \
str-list windowing_modules 'tmux' 'screen' 'zellij' 'kitty' 'iterm' 'sway' 'wayland' 'x11' 'wezterm'
declare-option -docstring %{
windowing module to use in the 'terminal' command
} str windowing_module
declare-option -docstring %{
where to create new windows in the 'terminal' command.
Possible values:
- "window" (default) - new window
- "horizontal" - horizontal split (left/right)
- "vertical" - vertical split (top/bottom)
- "tab" - new tab besides current window
} str windowing_placement window
define-command terminal -params 1.. -docstring %{
terminal <program> [<arguments>]: create a new terminal using the preferred windowing environment and placement
This executes "%opt{windowing_module}-terminal-%opt{windowing_placement}" with the given arguments.
If the windowing module is 'wayland', 'sway' or 'x11', then the 'termcmd' option is used as terminal program.
Example usage:
terminal sh
with-option windowing_placement horizontal terminal sh
See also the 'new' command.
} %{
"%opt{windowing_module}-terminal-%opt{windowing_placement}" %arg{@}
}
complete-command terminal shell
# TODO Move this?
define-command with-option -params 3.. -docstring %{
with-option <option_name> <new_value> <command> [<arguments>]: evaluate a command with a modified option
} %{
evaluate-commands -save-regs s %{
evaluate-commands set-register s %exp{%%opt{%arg{1}}}
set-option current %arg{1} %arg{2}
evaluate-commands %sh{
shift 2
for arg
do
printf "'%s' " "$(printf %s "$arg" | sed "s/'/''/g")"
done
}
set-option current %arg{1} %reg{s}
}
}
hook -group windowing global KakBegin .* %{
evaluate-commands %sh{
set -- ${kak_opt_windowing_modules}
if [ $# -gt 0 ]; then
echo 'try %{ '
while [ $# -ge 1 ]; do
echo "require-module ${1}; set-option global windowing_module ${1} } catch %{ "
shift
done
echo "echo -debug 'no windowing module detected' }"
fi
}
}