init-search.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. ;;; init-search.el --- Search Configuration File -*- lexical-binding: t -*-
  2. ;;; Commentary:
  3. ;;; Code:
  4. (use-feature isearch
  5. :config
  6. (defface isearch-prompt
  7. '((t (:foreground "gold")))
  8. "Face for isearch minibuffer prompt."
  9. :group 'isearch)
  10. ;; https://www.emacs.dyerdwelling.family/emacs/20230503211610-emacs--isearch-occur-advice-window-focus/
  11. (defun isearch-occur-advice (origin &rest args)
  12. (isearch-exit)
  13. (select-window (get-buffer-window "*Occur*"))
  14. (goto-char (point-min)))
  15. (advice-add 'isearch-occur :after 'isearch-occur-advice)
  16. :custom
  17. (search-whitespace-regexp ".*\\b")
  18. (isearch-lax-whitespace t)
  19. (isearch-allow-scroll t)
  20. (isearch-yank-on-move 'shift)
  21. (isearch-lazy-count t)
  22. (lazy-count-prefix-format nil)
  23. (lazy-count-suffix-format " (%s/%s)")
  24. (lazy-highlight-initial-delay 0)
  25. (isearch-message-properties '(read-only t cursor-intangible t face isearch-prompt))
  26. :bind-keymap ("C-c s" . search-map) ;; M-s clashes with paredit/smartparens bindings
  27. :bind
  28. (:map search-map
  29. ("<" . isearch-beginning-of-buffer)
  30. (">" . isearch-end-of-buffer)))
  31. (use-feature casual-isearch
  32. :after isearch
  33. :bind (:map isearch-mode-map ("C-o" . casual-isearch-tmenu))
  34. :config
  35. ;; Replace isearch-query-replace functions with their aznu equivalents
  36. (transient-replace-suffix 'casual-isearch-tmenu "r" '("r" "Start ‘anzu-query-replace’" anzu-isearch-query-replace
  37. :if-nil buffer-read-only))
  38. (transient-replace-suffix 'casual-isearch-tmenu "x" '("x" "Start ‘anzu-query-replace-regexp’" anzu-isearch-query-replace-regexp
  39. :if-nil buffer-read-only))
  40. ;; Add consult-line to Misc section
  41. (transient-append-suffix 'casual-isearch-tmenu "u" '("l" "consult-line" consult-line)))
  42. (use-package isearch-dabbrev
  43. :after isearch
  44. :bind (:map isearch-mode-map ("M-/" . isearch-dabbrev-expand)))
  45. (use-package anzu
  46. :diminish
  47. :config
  48. (global-anzu-mode)
  49. (set-face-attribute 'anzu-mode-line nil :foreground "yellow" :weight 'bold)
  50. :custom
  51. (anzu-deactivate-region t)
  52. (anzu-search-threshold 1000)
  53. (anzu-replace-threshold 100)
  54. (anzu-replace-to-string-separator " => ")
  55. :bind
  56. ([remap query-replace] . anzu-query-replace)
  57. ([remap query-replace-regexp] . anzu-query-replace-regexp)
  58. (:map isearch-mode-map
  59. ([remap isearch-query-replace] . anzu-isearch-query-replace)
  60. ([remap isearch-query-replace-regexp] . anzu-isearch-query-replace-regexp)))
  61. (use-package rg
  62. :bind
  63. ("C-c C-M-S-r" . rg-menu)
  64. ("C-c C-M-r" . rg)
  65. ("C-z" . rg-dwim)
  66. (:map search-map ("s" . rg)))
  67. (use-package deadgrep
  68. :config
  69. (defun deadgrep-current-directory (search-term)
  70. (interactive (list (deadgrep--read-search-term)))
  71. (deadgrep search-term (file-name-directory buffer-file-name)))
  72. (defvar deadgrep--include-all nil)
  73. (defun deadgrep--include-all-advice (rg-args)
  74. (if deadgrep--include-all
  75. (push "-uuuLz" rg-args)
  76. rg-args))
  77. (advice-add 'deadgrep--arguments :filter-return #'deadgrep--include-all-advice)
  78. (defun deadgrep-all (search-term)
  79. (interactive (list (deadgrep--read-search-term)))
  80. (let ((deadgrep--include-all t))
  81. (deadgrep search-term)))
  82. :hook (deadgrep-mode . next-error-follow-minor-mode)
  83. :bind
  84. ("C-c c d" . deadgrep)
  85. ("C-c c M-d" . deadgrep-all)
  86. ("C-c c C-d" . deadgrep-current-directory)
  87. (:map deadgrep-mode-map
  88. ("e" . deadgrep-edit-mode)
  89. ("{" . deadgrep-backward-filename)
  90. ("}" . deadgrep-forward-filename))
  91. (:map search-map
  92. ("d" . deadgrep)
  93. ("M-d" . deadgrep-all)
  94. ("C-d" . deadgrep-current-directory)))
  95. (use-package affe
  96. :config
  97. (setq affe-grep-command (replace-regexp-in-string "rg" "rg -Suu" affe-grep-command))
  98. ;; Configure Orderless
  99. (defun affe-orderless-regexp-compiler (input _type _ignorecase)
  100. (setq input (cdr (orderless-compile input)))
  101. (cons input (apply-partially #'orderless--highlight input t)))
  102. (setq affe-regexp-compiler #'affe-orderless-regexp-compiler)
  103. (defalias 'affe-grep-symbol-at-point 'affe-grep)
  104. (defalias 'affe-find-symbol-at-point 'affe-find)
  105. (consult-customize
  106. affe-grep :preview-key "M-."
  107. affe-grep-symbol-at-point :initial (thing-at-point 'symbol) :preview-key "M-."
  108. affe-find-symbol-at-point :initial (thing-at-point 'symbol) :preview-key "M-.")
  109. :bind
  110. ("C-c C-#" . affe-grep)
  111. ("C-c z" . affe-find)
  112. ("C-c Z" . affe-find-symbol-at-point)
  113. ("C-c C-~" . affe-grep-symbol-at-point)
  114. (:map search-map
  115. ("#" . affe-grep)
  116. ("~" . affe-grep-symbol-at-point)
  117. ("a" . affe-find)
  118. ("A" . affe-find-symbol-at-point)))
  119. (provide 'init-search)
  120. ;;; init-search.el ends here