|
156 | 156 |
|
157 | 157 | (map [:n] :fl (bindf builtin.lsp_document_symbols ivy_config) |
158 | 158 | {:desc "Find symbols [LSP]"}) |
159 | | -(map [:n] :fs (bindf builtin.grep_string ivy_config) {:desc "Find string"}) |
160 | 159 | (map [:n] :fz (bindf builtin.grep_string fzf_opts_theme) |
161 | 160 | {:desc "Fuzzy grep string"}) |
162 | 161 | (map [:n] :fb (bindf builtin.buffers ivy_config) {:desc "Buffer list"}) |
|
283 | 282 | (tset self.state :active :file) |
284 | 283 | ((. file-previewer :preview) file-previewer entry status))))}))) |
285 | 284 |
|
286 | | -(fn new_magic_finder_job [opts] |
| 285 | +(fn new_magic_finder [opts] |
287 | 286 | "Create a new finder job for the magic picker" |
288 | 287 | (var fcmd [:fd :--type :f :--color :never :--follow :--max-results :5000]) |
289 | 288 | (set fcmd (cons fcmd [:--max-depth (. opts :fcmd_depth)])) |
290 | 289 | (when (. opts :hidden) |
291 | 290 | (set fcmd (cons fcmd :--hidden))) |
292 | 291 | (finders.new_oneshot_job fcmd opts)) |
293 | 292 |
|
| 293 | +(fn new_magic_grepper [opts] |
| 294 | + "Create a live grep finder job for the magic picker" |
| 295 | + (var gcmd [:rg :--color=never :--no-heading :--with-filename |
| 296 | + :--line-number :--column :--smart-case |
| 297 | + :--max-depth (. opts :fcmd_depth)]) |
| 298 | + (when (. opts :hidden) |
| 299 | + (set gcmd (cons gcmd :--hidden))) |
| 300 | + (finders.new_job |
| 301 | + (fn [query] |
| 302 | + (when (and query (not= query "")) |
| 303 | + (cons gcmd [query :-- (. opts :cwd)]))) |
| 304 | + (make_entry.gen_from_vimgrep opts) |
| 305 | + nil |
| 306 | + (. opts :cwd))) |
| 307 | + |
| 308 | +(fn update_magic_prompt_title [opts] |
| 309 | + (var mode (if (= (. opts :mode) :grep) :grep :find)) |
| 310 | + (var cwd (. opts :cwd)) |
| 311 | + (var relcwd (cwd:gsub (os.getenv :HOME) :$HOME)) |
| 312 | + (tset opts :prompt_title (.. mode " [d:" (. opts :fcmd_depth) "] " relcwd))) |
| 313 | + |
294 | 314 | (fn refresh [picker opts popts] |
295 | 315 | "Refresh the picker with new options" |
| 316 | + (update_magic_prompt_title opts) |
| 317 | + (when (and picker.prompt_border picker.prompt_border.change_title) |
| 318 | + (picker.prompt_border:change_title (. opts :prompt_title))) |
296 | 319 | (picker.refresh_previewer picker) |
297 | | - (picker.refresh picker (new_magic_finder_job opts) popts)) |
298 | | - |
299 | | -(fn update_magic_prompt_title [opts] |
300 | | - (tset opts :prompt_title (.. "Magic in " (. opts :cwd)))) |
| 320 | + (var finder (if (= (. opts :mode) :grep) |
| 321 | + (new_magic_grepper opts) |
| 322 | + (new_magic_finder opts))) |
| 323 | + (picker.refresh picker finder popts)) |
301 | 324 |
|
302 | 325 | (var magic (fn [_opts] |
303 | 326 | (var opts (deep-copy (or _opts {}))) |
304 | 327 | (var cwd (utils.buffer_dir)) |
305 | 328 | (tset opts :fcmd_depth 4) |
306 | 329 | (tset opts :cwd cwd) |
307 | | - (tset opts :entry_maker (make_entry.gen_from_file opts)) |
| 330 | + (tset opts :mode (or (. opts :mode) :files)) |
| 331 | + (tset opts :entry_maker (if (= (. opts :mode) :grep) |
| 332 | + (make_entry.gen_from_vimgrep opts) |
| 333 | + (make_entry.gen_from_file opts))) |
308 | 334 | (update_magic_prompt_title opts) |
309 | 335 | (var p |
310 | 336 | (pickers.new opts |
311 | | - {:finder (new_magic_finder_job opts) |
| 337 | + {:finder (if (= (. opts :mode) :grep) |
| 338 | + (new_magic_grepper opts) |
| 339 | + (new_magic_finder opts)) |
312 | 340 | :previewer (magic-previewer opts) |
313 | 341 | :sorter (conf.file_sorter opts) |
314 | 342 | :cache_picker false |
315 | | - :attach_mappings (fn [_ map] |
| 343 | + :attach_mappings (fn [buf map] |
316 | 344 | (map [:n] :S |
317 | 345 | (fn [] |
318 | 346 | ;; decrease search depth |
319 | | - (when (> (. opts |
320 | | - :fcmd_depth) |
321 | | - 1) |
322 | | - (tset opts |
323 | | - :fcmd_depth |
324 | | - (- (. opts |
325 | | - :fcmd_depth) |
326 | | - 1)) |
327 | | - (print (.. "search in " |
328 | | - (. opts |
329 | | - :cwd) |
330 | | - " @ depth " |
331 | | - (. opts |
332 | | - :fcmd_depth))) |
333 | | - (refresh p opts |
334 | | - {:reset_prompt false})))) |
| 347 | + (when (> (. opts :fcmd_depth) 1) |
| 348 | + (tset opts :fcmd_depth |
| 349 | + (- (. opts :fcmd_depth) 1)) |
| 350 | + (refresh p opts {:reset_prompt false})))) |
335 | 351 | (map [:n] :D |
336 | 352 | (fn [] |
337 | 353 | ;; increase search depth |
338 | | - (tset opts |
339 | | - :fcmd_depth |
340 | | - (+ (. opts |
341 | | - :fcmd_depth) |
342 | | - 1)) |
343 | | - (print (.. "search in " |
344 | | - (. opts |
345 | | - :cwd) |
346 | | - " @ depth " |
347 | | - (. opts |
348 | | - :fcmd_depth))) |
349 | | - (refresh p opts |
350 | | - {:reset_prompt false}))) |
| 354 | + (tset opts :fcmd_depth |
| 355 | + (+ (. opts :fcmd_depth) 1)) |
| 356 | + (refresh p opts {:reset_prompt false}))) |
351 | 357 | (map [:i :n] :<C-h> |
352 | 358 | (fn [] |
353 | 359 | (tset opts :hidden |
354 | 360 | (not (. opts |
355 | 361 | :hidden))) |
356 | | - (tset opts |
357 | | - :entry_maker |
358 | | - (make_entry.gen_from_file opts)) |
359 | 362 | (refresh p opts |
360 | 363 | {:reset_prompt false}))) |
361 | 364 | (map [:i :n] :<C-e> |
|
367 | 370 | nwd) |
368 | 371 | (set cwd nwd) |
369 | 372 | (tset opts :cwd cwd) |
370 | | - (update_magic_prompt_title opts) |
371 | | - (tset opts |
372 | | - :entry_maker |
373 | | - (make_entry.gen_from_file opts)) |
374 | | - (print (.. "search in " |
375 | | - (. opts |
376 | | - :cwd) |
377 | | - " @ depth " |
378 | | - (. opts |
379 | | - :fcmd_depth))) |
380 | | - (refresh p opts |
381 | | - {:reset_prompt false})))) |
| 373 | + (refresh p opts {:reset_prompt false})))) |
382 | 374 | (map [:i :n] :<tab> |
383 | 375 | (fn [] |
384 | | - (var nwd |
385 | | - (all_dirs_from_entry cwd)) |
386 | | - (when (should_cd cwd |
387 | | - nwd) |
| 376 | + (var nwd (all_dirs_from_entry cwd)) |
| 377 | + (when (should_cd cwd nwd) |
388 | 378 | (set cwd nwd) |
389 | 379 | (tset opts :cwd cwd) |
390 | | - (update_magic_prompt_title opts) |
391 | | - (tset opts |
392 | | - :entry_maker |
393 | | - (make_entry.gen_from_file opts)) |
394 | | - (print (.. "search in " |
395 | | - (. opts |
396 | | - :cwd) |
397 | | - " @ depth " |
398 | | - (. opts |
399 | | - :fcmd_depth))) |
400 | | - (refresh p opts |
401 | | - {:reset_prompt true |
402 | | - :prompt_title (.. "Magic in " |
403 | | - (. opts |
404 | | - :cwd))})))) |
| 380 | + (refresh p opts {:reset_prompt true})))) |
405 | 381 | (map [:i :n] :<S-tab> |
406 | 382 | (fn [] |
407 | 383 | (when (not (= cwd "/")) |
408 | | - (set cwd |
409 | | - (vim.fn.resolve (.. cwd |
410 | | - "/.."))) |
| 384 | + (set cwd (vim.fn.resolve (.. cwd "/.."))) |
411 | 385 | (tset opts :cwd cwd) |
412 | | - (update_magic_prompt_title opts) |
413 | | - (tset opts |
414 | | - :entry_maker |
415 | | - (make_entry.gen_from_file opts)) |
416 | | - (print (.. "search in " |
417 | | - (. opts |
418 | | - :cwd) |
419 | | - " @ depth " |
420 | | - (. opts |
421 | | - :fcmd_depth))) |
422 | | - (refresh p opts |
423 | | - {:reset_prompt false})))) |
| 386 | + (refresh p opts {:reset_prompt false})))) |
| 387 | + (map [:i :n] :<C-g> |
| 388 | + (fn [] |
| 389 | + (tset opts :mode |
| 390 | + (if (= (. opts :mode) :grep) |
| 391 | + :files |
| 392 | + :grep)) |
| 393 | + (if (= (. opts :mode) :grep) |
| 394 | + (tset opts :entry_maker |
| 395 | + (make_entry.gen_from_vimgrep opts)) |
| 396 | + (tset opts :entry_maker |
| 397 | + (make_entry.gen_from_file opts))) |
| 398 | + (refresh p opts {:reset_prompt false}))) |
424 | 399 | true)})) |
425 | 400 | (p.find p))) |
426 | 401 |
|
427 | 402 | (local themed_magic (bindf magic ivy_config)) |
428 | 403 | (local small_themed_magic (bindf magic small_ivy_config)) |
429 | 404 |
|
| 405 | +(fn magic-grep-cword [] |
| 406 | + (magic (merge-table ivy_config {:mode :grep |
| 407 | + :default_text (vim.fn.expand "<cword>")}))) |
| 408 | + |
| 409 | +(fn magic-grep-visual [] |
| 410 | + (vim.cmd "noau normal! \"vy") |
| 411 | + (var text (vim.fn.getreg :v)) |
| 412 | + (vim.fn.setreg :v []) |
| 413 | + (magic (merge-table ivy_config {:mode :grep :default_text text}))) |
| 414 | + |
430 | 415 | (map [:n] :F themed_magic {:desc "Find and move around"}) |
431 | 416 | (map [:n] :<C-f> small_themed_magic {:desc "Find and move around"}) |
| 417 | +(map [:n] :fs magic-grep-cword {:desc "Find string [magic grep]"}) |
| 418 | +(map [:v] :fs magic-grep-visual {:desc "Find selection [magic grep]"}) |
0 commit comments