zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: completion for perldoc
@ 1999-09-05 21:38 Adam Spiers
  0 siblings, 0 replies; 12+ messages in thread
From: Adam Spiers @ 1999-09-05 21:38 UTC (permalink / raw)
  To: zsh workers mailing list

Here's a completion system for perldoc.  It's my first stab at
completions with the new system, so be warned: you may need to change
some glaring mistakes.  If it's very wrong, let me know; OTOH if it's
OKish then maybe there's more where that came from.

(Completion/User is the right place, right?)

Oh, and the zle -R bit doesn't work: it displays the text and then
immediately erases it.  No doubt this is some misunderstanding on my
part.


diff -urN zsh-3.1.6-clean/Completion/User/_perl_basepods zsh-3.1.6-adam/Completion/User/_perl_basepods
--- zsh-3.1.6-clean/Completion/User/_perl_basepods	Thu Jan  1 01:00:00 1970
+++ zsh-3.1.6-adam/Completion/User/_perl_basepods	Sun Sep  5 22:24:50 1999
@@ -0,0 +1,30 @@
+#autoload
+#
+# _perl_basepods - zsh completion function
+#
+# Adam Spiers <adam@spiers.net>
+#
+# Calculate all installed Perl base pods (perlfunc, perlfaq etc.).
+# The result is cached for future use.
+#
+
+if [[ ${+_perl_basepods} -eq 0 ]]; then
+  typeset -agU _perl_basepods
+
+  if which basdepods >/dev/null; then
+    _perl_basepods=( ${$(basepods):t:r} )
+  else
+    local podpath
+    podpath=$(perl -MConfig -e 'print "$Config{installprivlib}/pod"')
+    if [[ ! -e $podpath/perl.pod ]]; then
+      echo "Couldn't find perl.pod from Config.pm; giving up."
+      return 1
+    else
+      cd $podpath
+      _perl_basepods=( *.pod(:r:t) )
+      cd $OLDPWD
+    fi
+  fi
+fi
+
+compadd - $_perl_basepods
diff -urN zsh-3.1.6-clean/Completion/User/_perl_builtin_funcs zsh-3.1.6-adam/Completion/User/_perl_builtin_funcs
--- zsh-3.1.6-clean/Completion/User/_perl_builtin_funcs	Thu Jan  1 01:00:00 1970
+++ zsh-3.1.6-adam/Completion/User/_perl_builtin_funcs	Sun Sep  5 22:24:50 1999
@@ -0,0 +1,29 @@
+#autoload
+#
+# _perl_builtin_funcs - zsh completion function
+#
+# Adam Spiers <adam@spiers.net>
+#
+# Calculate all built-in Perl functions.  The result is cached
+# for future use.
+#
+
+if [[ ${+_perl_builtin_funcs} -eq 0 ]]; then
+  typeset -agU _perl_builtin_funcs
+  local perlfunc
+
+  if perlfunc=`man -w perlfunc 2>&1`; then
+    _perl_builtin_funcs=( `perl -lne '
+                             $in_funcs++, next if /Alphabetical/;     \
+                             next unless $in_funcs;                   \
+                             if (/^\.Ip "(\w+)/) {                    \
+                               print $1 unless $func{$1}; $func{$1}++ \
+                             }' $perlfunc`
+               )
+  else
+    echo "Couldn't find perlfunc man page; giving up."
+    return 1
+  fi
+fi
+
+compadd - $_perl_builtin_funcs
diff -urN zsh-3.1.6-clean/Completion/User/_perl_modules zsh-3.1.6-adam/Completion/User/_perl_modules
--- zsh-3.1.6-clean/Completion/User/_perl_modules	Thu Jan  1 01:00:00 1970
+++ zsh-3.1.6-adam/Completion/User/_perl_modules	Sun Sep  5 22:24:50 1999
@@ -0,0 +1,47 @@
+#compdef pmpath pmvers pmdesc pmload pmexp pmeth pmls pmcat pman pmfunc podgrep podtoc podpath
+#
+#
+# _perl_modules - zsh completion function
+#
+# Adam Spiers <adam@spiers.net>
+#
+# Calculate all installed Perl modules.  The result is cached
+# for future use.
+#
+# Bugs:
+#   - can't cope with multiple installs of Perl
+
+# Change this if you have pminst and want to use it.  The zsh code
+# actually produces better results because pminst misses modules of
+# the form Foo/bar/Baz.pm through its clumsy -d && /^[A-Z]/ && prune
+# algorithm (the zsh code does almost the same, but only misses modules
+# which don't begin with an uppercase letter).
+local try_to_use_pminst=0
+
+if [[ ${+_perl_modules} -eq 0 ]]; then
+  if [[ $try_to_use_pminst -ne 0 ]] && which pminst >/dev/null; then
+    _perl_modules=( $(pminst) )
+  else
+    local inc libdir new_pms
+    inc=( $( perl -e 'print "@INC"' ) )
+    typeset -agU _perl_modules	# _perl_modules is global, no duplicates
+    _perl_modules=( )
+
+    for libdir in $inc; do
+        # Ignore cwd - could be too expensive e.g. if we're near /
+        if [[ $libdir == '.' ]]; then break; fi
+
+	# Find all modules
+	cd $libdir
+        new_pms=( {[A-Z]*/**/,}*.pm(N) )
+	cd $OLDPWD
+
+	# Convert to Perl nomenclature
+	new_pms=( ${new_pms:r:fs#/#::#} )
+
+        _perl_modules=( $new_pms $_perl_modules )
+    done
+  fi
+fi
+
+compadd - $_perl_modules
diff -urN zsh-3.1.6-clean/Completion/User/_perldoc zsh-3.1.6-adam/Completion/User/_perldoc
--- zsh-3.1.6-clean/Completion/User/_perldoc	Thu Jan  1 01:00:00 1970
+++ zsh-3.1.6-adam/Completion/User/_perldoc	Sun Sep  5 22:31:18 1999
@@ -0,0 +1,23 @@
+#compdef perldoc
+#
+#
+# _perldoc - zsh completion function for perldoc
+#
+# Adam Spiers <adam@spiers.net>
+#
+# Behaviour should be roughly equivalent to:
+# compctl -k perl_modules -k perl_basepods -f 
+#           -x 'c[-1,-f]' -k perl_funcs --
+#	  + -k man_pages
+#     perldoc
+
+if [[ $CURRENT -eq 3 && $words[2] == '-f' ]]; then
+  _perl_builtin_funcs
+elif [[ $CURRENT -eq 3 && $words[2] == '-q' ]]; then
+  zle -R "I can't read your mind!"
+else
+  _perl_modules
+  _perl_basepods
+  _path_files -/ -g '*.(pod|pm)'
+fi
+


^ permalink raw reply	[flat|nested] 12+ messages in thread
* Re: PATCH: completion for perldoc
@ 1999-09-06  9:28 Sven Wischnowsky
  1999-09-06 10:07 ` Adam Spiers
  0 siblings, 1 reply; 12+ messages in thread
From: Sven Wischnowsky @ 1999-09-06  9:28 UTC (permalink / raw)
  To: zsh-workers


Adam Spiers wrote:

> Here's a completion system for perldoc.  It's my first stab at
> completions with the new system, so be warned: you may need to change
> some glaring mistakes.  If it's very wrong, let me know; OTOH if it's
> OKish then maybe there's more where that came from.

Every attempt to write stuff for the new completion system is OKish.

> (Completion/User is the right place, right?)

Yes, but we somehow seem to have agreed (or `most of us...') to put
everything for one command or suite of command into one function.

> Oh, and the zle -R bit doesn't work: it displays the text and then
> immediately erases it.  No doubt this is some misunderstanding on my
> part.

`zle -R' is intended for interactive functions (normal widgets). If
you just want to display a message, use the function `_message'.

You may also want to have a look at `Etc/zsh-development-guide' and
`Etc/Completion-style-guide' (the latter is not at all complete --
I'll be thankful for every comment that helps improve it).

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 12+ messages in thread
* Re: PATCH: completion for perldoc
@ 1999-09-06 11:37 Sven Wischnowsky
  1999-09-06 18:18 ` Adam Spiers
  0 siblings, 1 reply; 12+ messages in thread
From: Sven Wischnowsky @ 1999-09-06 11:37 UTC (permalink / raw)
  To: zsh-workers


Adam Spiers wrote:

> How would you define a suite of command exactly?  I assumed it would
> be a function whose #compdef top line had many commands on it.  My
> _perl* stuff has a #compdef line for the pm* commands from pmtools,
> and a #compdef for perldoc, but I couldn't figure out how to squash
> them all into one file.

Well, you can always compare `$words[1]' in an `if' or `case' and then 
put the stuff from the #autoloaded functions in the same file and
execute it only if some parameter was set by the tests at the
beginning or something like that.

> I noticed Etc/Completion-style-guide soon after posting and realised
> the many problems with that _perl* :-(

Never mind. All this isn't fixed in stone yet and the `style-guide' is 
quite new.

Bye
 Sven

P.S.:   And, after all, we are all still learning what is possible with
        the function system.
P.P.S.: The function system started as an attempt to make the syntax
        easier to understand. And now we have a completion function
	that explains what it does by giving an equivalent `compctl' ;-)

--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 12+ messages in thread
* Re: PATCH: completion for perldoc
@ 1999-09-07  8:32 Sven Wischnowsky
  1999-09-07  9:49 ` Adam Spiers
  0 siblings, 1 reply; 12+ messages in thread
From: Sven Wischnowsky @ 1999-09-07  8:32 UTC (permalink / raw)
  To: zsh-workers


Adam Spiers wrote:

> Sven Wischnowsky (wischnow@informatik.hu-berlin.de) wrote:
> > Adam Spiers wrote:
> > > I noticed Etc/Completion-style-guide soon after posting and realised
> > > the many problems with that _perl* :-(
> > 
> > Never mind. All this isn't fixed in stone yet and the `style-guide' is 
> > quite new.
> 
> I've been uploading the style-guide and the _arguments documentation
> to my brain, and improved versions are on the way.  _arguments is
> great!  However a few things perplex me:
> 
> - Why can't you have multiple MESSAGE:ACTION pairs with the
>   *:MESSAGE:ACTION form?  I'm having to resort to using an auxiliary
>   function, e.g.:

There three forms that allow you to specify completion for
(non-option-) arguments:

  `num:mesg:action'
    specify completion for the num'th argument

  `:mesg:action'
    specify completion for the next argument (this is like the first
    one with `_argument' filling in the number automatically, for the
    cases where you just specify the completions for all arguments in
    the normal order one after another)

   `*:mesg:action'
     specify completion for `all other' arguments

These can be combined, of course:

  _arguments ':foo:(1 2)' '*:bar:(3 4)'

    completes `(1 2)' for the first argument and `(3 4)' for all other 
    arguments

  _arguments '2:foo:(1 2)' '*:bar:(3 4)'

    completes `(1 2)' for the *second* argument and `(3 4)' for all other 
    arguments


> - Is it possible to do something like (-foo)*:MESSAGE:ACTION, i.e.
>   specifying completion for normal args only if -foo hasn't been
>   specified?

There is no direct support for this. I'm not sure what you really want 
to have, but I think (or: hope) you can do it with:

  `-foo:*:mesg1:action1' '*:mesg2:action2'

    which makes *all* arguments after the option `-foo' be completed
    with `action1' (if that is empty, nothing will be completed, of
    course), and if `-foo' is not given, the second specification will 
    be used

This can then be used together with some trickery for `action1' to
achieve almost everything. If that is not enough or too clumsy to use, 
I'd like to see the real world example that made you wish for this.
And then I'll think about ways to make this easier.

Ok?

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 12+ messages in thread
* Re: PATCH: completion for perldoc
@ 1999-09-07  8:59 Sven Wischnowsky
  0 siblings, 0 replies; 12+ messages in thread
From: Sven Wischnowsky @ 1999-09-07  8:59 UTC (permalink / raw)
  To: zsh-workers


Adam Spiers wrote:

> Adam Spiers (adam@thelonious.new.ox.ac.uk) wrote:
> > _perldoc {
> >     _arguments -s \
> >         '-h:help:' \
> >         '(-q)-f:Perl built-in function:_perl_builtin_funcs' \
> >         '(-f)-q:Perl FAQ keyword:' \
> >         '*:Perl pod pages:_perl_pods'
> > }
> > 
> > _perl_pods {
> >     local nm="$compstate[nmatches]"
> > 
> >    _perl_modules
> >    _perl_basepods
> >    _path_files -J "Perl modules and .pods" -/ -g '*.(pod|pm)'
> > 
> >     [[ nm -ne compstate[nmatches] ]]
> > }
> 
> I forgot to say: this kills zsh :-(
>
> ...
> 
> If you need any more debugging info, let me know.  (Why are all the
> parameter lists empty?)

Yes I need more, namely the exact command line where you tried
completion. I've tested `perldoc' with completion after `-f', `-q',
and in a normal argument position and all this seems to work for me,
but I had to change `_perl_basepods' to use `whence -p basepod'
instead of `which ...' because I don't have `basepods'. I also had to
change `_perl_builtin_funcs' to use `command man ...' because I happen 
to have a function called `man'.

About the argument lists: did you really compile the beast with
debugging? (`-ggdb' in your Makefile, or at least `-g'?)

Otherwise the backtrace seems very fishy, because `comp_setunset'
doesn't call `runshfunc'.

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


^ permalink raw reply	[flat|nested] 12+ messages in thread
* Re: PATCH: completion for perldoc
@ 1999-09-07  9:02 Sven Wischnowsky
  0 siblings, 0 replies; 12+ messages in thread
From: Sven Wischnowsky @ 1999-09-07  9:02 UTC (permalink / raw)
  To: zsh-workers


Peter Stephenson wrote:

> Adam Spiers wrote:
> > > _perldoc {
> > > ...
> > 
> > I forgot to say: this kills zsh :-(
> 
> There's no () after _perldoc, so it thinks it's a function call, not a
> definition.  You just generate hundreds of recursive calls to _perldoc
> until the machine gets board of having enough space on the stack, or
> something.

Ah, was that it? I thought he had just copied the bodies from
somewhere and added the `perldoc {' by hand.

So, just forget the end of my last message, please.

> The may be a bug here.  Has anyone the slightest idea why `_perldoc {'
> is accepted as a complete command line with the argument "{"?

Dunno, but `ksh' and `bash' behave in the same way.

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


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

end of thread, other threads:[~1999-09-07  9:49 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-09-05 21:38 PATCH: completion for perldoc Adam Spiers
1999-09-06  9:28 Sven Wischnowsky
1999-09-06 10:07 ` Adam Spiers
1999-09-06 11:37 Sven Wischnowsky
1999-09-06 18:18 ` Adam Spiers
1999-09-06 18:21   ` Adam Spiers
1999-09-07  8:57     ` Peter Stephenson
1999-09-07  9:18       ` Adam Spiers
1999-09-07  8:32 Sven Wischnowsky
1999-09-07  9:49 ` Adam Spiers
1999-09-07  8:59 Sven Wischnowsky
1999-09-07  9:02 Sven Wischnowsky

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).