port :Grep to lua

Sun, 12 Mar 2023 01:58:19 -0600

author
Meredith Howard <mhoward@roomag.org>
date
Sun, 12 Mar 2023 01:58:19 -0600
changeset 1079
b59861305252
parent 1078
aa4c1aa529a5
child 1080
83c9f8460bde

port :Grep to lua

.config/nvim/autoload/vimrc.vim file | annotate | diff | comparison | revisions
.config/nvim/lua/config/commands.lua file | annotate | diff | comparison | revisions
.config/nvim/plugin/vimrc/commands.vim file | annotate | diff | comparison | revisions
--- a/.config/nvim/autoload/vimrc.vim
+++ b/.config/nvim/autoload/vimrc.vim
@@ -6,18 +6,6 @@ func! vimrc#AutoFmtToggle() abort
   endif
 endfunc
 
-func! vimrc#Grep(...) abort
-  let pattern = get(a:000, 0, expand('<cword>'))
-  let cmd = join([&grepprg, shellescape(pattern)] + a:000[1:], ' ')
-
-  echo cmd
-  cgetexpr system(cmd)
-  call setqflist([], 'a', {"title": cmd})
-  let @/ = '\v' . pattern
-  copen
-  cfirst
-endfunc
-
 func! vimrc#SafeFilterFile(cmd)
   let errors = tempname()
   try
--- a/.config/nvim/lua/config/commands.lua
+++ b/.config/nvim/lua/config/commands.lua
@@ -17,6 +17,20 @@ command("Gcd", function()
   end
 end, {})
 
+command("Grep", function(ctx)
+  local pattern = ctx.fargs[1] or fn.expand("<cword>")
+  local grepcmd = table.concat({
+    vim.o.grepprg,
+    fn.shellescape(pattern),
+    table.concat(vim.list_slice(ctx.fargs, 2, #ctx.fargs), " "),
+  }, " ")
+
+  fn.setqflist({}, " ", { title = grepcmd, lines = fn.systemlist(grepcmd) })
+  fn.setreg("/", [[\V]] .. pattern)
+  cmd.copen()
+  cmd.cfirst()
+end, { nargs = "*", complete = "file" })
+
 calias("Q", "q")
 calias("Qa", "qa")
 calias("W", "w")
--- a/.config/nvim/plugin/vimrc/commands.vim
+++ b/.config/nvim/plugin/vimrc/commands.vim
@@ -6,5 +6,3 @@ command! PruneSession call vimrc#PruneSe
 " Preview markdown mail -- I edit with headers so I box them in a code block.
 command! MailPreview     enew | set bt=nofile | 0r # | exe 'norm! 0O```<Esc>}O```' | silent exe '%!mutt-md2html | mutt-html2txt' | 0
 command! MailPreviewHTML enew | set bt=nofile | setf html | 0r # | exe 'norm! 0O```<Esc>}O```' | silent exe '%!mutt-md2html' | 0
-
-command! -nargs=* -complete=file Grep call vimrc#Grep(<f-args>)

mercurial