diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-09 08:35:09 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-09 08:35:09 +0300 |
| commit | f2cbc011c6f58e1ebcb6080cec2f1aed4a8a6119 (patch) | |
| tree | e6265091d54e4085659b1d471c569bb7de5f018c /fish | |
| parent | 68373439d4ab92f0b988bfbc42f38d7f6edbbc2b (diff) | |
Update
Diffstat (limited to 'fish')
| -rw-r--r-- | fish/conf.d/editor.fish | 71 |
1 files changed, 63 insertions, 8 deletions
diff --git a/fish/conf.d/editor.fish b/fish/conf.d/editor.fish index 8706eea..5f2c8f8 100644 --- a/fish/conf.d/editor.fish +++ b/fish/conf.d/editor.fish @@ -3,29 +3,84 @@ set -gx VISUAL $EDITOR set -gx GIT_EDITOR $EDITOR set -gx HELIX_CONFIG_DIR $HOME/.config/helix +function editor::helix::lock_owner_pid + set -l lock $argv[1] + if not test -f "$lock" + return 1 + end + + set -l pid (string trim -- (head -n 1 "$lock" 2>/dev/null)) + if not string match -rq '^[0-9]+$' -- "$pid" + return 1 + end + + echo $pid +end + +function editor::helix::lock_owner_is_active_fish + set -l lock $argv[1] + set -l pid (editor::helix::lock_owner_pid "$lock") + if test $status -ne 0 + return 1 + end + + if not kill -0 $pid 2>/dev/null + return 1 + end + + set -l command_name (string trim -- (ps -p $pid -o comm= 2>/dev/null)) + if not string match -rq '(^|/)fish$' -- "$command_name" + return 1 + end + + return 0 +end + function editor::helix::open_with_lock set -l file $argv[1] set -l lock "$file.lock" if test -f "$lock" - echo "File lock $lock exists! Another instance is editing it?" - return 2 + if editor::helix::lock_owner_is_active_fish "$lock" + set -l pid (editor::helix::lock_owner_pid "$lock") + echo "File lock $lock exists and is owned by active fish shell PID $pid." + return 2 + end + + echo "Removing stale or invalid file lock $lock." + rm -f "$lock" end - touch $lock + + printf '%s\n' $fish_pid >"$lock" hx $file $argv[2..-1] - rm $lock + set -l hx_status $status + rm -f "$lock" + return $hx_status end function editor::helix::open_with_lock::force set -l file $argv[1] set -l lock "$file.lock" + set -l should_kill 0 if test -f "$lock" - echo "File lock $lock exists! Force deleting it and terminating all $EDITOR instances?" - rm -f $lock + if editor::helix::lock_owner_is_active_fish "$lock" + set -l pid (editor::helix::lock_owner_pid "$lock") + echo "File lock $lock exists and is owned by active fish shell PID $pid. Force deleting it and terminating all $EDITOR instances?" + set should_kill 1 + else + echo "Removing stale or invalid file lock $lock." + end + rm -f "$lock" + end + + if test $should_kill -eq 1 pkill -f $EDITOR end - touch $lock + + printf '%s\n' $fish_pid >"$lock" hx $file $argv[2..-1] - rm $lock + set -l hx_status $status + rm -f "$lock" + return $hx_status end function editor::helix::edit::remote |
