From 908b1fb699518e329af08581292d094d818475f7 Mon Sep 17 00:00:00 2001 From: js0ny Date: Wed, 13 Nov 2024 10:43:39 +0000 Subject: [PATCH] winfix and ipython --- .../ipython/profile_default/ipython_config.py | 43 ++++++ .../ipython/profile_default/starup/keymap.py | 69 +++++++++ .../profile_default/starup/magic_ps.py | 10 ++ .config/npm/npmrc | 2 + .config/vim/vimrc | 3 + .gitignore | 5 + powershell/Config.ps1 | 7 + setup/win_setup.ps1 | 133 ++++++++++++++++-- win/Microsoft.PowerShell_profile.ps1 | 2 +- zsh/config.zsh | 2 +- 10 files changed, 263 insertions(+), 13 deletions(-) create mode 100644 .config/ipython/profile_default/ipython_config.py create mode 100644 .config/ipython/profile_default/starup/keymap.py create mode 100644 .config/ipython/profile_default/starup/magic_ps.py create mode 100644 powershell/Config.ps1 diff --git a/.config/ipython/profile_default/ipython_config.py b/.config/ipython/profile_default/ipython_config.py new file mode 100644 index 0000000..889258b --- /dev/null +++ b/.config/ipython/profile_default/ipython_config.py @@ -0,0 +1,43 @@ +c.TerminalIPythonApp.display_banner = True +c.TerminalInteractiveShell.editing_mode = 'vi' +c.TerminalInteractiveShell.confirm_exit = False + +# Source: https://ipython.readthedocs.io/en/stable/config/details.html#keyboard-shortcuts +def custom_return(shell): + + """This function is required by the API. It takes a reference to + the shell, which is the same thing `get_ipython()` evaluates to. + This function must return a function that handles each keypress + event. That function, named `handle` here, references `shell` + by closure.""" + + def handle(event): + + """This function is called each time `Enter` is pressed, + and takes a reference to a Prompt Toolkit event object. + If the current input starts with a bang or modulo, then + the input is executed, otherwise a newline is entered, + followed by any spaces needed to auto-indent.""" + + # set up a few handy references to nested items... + + buffer = event.current_buffer + document = buffer.document + text = document.text + + if text.startswith('!') or text.startswith('%'): # execute the input... + + buffer.accept_action.validate_and_handle(event.cli, buffer) + + else: # insert a newline with auto-indentation... + + if document.line_count > 1: text = text[:document.cursor_position] + indent = shell.check_complete(text)[1] + buffer.insert_text('\n' + indent) + + # if you just wanted a plain newline without any indentation, you + # could use `buffer.insert_text('\n')` instead of the lines above + + return handle + +c.TerminalInteractiveShell.handle_return = custom_return \ No newline at end of file diff --git a/.config/ipython/profile_default/starup/keymap.py b/.config/ipython/profile_default/starup/keymap.py new file mode 100644 index 0000000..2dae6da --- /dev/null +++ b/.config/ipython/profile_default/starup/keymap.py @@ -0,0 +1,69 @@ +from prompt_toolkit.key_binding import KeyBindings +from prompt_toolkit.key_binding.vi_state import InputMode +from prompt_toolkit.filters import Condition +from IPython import get_ipython + +ip = get_ipython() +key_bindings = KeyBindings() + +@Condition +def in_navigation_mode(): + return ip.pt_app.app.vi_state.input_mode == InputMode.NAVIGATION + +# colemak keymap hnei +@key_bindings.add("n", filter=in_navigation_mode) +def _(event): + "Move cursor down by visual line" + event.current_buffer.auto_down(count=event.arg) + +@key_bindings.add("e", filter=in_navigation_mode) +def _(event): + "Move cursor up by visual line" + event.current_buffer.auto_up(count=event.arg) + +@key_bindings.add("i", filter=in_navigation_mode) +def _(event): + "Move cursor right" + event.current_buffer.cursor_right(count=event.arg) + +# Insert with 'l' and 'L' +@key_bindings.add("l", filter=in_navigation_mode) +def _(event): + "Enter insert mode (similar position to 'i' in Colemak)" + event.app.vi_state.input_mode = InputMode.INSERT + +@key_bindings.add("L", filter=in_navigation_mode) +def _(event): + "Enter insert mode at the beginning of the line" + event.current_buffer.cursor_position += event.current_buffer.document.get_start_of_line_position() + +# Ne[k]st +@key_bindings.add("k", filter=in_navigation_mode) +def _(event): + "Find next match" + event.current_buffer.forward_search() + +@key_bindings.add("K", filter=in_navigation_mode) +def _(event): + "Find previous match" + event.current_buffer.reverse_search() + +# [J]ump +@key_bindings.add("j", filter=in_navigation_mode) +def _(event): + "Move to end of next word" + event.current_buffer.cursor_right_word() + +@key_bindings.add("J", filter=in_navigation_mode) +def _(event): + "Move to end of next word with capital E" + event.current_buffer.cursor_right_word(end=True) + +# Yank to end of line with 'Y' +@key_bindings.add("Y", filter=in_navigation_mode) +def _(event): + "Yank to the end of the line" + text_to_yank = event.current_buffer.document.text_after_cursor + event.app.clipboard.set_text(text_to_yank) + +ip.pt_app.key_bindings = key_bindings diff --git a/.config/ipython/profile_default/starup/magic_ps.py b/.config/ipython/profile_default/starup/magic_ps.py new file mode 100644 index 0000000..d4c5e8a --- /dev/null +++ b/.config/ipython/profile_default/starup/magic_ps.py @@ -0,0 +1,10 @@ +from IPython.core.magic import register_line_magic + +@register_line_magic +def ps(cmd): + output = get_ipython().getoutput(f"powershell -Command {cmd}") + # If no variable is assigned to the output, print it + if get_ipython().last_execution_result is None: + print("\n".join(output)) + else: + return "\n".join(output) diff --git a/.config/npm/npmrc b/.config/npm/npmrc index 042f19c..222bca4 100644 --- a/.config/npm/npmrc +++ b/.config/npm/npmrc @@ -1,4 +1,6 @@ # export NPM_CONFIG_USERCONFIG="$XDG_CONFIG_HOME"/npm/npmrc +# =========================================================== # $XDG_CONFIG_HOME/npm/npmrc # ln -s $DOTFILES/.config/npm/npmrc $NPM_CONFIG_USERCONFIG +# New-Item -ItemType SymbolicLink -Path $env:NPM_CONFIG_USERCONFIG -Value $env:DOTFILES/.config/npm/npmrc # registry=https://registry.npmmirror.com diff --git a/.config/vim/vimrc b/.config/vim/vimrc index 330e59b..c29f0de 100644 --- a/.config/vim/vimrc +++ b/.config/vim/vimrc @@ -34,6 +34,9 @@ set incsearch set ignorecase set smartcase +set number +set relativenumber + " XDG Directory Specifications " Reference to https://jorenar.com/blog/vim-xdg diff --git a/.gitignore b/.gitignore index 7aadea2..537db61 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,10 @@ *private* .private.env.* +.config/ipython/profile_default/db +.config/ipython/profile_default/log +.config/ipython/profile_default/pid +.config/ipython/profile_default/security +.config/ipython/profile_default/history.sqlite .config/nvim/tt.* .config/nvim/.tests .config/nvim/doc/tags diff --git a/powershell/Config.ps1 b/powershell/Config.ps1 new file mode 100644 index 0000000..cf8f566 --- /dev/null +++ b/powershell/Config.ps1 @@ -0,0 +1,7 @@ +# Use XDG Base Directory Specification and its similar structure for Windows + +# wget +${function:wget} = {wget --hsts-file $XDG_CACHE_HOME/wget-hsts $args} + +# yarn v1 +${function:yarn} = {yarn --use-yarnrc $XDG_CONFIG_HOME/yarn/config.yaml $args} \ No newline at end of file diff --git a/setup/win_setup.ps1 b/setup/win_setup.ps1 index 492f9d4..8ae2e8d 100644 --- a/setup/win_setup.ps1 +++ b/setup/win_setup.ps1 @@ -11,20 +11,131 @@ New-Item -ItemType SymbolicLink -Path $PROFILE -Target "$DOTFILES\win\Microsoft. Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression # Install Chocolatey -Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) +# Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) + +# Set Environment Variables +# Use %PATH_EXT% to prevent PATH from being too long +[System.Environment]::SetEnvironmentVariable("Path_EXT_0", "D:\bin", "User") +[System.Environment]::SetEnvironmentVariable("Path_EXT_1", "", "User") +[System.Environment]::SetEnvironmentVariable("Path_EXT_2", "", "User") +$currentPath = [System.Environment]::GetEnvironmentVariable("Path", "User") +if ($currentPath -notlike "*%PATH_EXT%*") { + $currentPath += ";%PATH_EXT_0%;%PATH_EXT_1%;%PATH_EXT_2%" +} +[System.Environment]::SetEnvironmentVariable("Path", $currentPath, "User") +[System.Environment]::SetEnvironmentVariable("PATH_EXT_0", "", [System.EnvironmentVariableTarget]::Machine) +[System.Environment]::SetEnvironmentVariable("PATH_EXT_1", "", [System.EnvironmentVariableTarget]::Machine) +[System.Environment]::SetEnvironmentVariable("PATH_EXT_2", "", [System.EnvironmentVariableTarget]::Machine) +$currentPath = [System.Environment]::GetEnvironmentVariable("Path", [System.EnvironmentVariableTarget]::Machine) +if ($currentPath -notlike "*%PATH_EXT%*") { + $currentPath += ";%PATH_EXT_0%;%PATH_EXT_1%;%PATH_EXT_2%" +} +[System.Environment]::SetEnvironmentVariable("Path", $currentPath, [System.EnvironmentVariableTarget]::Machine) +# Simulate XDG Base Directory Specification +$Env:XDG_CONFIG_HOME = "$Env:AppData" +$Env:XDG_DATA_HOME = "$Env:LocalAppData" +$Env:XDG_CACHE_HOME = "$Env:LocalAppData\cache" +$Env:XDG_STATE_HOME = "$Env:LocalAppData\state" +[System.Environment]::SetEnvironmentVariable("XDG_CONFIG_HOME", "$Env:XDG_CONFIG_HOME", "User") +[System.Environment]::SetEnvironmentVariable("XDG_DATA_HOME", "$Env:XDG_DATA_HOME", "User") +New-Item -ItemType Directory -Path "$Env:LocalAppData\cache" -Force +[System.Environment]::SetEnvironmentVariable("XDG_CACHE_HOME", "$Env:XDG_CACHE_HOME", "User") +New-Item -ItemType Directory -Path "$Env:LocalAppData\state" -Force +[System.Environment]::SetEnvironmentVariable("XDG_STATE_HOME", "$Env:XDG_STATE_HOME", "User") +# Add environment variables +[System.Environment]::SetEnvironmentVariable("DOTFILES", "$DOTFILES", "User") +# Set potential environment variables to prevent softwares put their configs in the wrong place +# eg. dotfiles in %UserProfile% +# ~\.aws -> %AppData%\aws :: AWS CLI +New-Item -ItemType Directory -Path "$Env:AppData\aws" -Force +[System.Environment]::SetEnvironmentVariable("AWS_CONFIG_FILE", "$Env:AppData\aws\config", "User") +[System.Environment]::SetEnvironmentVariable("AWS_SHARED_CREDENTIALS_FILE", "$Env:AppData\aws\credentials", "User") +# ~\.azure -> %LocalAppData%\Azure :: Azure CLIcargo install --list +[System.Environment]::SetEnvironmentVariable("AZURE_CONFIG_DIR", "$Env:LocalAppData\Azure", "User") +# ~\.cargo -> %LocalAppData%\Cargo :: Rust https://doc.rust-lang.org/cargo/guide/cargo-home.html +[System.Environment]::SetEnvironmentVariable("CARGO_HOME", "$Env:LocalAppData\Cargo", "User") +# ~\.docker -> %AppData%\dotDocker :: Docker (Docker takes %AppData%\Docker, use dotDocker instead) +[System.Environment]::SetEnvironmentVariable("DOCKER_CONFIG", "$Env:AppData\dotDocker", "User") +# ~\.dotnet -> %LocalAppData%\dotNET :: .NET Core +[System.Environment]::SetEnvironmentVariable("DOTNET_CLI_HOME", "$Env:LocalAppData\dotNET", "User") +# ~\.emacs.d -> %AppData%\.emacs.d :: Emacs (This is default) +# [System.Environment]::SetEnvironmentVariable("EMACS_HOME", "$Env:AppData\emacs.d", "User") +# ~\go -> %LocalAppData%\GO :: golang +[System.Environment]::SetEnvironmentVariable("GOPATH", "$Env:LocalAppData\GO", "User") +# ~\.ipython -> %AppData%\ipython :: IPython +# IPython won't check XDG on Windows https://github.com/ipython/ipython/blob/0615526f80691452f2e282c363bce197c0141561/IPython/utils/path.py#L200 +[System.Environment]::SetEnvironmentVariable("IPYTHONDIR", "$Env:AppData\ipython", "User") +# ~\.julia -> %LocalAppData%\julia :: Julia +[System.Environment]::SetEnvironmentVariable("JULIA_DEPOT_PATH", "$Env:LocalAppData\julia", "User") +# ~\_lesshst -> %LocalAppData%\state\lesshst :: less +[System.Environment]::SetEnvironmentVariable("LESSHISTFILE", "$Env:XDG_STATE_HOME\lesshst", "User") +# %AppData%\less\lesskey :: less +New-Item -ItemType Directory -Path "$Env:AppData\less" -Force +[System.Environment]::SetEnvironmentVariable("LESSKEYIN", "$Env:AppData\less\lesskey", "User") +# ~\.matplotlib -> %LocalAppData%\matplotlib :: Matplotlib +[System.Environment]::SetEnvironmentVariable("MPLCONFIGDIR", "$Env:LocalAppData\matplotlib", "User") +# ~\.npmrc -> %AppData%\npm\npmrc :: npm +[System.Environment]::SetEnvironmentVariable("NPM_CONFIG_USERCONFIG", "$Env:AppData\npm\npmrc", "User") +# ~\.node_repl_history -> %LocalAppData%\state\node\repl_history :: Node.js +New-Item -ItemType Directory -Path "$Env:XDG_STATE_HOME\node" -Force +[System.Environment]::SetEnvironmentVariable("NODE_REPL_HISTORY", "$Env:XDG_STATE_HOME\node\repl_history", "User") +# ~\.ts_node_repl_history -> %LocalAppData%\state\node\ts_node_repl_history :: Node.js +[System.Environment]::SetEnvironmentVariable("TS_NODE_REPL_HISTORY", "$Env:XDG_STATE_HOME\node\ts_node_repl_history", "User") +# ~\.nuget\packages -> %LocalAppData%\cache\NuGet\packages :: NuGet +[System.Environment]::SetEnvironmentVariable("NUGET_PACKAGES", "$Env:XDG_CACHE_HOME\NuGet\packages", "User") +# ~\.omnisharp -> %AppData%\OmniSharp :: OmniSharp +[System.Environment]::SetEnvironmentVariable("OMNISHARPHOME", "$Env:AppData\OmniSharp", "User") +# ~\.python_history -> %LocalAppData%\state\python\python_history :: Python +# Only works for Python 3.13+ https://docs.python.org/3.13/using/cmdline.html#envvar-PYTHON_HISTORY +New-Item -ItemType Directory -Path "$Env:XDG_STATE_HOME\python" -Force +[System.Environment]::SetEnvironmentVariable("PYTHON_HISTORY", "$Env:XDG_STATE_HOME\python\python_history", "User") +# ~\.rustup -> %LocalAppData%\Rustup :: Rust +[System.Environment]::SetEnvironmentVariable("RUSTUP_HOME", "$Env:LocalAppData\Rustup", "User") +# ~\.vimrc -> %AppData%\Vim\_vimrc :: Vim +New-Item -ItemType Directory -Path "$Env:AppData\Vim" -Force +[System.Environment]::SetEnvironmentVariable("VIM", "$Env:AppData\Vim", "User") +# ~\.vuerc -> %AppData%\vue\.vuerc :: Vue CLI +# Currently, Vue CLI doesn't support file path configuration:https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli/lib/confifile +# [System.Environment]::SetEnvironmentVariable("VUE_CLI_CONFIG_PATH", "$Env:AppData\vue\.vuerc", "User") +# ~\.wget-hsts -> %LocalAppData%\cache\wget-hsts :: wget +# Use Alias, wget host file path is not configurable +# ~\.yarnrc -> %AppData%\yarn\config.yaml :: Yarn v1 +# Use Alias, yarnrc path is not configurable + +# Reload required here to make sure the environment variables are set + # Set Dotfiles -New-Item -ItemType SymbolicLink -Path "$Env:XDG_CONFIG_HOME\conda\.condarc" -Target "$DOTFILES\.config\conda\condarc.yaml" -New-Item -ItemType SymbolicLink -Path "$Env:XDG_CONFIG_HOME\git\config" -Target "$DOTFILES\.config\git\.gitconfig" -New-Item -ItemType SymbolicLink -Path "~\.haskline" -Target "$DOTFILES\.haskline" -New-Item -ItemType SymbolicLink -Path "~\.ideavimrc" -Target "$DOTFILES\.ideavimrc" -New-Item -ItemType SymbolicLink -Path "~\.markdownlint.json" -Target "$DOTFILES\.markdownlint.json" -New-Item -ItemType SymbolicLink -Path "~\.npmrc" -Target "$DOTFILES\.npmrc" -New-Item -ItemType SymbolicLink -Path "~\.pip.conf" -Target "$DOTFILES\.pip.conf" -New-Item -ItemType SymbolicLink -Path "~\.wslconfig" -Target "$DOTFILES\win\.wslconfig" -New-Item -ItemType SymbolicLink -Path "~\.vscode.vimrc" -Target "$DOTFILES\vscode\vscode.vimrc" +# $DOTFILES\.config +$folders = @("conda", "git", "ideavim", "ipython", "markdownlint", "npm", "NuGet", "nvim", "pip", "Vim") +foreach ($folder in $folders) { + New-Item -ItemType Directory -Path "$Env:XDG_CONFIG_HOME\$folder" -Force +} +New-Item -ItemType SymbolicLink -Path "$Env:XDG_CONFIG_HOME\conda\.condarc" -Target "$DOTFILES\.config\conda\condarc.yaml" -Force +New-Item -ItemType SymbolicLink -Path "$Env:XDG_CONFIG_HOME\git\config" -Target "$DOTFILES\.config\git\.gitconfig" -Force +New-Item -ItemType SymbolicLink -Path "~\.haskeline" -Target "$DOTFILES\.haskeline" -Force +New-Item -ItemType SymbolicLink -Path "$Env:XDG_CONFIG_HOME\ideavim\.ideavimrc" -Target "$DOTFILES\.config\ideavim\.ideavimrc" -Force +New-Item -ItemType SymbolicLink -Path "$Env:XDG_CONFIG_HOME\ipython" -Target "$DOTFILES\.config\ipython" -Force +# New-Item -ItemType SymbolicLink -Path "~\.markdownlint.json" -Target "$DOTFILES\.markdownlint.json" -Force +New-Item -ItemType SymbolicLink -Path "$Env:XDG_CONFIG_HOME\npm\npmrc" -Target "$DOTFILES\.config\npm\npmrc" -Force +New-Item -ItemType SymbolicLink -Path "$Env:AppData\NuGet\NuGet.Config" -Target "$DOTFILES\.config\NuGet\NuGet.Config" -Force +New-Item -ItemType SymbolicLink -Path "$Env:XDG_CONFIG_HOME\nvim" -Target "$DOTFILES\.config\nvim" -Force +New-Item -ItemType SymbolicLink -Path "$Env:AppData\pip\pip.ini" -Target "$DOTFILES\.config\pip\pip.conf" -Force +New-Item -ItemType SymbolicLink -Path "$Env:XDG_CONFIG_HOME\Vim\_vimrc" -Target "$DOTFILES\.config\vim\vimrc" -Force +New-Item -ItemType SymbolicLink -Path "~\.wslconfig" -Target "$DOTFILES\win\.wslconfig" -Force +New-Item -ItemType SymbolicLink -Path "$Env:AppData\neovide\config.toml" -Target "$DOTFILES\win\neovide.toml" -Force +# New-Item -ItemType SymbolicLink -Path "~\.vscode.vimrc" -Target "$DOTFILES\vscode\vscode.vimrc" Use Absolute Path # Hide dotfiles -Get-ChildItem -Force -Filter .* | ForEach-Object { $_.Attributes += "Hidden" } +Get-ChildItem -Force -Filter .* -Path $HOME | ForEach-Object { $_.Attributes += "Hidden" } +@( + Join-Path $HOME '.config' + Join-Path $HOME '.dotfiles' +) | ForEach-Object { + $_.Attributes -band -bnot [System.IO.FileAttributes]::Hidden +} +Set-Location $HOME\Documents +# Match WindowsPowerShell and PowerShell +Get-ChildItem -Force -Filter *owerShell | ForEach-Object { $_.Attributes += "Hidden" } # Windows Terminal if (Get-Command wt -ErrorAction SilentlyContinue) { diff --git a/win/Microsoft.PowerShell_profile.ps1 b/win/Microsoft.PowerShell_profile.ps1 index 6730268..b176736 100644 --- a/win/Microsoft.PowerShell_profile.ps1 +++ b/win/Microsoft.PowerShell_profile.ps1 @@ -93,4 +93,4 @@ if (-not ($__lastStartup -eq $_currentDate)) { } Remove-Variable SystemlogFilePath Remove-Variable __lastStartup -Remove-Variable _currentDate \ No newline at end of file +Remove-Variable _currentDate diff --git a/zsh/config.zsh b/zsh/config.zsh index a647393..15da6a7 100644 --- a/zsh/config.zsh +++ b/zsh/config.zsh @@ -68,7 +68,7 @@ export W3M_DIR="$XDG_DATA_HOME"/w3m # ~/.wakatime/ -> $XDG_CONFIG_HOME/wakatime/ export WAKATIME_HOME="$XDG_CONFIG_HOME/wakatime" # ~/.wget-hsts -> $XDG_DATA_HOME/wget-hsts -alias wget='wget --hsts-file="$XDG_DATA_HOME/wget-hsts"' +alias wget='wget --hsts-file="$XDG_CACHE_HOME/wget-hsts"' # ~/.z -> $XDG_DATA_HOME/z export _Z_DATA="$XDG_DATA_HOME/z" # ~/.zcompdump* -> $XDG_CACHE_HOME/zsh/zcompdump*