refactor: Change the way that all hooks work

Move hooks to a dedicated module.
This allows for greater flexibility and easier editing.

TODO: Make hook handling neater (possibly through parsing the hook
	script to read an "hook-to" marker that allows for automated
	loading of properly formatted hook functions)
This commit is contained in:
Robert Morrison 2023-04-11 22:45:38 +01:00
parent 43e3ce2765
commit 9da7b6babf
Signed by: robert
GPG Key ID: 73E012EB3F4EC696
7 changed files with 47 additions and 7 deletions

View File

@ -108,11 +108,6 @@ program.
in that order of preference.
`pdf` files are mapped to `zathura` when it's installed.
#### `./chpwd`
This module sets the `chpwd` hook and makes ZSH display any (markdown
formatted) readme files
found in the current directory using glow.
#### `./completion`
This file configures the powerful completion engine of
ZSH.
@ -145,6 +140,10 @@ This makes a few changes:
- Ignores duplicates when searching history
- Requires you press enter after expanding `!!`
#### './hooks'
This file loads hook functions found in './hooks.d'
and makes it easier to assign hooks where necessary.
#### `./keyboard`
This file uses `zkbd`(see `man zshcontrib`) to get information about your
keyboard and how your terminal handles it. This can fix issues with keys

11
hooks Normal file
View File

@ -0,0 +1,11 @@
#This file manages hooks so I can have multiple functions hooked the way I want
for file in $(find $ZDOTDIR/hooks.d -type f); do
source "$file"
done
add-zsh-hook -Uz chpwd glow
add-zsh-hook -Uz chpwd osc7
add-zsh-hook -Uz precmd termtitle
add-zsh-hook -Uz precmd osc113
# vim: ft=zsh

View File

@ -1,4 +1,7 @@
chpwd() {
## Load readme with glow (if available)
glow() {
has glow || return 0
find $PWD -maxdepth 1 -iname 'readme.md' -exec glow {} \;
}
## vim: ft=zsh

7
hooks.d/osc113 Normal file
View File

@ -0,0 +1,7 @@
## output OSC-113;A to tell foot how to jump between prompts
osc113() {
print -Pn "\e]133;A\e\\"
}
## vim: ft=zsh

13
hooks.d/osc7 Normal file
View File

@ -0,0 +1,13 @@
## output the current directory in the correct format for foot.
function osc7 {
local LC_ALL=C
export LC_ALL
setopt localoptions extendedglob
input=( ${(s::)PWD} )
uri=${(j::)input/(#b)([^A-Za-z0-9_.\!~*\'\(\)-\/])/%${(l:2::0:)$(([##16]#match))}}
print -n "\e]7;file://${HOSTNAME}${uri}\e\\"
}
# vim: ft=zsh

6
hooks.d/termtitle Normal file
View File

@ -0,0 +1,6 @@
## set terminal title
termtitle () {
setTermTitle "($(whoami)@$(hostname) $(pwd))"
}
## vim: ft=zsh

3
zshrc
View File

@ -13,6 +13,7 @@ autoload -U calendar
autoload -U run-help
autoload -U +X compinit
autoload -U edit-command-line
autoload -U add-zsh-hook
## zmodload Modules
zmodload zsh/complist
@ -33,7 +34,7 @@ source ${ZDOTDIR}/dir_aliases
source ${ZDOTDIR}/prompt
source ${ZDOTDIR}/keyboard
source ${ZDOTDIR}/history
source ${ZDOTDIR}/chpwd
source ${ZDOTDIR}/hooks
## Set options