diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-10-13 15:25:24 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-10-21 13:39:45 +0530 |
| commit | 351d55648ea26335b713140bfc0c4f69e2d70489 (patch) | |
| tree | b43cc6559aec53278a032ebb99657ac537b8fcba | |
| parent | 38dc33178ed1ec08277782dba40c4fa55c07287c (diff) | |
| download | kakoune-config-351d55648ea26335b713140bfc0c4f69e2d70489.tar.gz kakoune-config-351d55648ea26335b713140bfc0c4f69e2d70489.zip | |
Add grep-write command
| -rw-r--r-- | autoload/files.kak | 7 | ||||
| -rw-r--r-- | autoload/git.kak | 2 | ||||
| -rw-r--r-- | autoload/grep.kak | 14 | ||||
| -rwxr-xr-x | scripts/apply_vimgrep_updates.js | 29 |
4 files changed, 44 insertions, 8 deletions
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<ret>' -docstring 'Save' declare-user-mode file map global user f ':enter-user-mode file<ret>' -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}<ret>' -docstring 'File manager' - -# TODO: Proper mappings for next/prev on results -hook global -always BufOpenFifo '\*grep\*' %{ map global normal <minus> ': grep-next-match<ret>' } -hook global -always BufOpenFifo '\*make\*' %{ map global normal <minus> ': make-next-error<ret>' } 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<ret>' -docstring 'Git mode' -map global git s ':gitu<ret>' -docstring 'Gitu tui' +map global git s ':gitui<ret>' -docstring 'Git tui' map global git A ':git add %val{buffile}<ret>' -docstring 'Add file' map global git c ':git add commit<ret>' -docstring 'Commit' map global git C ':git add commit --amend<ret>' -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 <minus> ': grep-next-match<ret>' } +hook global -always BufOpenFifo '\*make\*' %{ map global normal <minus> ': make-next-error<ret>' } 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`) + |
