dotfiles/herbstluftwm/fish_functions/group.fish

129 lines
3.9 KiB
Fish
Executable File

#!/usr/bin/env
import notify
import log
alias hc=herbstclient
set -g tag_color "ff99cc"
# group_active_reset resets the active group (unset)
function group_active_reset
set -q hlwm_group_active
end
function group_switch
set -f current $hlwm_group_active
set -f arg $argv[1]
if not set -q arg
set -f arg '+'
end
if test "$arg" = "+"
group_up
else if test "$arg" = "-"
group_down
else if test $arg -eq $arg
# is a number
group_index $arg
else
log -c $tag_color "invalid group index [$arg], use '+', '-', or a real integer"
return 1
end
# update the tag it should now be on, if it switched
if test $hlwm_group_active -ne $current
update_tag $current
end
group_set_keybinds
notify "Group $hlwm_group_active"
log -c $tag_color "group $current -> $hlwm_group_active"
end
# move_to_group moves the focused window to the relative tag on
# a given group
function move_to_group
set -f move_to "$argv"
if test "$move_to" = "$hlwm_group_active"
log -c $tag_color "ignoring move as it's already in the active group"
return 0
else if test $move_to -lt 1 -o $move_to -gt $hlwm_group_count
log -c $tag_color "invalid group index [$move_to]:" \
"out of range [1:$hlwm_group_count]"
return 1
end
set -f new_tag (translate_tag $hlwm_group_active $move_to)
log -c $tag_color "moving window to group [$move_to]" \
"tag -> $new_tag"
end
# translate_tag translates the current tag from
# the group provided in the first argument,
# from which the relative tag is calculated, to
# what the tag should be if it was in the second argument
function translate_tag
set -f current_actual (hc attr tags.focus.index)
set -f tl_from $argv[1]
set -f tl_to $argv[2]
set -f offset_from (math (math $tl_from - 1) x $hlwm_tags_per_group)
set -f offset_to (math (math $tl_to - 1) x $hlwm_tags_per_group)
set -f relative_tag_from (math $current_actual - $offset_from)
log -c $tag_color \
"calculated relative tag to group [$tl_from] to be [$relative_tag_from]"
set -f actual_tag_to (math $relative_tag_from + $offset_to)
log -c $tag_color \
"relative tag [$relative_tag_from] translated to group [$tl_to]" \
"becomes actual tag [$actual_tag_to]"
echo $actual_tag_to
end
function update_tag
set -f new_tag (translate_tag $argv $hlwm_group_active)
log -c $tag_color "moving frame focus to tag: [$new_tag]"
hc use_index $new_tag
end
function group_down
if not set -q hlwm_group_active
# Treat unset == 1, so move back to the last one
set -U hlwm_group_active $hlwm_group_count
else if test $hlwm_group_active -eq 1
set -U hlwm_group_active $hlwm_group_count
else
set -U hlwm_group_active (math $hlwm_group_active - 1)
end
end
function group_index
if test $argv -gt $hlwm_group_count
set -f $argv $hlwm_group_count
else if test $argv -lt 1
set -f $argv 1
end
set -U hlwm_group_active $argv
end
function group_up
if not set -q hlwm_group_active
set -U hlwm_group_active 2
else if test $hlwm_group_active -eq $hlwm_group_count
set -U hlwm_group_active 1
else
set -U hlwm_group_active (math $hlwm_group_active + 1)
end
end
# group_set_keybinds sets the keybinds to the given tab group
function group_set_keybinds
set_default hlwm_mod Mod4
log -c $tag_color "setting keybinds for group: $hlwm_group_active"
if not set -q hlwm_group_active;
set -U hlwm_group_active 1
end
set -l offset (math (math $hlwm_group_active-1) x $hlwm_tags_per_group)
for tag in (seq $hlwm_tags_per_group)
set -l actual_tag (math $tag + $offset)
hc keybind $hlwm_mod-$tag use_index (math $actual_tag-1)
hc keybind $hlwm_mod-Shift-$tag move_index (math $actual_tag-1)
end
end