From 351d55648ea26335b713140bfc0c4f69e2d70489 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Mon, 13 Oct 2025 15:25:24 +0530 Subject: Add grep-write command --- autoload/files.kak | 7 ------- autoload/git.kak | 2 +- autoload/grep.kak | 14 ++++++++++++++ scripts/apply_vimgrep_updates.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 autoload/grep.kak create mode 100755 scripts/apply_vimgrep_updates.js diff --git a/autoload/files.kak b/autoload/files.kak index 3f5d7b1..09477c1 100644 --- a/autoload/files.kak +++ b/autoload/files.kak @@ -1,5 +1,3 @@ -set-option global grepcmd "rg -S --vimgrep --hidden -g '!**/.git/**'" - def find -docstring "find files" -menu -params 1 %{ edit -existing %arg{1} } \ -shell-script-candidates %{ fd -t f --hidden --color=never -E .git } @@ -14,9 +12,4 @@ map global buffer s ':write' -docstring 'Save' declare-user-mode file map global user f ':enter-user-mode file' -docstring 'File mode' map global file f ':find ' -docstring 'Find files' -map global file g ':grep ' -docstring 'Grep' map global file n ':file-manager %val{buffile}' -docstring 'File manager' - -# TODO: Proper mappings for next/prev on results -hook global -always BufOpenFifo '\*grep\*' %{ map global normal ': grep-next-match' } -hook global -always BufOpenFifo '\*make\*' %{ map global normal ': make-next-error' } diff --git a/autoload/git.kak b/autoload/git.kak index 90e5f75..86f6d76 100644 --- a/autoload/git.kak +++ b/autoload/git.kak @@ -2,7 +2,7 @@ define-command gitui -params .. %{ connect terminal env "EDITOR=kcr edit" gitu % declare-user-mode git map global user g ':enter-user-mode git' -docstring 'Git mode' -map global git s ':gitu' -docstring 'Gitu tui' +map global git s ':gitui' -docstring 'Git tui' map global git A ':git add %val{buffile}' -docstring 'Add file' map global git c ':git add commit' -docstring 'Commit' map global git C ':git add commit --amend' -docstring 'Amend commit' diff --git a/autoload/grep.kak b/autoload/grep.kak new file mode 100644 index 0000000..cef77ee --- /dev/null +++ b/autoload/grep.kak @@ -0,0 +1,14 @@ +set-option global grepcmd "rg -S --vimgrep --hidden -g '!**/.git/**'" + +map global file g ':grep ' -docstring 'Grep' + +define-command grep-write %{ + execute-keys '%' # Consider selecting manually + evaluate-commands %sh{ + echo "$kak_selections" | "$kak_config/scripts/apply_vimgrep_updates.js" | xargs -i echo "info '{}'" + } +} + +# TODO: Proper mappings for next/prev on results +hook global -always BufOpenFifo '\*grep\*' %{ map global normal ': grep-next-match' } +hook global -always BufOpenFifo '\*make\*' %{ map global normal ': make-next-error' } diff --git a/scripts/apply_vimgrep_updates.js b/scripts/apply_vimgrep_updates.js new file mode 100755 index 0000000..9707aab --- /dev/null +++ b/scripts/apply_vimgrep_updates.js @@ -0,0 +1,29 @@ +#!/usr/bin/env bun + +const main = async () => { + let count = 0 + for await (const line of console) { + const [_, filepath, row, _col, text] = line.match(/(.*):(\d+):(\d+):(.*)/) ?? [] + if (filepath) { + const isApplied = await apply_edit(filepath, Number(row), text); + if (isApplied) count++ + } + } + return count +} + +const apply_edit = async (filepath, row, text) => { + if (!Number.isFinite(row) || row <= 0) return false; + const file = Bun.file(filepath) + const lines = (await file.text()).split('\n') + if (lines.length > row) { + lines[row - 1] = text + await Bun.write(filepath, lines.join('\n')) + return true + } + return false +} + +const changesCount = await main() +console.log(`Applied ${changesCount} changes`) + -- cgit v1.3.1