<commit_before>" A basic fzf-powered grep doohickey.
"
" This file was written by <NAME>, <<EMAIL>>. This is
" free and unencumbered software released into the public domain. For
" more information, please refer to the accompanying "UNLICENCE" file.

function s:FuzzyGrep(abandon, query) abort
  function! s:FzfGrepHandler(abandon, lines) abort
    function! s:RgToQuickfix(line)
      let l:parts = split(a:line, ':')

      return {
        \ 'filename': parts[0],
        \ 'lnum': parts[1],
        \ 'col': parts[2],
        \ 'text': join(parts[3:], ':'),
      \ }
    endfunction

    if len(a:lines) < 2
      return
    endif

    let l:command = get(
      \ { 'ctrl-t': 'tabe', 'ctrl-v': 'vsplit', 'ctrl-x': 'split' },
      \ a:lines[0],
      \ 'e' . (a:abandon ? '!' : '')
    \ )
    let l:results = map(a:lines[1:], 's:RgToQuickfix(v:val)')

    execute l:command escape(l:results[0].filename, ' %#\')
    execute l:results[0].lnum
    execute 'normal!' l:results[0].col . '|zz'

    if len(l:results) > 1
      call setqflist(l:results)
      copen
      wincmd p
    endif
  endfunction

  if !a:abandon && getbufvar(bufname('%'), "&mod")
    echohl ErrorMsg
    echom "E37: No write since last change (add ! to override)"
    echohl None
    return
  endif

  let l:gitRoot = system('git rev-parse --show-toplevel 2>/dev/null')[:-2]
  let l:rgCommand = 'rg --hidden --glob=''!.git/objects'' --column --line-number --color=always --smart-case -- %s || true'
  let l:root = strlen(l:gitRoot) > 0 ? l:gitRoot : getcwd()

  call fzf#run(
    \ fzf#wrap(
      \ {
        \ 'dir': l:root,
        \ 'options': '--ansi --bind=ctrl-a:select-all,ctrl-d:deselect-all,ctrl-z:abort'
          \ . ' --bind "change:reload:sleep 0.05;' . printf(l:rgCommand, '{q}') . '"'
          \ . ' --disabled --delimiter=":" --expect=ctrl-t,ctrl-v,ctrl-x --multi'
          \ . ' --preview="(bat --color=always --style=plain {1} --highlight-line {2} || cat {1}) 2>/dev/null"'
          \ . ' --preview-window=''+{2}/3'' --prompt="(' . pathshorten(l:root) . ') > "'
          \ . ' --query=''' . shellescape(a:query) . '''',
        \ 'sink*': function('s:FzfGrepHandler', [a:abandon]),
        \ 'source': printf(l:rgCommand, shellescape(a:query)),
      \ }
    \ )
  \ )
endfunction

command! -nargs=* -complete=dir -bang FG call s:FuzzyGrep(<bang>0, <q-args>)<commit_msg>"fuzzy-grep.vim": Do some refactorin'.<commit_after>" A simple fzf-powered interactive grep/ripgrep doohickey.
"
" This file was written by <NAME>, <<EMAIL>>. This is
" free and unencumbered software released into the public domain. For
" more information, please refer to the accompanying "UNLICENCE" file.

if exists('g:loaded_fuzzy_grep')
  finish
endif

let g:fuzzy_grep_source_command = 'rg --color=always --column --hidden --glob="!.git/" --line-number --smart-case -- %s || true'
let g:loaded_fuzzy_grep = 1

function! s:FuzzyGrep(abandon, ...) abort
  if !executable('fzf') || !executable('rg') || !executable('git') || !exists('g:loaded_fzf')
    throw 'FuzzyGrep requires fzf, fzf.vim, Git, and ripgrep'
  endif

  let l:arguments = copy(a:000)
  let l:gitRoot = system('git rev-parse --show-toplevel 2>/dev/null')[:-2]
  let l:query = len(l:arguments) > 0 ? remove(l:arguments, -1) : ''
  let l:spec = copy(g:fzf_base_spec)

  call extend(l:spec, { 'dir': strlen(l:gitRoot) > 1 ? l:gitRoot : getcwd() })
  call extend(
    \ l:spec,
    \ {
      \ 'options': [
        \ '--ansi', '--bind',
        \ 'ctrl-a:select-all,ctrl-d:deselect-all,ctrl-z:abort','--bind',
        \ 'change:reload:sleep 0.05;' . printf(g:fuzzy_grep_source_command, '{q}'),
        \ '--disabled', '--delimiter', ':', '--expect',
        \ 'ctrl-t,ctrl-v,ctrl-x', '--multi', '--preview',
        \ g:fzf_preview_line_command, '--preview-window', '+{2}/3',
        \ '--prompt', '(' . pathshorten(l:spec.dir) . ') > ', '--query', l:query,
      \ ],
      \ 'sink*': function('s:FuzzyGrepHandler', [a:abandon]),
      \ 'source': printf(g:fuzzy_grep_source_command, shellescape(l:query)),
    \ }
  \ )
  call extend(l:spec.options, l:arguments)
  call fzf#run(l:spec)
endfunction

function! s:FuzzyGrepHandler(abandon, lines) abort
  if len(a:lines) < 2
    return
  endif

  let l:command = get(
    \ { 'ctrl-t': 'tabe', 'ctrl-v': 'vsplit', 'ctrl-x': 'split' },
    \ a:lines[0],
    \ 'e' . (a:abandon ? '!' : '')
  \ )

  " Handling the no-write-since-last-change error ourselves produces
  " a slightly better looking error message (otherwise you get a small
  " stack-track-looking thing).
  if l:command == 'e' && getbufvar(bufname('%'), '&mod')
    echohl ErrorMsg
    echom "E37: No write since last change (add ! to override)"
    echohl None
    return
  endif

  let l:results = map(a:lines[1:], 's:ToQuickfix(v:val)')

  execute l:command escape(l:results[0].filename, ' %#\')
  execute l:results[0].lnum
  execute 'normal!' l:results[0].col . '|zz'

  if len(l:results) > 1
    call setqflist(l:results)
    copen
    wincmd p
  endif
endfunction

function! s:ToQuickfix(line)
  let l:parts = split(a:line, ':')

  return {
    \ 'filename': parts[0],
    \ 'lnum': parts[1],
    \ 'col': parts[2],
    \ 'text': join(parts[3:], ':'),
  \ }
endfunction

command! -nargs=* -complete=dir -bang FG call s:FuzzyGrep(<bang>0, <f-args>)
{'fraction_non_alphanumeric': 0.19600075457460855, 'fraction_numerical': 0.010186757215619695, 'mean_word_length': 2.9186991869918697, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5738692', 'n_tokens_mistral': 2068, 'n_tokens_neox': 1988, 'n_words': 426}