zsh-workers
 help / color / mirror / code / Atom feed
* User installation script for new completion
@ 1999-05-02 14:03 Peter Stephenson
  1999-05-02 14:56 ` Bruce Stephens
  1999-05-02 23:52 ` Bart Schaefer
  0 siblings, 2 replies; 9+ messages in thread
From: Peter Stephenson @ 1999-05-02 14:03 UTC (permalink / raw)
  To: Zsh hackers list

Here's a script to be run by a user who wants to use the new completion
system.  Note that it assumes that all the functions from under Completion/
are already available somewhere --- we need a set of Makefile.in's with
install targets to do that.

It should be considered work in progress, but I will include it with the
next pws-* release for people to have a go at.  Some notes about things
which need doing are included.

--- Completion/Core/compinstall.old	Sun May  2 15:47:53 1999
+++ Completion/Core/compinstall	Sun May  2 16:00:09 1999
@@ -0,0 +1,216 @@
+# This script is to be run by a user to setup the new function based
+# completion system.  The functions themselves are assumed to be already
+# available in some directory; they should have been installed with the
+# the shell (except we haven't written that yet).
+#
+# Run it as a script under zsh and answer the questions.
+# You can run it as `zsh compinstall $FPATH' and it will be able to check
+# your function path for the completion functions.
+#
+# Normally, this will alter ~/.zshrc (or wherever ZDOTDIR puts it),
+# but you can make that unwritable and it will leave the lines in a
+# temporary file instead.
+#
+# You can use this script to modify what compinstall previously
+# added to ~/.zshrc.
+#
+# It is safe to abort with ^C any time you are being prompted for
+# information; your .zshrc will not be altered.
+#
+# To do:
+#  - Maybe this should be sourced, then it can check the user's current
+#    setup better.  But then there is a potentially horrendous option
+#    setting/resetting problem.  (Maybe we need another way of doing that.)
+#  - Should probably offer to set different options for _approximate than
+#    for _complete if both are used.
+#  - Could add code for setting other completers and options.
+#  - Could add keys for context-sensitive help.
+#  - Probably should allow a set of directories to be added to $fpath,
+#    like Core, Base, etc.
+
+# In case a startup script changed options
+emulate zsh
+
+[[ -n $1 ]] && FPATH=$1
+
+for f in $fpath; do
+  if [[ $f != . && -f $f/compinit && -f $f/compdump ]]; then
+    fdir=$f
+    break
+  fi
+done
+
+if [[ -z $fdir ]]; then
+  print "Trying to find where the completion functions are..."
+  if [[ $0 = */* && -f $0:h/compinit && -f $0:h/compdump ]]; then
+    fdir=$0:h
+  else
+    # more guesses?
+    print \
+"Please edit the name of the directory where the completion functions are
+installed.  If they are not installed, you will need to find them in the
+Completion/* directories of the zsh distribution and install them yourself,
+or insult your system manager for incompetence."
+    vared -c fdir
+    while [[ ! -d ${~fdir} || ! -f ${~fdir}/compinit || 
+      ! -f ${~fdir}/compdump ]]; do
+      print "I can't find them in that directory.  Try again or abort."
+      vared fdir
+    done
+  fi
+  eval "fpath=($fdir \$fpath)"
+  fdir=${fdir/#$HOME/\~}
+  lines="fpath=($fdir \$fpath)\n"
+else
+  print "Found completion functions in your fpath, will not alter it."
+fi
+
+files=( ${^~fpath:/.}/_(|*[^~])(N:t) )
+if [[ $#files -lt 20 ]]; then
+  print "
+Hmmm, completion functions seem a bit thin on the ground.  There should
+be lots of files with names beginning with an underscore (_).  You should
+look and see what's happened to these.
+[Hit return to continue]"
+  read
+fi
+
+if [[ -w ${~fdir} && ( ! -f ${~fdir}/compinit.dump ||
+  -w ${~fdir}/compinit.dump ) ]]
+then
+  print "
+Using standard dumpfile
+  ${~fdir}/compinit.dump
+to speed up initialisation.
+[Hit return to continue]"
+  read
+else
+  print "
+I will force completion to dump its status, which will speed up the shell's
+start-up considerably.  However, I can't write the file I'd like to, namely
+$fdir/compinit.dump.  Please edit a replacement."
+  dumpfile='~/.compinit.dump'
+  vared dumpfile
+  while ! touch ${~dumpfile} >& /dev/null; do
+    print "Sorry, I can't write that either.  Try again."
+    vared dumpfile
+  done
+  [[ -s $dumpfile ]] || rm -f $dumpfile
+  dumpfile=" $dumpfile"
+fi
+
+fdir=${fdir/#$HOME/\~}
+
+lines="${lines}. $fdir/compinit -d$dumpfile\n"
+
+
+print "
+In addition to completion, zsh can also perform correction of the
+current word, or approximate completion, i.e. completion where the part of
+the word typed so far can be corrected; or it can try correction, then
+approximate completion if that fails.  Would you like:
+  0:  Just ordinary completion
+  C:  Correction
+  A:  Approximate completion
+  B:  Both?
+Please type one of the keys above:"
+while read -k type; do
+  print
+  case $type in
+    0*) completer=_complete
+	break
+	;;
+    [cC]*) completer=_complete:_correct
+	   break
+	   ;;
+    [aA]*) completer=_complete:_approximate
+	   break;
+	   ;;
+    [bB]*) completer=_complete:_correct:_approximate
+	   break
+	   ;;
+    *) print Try again
+       ;;
+  esac
+done
+
+lines="${lines}compconf completer=$completer"
+
+
+if [[ $completer = *(correct|approx)* ]]; then
+  print "
+Correction and approximation will normally allow up to two errors,
+and you will be able to use a numeric prefix (e.g. <Esc>4) to allow
+more.  The standard prompt is \`correct to:'. Do you want to change
+any of this? [n]"
+  if read -q; then
+    print "Number of errors to accept normally (0 is OK):"
+    read accept
+    while [[ $accept != <-> ]]; do
+      read accept"?Please enter a number: "
+    done
+    print \
+"How would you like the numeric prefix to be treated:
+  0:  Not used by correction
+  U:  Used to given the number of errors
+  I:  If present, and not 1, do not perform correction?
+Please type one of the keys above:"
+    while read -k type; do
+      print
+      case $type in
+	0*) break
+	    ;;
+	[uU]*) accept="${accept}n"
+	       break
+	       ;;
+	[Ii]*) accept="${accept}!n"
+	       break
+	       ;;
+	*) print Try again
+	   ;;
+      esac
+    done
+    lines="$lines \\\\
+  correct_accept='$accept'"
+    print "
+Instead of the prompt \`correct to:', you can have no prompt, or a
+prompt of your choosing which can display the number of errors found by
+containing the string \`%e'.  Do you wish to change the correction
+prompt? [n]"
+    if read -q; then
+      cprompt=''
+      print "Edit a new prompt (may be empty):"
+      vared cprompt
+      lines="$lines \\\\
+  correct_prompt='${cprompt//\'/\'\\\'\'}'"
+    fi
+  fi
+fi
+
+lines="$lines\n"
+
+
+startline='# The following lines were added by compinstall'
+endline='# End of lines added by compinstall'
+
+ifile=${ZDOTDIR:-~}/.zshrc
+[[ -f $ifile ]] || touch $ifile
+tmpf=${TMPPPREFIX:-/tmp/zsh}compinstall$$
+
+if [[ ! -w $ifile ]]; then
+  print "\nI can't write to $ifile.  I will leave the lines to add in
+\`$tmpf' and you must add them by hand."
+  print "\n$startline\n$lines\n$endline" >$tmpf
+  return 0
+fi
+
+if grep $endline $ifile >& /dev/null; then
+  print -- "$startline\n$lines$endline" >$tmpf
+  sed -e "/^$endline/r $tmpf
+/^$startline/,/^$endline/d" $ifile >${tmpf}2 && mv ${tmpf}2 $ifile &&
+  print "\nSuccesfully modified old compinstall lines in $ifile."
+  rm -f $tmpf ${tmpf}2
+else
+  print "\n$startline\n$lines\n$endline" >>$ifile &&
+  print "\nSuccessfully appended lines to $ifile."
+fi

-- 
Peter Stephenson <pws@ibmth.df.unipi.it>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy


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

* Re: User installation script for new completion
  1999-05-02 14:03 User installation script for new completion Peter Stephenson
@ 1999-05-02 14:56 ` Bruce Stephens
  1999-05-03 10:00   ` Peter Stephenson
  1999-05-02 23:52 ` Bart Schaefer
  1 sibling, 1 reply; 9+ messages in thread
From: Bruce Stephens @ 1999-05-02 14:56 UTC (permalink / raw)
  To: Zsh hackers list

Peter Stephenson <pws@ibmth.df.unipi.it> writes:

> Here's a script to be run by a user who wants to use the new
> completion system.  Note that it assumes that all the functions from
> under Completion/ are already available somewhere --- we need a set
> of Makefile.in's with install targets to do that.

Yes, and I should update RedHat's zsh.spec file too, to include all
this stuff.

> +#  - Probably should allow a set of directories to be added to $fpath,
> +#    like Core, Base, etc.

Yes, probably.  Couldn't compinit do this, though?  i.e., it could
look at the directory it's in for subdirectories, and add them?

I think I'd split this in to 2 scripts.  

One does a basic initialisation, adding the directory(ies) to fpath
and copying the scripts to ~/completion or whatever.  It can set the
various options to suitable defaults.

The other provides some convenient way for me to alter the fancier
options (like approximate completion and things).

Hmm, it's slightly more complex than that, I suppose, since I'd want
to rerun the first script each time I upgraded zsh to upgrade my
scripts, and I wouldn't want it to clobber other settings, but that's
probably not hard to handle.

Anyway, the idea is not to require innocent users from making choices
they can't reasonably be expected to make yet: it ought to be really
easy to get something that works reasonably, and subtleties should be
postponed if possible.


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

* Re: User installation script for new completion
  1999-05-02 14:03 User installation script for new completion Peter Stephenson
  1999-05-02 14:56 ` Bruce Stephens
@ 1999-05-02 23:52 ` Bart Schaefer
  1999-05-03  0:34   ` Bart Schaefer
  1 sibling, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 1999-05-02 23:52 UTC (permalink / raw)
  To: Peter Stephenson, Zsh hackers list

On May 2,  4:03pm, Peter Stephenson wrote:
} Subject: User installation script for new completion
}
} +# To do:
} +#  - Maybe this should be sourced, then it can check the user's current
} +#    setup better.  But then there is a potentially horrendous option
} +#    setting/resetting problem.  (Maybe we need another way of doing that.)

You don't mean this, do you?

	typeset -A options
	options=($(setopt kshoptionprint;setopt))
	[[ -o kshoptionprint ]] || options[kshoptionprint]=off
	[[ -o monitor ]] && options[monitor]=on
	[[ -o zle ]] && options[zle]=on

	for i in ${(k)options}
	do case $options[$i] in
	on) setopt $i;;
	off) unsetopt $i;;
	esac done

If you do mean that, then why not have the whole install script be a shell
function with "emulate -LR zsh" at the top?  The file that you source can
define the function and then call it.

} +"How would you like the numeric prefix to be treated:
} +  0:  Not used by correction
} +  U:  Used to given the number of errors
} +  I:  If present, and not 1, do not perform correction?

"Used to given the number of errors"??  "give"?  "govern"?  And what does
it do with "the number of errors" once given it?  (I know, but presumably
a person running this script probably doesn't.)

If I were to run compinstall, it would have no effect at all on most of
my shells, because my .zshrc exits early by calling 'return' if certain
settings are already present in the environment when it runs.  I don't
have any good suggestions for what to do about this, but I thought I'd
mention it anyway.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


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

* Re: User installation script for new completion
  1999-05-02 23:52 ` Bart Schaefer
@ 1999-05-03  0:34   ` Bart Schaefer
  1999-05-03  8:11     ` Peter Stephenson
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 1999-05-03  0:34 UTC (permalink / raw)
  To: Peter Stephenson, Zsh hackers list

On May 2,  4:52pm, Bart Schaefer wrote:
} Subject: Re: User installation script for new completion
}
} You don't mean this, do you?
} 
} 	typeset -A options
} 	options=($(setopt kshoptionprint;setopt))
} 	[[ -o kshoptionprint ]] || options[kshoptionprint]=off
} 	[[ -o monitor ]] && options[monitor]=on
} 	[[ -o zle ]] && options[zle]=on
} 
} 	for i in ${(k)options}
} 	do case $options[$i] in
} 	on) setopt $i;;
} 	off) unsetopt $i;;
} 	esac done

Even better:

	unsetopt ${(k)options[(R)off]} no${(k)^options[(R)on]}

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


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

* Re: User installation script for new completion
  1999-05-03  0:34   ` Bart Schaefer
@ 1999-05-03  8:11     ` Peter Stephenson
  0 siblings, 0 replies; 9+ messages in thread
From: Peter Stephenson @ 1999-05-03  8:11 UTC (permalink / raw)
  To: Zsh hackers list

"Bart Schaefer" wrote:
> } 	typeset -A options
> } 	options=($(setopt kshoptionprint;setopt))
> } 	[[ -o kshoptionprint ]] || options[kshoptionprint]=off
> } 	[[ -o monitor ]] && options[monitor]=on
> } 	[[ -o zle ]] && options[zle]=on
> 
> 	unsetopt ${(k)options[(R)off]} no${(k)^options[(R)on]}

Unfortunately that doesn't work because of the way options on by default
are printed with `no' in front anyway, in which case sticking another no in
front messes it up.  (Maybe your setopt is a function to get round it.)

However,

  unsetopt ${(k)options[(R)off]}
  setopt ${(k)options[(R)on]}

ought to do the trick.  But it doesn't; you get things like

  unsetopt: can't change option: singlecommand

which is misleading anyway.  Why don't we change it so that it doesn't
complain if you're setting an option to its existing value?

--- Src/options.c.so	Thu Mar 25 10:38:43 1999
+++ Src/options.c	Mon May  3 10:07:14 1999
@@ -635,6 +635,8 @@
 	}
     } else if(!force && (optno == INTERACTIVE || optno == SHINSTDIN ||
 	    optno == SINGLECOMMAND)) {
+	if (opts[optno] == value)
+	    return 0;
 	/* it is not permitted to change the value of these options */
 	return -1;
     } else if(!force && optno == USEZLE && value) {

-- 
Peter Stephenson <pws@ibmth.df.unipi.it>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy


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

* Re: User installation script for new completion
  1999-05-02 14:56 ` Bruce Stephens
@ 1999-05-03 10:00   ` Peter Stephenson
  1999-05-03 10:46     ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 1999-05-03 10:00 UTC (permalink / raw)
  To: Zsh hackers list

Bruce Stephens wrote:
> > +#  - Probably should allow a set of directories to be added to $fpath,
> > +#    like Core, Base, etc.
> 
> Yes, probably.  Couldn't compinit do this, though?  i.e., it could
> look at the directory it's in for subdirectories, and add them?

Not quite what you suggested, but it should simplify installation when I
modify compinstall:  compinit looks at how many completion files it has
found, and if there are fewer than twenty (that's about the number in the
Core directory) it adds the path to itself to fpath.  I haven't quite
worked out on what occasions it should look for subdirectories.

This fails when functionargzero is not set.  Great.  Any better
suggestions?

--- Completion/Core/compinit.old	Mon May  3 09:54:38 1999
+++ Completion/Core/compinit	Mon May  3 11:54:13 1999
@@ -272,6 +272,11 @@
 
 typeset -U _i_files
 _i_files=( ${^~fpath}/_(|*[^~])(N:t) )
+if [[ $#_i_files -lt 20 && $0 = */* ]]; then
+  # Assume that we need to add the compinit directory to fpath.
+  fpath=(${0:h} $fpath)
+  _i_files=( ${^~fpath}/_(|*[^~])(N:t) )
+fi
 _i_initname=$0
 _i_done=''
 
-- 
Peter Stephenson <pws@ibmth.df.unipi.it>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy


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

* Re: User installation script for new completion
  1999-05-03 10:00   ` Peter Stephenson
@ 1999-05-03 10:46     ` Bart Schaefer
  1999-05-03 11:19       ` Peter Stephenson
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 1999-05-03 10:46 UTC (permalink / raw)
  To: Zsh hackers list

On May 3, 12:00pm, Peter Stephenson wrote:
} Subject: Re: User installation script for new completion
}
} ... compinit looks at how many completion files it has found, and if
} there are fewer than twenty ... it adds the path to itself to fpath.
}
} This fails when functionargzero is not set.  Great.  Any better
} suggestions?

Have it test [[ -o functionargzero ]] and skip this when it's not set?


-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


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

* Re: User installation script for new completion
  1999-05-03 10:46     ` Bart Schaefer
@ 1999-05-03 11:19       ` Peter Stephenson
  1999-05-03 16:12         ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Peter Stephenson @ 1999-05-03 11:19 UTC (permalink / raw)
  To: Zsh hackers list

"Bart Schaefer" wrote:
> On May 3, 12:00pm, Peter Stephenson wrote:
> } This fails when functionargzero is not set.  Great.  Any better
> } suggestions?
> 
> Have it test [[ -o functionargzero ]] and skip this when it's not set?

That doesn't really matter, since if the option isn't set, $0 is the shell
name and won't have a / in, so the test fails anyway.  Maybe I'll add an
option to compinit to pass down it's own path for use in the test --- it's
not very useful for ordinary users, but could smooth things with
compinstall.  And I should turn . into $PWD.

Maybe it's worth checking for Core etc. subdirectories at that point and
adding them all to $fpath.  Does anyone have any preferences for where to
install functions, if I ever get round to this?  I had imagined just
putting them all (i.e. including Functions stuff) in one big directory.

-- 
Peter Stephenson <pws@ibmth.df.unipi.it>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy


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

* Re: User installation script for new completion
  1999-05-03 11:19       ` Peter Stephenson
@ 1999-05-03 16:12         ` Bart Schaefer
  0 siblings, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 1999-05-03 16:12 UTC (permalink / raw)
  To: Zsh hackers list

On May 3,  1:19pm, Peter Stephenson wrote:
} Subject: Re: User installation script for new completion
}
} > Have it test [[ -o functionargzero ]] and skip this when it's not set?
} 
} That doesn't really matter, since if the option isn't set, $0 is the shell
} name and won't have a / in, so the test fails anyway.

Eh?  $0 will have a / in it if the shell was invoked by any mechanism other
than path search, or even if PATHDIRS is set and a partial path was used:

zagzig% echo $0
../zsh-3.1.5-build/zsh
zagzig% unsetopt functionargzero
zagzig% function foo() { echo $0 }
zagzig% foo
../zsh-3.1.5-build/zsh
zagzig% 

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


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

end of thread, other threads:[~1999-05-03 16:15 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-05-02 14:03 User installation script for new completion Peter Stephenson
1999-05-02 14:56 ` Bruce Stephens
1999-05-03 10:00   ` Peter Stephenson
1999-05-03 10:46     ` Bart Schaefer
1999-05-03 11:19       ` Peter Stephenson
1999-05-03 16:12         ` Bart Schaefer
1999-05-02 23:52 ` Bart Schaefer
1999-05-03  0:34   ` Bart Schaefer
1999-05-03  8:11     ` Peter Stephenson

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