Skip to content

Commit

Permalink
Make percent characters work in all contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
mataha committed Jun 6, 2023
1 parent 70321ca commit a0c6535
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 127 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ zoxide can be installed in 4 easy steps:
</details>

<details>
<summary>`cmd.exe`</summary>

> Add this to the **end** of your config file or AutoRun command:
>
> ```batchfile
> zoxide init cmd | cmd /d >nul
> ```
</details>

<details>
<summary>Elvish</summary>

Expand Down
3 changes: 1 addition & 2 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,10 @@ mod tests {
let source = Cmd(&opts).render().unwrap();

Command::new("cmd.exe")
.args(["/a", "/d", "/e:on", "/q", "/v:off", "/k", "@doskey", "/macros:cmd.exe"])
.args(["/a", "/d", "/e:on", "/q", "/v:off", "/k", "@doskey", "/macros:all"])
.write_stdin(source)
.assert()
.success()
.stdout("")
.stderr("");
}

Expand Down
242 changes: 117 additions & 125 deletions templates/cmd.txt
Original file line number Diff line number Diff line change
@@ -1,84 +1,91 @@
{%- import "utils/batch.txt" as batch -%}

{%- macro cd(directory, tabs) -%} if /i not "%$p%CD%$p%"=="{{ directory }}" (^
{%- call batch::indent(tabs) %} %__builtin_cd% {{ directory|safe }}^
{%- call batch::indent(tabs) %} ^&^& set "OLDPWD=%$p%CD%$p%"^
{%- if echo -%}
{%- call batch::indent(tabs) %} ^&^& {%~ call pwd(tabs + 1) ~%}^
{%- endif -%}
{%- if hook != InitHook::None -%}
{%- call batch::indent(tabs) %} ^&^& (for /f "delims=" %$p%a in ('"%__builtin_pwd%"') do @(zoxide add -- "%$p%~fa\."))^
{%- endif -%}
{%- call batch::indent(tabs) %})
{%- endmacro cd -%}

{%- macro pwd(tabs) -%} (^
{%- if resolve_symlinks -%}
{%- call batch::indent(tabs) %} (^
{%- call batch::indent(tabs) %} for /f "skip=9 tokens=1,2,*" %$p%j in ('""%$fsutil%" reparsepoint query ."') do @(^
{%- call batch::indent(tabs) %} if "%$p%~j"=="Print" if "%$p%~k"=="Name:" if not "%$p%~l"=="" (echo(%$p%~l)^
{%- call batch::indent(tabs) %} )^
{%- call batch::indent(tabs) %} ) ^|^| %__builtin_pwd%^
{%- else -%}
{%- call batch::indent(tabs) %} %__builtin_pwd%^
{%- endif -%}
{%- call batch::indent(tabs) %})
{%- endmacro pwd -%}

{%- let section = "@rem ==========================================================================\n@rem" -%}
{%- let not_configured = "@rem -- not configured --" -%}
@setlocal EnableDelayedExpansion EnableExtensions & set "CMD=!CMDCMDLINE!" 2>nul
@if /i not "!CMD!"=="!CMD:/=!" (goto :EOF) else @if not defined DEBUG (echo off)
@reg query "HKCU\Software\Microsoft\Command Processor" /v "AutoRun" /z >nul 2>&1
@if !ERRORLEVEL! equ 0 (endlocal & set "CMD_ENV=%~0") else (echo(cmd& goto :EOF)

{{ section }}
@rem Utility functions for zoxide.
@rem
@echo off & setlocal EnableDelayedExpansion EnableExtensions

@rem Full credits to jeb for this - https://stackoverflow.com/a/76213522/6724141
set i_AS_$*=%%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^")
@if "%~f0"=="%~dpnx0" (
set ^"$p=%%<nul"
) else (
set ^"$p=^%<nul"
)

set __ZOXIDE_CD=chdir /d
set __ZOXIDE_PWD=chdir
set ^"$true=(call )"
set ^"$false=(call)"

@rem cd + custom logic based on the value of _ZO_ECHO.
doskey cd = ( ^
{#- Full credits to jeb (see https://stackoverflow.com/a/76213522/6724141) #}
for %%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^") do @( ^
if "%%~i"=="" ( ^
if defined USERPROFILE ( ^
if /i not "%%CD%%"=="%%USERPROFILE%%" ( ^
%__ZOXIDE_CD% "%%USERPROFILE%%" ^&^& ^
}
{%- if echo %}
%__ZOXIDE_PWD% ^&^& ^
set root=%SystemRoot%\System32
set ^"$doskey=%root%\doskey.exe"
{%- if resolve_symlinks %}
set ^"$fsutil=%root%\fsutil.exe"
{%- endif %}
{% if hook != InitHook::None -%}
if defined __ZOXIDE_HOOK (%%__ZOXIDE_HOOK%%) ^&^& ^
{%- endif %}
set "OLDCD=%%CD%%" ^
) ^
) ^
) else if "%%~i"=="-" ( ^
if defined OLDCD ( ^
if /i not "%%CD%%"=="%%OLDCD%%" ( ^
%__ZOXIDE_CD% "%%OLDCD%%" ^&^& ^
{%- if echo %}
%__ZOXIDE_PWD% ^&^& ^
{%- endif %}
{% if hook != InitHook::None -%}
if defined __ZOXIDE_HOOK (%%__ZOXIDE_HOOK%%) ^&^& ^
{%- endif %}
set "OLDCD=%%CD%%" ^
) ^
) ^
) else ( ^
( ^
if /i not "%%CD%%"=="%%~fi" ( ^
%__ZOXIDE_CD% %%~fi ^&^& ^
{%- if echo %}
%__ZOXIDE_PWD% ^&^& ^
{%- endif %}
{% if hook != InitHook::None -%}
if defined __ZOXIDE_HOOK (%%__ZOXIDE_HOOK%%) ^&^& ^
{%- endif %}
set "OLDCD=%%CD%%" ^
) ^
) ^
) ^
) ^
)

doskey pwd = (%__ZOXIDE_PWD%)

{{ section }}
@rem Hook configuration for zoxide.
@rem Utility functions for zoxide.
@rem

if not defined __ZOXIDE_HOOKED (
set __ZOXIDE_HOOKED=1
{% if hook == InitHook::None -%}
@rem {{ not_configured }}
set __ZOXIDE_HOOK=
{%- else -%}
@rem Initialize hook to add new entries to the database.
set __ZOXIDE_HOOK=for /f "delims=" %%z in ('%__ZOXIDE_PWD%') do (zoxide add -- "%%~fz ")
{%- endif %}
)
set __builtin_cd=chdir /d
set __builtin_pwd=chdir

set __zoxide_cd=cd
set __zoxide_pwd=pwd

@rem cd + custom logic based on the value of _ZO_ECHO.
"%$doskey%" %__zoxide_cd% = (^
{% call batch::for_caret("%$p%", "i") ~%} @(^
if "%$p%~i"=="" (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 4) %}^
) else (^
(echo(%__zoxide_cd%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
) else if "%$p%~i"=="~" (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 4) %}^
) else (^
(echo(%__zoxide_cd%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
) else if "%$p%~i"=="-" (^
if defined OLDPWD (^
{% call cd("%$p%OLDPWD%$p%", 4) %}^
) else (^
(echo(%__zoxide_cd%: OLDPWD is not defined) ^>^&2 ^& %$false%^
)^
) else (^
(^
{% call cd("%$p%~fi", 4) %}^
)^
)^
)^
) ^&^& %$true%

@rem pwd based on the value of _ZO_RESOLVE_SYMLINKS.
"%$doskey%" %__zoxide_pwd% = {%~ call pwd(0) %}

{{ section }}
@rem Commands for zoxide. Disable these using --no-cmd.
Expand All @@ -87,70 +94,55 @@ if not defined __ZOXIDE_HOOKED (
{%- match cmd %}
{%- when Some with (cmd) %}

set __zoxide_z_prefix={{cmd}}

@rem Jump to a directory using only keywords.
doskey {{cmd}} = ( ^
for %%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^") do @( ^
if "%%~i"=="" ( ^
if defined HOME ( ^
if /i not "%%CD%%"=="%%HOME%%" ( ^
%__CD% "%%HOME%%" ^&^& ^
{%- if echo %}
%__ZOXIDE_PWD% ^&^& ^
{%- endif %}
set "OLDCD=%%CD%%" ^&^& ^
%__ZOXIDE_HOOK% ^
) ^
) ^
) else if "%%~i"=="-" ( ^
if defined OLDCD ( ^
if /i not "%%CD%%"=="%%OLDCD%%" ( ^
%__CD% "%%OLDCD%%" ^&^& ^
{%- if echo %}
%__ZOXIDE_PWD% ^&^& ^
{%- endif %}
set "OLDCD=%%CD%%" ^&^& ^
%__ZOXIDE_HOOK% ^
) ^
) ^
) else ( ^
for /f "delims=" %%p in ('zoxide query --exclude "%%CD%%" -- "%%~i"') do @( ^
if /i not "%%CD%%"=="%%~fp" ( ^
%__CD% %%~fp ^&^& ^
{%- if echo %}
%__ZOXIDE_PWD% ^&^& ^
{%- endif %}
set "OLDCD=%%CD%%" ^&^& ^
%__ZOXIDE_HOOK% ^
) ^
) ^
) ^
) ^
)
"%$doskey%" %__zoxide_z_prefix% = (^
{% call batch::for_caret("%$p%", "i") ~%} @(^
if "%$p%~i"=="" (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 4) %}^
) else (^
(echo(%__zoxide_z_prefix%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
) else if "%$p%~i"=="~" (^
if defined USERPROFILE (^
{% call cd("%$p%USERPROFILE%$p%", 4) %}^
) else (^
(echo(%__zoxide_z_prefix%: USERPROFILE is not defined) ^>^&2 ^& %$false%^
)^
) else if "%$p%~i"=="-" (^
if defined OLDPWD (^
{% call cd("%$p%OLDPWD%$p%", 4) %}^
) else (^
(echo(%__zoxide_z_prefix%: OLDPWD is not defined) ^>^&2 ^& %$false%^
)^
) else (^
for /f "delims=" %$p%p in ('"zoxide query --exclude "%$p%CD%$p%\." -- %$p%~i"') do @(^
{% call cd("%$p%~fp", 4) %}^
)^
)^
)^
) ^&^& %$true%

@rem Jump to a directory using interactive search.
doskey {{cmd}}i = ( ^
for %%^^^^ in ("") do @for /f "delims=" %%i in (^^""$*%%~^^"^") do @( ^
for /f "delims=" %%q in ('zoxide query --interactive -- "%%~i"') do @( ^
%__CD% %%~fq ^&^& ^
{%- if echo %}
%__ZOXIDE_PWD% ^&^& ^
{%- endif %}
set "OLDCD=%%CD%%" ^&^& ^
%__ZOXIDE_HOOK% ^
) ^
) ^
)
"%$doskey%" %__zoxide_z_prefix%i = (^
{% call batch::for_caret("%$p%", "i") ~%} @(^
for /f "delims=" %$p%p in ('"zoxide query --interactive -- %$p%~i"') do @(^
{% call cd("%$p%~fp", 3) %}^
)^
)^
) ^&^& %$true%

{%- when None %}

{{ not_configured }}

{%- endmatch %}

@endlocal

{{ section }}
@rem To initialize zoxide, add the contents of the following command to your
@rem configuration:
@rem
@rem zoxide init cmd
@rem To initialize zoxide, add this to your configuration or AutoRun command:
@rem
@rem If you don't have one: <TODO: explain how to run `AutoRun` scripts>
@rem zoxide init cmd | cmd /d >nul
9 changes: 9 additions & 0 deletions templates/utils/batch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{%- macro indent(tabs) %}
{% for tab in 0..tabs %} {% endfor -%}
{%- endmacro indent -%}

{%- macro for_caret(percent, for_parameter) -%}
{#- `for /f` statement with disappearing carets suitable for use in aliases. -#}
{#- Credits for this go to jeb: https://stackoverflow.com/a/76213522/6724141 -#}
for {{ percent }}^^^^ in ("") do @for /f "delims=" {{ percent }}{{ for_parameter }} in (^^""$*{{ percent }}~^^"^") do
{%- endmacro for_caret -%}

0 comments on commit a0c6535

Please sign in to comment.