2015-07-09 10:20:43 +08:00
|
|
|
function pushd --description 'Push directory to stack'
|
2017-07-14 02:31:08 +08:00
|
|
|
set -l rot_r
|
|
|
|
set -l rot_l
|
2017-09-14 12:30:04 +08:00
|
|
|
|
|
|
|
if count $argv >/dev/null
|
|
|
|
# check for --help
|
|
|
|
switch $argv[1]
|
|
|
|
case -h --h --he --hel --help
|
|
|
|
__fish_print_help pushd
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# emulate bash by checking if argument of form +n or -n
|
|
|
|
if string match -qr '^-[0-9]+$' -- $argv[1]
|
|
|
|
set rot_r (string sub -s 2 -- $argv[1])
|
|
|
|
else if string match -qr '^\+[0-9]+$' -- $argv[1]
|
|
|
|
set rot_l (string sub -s 2 -- $argv[1])
|
|
|
|
end
|
|
|
|
end
|
2015-07-09 10:20:43 +08:00
|
|
|
|
2017-07-04 01:16:31 +08:00
|
|
|
set -q dirstack
|
|
|
|
or set -g dirstack
|
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# emulate bash: an empty pushd should switch the top of dirs
|
2017-07-04 01:16:31 +08:00
|
|
|
if not set -q argv[1]
|
2016-11-28 13:27:22 +08:00
|
|
|
# check that the stack isn't empty
|
2017-07-04 01:16:31 +08:00
|
|
|
if not set -q dirstack[1]
|
2016-11-28 13:27:22 +08:00
|
|
|
echo "pushd: no other directory"
|
|
|
|
return 1
|
|
|
|
end
|
2015-07-09 10:20:43 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# get the top two values of the dirs stack ... the first is pwd
|
2017-01-05 08:49:03 +08:00
|
|
|
set -l top_dir $PWD
|
2016-11-28 13:27:22 +08:00
|
|
|
set -l next_dir $dirstack[1]
|
2015-07-09 10:20:43 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# alter the top of dirstack and move to directory
|
|
|
|
set -g dirstack[1] $top_dir
|
|
|
|
cd $next_dir
|
|
|
|
return
|
|
|
|
end
|
2015-07-09 10:20:43 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# emulate bash: check for rotations
|
|
|
|
if test -n "$rot_l" -o -n "$rot_r"
|
|
|
|
# grab the current stack
|
2017-01-05 08:49:03 +08:00
|
|
|
set -l stack $PWD $dirstack
|
2015-07-09 10:20:43 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# translate a right rotation to a left rotation
|
|
|
|
if test -n "$rot_r"
|
|
|
|
# check the rotation in range
|
|
|
|
if test $rot_r -ge (count $stack)
|
|
|
|
echo "pushd: -$rot_r: directory stack index out of range"
|
|
|
|
return 1
|
|
|
|
end
|
2015-07-09 10:20:43 +08:00
|
|
|
|
2017-09-10 14:35:47 +08:00
|
|
|
set rot_l (math (count $stack) - 1 - $rot_r)
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
2015-07-09 10:20:43 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# check the rotation in range
|
|
|
|
if test $rot_l -ge (count $stack)
|
|
|
|
echo "pushd: +$rot_l: directory stack index out of range"
|
|
|
|
return 1
|
|
|
|
else
|
|
|
|
# rotate stack unless rot_l is 0
|
|
|
|
if test $rot_l -gt 0
|
2017-09-10 14:35:47 +08:00
|
|
|
set stack $stack[(math $rot_l + 1)..(count $stack)] $stack[1..$rot_l]
|
2016-11-28 13:27:22 +08:00
|
|
|
end
|
2015-07-09 10:20:43 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# now reconstruct dirstack and change directory
|
|
|
|
set -g dirstack $stack[2..(count $stack)]
|
|
|
|
cd $stack[1]
|
|
|
|
end
|
2015-07-09 10:20:43 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# print the new stack
|
|
|
|
dirs
|
|
|
|
return
|
|
|
|
end
|
2006-11-18 00:24:38 +08:00
|
|
|
|
2016-11-28 13:27:22 +08:00
|
|
|
# argv[1] is a directory
|
2020-04-26 17:29:46 +08:00
|
|
|
set -l old_pwd $PWD
|
|
|
|
cd $argv[1]; and set -g -p dirstack $old_pwd
|
2015-07-09 10:20:43 +08:00
|
|
|
end
|