zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: (0/4)
@ 2011-03-30 20:37 Frank Terbeck
  2011-03-30 20:37 ` PATCH: (1/4) vcs_info: Support registering hooks independent of the context Frank Terbeck
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Frank Terbeck @ 2011-03-30 20:37 UTC (permalink / raw)
  To: zsh-workers

Here's a series I had laying around for a bit.

I was writing a little add-on to vcs_info and was wondering how I could
add hooks to certain events to make the add-on work, but without
interfering with hooks the user may have set already.

It turned out that I couldn't so I added "statically" registered hooks,
which are run independently of any context. This is what the first two
patches do.

The third patch adds a new hooks called no-vcs, which is needed by
my little add-on.

The last patch should make sure the nvcsformats style is used to fill
the $vcs_info_msg_N_ variables even if the system is disabled otherwise.
That should make sure that you can turn over full control of your
prompt to these variables, if you'd like to (as advertised in the
manual).

Frank Terbeck (4):
  vcs_info: Support registering hooks independent of the context
  vcs_info: Add functions to add/remove static hooks
  vcs_info: Add `no-vcs' hook
  vcs_info: nvcsformats style should be used if the system is disabled

 Doc/Zsh/contrib.yo                  |   43 ++++++++++++++++++++++++++++----
 Functions/VCS_Info/.distfiles       |    2 +
 Functions/VCS_Info/VCS_INFO_hook    |   18 +++++++++++--
 Functions/VCS_Info/VCS_INFO_set     |    6 +----
 Functions/VCS_Info/vcs_info         |    7 ++++-
 Functions/VCS_Info/vcs_info_hookadd |   22 +++++++++++++++++
 Functions/VCS_Info/vcs_info_hookdel |   45 +++++++++++++++++++++++++++++++++++
 7 files changed, 127 insertions(+), 16 deletions(-)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* PATCH: (1/4) vcs_info: Support registering hooks independent of the context
  2011-03-30 20:37 PATCH: (0/4) Frank Terbeck
@ 2011-03-30 20:37 ` Frank Terbeck
  2011-03-30 20:37 ` PATCH: (2/4) vcs_info: Add functions to add/remove static hooks Frank Terbeck
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Frank Terbeck @ 2011-03-30 20:37 UTC (permalink / raw)
  To: zsh-workers

---
 Functions/VCS_Info/VCS_INFO_hook |   18 +++++++++++++++---
 1 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/Functions/VCS_Info/VCS_INFO_hook b/Functions/VCS_Info/VCS_INFO_hook
index 7274d72..479f596 100644
--- a/Functions/VCS_Info/VCS_INFO_hook
+++ b/Functions/VCS_Info/VCS_INFO_hook
@@ -2,24 +2,36 @@
 ## Written by Frank Terbeck <ft@bewatermyfriend.org>
 ## Distributed under the same BSD-ish license as zsh itself.
 
-local hook func
+local hook static func
 local -x context hook_name
 local -xi ret
-local -a hooks
+local -a hooks tmp
 local -i debug
 
 ret=0
 hook_name="$1"
 shift
 context=":vcs_info:${vcs}+${hook_name}:${usercontext}:${rrn}"
+static=":vcs_info-static_hooks:${hook_name}"
 
 zstyle -t "${context}" debug && debug=1 || debug=0
 if (( debug )); then
     printf 'VCS_INFO_hook: running hook: "%s"\n' "${hook_name}"
     printf 'VCS_INFO_hook: current context: "%s"\n' "${context}"
+    printf 'VCS_INFO_hook: static context: "%s"\n' "${static}"
 fi
 
-zstyle -a "${context}" hooks hooks || return 0
+zstyle -a "${static}" hooks hooks
+if (( debug )); then
+    printf '+ static hooks: %s\n' "${(j:, :)hooks}"
+fi
+zstyle -a "${context}" hooks tmp
+if (( debug )); then
+    printf '+ context hooks: %s\n' "${(j:, :)tmp}"
+fi
+hooks+=( "${tmp[@]}" )
+(( ${#hooks} == 0 )) && return 0
+
 # Protect some internal variables in hooks. The `-g' parameter to
 # typeset does *not* make the parameters global here (they are already
 # "*-local-export). It prevents typeset from creating *new* *local*
-- 
1.7.4.1.140.g89781


^ permalink raw reply	[flat|nested] 5+ messages in thread

* PATCH: (2/4) vcs_info: Add functions to add/remove static hooks
  2011-03-30 20:37 PATCH: (0/4) Frank Terbeck
  2011-03-30 20:37 ` PATCH: (1/4) vcs_info: Support registering hooks independent of the context Frank Terbeck
@ 2011-03-30 20:37 ` Frank Terbeck
  2011-03-30 20:37 ` PATCH: (3/4) vcs_info: Add `no-vcs' hook Frank Terbeck
  2011-03-30 20:37 ` PATCH: (4/4) vcs_info: nvcsformats style should be used if the system is disabled Frank Terbeck
  3 siblings, 0 replies; 5+ messages in thread
From: Frank Terbeck @ 2011-03-30 20:37 UTC (permalink / raw)
  To: zsh-workers

---
 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@bewatermyfriend.org>
+## 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@bewatermyfriend.org>
+## 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* PATCH: (3/4) vcs_info: Add `no-vcs' hook
  2011-03-30 20:37 PATCH: (0/4) Frank Terbeck
  2011-03-30 20:37 ` PATCH: (1/4) vcs_info: Support registering hooks independent of the context Frank Terbeck
  2011-03-30 20:37 ` PATCH: (2/4) vcs_info: Add functions to add/remove static hooks Frank Terbeck
@ 2011-03-30 20:37 ` Frank Terbeck
  2011-03-30 20:37 ` PATCH: (4/4) vcs_info: nvcsformats style should be used if the system is disabled Frank Terbeck
  3 siblings, 0 replies; 5+ messages in thread
From: Frank Terbeck @ 2011-03-30 20:37 UTC (permalink / raw)
  To: zsh-workers

---
 Doc/Zsh/contrib.yo              |    5 +++++
 Functions/VCS_Info/VCS_INFO_set |    1 +
 2 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index 5c82c5d..bc4a814 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -1307,6 +1307,11 @@ When setting tt(ret) to non-zero, the string in
 tt(${hook_com[guards-string]}) will be used in the var(%g) escape in the
 tt(patch-format) and tt(nopatch-format) styles.
 )
+item(tt(no-vcs))(
+This hooks is called when no version control system was detected.
+
+The `hook_com' parameter is not used.
+)
 item(tt(post-quilt))(
 Called after the tt(quilt) support is done. The following information
 is passed as arguments to the hook: 1. the quilt-support mode (`addon' or
diff --git a/Functions/VCS_Info/VCS_INFO_set b/Functions/VCS_Info/VCS_INFO_set
index a2b838c..23dc06b 100644
--- a/Functions/VCS_Info/VCS_INFO_set
+++ b/Functions/VCS_Info/VCS_INFO_set
@@ -16,6 +16,7 @@ if [[ $1 == '--nvcs' ]] ; then
         typeset -gx vcs_info_msg_${i}_=
     done
     VCS_INFO_nvcsformats $2
+    [[ $2 != '-preinit-' ]] && VCS_INFO_hook "no-vcs"
 fi
 
 (( ${#msgs} - 1 < 0 )) && return 0
-- 
1.7.4.1.140.g89781


^ permalink raw reply	[flat|nested] 5+ messages in thread

* PATCH: (4/4) vcs_info: nvcsformats style should be used if the system is disabled
  2011-03-30 20:37 PATCH: (0/4) Frank Terbeck
                   ` (2 preceding siblings ...)
  2011-03-30 20:37 ` PATCH: (3/4) vcs_info: Add `no-vcs' hook Frank Terbeck
@ 2011-03-30 20:37 ` Frank Terbeck
  3 siblings, 0 replies; 5+ messages in thread
From: Frank Terbeck @ 2011-03-30 20:37 UTC (permalink / raw)
  To: zsh-workers

I was actually wondering, that this was not the case yet...
---
 Doc/Zsh/contrib.yo              |   10 +++++-----
 Functions/VCS_Info/VCS_INFO_set |    5 -----
 Functions/VCS_Info/vcs_info     |    5 +++--
 3 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index bc4a814..476e7ca 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -756,11 +756,11 @@ revision number. This style lets you modify how that string should look.
 )
 kindex(nvcsformats)
 item(tt(nvcsformats))(
-These "formats" are exported when we didn't detect
-a version control system for the current directory. This is useful if you
-want var(vcs_info) to completely take over the generation of your prompt.
-You would do something like tt(PS1='${vcs_info_msg_0_}') to accomplish
-that.
+These "formats" are exported when we didn't detect a version control system
+for the current directory or var(vcs_info) was disabled. This is useful if
+you want var(vcs_info) to completely take over the generation of your
+prompt. You would do something like tt(PS1='${vcs_info_msg_0_}') to
+accomplish that.
 )
 kindex(hgrevformat)
 item(tt(hgrevformat))(
diff --git a/Functions/VCS_Info/VCS_INFO_set b/Functions/VCS_Info/VCS_INFO_set
index 23dc06b..5087be4 100644
--- a/Functions/VCS_Info/VCS_INFO_set
+++ b/Functions/VCS_Info/VCS_INFO_set
@@ -5,11 +5,6 @@
 setopt localoptions noksharrays NO_shwordsplit
 local -i i j
 
-if [[ $1 == '--clear' ]] ; then
-    for i in {0..9} ; do
-        unset vcs_info_msg_${i}_
-    done
-fi
 if [[ $1 == '--nvcs' ]] ; then
     [[ $2 == '-preinit-' ]] && (( maxexports == 0 )) && (( maxexports = 1 ))
     for i in {0..$((maxexports - 1))} ; do
diff --git a/Functions/VCS_Info/vcs_info b/Functions/VCS_Info/vcs_info
index 385a451..513489b 100644
--- a/Functions/VCS_Info/vcs_info
+++ b/Functions/VCS_Info/vcs_info
@@ -37,6 +37,7 @@ for func in ${static_functions} ; do
     autoload -Uz ${func}
 done
 
+[[ -n ${(Mk)parameters:#vcs_info_msg_<->_} ]] && unset ${parameters[(I)vcs_info_msg_<->_]}
 VCS_INFO_set --nvcs '-preinit-'
 vcs_info_setsys
 
@@ -77,7 +78,7 @@ vcs_info () {
     (( ${#enabled} == 0 )) && enabled=( all )
 
     if [[ -n ${(M)enabled:#(#i)none} ]] ; then
-        [[ -n ${vcs_info_msg_0_} ]] && VCS_INFO_set --clear
+        [[ -n ${vcs_info_msg_0_} ]] && VCS_INFO_set --nvcs
         return 0
     fi
 
@@ -90,7 +91,7 @@ vcs_info () {
 
     for pat in ${dps} ; do
         if [[ ${PWD} == ${~pat} ]] ; then
-            [[ -n ${vcs_info_msg_0_} ]] && VCS_INFO_set --clear
+            [[ -n ${vcs_info_msg_0_} ]] && VCS_INFO_set --nvcs
             return 0
         fi
     done
-- 
1.7.4.1.140.g89781


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-03-30 21:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-30 20:37 PATCH: (0/4) Frank Terbeck
2011-03-30 20:37 ` PATCH: (1/4) vcs_info: Support registering hooks independent of the context Frank Terbeck
2011-03-30 20:37 ` PATCH: (2/4) vcs_info: Add functions to add/remove static hooks Frank Terbeck
2011-03-30 20:37 ` PATCH: (3/4) vcs_info: Add `no-vcs' hook Frank Terbeck
2011-03-30 20:37 ` PATCH: (4/4) vcs_info: nvcsformats style should be used if the system is disabled Frank Terbeck

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).