From 9da7b6babf05c25f8e61554bb1350b7fbadae7f3 Mon Sep 17 00:00:00 2001 From: Robert Morrison Date: Tue, 11 Apr 2023 22:45:38 +0100 Subject: [PATCH] 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) --- Readme.md | 9 ++++----- hooks | 11 +++++++++++ chpwd => hooks.d/glow | 5 ++++- hooks.d/osc113 | 7 +++++++ hooks.d/osc7 | 13 +++++++++++++ hooks.d/termtitle | 6 ++++++ zshrc | 3 ++- 7 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 hooks rename chpwd => hooks.d/glow (56%) create mode 100644 hooks.d/osc113 create mode 100644 hooks.d/osc7 create mode 100644 hooks.d/termtitle diff --git a/Readme.md b/Readme.md index c82d2bb..a2bf452 100644 --- a/Readme.md +++ b/Readme.md @@ -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 diff --git a/hooks b/hooks new file mode 100644 index 0000000..3d703de --- /dev/null +++ b/hooks @@ -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 diff --git a/chpwd b/hooks.d/glow similarity index 56% rename from chpwd rename to hooks.d/glow index bf7edbf..f0d55fd 100644 --- a/chpwd +++ b/hooks.d/glow @@ -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 diff --git a/hooks.d/osc113 b/hooks.d/osc113 new file mode 100644 index 0000000..f1ac4d4 --- /dev/null +++ b/hooks.d/osc113 @@ -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 diff --git a/hooks.d/osc7 b/hooks.d/osc7 new file mode 100644 index 0000000..eb80e24 --- /dev/null +++ b/hooks.d/osc7 @@ -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 diff --git a/hooks.d/termtitle b/hooks.d/termtitle new file mode 100644 index 0000000..abf8e54 --- /dev/null +++ b/hooks.d/termtitle @@ -0,0 +1,6 @@ +## set terminal title +termtitle () { + setTermTitle "($(whoami)@$(hostname) $(pwd))" +} + +## vim: ft=zsh diff --git a/zshrc b/zshrc index 26c8340..323d4b0 100644 --- a/zshrc +++ b/zshrc @@ -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