Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: (2/4) vcs_info: Add functions to add/remove static hooks



---
 Doc/Zsh/contrib.yo                  |   28 +++++++++++++++++++++-
 Functions/VCS_Info/.distfiles       |    2 +
 Functions/VCS_Info/vcs_info         |    2 +
 Functions/VCS_Info/vcs_info_hookadd |   22 +++++++++++++++++
 Functions/VCS_Info/vcs_info_hookdel |   45 +++++++++++++++++++++++++++++++++++
 5 files changed, 98 insertions(+), 1 deletions(-)
 create mode 100644 Functions/VCS_Info/vcs_info_hookadd
 create mode 100644 Functions/VCS_Info/vcs_info_hookdel

diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index 81167bf..5c82c5d 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -1117,6 +1117,24 @@ tt(Variable description) below). If an argument is given, that string will be
 used instead of tt(default) in the tt(user-context) field of the style
 context.
 )
+findex(vcs_info_hookadd)
+item(tt(vcs_info_hookadd))(
+Statically registers a number of functions to a given hook. The hook needs
+to be given as the first argument; what follows is a list of hook-function
+names to register to the hook. The `tt(+vi-)' prefix needs to be left out
+here. See tt(Hooks in vcs_info) below for details.
+)
+findex(vcs_info_hookdel)
+item(tt(vcs_info_hookdel))(
+Remove hook-functions from a given hook. The hook needs to be given as the
+first non-option argument; what follows is a list of hook-function
+names to un-register from the hook. If `tt(-a)' is used as the first
+argument, tt(all) occurances of the functions are unregistered. Otherwise
+only the last occurance is removed (if a function was registered to a hook
+more than once) . The `tt(+vi-)' prefix needs to be left out here. See
+tt(Hooks in vcs_info) below for details.
+)
+findex(vcs_info_lastmsg)
 item(tt(vcs_info_lastmsg))(
 Outputs the last var(${vcs_info_msg_*_}) value.
 Takes into account the value of the tt(use-prompt-escapes) style in
@@ -1129,6 +1147,7 @@ Prints a list of all
 supported version control systems. Useful to find out possible contexts
 (and which of them are enabled) or values for the var(disable) style.
 )
+findex(vcs_info_setsys)
 item(tt(vcs_info_setsys))(
 Initializes var(vcs_info)'s internal list of
 available backends. With this function, you can add support for new VCSs
@@ -1176,13 +1195,20 @@ avoid namespace problems, all registered function names are prepended by
 a `+vi-', so the actual functions called for the `foo' hook are
 `tt(+vi-bar)' and `tt(+vi-baz)'.
 
+If you would like to register a function to a hook regardless of the
+current context, you may use the var(vcs_info_hookadd) function. To remove
+a function that was added like that, the var(vcs_info_hookdel) function
+can be used.
+
 If something seems weird, you can enable the `debug' boolean style in
 the proper context and the hook-calling code will print what it tried
 to execute and whether the function in question existed.
 
 When you register more than one function to a hook, all functions are
 executed one after another until one function returns non-zero or until
-all functions have been called.
+all functions have been called. Context-sensitive hook functions are
+executed tt(before) statically registered ones (the ones added by
+var(vcs_info_hookadd)).
 
 You may pass data between functions via an associative array, tt(user_data).
 For example:
diff --git a/Functions/VCS_Info/.distfiles b/Functions/VCS_Info/.distfiles
index 988e7ad..b6e55d2 100644
--- a/Functions/VCS_Info/.distfiles
+++ b/Functions/VCS_Info/.distfiles
@@ -1,6 +1,8 @@
 DISTFILES_SRC='
 .distfiles
 vcs_info
+vcs_info_hookadd
+vcs_info_hookdel
 VCS_INFO_adjust
 VCS_INFO_bydir_detect
 VCS_INFO_check_com
diff --git a/Functions/VCS_Info/vcs_info b/Functions/VCS_Info/vcs_info
index 6ce1cd7..385a451 100644
--- a/Functions/VCS_Info/vcs_info
+++ b/Functions/VCS_Info/vcs_info
@@ -26,6 +26,8 @@ static_functions=(
     VCS_INFO_reposub
     VCS_INFO_set
 
+    vcs_info_hookadd
+    vcs_info_hookdel
     vcs_info_lastmsg
     vcs_info_printsys
     vcs_info_setsys
diff --git a/Functions/VCS_Info/vcs_info_hookadd b/Functions/VCS_Info/vcs_info_hookadd
new file mode 100644
index 0000000..867f7e2
--- /dev/null
+++ b/Functions/VCS_Info/vcs_info_hookadd
@@ -0,0 +1,22 @@
+## vim:ft=zsh
+## Written by Frank Terbeck <ft@xxxxxxxxxxxxxxxxxxx>
+## Distributed under the same BSD-ish license as zsh itself.
+
+emulate -L zsh
+setopt extendedglob
+
+if (( ${#argv} < 2 )); then
+    print 'usage: vcs_info_hookadd <HOOK> <FUNCTION(s)...>'
+    return 1
+fi
+
+local hook func context
+local -a old
+
+hook=$1
+shift
+context=":vcs_info-static_hooks:${hook}"
+
+zstyle -a "${context}" hooks old
+zstyle "${context}" hooks "${old[@]}" "$@"
+return $?
diff --git a/Functions/VCS_Info/vcs_info_hookdel b/Functions/VCS_Info/vcs_info_hookdel
new file mode 100644
index 0000000..e09a057
--- /dev/null
+++ b/Functions/VCS_Info/vcs_info_hookdel
@@ -0,0 +1,45 @@
+## vim:ft=zsh
+## Written by Frank Terbeck <ft@xxxxxxxxxxxxxxxxxxx>
+## Distributed under the same BSD-ish license as zsh itself.
+
+emulate -L zsh
+setopt extendedglob
+
+local -i all
+
+if [[ "x$1" == 'x-a' ]]; then
+    all=1
+    shift
+else
+    all=0
+fi
+
+if (( ${#argv} < 2 )); then
+    print 'usage: vcs_info_hookdel [-a] <HOOK> <FUNCTION(s)...>'
+    return 1
+fi
+
+local hook func context
+local -a old
+
+hook=$1
+shift
+context=":vcs_info-static_hooks:${hook}"
+
+zstyle -a "${context}" hooks old || return 0
+for func in "$@"; do
+    if [[ -n ${(M)old:#$func} ]]; then
+        old[(Re)$func]=()
+    else
+        printf 'Not statically registered to `%s'\'': "%s"\n' \
+            "${hook}" "${func}"
+        continue
+    fi
+    if (( all )); then
+        while [[ -n ${(M)old:#$func} ]]; do
+            old[(Re)$func]=()
+        done
+    fi
+done
+zstyle "${context}" hooks "${old[@]}"
+return $?
-- 
1.7.4.1.140.g89781



Messages sorted by: Reverse Date, Date, Thread, Author