Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: newbie questions



Guido Koopman wrote:
> - Is there a way to have a functionor alias run before every
>   external command? I'd like the xterm titlebar reflect the
>   title of the currently running program, instead of just
>   'zsh:$PWD'.

Part one of the answer is: at the moment, not without great difficulty.

Part two of the answer should really go to zsh-workers: Here's a
suggestion for 3.1, but the functional part should apply to 3.0 easily
enough.  "preexec" is a bit like "precmd", but is executed after the
line is read and in addition it tries to make the command line being
executed available.  This comes from the history list, so hiccups are
possible (for example, aliases haven't yet been expanded).  It
wouldn't be too hard to extract the entry as words instead of a single
string.

Now you do something like

preexec() { 
  typeset words
  words=(${=1})
  print "\033]2;${words[1]}\a"
}

I though of adding some feature that allowed you to skip the main
command, but it's a little dangerous because it's impossible to get
out of if you make a mistake.  For the same reason, errflag is always
set to 0 afterwards.  Note that it's too late to alter any aspect of
how the command is run, beyond redefining a variable, function or
external command, since it has already been parsed.

Other possible quirks: preexec does get called before an `fc', even if
that is subsequently deleted from the history list, but with my recent
fix for r's in command substitution it won't get called from the
commands that fc invokes (without that patch, it will).  You can alter
that by changing the `toplevel' test to `(toplevel || justonce)'.
!-history is normal.

*** Doc/Zsh/func.yo.preexec	Mon Jun  2 08:37:08 1997
--- Doc/Zsh/func.yo	Thu Oct  9 15:16:43 1997
***************
*** 73,78 ****
--- 73,84 ----
  item(tt(precmd))(
  Executed before each prompt.
  )
+ findex(preexec)
+ item(tt(preexec))(
+ Executed just after a command has been read and is about to be
+ executed.  If the history mechanism is active, the string to be
+ executed is passed as an argument.
+ )
  item(tt(TRAP)var(NAL))(
  cindex(signals, trapping)
  cindex(trapping signals)
*** Src/init.c.preexec	Thu Oct  9 14:52:52 1997
--- Src/init.c	Thu Oct  9 15:54:46 1997
***************
*** 69,75 ****
--- 69,89 ----
  	}
  	if (hend()) {
  	    int toksav = tok;
+ 	    List prelist;
  
+ 	    if (toplevel && (prelist = getshfunc("preexec")) != &dummy_list) {
+ 		Histent he = gethistent(curhist);
+ 		LinkList args;
+ 		PERMALLOC {
+ 		    args = newlinklist();
+ 		    addlinknode(args, "preexec");
+ 		    if (he && he->text)
+ 			addlinknode(args, he->text);
+ 		} LASTALLOC;
+ 		doshfunc(prelist, args, 0, 1);
+ 		freelinklist(args, (FreeFunc) NULL);
+ 		errflag = 0;
+ 	    }
  	    if (stopmsg)	/* unset 'you have stopped jobs' flag */
  		stopmsg--;
  	    execlist(list, 0, 0);

-- 
Peter Stephenson <pws@xxxxxx>       Tel: +49 33762 77366
WWW:  http://www.ifh.de/~pws/       Fax: +49 33762 77413
Deutsches Elektronen-Synchrotron --- Institut fuer Hochenergiephysik Zeuthen
DESY-IfH, Platanenallee 6, 15738 Zeuthen, Germany.



Messages sorted by: Reverse Date, Date, Thread, Author