grep.kak (2370B)
1 declare-option -docstring "shell command run to search for subtext in a file/directory" \ 2 str grepcmd 'grep -RHn' 3 4 provide-module grep %{ 5 6 require-module fifo 7 require-module jump 8 9 define-command -params .. -docstring %{ 10 grep [<arguments>]: grep utility wrapper 11 All optional arguments are forwarded to the grep utility 12 Passing no argument will perform a literal-string grep for the current selection 13 } grep %{ 14 evaluate-commands -save-regs gs %{ 15 set-register g %opt{grepcmd} 16 set-register s %val{selection} 17 evaluate-commands -try-client %opt{toolsclient} %{ 18 fifo -name *grep* -script %{ 19 trap - INT QUIT 20 grepcmd=${kak_reg_g} 21 selection=${kak_reg_s} 22 if [ $# -eq 0 ]; then 23 case "$grepcmd" in 24 ag\ * | git\ grep\ * | grep\ * | rg\ * | ripgrep\ * | ugrep\ * | ug\ *) 25 set -- -F -- "$selection" 26 ;; 27 ack\ *) 28 set -- -Q -- "$selection" 29 ;; 30 *) 31 set -- -- "$selection" 32 ;; 33 esac 34 fi 35 eval "$grepcmd \"\$@\"" 2>&1 | tr -d '\r' 36 } -- %arg{@} 37 set-option buffer filetype grep 38 set-option buffer jump_current_line 0 39 } 40 } 41 } 42 complete-command grep file 43 44 hook -group grep-highlight global WinSetOption filetype=grep %{ 45 add-highlighter window/grep group 46 add-highlighter window/grep/ regex "^([^:\n]+):(\d+):(\d+)?" 1:cyan 2:green 3:green 47 add-highlighter window/grep/ line %{%opt{jump_current_line}} default+b 48 hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/grep } 49 } 50 51 hook global WinSetOption filetype=grep %{ 52 hook buffer -group grep-hooks NormalKey <ret> jump 53 hook -once -always window WinSetOption filetype=.* %{ remove-hooks buffer grep-hooks } 54 } 55 56 define-command -hidden grep-jump %{ 57 jump 58 } 59 60 define-command grep-next-match -docstring %{alias for "jump-next *grep*"} %{ 61 jump-next -matching \*grep(-.*)?\* 62 } 63 64 define-command grep-previous-match -docstring %{alias for "jump-previous *grep*"} %{ 65 jump-previous -matching \*grep(-.*)?\* 66 } 67 68 } 69 70 hook -once global KakBegin .* %{ require-module grep }