zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Karel Balej <karelb@gimli.ms.mff.cuni.cz>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: man completion
Date: Mon, 16 May 2022 12:41:15 -0700	[thread overview]
Message-ID: <CAH+w=7Yt84uC0PVNvjoXjh3apmYMgvXNYeSUdqPcg9vaZ4DEKA@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7YMj_TnQ5711ZNDZSqPeYyV+4OuwgmtHFmj8qsroDT9BA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 794 bytes --]

On Sun, May 15, 2022 at 9:45 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> The completion function is calling $(manpath 2>/dev/null), but it's
> caching the result, so if you change $MANPATH that's not being picked
> up.
>
> It also clobbers the cache if called with the -M or -m options and
> doesn't reset it, which is clearly wrong.

Here's a patch that attempts to (1) skip the cache entirely if the -M
option appears and (2) assure a cache miss if $MANPATH has changed.

The question is whether it's worth this much effort to avoid running
$(manpath 2>/dev/null) every time.

Also ... I didn't change this part, but doing ${(s.:.)...} twice seems
redundant or even potentially wrong?

+      mp=( ${(s.:.)$(manpath 2>/dev/null)} )
+      [[ "$mp" == *:* ]] && mp=( ${(s.:.)mp} )

[-- Attachment #2: _man.txt --]
[-- Type: text/plain, Size: 1922 bytes --]

diff --git a/Completion/Unix/Command/_man b/Completion/Unix/Command/_man
index dba1d13dc..22fd348fa 100644
--- a/Completion/Unix/Command/_man
+++ b/Completion/Unix/Command/_man
@@ -16,7 +16,7 @@
 _man() {
   local dirs expl mrd awk variant noinsert
   local -a context line state state_descr args modes
-  local -aU sects
+  local -aU sects _manpath
   local -A opt_args val_args sect_descs
 
   if [[ $service == man ]]; then
@@ -168,29 +168,36 @@ _man() {
   _arguments -s -S : $args '*::: :->man' && return 0
   [[ -n $state ]] || return 1
 
-  if (( ! $#_manpath )); then
-    local mp
-    mp=( ${(s.:.)$(manpath 2>/dev/null)} )
-    [[ "$mp" == *:* ]] && mp=( ${(s.:.)mp} )
-    if (( $#mp )); then
-      _manpath=( $mp )
-    elif (( $#manpath )); then
-      _manpath=( $manpath )
-    fi
-  fi
-
-  (( $#_manpath )) ||
-      _manpath=( /usr/man(-/) /(opt|usr)/(pkg|dt|share|X11R6|local)/(cat|)man(-/) )
-
   # Override man path
   [[ -n ${opt_args[-M]} ]] &&
   _manpath=( ${(s<:>)opt_args[-M]} )
 
+  # Restore cached man path to avoid $(manpath) if we can
+  if (( ! $#_manpath )); then
+    if (( ! $+_manpath_cache )); then
+      typeset -gHA _manpath_cache
+    fi
+    if [[ -z $_manpath_cache[$MANPATH] ]]; then
+      local mp
+      mp=( ${(s.:.)$(manpath 2>/dev/null)} )
+      [[ "$mp" == *:* ]] && mp=( ${(s.:.)mp} )
+      if (( $#mp )); then
+        _manpath_cache[$MANPATH]=${(j.:.)mp}
+      elif (( $#manpath )); then
+        _manpath_cache[$MANPATH]=$MANPATH
+      fi
+    fi
+    _manpath=( ${(s.:.)_manpath_cache[$MANPATH]} )
+  fi
+
   # Augment man path
   [[ $variant == (netbsd|openbsd)* ]] &&
   [[ -n ${opt_args[-m]} ]] &&
   _manpath+=( ${(s<:>)opt_args[-m]} )
 
+  (( $#_manpath )) ||
+      _manpath=( /usr/man(-/) /(opt|usr)/(pkg|dt|share|X11R6|local)/(cat|)man(-/) )
+
   # `sman' is the SGML manual directory for Solaris 7.
   # 1M is system administrator commands on SVR4
 

  reply	other threads:[~2022-05-16 19:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-15 15:20 Karel Balej
2022-05-15 16:45 ` Bart Schaefer
2022-05-16 19:41   ` Bart Schaefer [this message]
2022-05-16 21:40     ` Mikael Magnusson
2022-05-17  0:41     ` Bart Schaefer
2022-05-21 12:57       ` Karel Balej
2022-05-21 20:22         ` Bart Schaefer
2022-05-22 16:00           ` Karel Balej
2022-05-22 21:26             ` Bart Schaefer
2022-05-23 12:28               ` Karel Balej

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7Yt84uC0PVNvjoXjh3apmYMgvXNYeSUdqPcg9vaZ4DEKA@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=karelb@gimli.ms.mff.cuni.cz \
    --cc=zsh-workers@zsh.org \
    --subject='Re: man completion' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this 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).