diff --git a/bin/omarchy-default-editor b/bin/omarchy-default-editor index 7247ec79..bf14dc44 100755 --- a/bin/omarchy-default-editor +++ b/bin/omarchy-default-editor @@ -1,11 +1,17 @@ #!/bin/bash -# omarchy:summary=Set the default editor for $EDITOR +# omarchy:summary=Set the default editor used by omarchy-launch-editor # omarchy:args=[code|cursor|zed|sublime_text|helix|vim|emacs|nvim] # omarchy:examples=omarchy default editor | omarchy default editor code | omarchy default editor helix +editor_file="$HOME/.local/state/omarchy/defaults/editor" + if (($# == 0)); then - sed -n 's/^export EDITOR=//p' ~/.config/uwsm/default | head -n 1 + if [[ -f $editor_file ]]; then + read -r editor <"$editor_file" + fi + + [[ -n $editor ]] && echo "$editor" || echo "nvim" exit 0 fi @@ -24,7 +30,7 @@ nvim) editor="nvim"; name="Neovim"; glyph= ;; ;; esac -sed -i "s/^export EDITOR=.*/export EDITOR=$editor/" ~/.config/uwsm/default +mkdir -p "$(dirname "$editor_file")" +printf '%s\n' "$editor" >"$editor_file" -export EDITOR="$editor" -omarchy-notification-send -g $glyph "$name is now the default editor" "Effective after logging out" +omarchy-notification-send -g $glyph "$name is now the default editor" diff --git a/bin/omarchy-launch-editor b/bin/omarchy-launch-editor index 9c3ec369..763055f0 100755 --- a/bin/omarchy-launch-editor +++ b/bin/omarchy-launch-editor @@ -1,15 +1,23 @@ #!/bin/bash -# omarchy:summary=Launch the default editor as determined by $EDITOR (set via ~/.config/uwsm/default) (or nvim if missing). +# omarchy:summary=Launch the default editor selected via Omarchy defaults. # omarchy:args= -omarchy-cmd-present "$EDITOR" || EDITOR=nvim +editor_file="$HOME/.local/state/omarchy/defaults/editor" +editor="nvim" -case "$EDITOR" in +if [[ -f $editor_file ]]; then + read -r editor <"$editor_file" +fi + +[[ -n $editor ]] || editor="nvim" +omarchy-cmd-present "$editor" || editor="nvim" + +case "${editor##*/}" in nvim | vim | nano | micro | hx | helix | fresh) - exec omarchy-launch-tui "$EDITOR" "$@" + exec omarchy-launch-tui "$editor" "$@" ;; *) - exec setsid uwsm-app -- "$EDITOR" "$@" + exec setsid uwsm-app -- "$editor" "$@" ;; esac diff --git a/config/uwsm/default b/config/uwsm/default index 92836d0c..3b48ff37 100644 --- a/config/uwsm/default +++ b/config/uwsm/default @@ -6,8 +6,8 @@ export TERMINAL=xdg-terminal-exec # Used by terminal programs (like gh) to open URLs detached from the terminal process tree export BROWSER=omarchy-launch-browser -# Use code for VSCode -export EDITOR=nvim +# Used by terminal programs to open files with the selected Omarchy default editor +export EDITOR=omarchy-launch-editor # Use a custom directory for screenshots (remember to make the directory!) # export OMARCHY_SCREENSHOT_DIR="$HOME/Pictures/Screenshots" diff --git a/migrations/1779219192.sh b/migrations/1779219192.sh new file mode 100644 index 00000000..9affc389 --- /dev/null +++ b/migrations/1779219192.sh @@ -0,0 +1,23 @@ +echo "Move default editor selection to ~/.local/state/omarchy/defaults/editor" + +editor=$(sed -n 's/^export EDITOR=//p' ~/.config/uwsm/default 2>/dev/null | head -n 1) + +if [[ -z $editor || $editor == "omarchy-launch-editor" ]]; then + editor="nvim" +fi + +mkdir -p "$HOME/.local/state/omarchy/defaults" + +if [[ ! -f $HOME/.local/state/omarchy/defaults/editor ]]; then + printf '%s\n' "$editor" >"$HOME/.local/state/omarchy/defaults/editor" +fi + +if [[ -f ~/.config/uwsm/default ]]; then + if grep -q '^export EDITOR=' ~/.config/uwsm/default; then + sed -i 's|^export EDITOR=.*|export EDITOR=omarchy-launch-editor|' ~/.config/uwsm/default + else + printf '\n# Used by terminal programs to open files with the selected Omarchy default editor\nexport EDITOR=omarchy-launch-editor\n' >>~/.config/uwsm/default + fi +else + omarchy-refresh-config uwsm/default +fi