* Help with directory switching functions
@ 2015-11-24 10:32 Dominik Vogt
2015-11-28 18:54 ` Bart Schaefer
0 siblings, 1 reply; 3+ messages in thread
From: Dominik Vogt @ 2015-11-24 10:32 UTC (permalink / raw)
To: Zsh Users
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). There are still a few
rough edges that might be improved.
The function makes use of the chpwd and precmd functions. chpwd
writes down the $HISTCMD of the last command that switched
directories, and precmd uses that information to mark the current
directory as one yuop might want to switch back later. When such
a directory is left, chpwd finally stores its path in a variable
that is then used by "-+".
-- snip --
function _swapdir_chpwd () {
local _PWD_A
local _OLDPWD_A
_CHPWD_N="$[HISTCMD + 1]"
_PWD_A=$(readlink -f "$PWD")
test x"$_PWD_A" = x"$_RECORDED_DIR_A"; then
_RECORDED_DIR="$PWD"
fi
_OLDPWD_A=$(readlink -f "$OLDPWD")
if test "$_IS_CWD_INTERESTING" = 1 -a ! x"$_PWD_A" = x"$_OLDPWD_A"; then
# really leaving an interesting dir -> record it
_RECORDED_DIR="$OLDPWD"
_RECORDED_DIR_A="$_OLDPWD_A"
_IS_CWD_INTERESTING="0"
fi
}
function chpwd () {
_swapdir_chpwd
}
function _swapdir_precmd () {
test ! "$HISTCMD" = "$_CHPWD_N" && _IS_CWD_INTERESTING="1"
}
function precmd () {
_swapdir_precmd
}
function _swapdir () {
_IS_CWD_INTERESTING=1
cd "$_RECORDED_DIR"
_IS_CWD_INTERESTING=1
}
alias -- +-=_swapdir
-- snip --
Problems:
1. Command lines with multiple command cannot be handled properly
because precmd is just called once for the line, not for each
command.
$ cd ~; ls; cd ..
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.
3. I'd prefer a shell builtin instead of "readlink".
Any suggestions for improvement are welcome.
Ciao
Dominik ^_^ ^_^
--
Dominik Vogt
IBM Germany
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Help with directory switching functions
2015-11-24 10:32 Help with directory switching functions Dominik Vogt
@ 2015-11-28 18:54 ` Bart Schaefer
2015-11-30 10:48 ` Dominik Vogt
0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2015-11-28 18:54 UTC (permalink / raw)
To: vogt, Zsh Users
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.
} 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"
Or if not that, why not?
Also, possibly use "cd -q" in _swapdir to avoid running chpwd (the work
will already have been done in preexec, I think).
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Help with directory switching functions
2015-11-28 18:54 ` Bart Schaefer
@ 2015-11-30 10:48 ` Dominik Vogt
0 siblings, 0 replies; 3+ messages in thread
From: Dominik Vogt @ 2015-11-30 10:48 UTC (permalink / raw)
To: Zsh Users
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-11-30 10:48 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-24 10:32 Help with directory switching functions Dominik Vogt
2015-11-28 18:54 ` Bart Schaefer
2015-11-30 10:48 ` Dominik Vogt
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).