@@ -1719,46 +1719,44 @@ function cd_backward(args, options, pwd)
17191719 local pwd = (pwd ~= nil ) and pwd or os .pwd ()
17201720 if nargs == 0 then
17211721 return find_vcs_root (pwd )
1722- elseif nargs == 1 then
1723- if args [1 ]:sub (1 , 2 ) == ' ..' then
1724- local size = args [1 ]:len () - 1
1725- if args [1 ]:match (' ^%.%.+$' ) then
1726- size = args [1 ]:len () - 1
1727- elseif args [1 ]:match (' ^%.%.%d+$' ) then
1728- size = tonumber (args [1 ]:sub (3 ))
1729- else
1730- return nil
1731- end
1732- local path = pwd
1733- for index = 1 , size do
1734- path = os .path .join (path , ' ..' )
1735- end
1736- return os .path .normpath (path )
1722+ elseif nargs == 1 and args [1 ]:sub (1 , 2 ) == ' ..' then
1723+ local size = args [1 ]:len () - 1
1724+ if args [1 ]:match (' ^%.%.+$' ) then
1725+ size = args [1 ]:len () - 1
1726+ elseif args [1 ]:match (' ^%.%.%d+$' ) then
1727+ size = tonumber (args [1 ]:sub (3 ))
17371728 else
1738- pwd = os .path .split (pwd )
1739- local test = windows and pwd :gsub (' \\ ' , ' /' ) or pwd
1740- local key = windows and args [1 ]:lower () or args [1 ]
1741- if not key :match (' %u' ) then
1742- test = test :lower ()
1743- end
1744- local pos , ends = test :rfind (' /' .. key )
1745- if pos then
1746- ends = test :find (' /' , pos + key :len () + 1 , true )
1747- ends = ends and ends or test :len ()
1748- return os .path .normpath (pwd :sub (1 , ends ))
1749- elseif windows and test :startswith (key ) then
1750- ends = test :find (' /' , key :len (), true )
1751- ends = ends and ends or test :len ()
1752- return os .path .normpath (pwd :sub (1 , ends ))
1753- end
1754- pos = test :rfind (key )
1755- if pos then
1756- ends = test :find (' /' , pos + key :len (), true )
1757- ends = ends and ends or test :len ()
1758- return os .path .normpath (pwd :sub (1 , ends ))
1759- end
17601729 return nil
17611730 end
1731+ local path = pwd
1732+ for index = 1 , size do
1733+ path = os .path .join (path , ' ..' )
1734+ end
1735+ return os .path .normpath (path )
1736+ elseif nargs == 1 then
1737+ pwd = os .path .split (pwd )
1738+ local test = windows and pwd :gsub (' \\ ' , ' /' ) or pwd
1739+ local key = windows and args [1 ]:lower () or args [1 ]
1740+ if not key :match (' %u' ) then
1741+ test = test :lower ()
1742+ end
1743+ local pos , ends = test :rfind (' /' .. key )
1744+ if pos then
1745+ ends = test :find (' /' , pos + key :len () + 1 , true )
1746+ ends = ends and ends or test :len ()
1747+ return os .path .normpath (pwd :sub (1 , ends ))
1748+ elseif windows and test :startswith (key ) then
1749+ ends = test :find (' /' , key :len (), true )
1750+ ends = ends and ends or test :len ()
1751+ return os .path .normpath (pwd :sub (1 , ends ))
1752+ end
1753+ pos = test :rfind (key )
1754+ if pos then
1755+ ends = test :find (' /' , pos + key :len (), true )
1756+ ends = ends and ends or test :len ()
1757+ return os .path .normpath (pwd :sub (1 , ends ))
1758+ end
1759+ return nil
17621760 elseif nargs == 2 then
17631761 local test = windows and pwd :gsub (' \\ ' , ' /' ) or pwd
17641762 local src = args [1 ]
@@ -1778,9 +1776,13 @@ function cd_backward(args, options, pwd)
17781776 return newpath
17791777 end
17801778
1779+ -- Get rid of the entire path component that matched `src`.
17811780 lhs = lhs :gsub (" [^/]*$" , " " )
17821781 rhs = rhs :gsub (" ^[^/]*" , " " )
17831782 return z_cd ({lhs , dst , rhs })
1783+ -- In the future, it would make sense to have `z -b -c from to to2`
1784+ -- to z_cd({lhs, dst[1], dst[2]}). Without `-c`, we probably still
1785+ -- want to support only 2 argumets.
17841786 end
17851787
17861788 io.stderr :write (" Error: " .. Z_CMD .. " -b takes at most 2 arguments.\n " )
0 commit comments