2015-07-09 02:20:43 +00:00
|
|
|
function pushd --description 'Push directory to stack'
|
2017-07-13 18:31:08 +00:00
|
|
|
set -l options 'h/help'
|
|
|
|
argparse -n pushd --max-args=1 $options -- $argv
|
|
|
|
or return
|
2017-06-02 00:58:29 +00:00
|
|
|
|
2017-07-13 18:31:08 +00:00
|
|
|
if set -q _flag_help
|
|
|
|
__fish_print_help pushd
|
|
|
|
return 0
|
|
|
|
end
|
2015-07-09 02:20:43 +00:00
|
|
|
|
2017-07-13 18:31:08 +00:00
|
|
|
set -l rot_r
|
|
|
|
set -l rot_l
|
|
|
|
if set -q argv[1]
|
2016-11-28 05:27:22 +00: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 02:20:43 +00:00
|
|
|
|
2017-07-03 17:16:31 +00:00
|
|
|
set -q dirstack
|
|
|
|
or set -g dirstack
|
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
# emulate bash: an empty pushd should switch the top of dirs
|
2017-07-03 17:16:31 +00:00
|
|
|
if not set -q argv[1]
|
2016-11-28 05:27:22 +00:00
|
|
|
# check that the stack isn't empty
|
2017-07-03 17:16:31 +00:00
|
|
|
if not set -q dirstack[1]
|
2016-11-28 05:27:22 +00:00
|
|
|
echo "pushd: no other directory"
|
|
|
|
return 1
|
|
|
|
end
|
2015-07-09 02:20:43 +00:00
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
# get the top two values of the dirs stack ... the first is pwd
|
2017-01-05 00:49:03 +00:00
|
|
|
set -l top_dir $PWD
|
2016-11-28 05:27:22 +00:00
|
|
|
set -l next_dir $dirstack[1]
|
2015-07-09 02:20:43 +00:00
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
# alter the top of dirstack and move to directory
|
|
|
|
set -g dirstack[1] $top_dir
|
|
|
|
cd $next_dir
|
|
|
|
return
|
|
|
|
end
|
2015-07-09 02:20:43 +00:00
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
# emulate bash: check for rotations
|
|
|
|
if test -n "$rot_l" -o -n "$rot_r"
|
|
|
|
# grab the current stack
|
2017-01-05 00:49:03 +00:00
|
|
|
set -l stack $PWD $dirstack
|
2015-07-09 02:20:43 +00:00
|
|
|
|
2016-11-28 05:27:22 +00: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 02:20:43 +00:00
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
set rot_l (math (count $stack) - 1 - $rot_r)
|
|
|
|
end
|
2015-07-09 02:20:43 +00:00
|
|
|
|
2016-11-28 05:27:22 +00: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
|
|
|
|
set stack $stack[(math $rot_l + 1)..(count $stack)] $stack[1..$rot_l]
|
|
|
|
end
|
2015-07-09 02:20:43 +00:00
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
# now reconstruct dirstack and change directory
|
|
|
|
set -g dirstack $stack[2..(count $stack)]
|
|
|
|
cd $stack[1]
|
|
|
|
end
|
2015-07-09 02:20:43 +00:00
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
# print the new stack
|
|
|
|
dirs
|
|
|
|
return
|
|
|
|
end
|
2006-11-17 16:24:38 +00:00
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
# argv[1] is a directory
|
2017-08-05 01:02:24 +00:00
|
|
|
set -g -p dirstack $PWD
|
2016-11-28 05:27:22 +00:00
|
|
|
cd $argv[1]
|
2015-07-09 02:20:43 +00:00
|
|
|
end
|