-
Notifications
You must be signed in to change notification settings - Fork 41
/
rg-isearch.el
88 lines (71 loc) · 3.06 KB
/
rg-isearch.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
;;; rg-isearch.el --- rg integration into isearch -*- lexical-binding: t; -*-
;; Copyright (C) 2020 David Landell <david.landell@sunnyhill.email>
;;
;; Author: David Landell <david.landell@sunnyhill.email>
;; URL: https://github.com/dajva/rg.el
;; This file is not part of GNU Emacs.
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License
;; as published by the Free Software Foundation; either version 3
;; of the License, or (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301, USA.
;;; Commentary:
;; This file provides rg isearch integration.
;; Add this to your configuration to bind chosen key in isearch minibuffer:
;;
;; (define-key isearch-mode-map "\M-sr" 'rg-isearch-menu)
;;
;; See info node `(rgel)Isearch search' for documentation or online at https://rgel.readthedocs.io/.
;;; Code:
(require 'rg)
(defun rg-get-isearch-string ()
"Extract the isearch string from the last isearch."
;; Mimic what `isearch-occur' does.
(or (cond
;; We can't handle arbitrary functions so just use the string.
((functionp isearch-regexp-function) isearch-string)
;; isearch-occur handles this as word search so insert word boundary
(isearch-regexp-function (concat "\b" isearch-string "\b"))
(isearch-regexp isearch-string)
(t isearch-string))
""))
;;;###autoload (autoload 'rg-isearch-current-file "rg-isearch.el" "" t)
(rg-define-search rg-isearch-current-file
"Run ripgrep on current file searching for latest isearch string."
:dir current
:query (rg-get-isearch-string)
:format literal
:files (rg-get-buffer-file-name)
:dir current)
;;;###autoload (autoload 'rg-isearch-current-dir "rg-isearch.el" "" t)
(rg-define-search rg-isearch-current-dir
"Run ripgrep in current directory searching for latest isearch string
in files matching the current file type."
:query (rg-get-isearch-string)
:format literal
:files current
:dir current)
;;;###autoload (autoload 'rg-isearch-project "rg-isearch.el" "" t)
(rg-define-search rg-isearch-project
"Run ripgrep in current project searching for latest isearch string
in files matching the current file type."
:dir project
:query (rg-get-isearch-string)
:format literal
:files current)
;;;###autoload (autoload 'rg-isearch-menu "rg-isearch.el" "" t)
(transient-define-prefix rg-isearch-menu ()
"Show menu for rg isearch commands."
[:description "Search with ripgrep"
(3 "f" "File" rg-isearch-current-file--transient)
(3 "d" "Dir" rg-isearch-current-dir--transient)
(3 "p" "Project" rg-isearch-project--transient)])
(provide 'rg-isearch)
;;; rg-isearch.el ends here