-
Notifications
You must be signed in to change notification settings - Fork 1
/
ifind-mode-plus.el
186 lines (156 loc) · 6.18 KB
/
ifind-mode-plus.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
;;; ifind-mode-plus.el --- A minor mode based on isearch, for interactively finding
;;; files in the workspace.
;; (c) 2016 Djangoliv'
;; from Christian Rovner ifind-mode
;; 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., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
;; Installation:
;;
;; 1. Copy this file into your .emacs.d/ directory
;; 2. Add these lines to your .emacs file:
;; (defvar workspace-dir "~/YOUR/WORKSPACE/DIR")
;; (load "~/.emacs.d/ifind-mode.el")
;;
;; Replace YOUR/WORKSPACE/DIR above by your actual workspace directory
;; Usage:
;;
;; Just type M-x ifind-mode RET
;; Then type part of the filename you're searching for. A list of matching
;; filenames in your workspace will appear, and it will get smaller as you keep
;; adding characters. Use the up/down arrows to navigate and press RET to visit
;; the file under the cursor. Any other key will abort the search.
;;; Code:
(eval-when-compile
(require 'cl))
(defvar ifind-dir workspace-dir
"Directory where to search files on.")
(defvar ifind-excluded-dirs-list vc-directory-exclusion-list
"directory list to exclude")
(defvar ifind-excluded-files-list '("*.pyc" "*.o")
"file list to exclude")
(defvar ifind-project-list nil "list of project")
(defvar ifind-command "find %s \\( %s \\) -prune -o -type f %s -iname \"*%s*\" -print"
"Shell command that performs the file search.
It's a string with three %s that get replaced by:
1. The root directory where to search files on
2. The directories to exclude; built using vc-directory-exclusion-list
3. The string to search for in the filenames")
(defvar ifind-min-length 2
"Minimum length of the search string to trigger the shell command.")
(defvar ifind-string ""
"The current search string.")
(defvar ifind-mode nil
"Name of the minor mode, if non-nil.")
(defvar ifind-mode-map
(let ((i ?\s)
(map (make-keymap)))
;; Printing characters, search
(while (< i 256)
(define-key map (vector i) 'ifind-printing-char)
(setq i (1+ i)))
(define-key map [up] 'previous-line)
(define-key map [down] 'next-line)
(define-key map [return] 'ifind-visit-file)
(define-key map [backspace] 'ifind-del-char)
;; All other keys will abort ifind
(define-key map [t] 'ifind-abort)
map)
"Keymap for `ifind-mode'.")
(defvar ifind-excluded-dirs
"part of the find command which exclude directories")
(defvar ifind-excluded-files
"part of the find command which exclude files")
;; Add ifind-mode to minor mode list
(or (assq 'ifind-mode minor-mode-alist)
(nconc minor-mode-alist
(list '(ifind-mode ifind-mode))))
;; except dirs and files
(defun ifind-excluded-string ()
(setq ifind-excluded-dirs
(concat "-path "
(mapconcat #'(lambda (dir)
(shell-quote-argument (concat "*/" dir)))
ifind-excluded-dirs-list
" -o -path ")))
(setq ifind-excluded-files
(concat "-not -name "
(mapconcat #'(lambda (file)
(shell-quote-argument file))
ifind-excluded-files-list
" -not -name "))))
(defvar ifind-current-project "GLOBAL" "")
(defun ifind-switch-project (project)
"Change project root"
(interactive
(list
(completing-read "Project: " ifind-project-list)))
(loop for (key . value) in ifind-project-list
collect (progn
(if (equal key project)
(progn
(setq ifind-dir value)
(setq ifind-current-project key)
(message (concat "Switch to Project: " key)))))))
(defun ifind-mode ()
"Start Ifind minor mode."
(interactive)
(setq ifind-string ""
ifind-mode " Ifind"
overriding-terminal-local-map ifind-mode-map)
(looking-at "")
(force-mode-line-update)
(ifind-update))
(defun ifind-printing-char ()
"Add this ordinary printing character to the search string and search."
(interactive)
(let ((char last-command-event))
(setq ifind-string (concat ifind-string (char-to-string char)))
(ifind-update)))
(defun ifind-abort ()
"Abort Ifind."
(interactive)
(ifind-exit)
(message "Ifind aborted."))
(defun ifind-visit-file ()
"Open the file under the cursor in the *ifind* buffer."
(interactive)
(set-buffer (concat "*ifind-[" ifind-current-project "]*"))
(let ((filename (buffer-substring-no-properties
(line-beginning-position) (line-end-position))))
(if (> (length filename) 0)
(find-file filename)))
(ifind-exit))
(defun ifind-del-char ()
"Delete character from end of search string and search again."
(interactive)
(when (> (length ifind-string) 0)
(setq ifind-string (substring ifind-string 0 -1))
(ifind-update)))
(defun ifind-exit ()
"Exit Ifind minor mode."
(setq ifind-mode nil
overriding-terminal-local-map nil)
(force-mode-line-update)
(kill-buffer (concat "*ifind-[" ifind-current-project "]*")))
(defun ifind-update ()
"Display the current search string and search for files."
(ifind-excluded-string)
(switch-to-buffer (concat "*ifind-[" ifind-current-project "]*"))
(message (format ifind-command ifind-dir ifind-excluded-dirs ifind-excluded-files ifind-string))
(let ((message-log-max nil))
(if (>= (length ifind-string) ifind-min-length)
(shell-command
(format ifind-command ifind-dir ifind-excluded-dirs ifind-excluded-files ifind-string)
(concat "*ifind-[" ifind-current-project "]*")))
(message "Find files matching: %s" ifind-string)))
;;; ifind-mode-plus.el ends here