config-search.el (5281B)
1 ;;; config-search.el --- -*- lexical-binding: t; -*- 2 ;;; Commentary: 3 ;;; Search related configuration 4 ;;; Code: 5 6 ;; UseISearch 7 (use-package isearch 8 :unless noninteractive 9 :config 10 (defun my-project-search-from-isearch () 11 (interactive) 12 (let ((query (if isearch-regexp 13 isearch-string 14 (regexp-quote isearch-string)))) 15 (isearch-update-ring isearch-string isearch-regexp) 16 (let (search-nonincremental-instead) 17 (ignore-errors (isearch-done t t))) 18 (project-find-regexp query))) 19 (defun my-occur-from-isearch () 20 (interactive) 21 (let ((query (if isearch-regexp 22 isearch-string 23 (regexp-quote isearch-string)))) 24 (isearch-update-ring isearch-string isearch-regexp) 25 (let (search-nonincremental-instead) 26 (ignore-errors (isearch-done t t))) 27 (occur query))) 28 (setq-default search-whitespace-regexp ".*?" 29 isearch-lax-whitespace t 30 isearch-regexp-lax-whitespace nil 31 isearch-lazy-count t 32 lazy-count-prefix-format nil 33 lazy-count-suffix-format " (%s/%s)") 34 (defun stribb/isearch-region (&optional not-regexp no-recursive-edit) 35 "If a region is active, make this the isearch default search 36 pattern." 37 (interactive "P\np") 38 (when (use-region-p) 39 (let ((search (buffer-substring-no-properties 40 (region-beginning) 41 (region-end)))) 42 (message "stribb/ir: %s %d %d" search (region-beginning) (region-end)) 43 (setq deactivate-mark t) 44 (isearch-yank-string search)))) 45 (advice-add 'isearch-forward-regexp :after 'stribb/isearch-region) 46 (advice-add 'isearch-forward :after 'stribb/isearch-region) 47 (advice-add 'isearch-backward-regexp :after 'stribb/isearch-region) 48 (advice-add 'isearch-backward :after 'stribb/isearch-region) 49 50 (defun contrib/isearchp-remove-failed-part-or-last-char () 51 "Remove failed part of search string, or last char if successful. 52 Do nothing if search string is empty to start with." 53 (interactive) 54 (if (equal isearch-string "") 55 (isearch-update) 56 (if isearch-success 57 (isearch-delete-char) 58 (while (isearch-fail-pos) (isearch-pop-state))) 59 (isearch-update))) 60 61 (defun contrib/isearch-done-opposite-end (&optional nopush edit) 62 "End current search in the opposite side of the match. 63 Particularly useful when the match does not fall within the 64 confines of word boundaries (e.g. multiple words)." 65 (interactive) 66 (funcall #'isearch-done nopush edit) 67 (when isearch-other-end (goto-char isearch-other-end))) 68 :bind (("M-s M-o" . multi-occur) 69 :map isearch-mode-map 70 ("C-o" . my-occur-from-isearch) 71 ("C-f" . my-project-search-from-isearch) 72 ("C-d" . isearch-forward-symbol-at-point) 73 ("DEL" . contrib/isearchp-remove-failed-part-or-last-char) 74 ("<C-return>" . contrib/isearch-done-opposite-end))) 75 ;; -UseISearch 76 77 ;; UseGrep 78 (use-package grep 79 :commands (find-grep grep find-grep-dired find-name-dired) 80 :bind (("M-s n" . find-name-dired) 81 ("M-s F" . find-grep) 82 ("M-s G" . grep) 83 ("M-s d" . find-grep-dired)) 84 :hook ((hook-mode . toggle-truncate-lines)) 85 :config 86 (setq-default grep-template (string-join '("ugrep" 87 "--color=always" 88 "--ignore-binary" 89 "--ignore-case" 90 "--include=<F>" 91 "--line-number" 92 "--null" 93 "--recursive" 94 "--regexp=<R>") 95 " ")) 96 (add-to-list 'grep-find-ignored-directories "auto") 97 (add-to-list 'grep-find-ignored-directories "elpa")) 98 ;; -UseGrep 99 100 ;; UseWgrep 101 (use-package wgrep 102 :unless noninteractive 103 :commands (wgrep-change-to-wgrep-mode) 104 :defer 2 105 :custom 106 (wgrep-auto-save-buffer t) 107 (wgrep-change-readonly-file t)) 108 ;; -UseWgrep 109 110 ;; UseRG 111 (use-package rg 112 :if *rg* 113 :commands (rg rg-project rg-dwim) 114 :bind (("M-s r r" . rg) 115 ("M-s r p" . rg-project) 116 ("M-s r s" . rg-dwim)) 117 :config 118 (setq rg-group-result t) 119 (setq rg-hide-command t) 120 (setq rg-show-columns nil) 121 (setq rg-show-header t) 122 (setq rg-default-alias-fallback "all") 123 (cl-pushnew '("tmpl" . "*.tmpl") rg-custom-type-aliases) 124 (cl-pushnew '("gotest" . "*_test.go") rg-custom-type-aliases) 125 (defun vde/rg-buffer-name () 126 "Generate a rg buffer name from project if in one" 127 (let ((p (project-root (project-current)))) 128 (if p 129 (format "rg: %s" (abbreviate-file-name p)) 130 "rg"))) 131 (setq rg-buffer-name #'vde/rg-buffer-name) 132 133 ;; (when (f-dir-p "~/src/tektoncd/") 134 ;; (rg-define-search rg-projects-tektoncd 135 ;; "Search tektoncd (projects)." 136 ;; :dir "~/src/tektoncd/" 137 ;; :files "*.*" 138 ;; :menu ("Projects" "t" "tektoncd"))) 139 ;; (when (f-dir-p "~/src/home/") 140 ;; (rg-define-search rg-projects-home 141 ;; "Search home." 142 ;; :dir "~/src/home/" 143 ;; :files "*.*" 144 ;; :menu ("Projects" "h" "home"))) 145 ) 146 147 ;; -UseRG 148 149 (provide 'config-search) 150 ;;; config-search.el ends here