zshrc: Add more robustness to completion caching setup [Closes: issue1933]
authorFrank Terbeck <ft@grml.org>
Fri, 9 Oct 2015 14:16:16 +0000 (16:16 +0200)
committerFrank Terbeck <ft@grml.org>
Fri, 9 Oct 2015 18:55:21 +0000 (20:55 +0200)
This is based on a suggestion by Thilo Six <tech@xk2c.de>.

The setup now uses $GRML_COMP_CACHING to decide on whether to configure
caching at all and $GRML_COMP_CACHE_DIR to tell where to put the caching
data.

It also makes sure the caching directory actually exists before using it.

See the included documentation for details.

Signed-off-by: Frank Terbeck <ft@grml.org>
doc/grmlzshrc.t2t
etc/zsh/zshrc

index ead2182..8accb74 100644 (file)
@@ -55,6 +55,15 @@ Deprecated. Use **GRML_DISPLAY_BATTERY** instead.
 A non zero value activates a handler, which is called when a command can not
 be found. The handler is defined by GRML_ZSH_CNF_HANDLER (see below).
 
+: **GRML_COMP_CACHING**
+If set to //yes// (the default), the setup will enable zsh's completion caching
+mechanism, with the caching data being placed into //$GRML_COMP_CACHE_DIR//.
+
+: **GRML_COMP_CACHE_DIR**
+This defines where zsh's completion caching data will be placed, if
+//$GRML_COMP_CACHING// is active. The default is //${ZDOTDIR:-$HOME}/.cache//.
+The setup will ensure the directory exists before attempting to use it.
+
 : **GRML_DISPLAY_BATTERY**
 If set to a value greater than zero, //grmlzshrc// will put the battery status
 into the right hand side interactive prompt. Supported OSes are //GNU/Linux//,
index a6f9670..80828e0 100644 (file)
@@ -805,9 +805,16 @@ grmlcomp() {
     # command for process lists, the local web server details and host completion
     zstyle ':completion:*:urls' local 'www' '/var/www/' 'public_html'
 
-    # caching
-    [[ -d $ZSHDIR/cache ]] && zstyle ':completion:*' use-cache yes && \
-                            zstyle ':completion::complete:*' cache-path $ZSHDIR/cache/
+    # Some functions, like _apt and _dpkg, are very slow. We can use a cache in
+    # order to speed things up
+    if [[ ${GRML_COMP_CACHING:-yes} == yes ]]; then
+        GRML_COMP_CACHE_DIR=${GRML_COMP_CACHE_DIR:-${ZDOTDIR:-$HOME}/.cache}
+        if [[ ! -d ${GRML_COMP_CACHE_DIR} ]]; then
+            command mkdir -p "${GRML_COMP_CACHE_DIR}"
+        fi
+        zstyle ':completion:*' use-cache  yes
+        zstyle ':completion:*:complete:*' cache-path "${GRML_COMP_CACHE_DIR}"
+    fi
 
     # host completion
     if is42 ; then