-
Notifications
You must be signed in to change notification settings - Fork 1
/
my-find.el
148 lines (125 loc) · 4.59 KB
/
my-find.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;;; my-find --- General searching configuration
;;
;; Copyright (C) 2014 Alex Bennée
;;
;; Author: Alex Bennée <[email protected]>
;;
;; This file is not part of GNU Emacs.
;;
;; This file 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, or (at your option)
;; any later version.
;;
;;; Commentary:
;;
;; This provides a very basic find front-end for f5/f6. It mostly
;; defers to packages and leaves f5 to be over-ridden when running
;; under a project mode.
;;
;;; Code:
;; Require prerequisites
;; Variables
;; Code
(eval-when-compile (require 'use-package))
; we want get to editable grep buffers from most searches
(use-package wgrep
:ensure t
:bind (:map grep-mode-map
("w" . wgrep-change-to-wgrep-mode)))
(defun my-get-initial-string ()
"Return substring from thing-at-point or empty string."
(substring-no-properties (or (thing-at-point 'symbol) "")))
;; from https://github.com/abo-abo/swiper/issues/1068
(defun my-ivy-with-thing-at-point (cmd &optional dir)
"Wrap a call to CMD with setting "
(let ((ivy-initial-inputs-alist
(list
(cons cmd (my-get-initial-string)))))
(funcall cmd nil dir)))
(defun my-counsel-ag-from-here (&optional dir)
"Start ag but from the directory the file is in (otherwise I would
be using git-grep)."
(interactive "D")
(my-ivy-with-thing-at-point
'counsel-ag
(or dir (file-name-directory (buffer-file-name)))))
(defun my-counsel-git-grep ()
(interactive)
(my-ivy-with-thing-at-point
'counsel-git-grep))
(defun my-consult-rg-from-here (&optional dir)
"Start rg but from the directory the file is in (otherwise I would be using git-grep)."
(interactive "D")
(funcall #'consult-ripgrep
(or dir (file-name-directory (buffer-file-name)))
(my-get-initial-string)))
(use-package consult
:bind ("<f6>" . my-consult-rg-from-here))
(use-package flx
:ensure t)
;; See swiper-map for extra keys
(defun my-swoop-with-swiper ()
"Replicate helm-swoop but with ivy. C-c C-o into occur C-c C-p into wgrep."
(interactive)
(swiper (or (and (use-region-p) (buffer-substring (mark) (point)))
(thing-at-point 'symbol))))
(use-package swiper
:ensure t
:bind (("C-s" . swiper-isearch)
("C-c o" . my-swoop-with-swiper)))
(defun my-ivy-rich-switch-buffer-project (candidate)
"Find the project compile root of a CANDIDATE."
(with-current-buffer (get-buffer candidate)
default-directory))
(defun my-counsel-mini ()
"Emulate helm-mini with my own preferences."
(interactive)
(let (collection)
;; Go backwards in priority (as add to list prepends by default)
(setq collection
(-concat
;; the buffer list
(--map (propertize (format "buf: %s" (buffer-name it))
'action 'switch-to-buffer
'value it)
(buffer-list))
;; The recentf files
(--map (propertize (format "rf: %s" it)
'action 'find-file
'value it)
recentf-list)
;; The bookmarks
(--map
(propertize (format "bkm: %s" it)
'action 'bookmark-jump
'value it)
(bookmark-all-names))))
;; Finally
(let ((result (ivy-read "counsel-mini:" collection)))
(apply
(get-text-property 0 'action result)
(list (get-text-property 0 'value result))))))
(defun my-project-find (&optional directory)
"Search within `DIRECTORY' using various search helpers.
If no directory is passed try and infer from the `default-directory' or
variable `buffer-file-name'."
(interactive)
;; Use default-directory or buffer-name if nothing passed
(when (not directory)
(setq directory (or default-directory buffer-file-name)))
(let ((is-git-dir (locate-dominating-file directory ".git")))
(if (and is-git-dir (file-directory-p is-git-dir))
(let ((default-directory directory)
(initial (my-get-initial-string)))
(cond
((functionp 'consult-git-grep)
(funcall-interactively #'consult-git-grep is-git-dir initial))
((functionp 'counsel-git-grep)
(call-interactively #'my-counsel-git-grep t (vector initial)))
(t
(call-interactively #'vc-git-grep initial))))
(call-interactively (key-binding (kbd "<f6>")) t (vector directory)))))
(global-set-key (kbd "<f5>") 'my-project-find)
(provide 'my-find)
;;; my-find.el ends here