From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6247 invoked by alias); 30 Nov 2015 10:48:26 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 21018 Received: (qmail 18594 invoked from network); 30 Nov 2015 10:48:24 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.0 X-IBM-Helo: d06dlp01.portsmouth.uk.ibm.com X-IBM-MailFrom: vogt@linux.vnet.ibm.com X-IBM-RcptTo: zsh-users@zsh.org Date: Mon, 30 Nov 2015 11:48:15 +0100 From: Dominik Vogt To: Zsh Users Subject: Re: Help with directory switching functions Message-ID: <20151130104815.GA13839@linux.vnet.ibm.com> Reply-To: vogt@linux.vnet.ibm.com Mail-Followup-To: Zsh Users References: <20151124103239.GA31057@linux.vnet.ibm.com> <151128105428.ZM27762@torch.brasslantern.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <151128105428.ZM27762@torch.brasslantern.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 15113010-0029-0000-0000-0000094C5293 On Sat, Nov 28, 2015 at 10:54:28AM -0800, Bart Schaefer wrote: > On Nov 24, 11:32am, Dominik Vogt wrote: > } > } I'm trying to write a function that alternately switches between > } two directories. With "+-" I can repeatedly switch between the > } current directory and the last one where I did anything (type any > } command except switching directories). > > [...] > > } The function makes use of the chpwd and precmd functions. > } > } Problems: > } > } 1. Command lines with multiple command cannot be handled properly > } because precmd is just called once for the line, not for each > } command. > > If you really need to sneak in before (or after) each command rather > than just each command line, read up on TRAPDEBUG, but see below. All right, I've tried that, and it gets executed way too often (e.g. when pressing cursor-up that is bound to a history search scripts). On the other hand I don't want to do my own command line parsing, so I'll just live with precmd for now. Maybe the situation never occurs anyway. > } 2. Depending on $HISTCMD is a bit hacky. What I'd really want to > } do is to look at the command being executed and decide > } individually which commands are "interesting" enough to warrant > } recording the current directory. > > If you use preexec instead of precmd you can examine the command line > instead of just examining the history event number. I think that > would allow you to start with _IS_CWD_INTERESTING="0" and only set it > to 1 when something interesting happens (if I read correctly right > now, you assume interesting and then zero it in chpwd if nothing else > has happened yet). > > Also note that preexec would happen before chpwd, so you'd need some > other corresponding logic changes. > > } 3. I'd prefer a shell builtin instead of "readlink". > > _PWD_A="$PWD:A" Ah, cool, that's exactly what I was looking for. > Also, possibly use "cd -q" in _swapdir to avoid running chpwd (the work > will already have been done in preexec, I think). Good point. Had to rewrite my cd script first, though. > Finally, you might consider using the directory stack; push each of the > directories that seems interesting, and then you can pop back through > them, rather than only having the two most recent to swap between. I already use pushd and popd with autopushd all the time, so it's not really an option. I'll probably enhance the script to use a stack because sometimes more history would be helpful. Thanks a lot, Bart! Ciao Dominik ^_^ ^_^ -- Dominik Vogt IBM Germany