10491
|
1 ;;; find-file.el --- find a file corresponding to this one given a pattern
|
|
2
|
|
3 ;; Author: Henry Guillaume <henry@qbd.com.au>
|
|
4 ;; Keywords: c, matching, tools
|
|
5
|
|
6 ;; Copyright (C) 1994 Free Software Foundation, Inc.
|
|
7
|
|
8 ;;; This file is part of GNU Emacs.
|
|
9
|
|
10 ;;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
11 ;;; it under the terms of the GNU General Public License as published by
|
|
12 ;;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;;; any later version.
|
|
14
|
|
15 ;;; GNU Emacs is distributed in the hope that it will be useful,
|
|
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 ;;; GNU General Public License for more details.
|
|
19
|
|
20 ;;; You should have received a copy of the GNU General Public License
|
|
21 ;;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
22 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
23
|
|
24 ;;; Commentary:
|
|
25
|
|
26 ;; PURPOSE:
|
|
27 ;; This package features a function called ff-find-other-file, which performs
|
|
28 ;; the following function:
|
|
29 ;;
|
|
30 ;; When in a .c file, find the first corresponding .h file in a set
|
|
31 ;; of directories and display it, and vice-versa from the .h file.
|
|
32 ;;
|
|
33 ;; Many people maintain their include file in a directory separate to their
|
|
34 ;; src directory, and very often you may be editing a file and have a need to
|
|
35 ;; visit the "other file". This package searches through a set of directories
|
|
36 ;; to find that file.
|
|
37 ;;
|
|
38 ;; THE "OTHER FILE", or "corresponding file", generally has the same basename,
|
|
39 ;; and just has a different extension as described by the ff-other-file-alist
|
|
40 ;; variable:
|
|
41 ;;
|
|
42 ;; '(("\\.cc$" (".hh" ".h"))
|
|
43 ;; ("\\.hh$" (".cc" ".C" ".CC" ".cxx" ".cpp")))
|
|
44 ;;
|
|
45 ;; If the current file has a .cc extension, ff-find-other-file will attempt
|
|
46 ;; to look for a .hh file, and then a .h file in some directory as described
|
|
47 ;; below. The mechanism here is to replace the matched part of the original
|
|
48 ;; filename with each of the corresponding extensions in turn.
|
|
49 ;;
|
|
50 ;; Alternatively, there are situations where the filename of the other file
|
|
51 ;; cannot be determined easily with regexps. For example, a .c file may
|
|
52 ;; have two corresponding .h files, for its public and private parts, or
|
|
53 ;; the filename for the .c file contains part of the pathname of the .h
|
|
54 ;; file, as between src/fooZap.cc and include/FOO/zap.hh. In that case, the
|
|
55 ;; format above can be changed to include a function to be called when the
|
|
56 ;; current file matches the regexp:
|
|
57 ;;
|
|
58 ;; '(("\\.cc$" cc-function)
|
|
59 ;; ("\\.hh$" hh-function))
|
|
60 ;;
|
|
61 ;; These functions must return a list consisting of the possible names of the
|
|
62 ;; corresponding file, with or without path. There is no real need for more
|
|
63 ;; than one function, and one could imagine the following value for cc-other-
|
|
64 ;; file-alist:
|
|
65 ;;
|
|
66 ;; (setq cc-other-file-alist
|
|
67 ;; '(("\\.cc$" ff-cc-hh-converter)
|
|
68 ;; ("\\.hh$" ff-cc-hh-converter)
|
|
69 ;; ("\\.c$" (".h"))
|
|
70 ;; ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp"))))
|
|
71 ;;
|
|
72 ;; ff-cc-hh-converter is included at the end of this file as a reference.
|
|
73 ;;
|
|
74 ;; SEARCHING is carried out in a set of directories specified by the
|
|
75 ;; ff-search-directories variable:
|
|
76 ;;
|
|
77 ;; ("." "../../src" "../include/*" "/usr/local/*/src/*" "$PROJECT/src")
|
|
78 ;;
|
|
79 ;; This means that the corresponding file will be searched for first in
|
|
80 ;; the current directory, then in ../../src, then in one of the directories
|
|
81 ;; under ../include, and so on. The star is _not_ a general wildcard
|
|
82 ;; character: it just indicates that the subdirectories of this directory
|
|
83 ;; must each be searched in turn. Environment variables will be expanded in
|
|
84 ;; the ff-search-directories variable.
|
|
85 ;;
|
|
86 ;; If the point is on a #include line, the file to be #included is searched
|
|
87 ;; for in the same manner. This can be disabled with the ff-ignore-include
|
|
88 ;; variable, or by calling ff-get-other-file instead of ff-find-other-file.
|
|
89 ;;
|
|
90 ;; If the file was not found, ff-find-other-file will prompt you for where
|
|
91 ;; to create the new "corresponding file" (defaults to the current directory),
|
|
92 ;; unless the variable ff-always-try-to-create is set to nil.
|
|
93 ;;
|
|
94 ;; GIVEN AN ARGUMENT (with the ^U prefix), ff-find-other-file will get the
|
|
95 ;; other file in another (the other?) window (see find-file-other-window and
|
|
96 ;; switch-to-buffer-other-window). This can be set on a more permanent basis
|
|
97 ;; by setting ff-always-in-other-window to t in which case the ^U prefix will
|
|
98 ;; do the opposite of what was described above.
|
|
99 ;;
|
|
100 ;; THERE ARE FIVE AVAILABLE HOOKS, called in this order if non-nil:
|
|
101 ;;
|
|
102 ;; - ff-pre-find-hooks - called before the search for the other file starts
|
|
103 ;; - ff-not-found-hooks - called when the other file could not be found
|
|
104 ;; - ff-pre-load-hooks - called just before the other file is 'loaded'
|
|
105 ;; - ff-file-created-hooks - called when the other file is created
|
|
106 ;; - ff-post-load-hooks - called just after the other file is 'loaded'
|
|
107 ;;
|
|
108 ;; The *load-hooks allow you to place point where you want it in the other
|
|
109 ;; file.
|
|
110
|
|
111 ;; LCD Archive Entry:
|
|
112 ;; find-file|Henry Guillaume|henry@qbd.com.au|
|
|
113 ;; Find a file associated with this buffer.|
|
|
114 ;; 21-Dec-1994|4.0|~/misc/find-file.el.Z|
|
|
115
|
|
116 ;; FEEDBACK:
|
|
117 ;; Please send me bug reports, bug fixes, and extensions, so that I can
|
|
118 ;; merge them into the master source.
|
|
119
|
|
120 ;; CREDITS:
|
|
121 ;; Many thanks go to TUSC Computer Systems Pty Ltd for providing an environ-
|
|
122 ;; ment that made the development of this package possible.
|
|
123 ;;
|
|
124 ;; Many thanks also go to all those who provided valuable feedback throughout
|
|
125 ;; the development of this package:
|
|
126 ;; Rolf Ebert in particular, Fritz Knabe, Heddy Boubaker, Sebastian Kremer,
|
|
127 ;; Vasco Lopes Paulo, Mark A. Plaksin, Robert Lang, Trevor West, Kevin
|
|
128 ;; Pereira & Benedict Lofstedt.
|
|
129
|
|
130 ;; Code:
|
|
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
132 ;; User definable variables:
|
|
133
|
|
134 (defvar ff-pre-find-hooks nil
|
|
135 "*List of functions to be called before the search for the file starts.")
|
|
136
|
|
137 (defvar ff-pre-load-hooks nil
|
|
138 "*List of functions to be called before the other file is loaded.")
|
|
139
|
|
140 (defvar ff-post-load-hooks nil
|
|
141 "*List of functions to be called after the other file is loaded.")
|
|
142
|
|
143 (defvar ff-not-found-hooks nil
|
|
144 "*List of functions to be called if the other file could not be found.")
|
|
145
|
|
146 (defvar ff-file-created-hooks nil
|
|
147 "*List of functions to be called if the other file needs to be created.")
|
|
148
|
|
149 (defvar ff-case-fold-search nil
|
|
150 "*Non-nil means ignore cases in matches (see case-fold-search).
|
|
151 If you have extensions in different cases, you will want this to be nil.")
|
|
152
|
|
153 (defvar ff-always-in-other-window nil
|
|
154 "*If non-nil, always open the other file in another window, unless an
|
|
155 argument is given to ff-find-other-file.")
|
|
156
|
|
157 (defvar ff-ignore-include nil
|
|
158 "*If non-nil, ignores include lines.")
|
|
159
|
|
160 (defvar ff-always-try-to-create t
|
|
161 "*If non-nil, always attempt to create the other file if it was not found.")
|
|
162
|
|
163 (defvar ff-quiet-mode nil
|
|
164 "*If non-nil, traces which directories are being searched.")
|
|
165
|
|
166 (defvar ff-special-constructs
|
|
167 '(
|
|
168 ;; C/C++ include, for NeXTSTEP too
|
|
169 ("^\#\\s *\\(include\\|import\\)\\s +[<\"]\\(.*\\)[>\"]" .
|
|
170 (lambda ()
|
|
171 (setq fname (buffer-substring (match-beginning 2) (match-end 2)))))
|
|
172
|
|
173 ;; Ada import
|
|
174 ("^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)" .
|
|
175 (lambda ()
|
|
176 (setq fname (buffer-substring (match-beginning 1) (match-end 1)))
|
|
177 (setq fname (concat (ada-make-filename-from-adaname fname)
|
|
178 ada-spec-suffix))))
|
|
179 )
|
|
180 "*A list of regular expressions specifying how to recognise special
|
|
181 constructs such as include files etc, and an associated method for
|
|
182 extracting the filename from that construct.")
|
|
183
|
|
184 (defvar ff-other-file-alist 'cc-other-file-alist
|
|
185 "*Alist of extensions to find given the current file's extension.
|
|
186
|
|
187 This list should contain the most used extensions before the others,
|
|
188 since the search algorithm searches sequentially through each
|
|
189 directory specified in ff-search-directories. If a file is not found,
|
|
190 a new one is created with the first matching extension (.cc yields .hh).
|
|
191 This alist should be set by the major-mode.")
|
|
192
|
|
193 (defvar ff-search-directories 'cc-search-directories
|
|
194 "*List of directories to search for a specific file.
|
|
195
|
|
196 Set by default to 'cc-search-directories, expanded at run-time.
|
|
197
|
|
198 This list is searched through with each extension specified in
|
|
199 ff-other-file-alist that matches this file's extension. So the
|
|
200 longer the list, the longer it'll take to realise that a file
|
|
201 may not exist.
|
|
202
|
|
203 A typical format is
|
|
204
|
|
205 '(\".\" \"/usr/include/*\" \"$PROJECT/*/include\")
|
|
206
|
|
207 Environment variables can be inserted between slashes ('/').
|
|
208 They will be replaced by their definition. If a variable does
|
|
209 not exist, it will (silently) be replaced with an empty string.
|
|
210
|
|
211 The stars are _not_ wildcards: they are searched for together with
|
|
212 the preceding slash. The star represents all the subdirectories except
|
|
213 '..', and each of these subdirectories will be searched in turn.")
|
|
214
|
|
215 (defvar cc-search-directories
|
|
216 '("." "/usr/include/*" "/usr/local/include/*")
|
|
217 "*See the description of the ff-search-directories variable.")
|
|
218
|
|
219 (defvar cc-other-file-alist
|
|
220 '(
|
|
221 ("\\.cc$" (".hh" ".h"))
|
|
222 ("\\.hh$" (".cc" ".C"))
|
|
223
|
|
224 ("\\.c$" (".h"))
|
|
225 ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp"))
|
|
226
|
|
227 ("\\.C$" (".H" ".hh" ".h"))
|
|
228 ("\\.H$" (".C" ".CC"))
|
|
229
|
|
230 ("\\.CC$" (".HH" ".H" ".hh" ".h"))
|
|
231 ("\\.HH$" (".CC"))
|
|
232
|
|
233 ("\\.cxx$" (".hh" ".h"))
|
|
234 ("\\.cpp$" (".hh" ".h"))
|
|
235 )
|
|
236 "*Alist of extensions to find given the current file's extension.
|
|
237
|
|
238 This list should contain the most used extensions before the others,
|
|
239 since the search algorithm searches sequentially through each directory
|
|
240 specified in ff-search-directories. If a file is not found, a new one
|
|
241 is created with the first matching extension (.cc yields .hh).")
|
|
242
|
|
243 (defvar ada-search-directories
|
|
244 '("." "/usr/adainclude" "/usr/local/adainclude")
|
|
245 "*See the description for the ff-search-directories variable.")
|
|
246
|
|
247 (defvar ada-other-file-alist
|
|
248 '(
|
|
249 ("\\.ads$" (".adb")) ;; Ada specs and bodies
|
|
250 ("\\.adb$" (".ads")) ;; GNAT filename conventions
|
|
251 )
|
|
252 "*Alist of extensions to find given the current file's extension.
|
|
253
|
|
254 This list should contain the most used extensions before the others,
|
|
255 since the search algorithm searches sequentially through each directory
|
|
256 specified in ada-search-directories. If a file is not found, a new one
|
|
257 is created with the first matching extension (.adb yields .ads).
|
|
258 ")
|
|
259
|
|
260 ;;;### autoload
|
|
261 (autoload 'ada-make-filename-from-adaname "ada-mode"
|
|
262 "Determine the filename of a package/procedure from its own Ada name.")
|
|
263 (defvar ada-spec-suffix ".ads"
|
|
264 "*Suffix of Ada specification files.")
|
|
265 (make-variable-buffer-local 'ada-spec-suffix)
|
|
266
|
|
267 (defvar modula2-other-file-alist
|
|
268 '(
|
|
269 ("\\.mi$" (".md")) ;; Modula-2 module definition
|
|
270 ("\\.md$" (".mi")) ;; and implementation.
|
|
271 )
|
|
272 "*See the description for the ff-search-directories variable.")
|
|
273
|
|
274 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
275 ;; No user definable variables beyond this point!
|
|
276 ;; ==============================================
|
|
277
|
|
278 (make-variable-buffer-local 'ff-pre-find-hooks)
|
|
279 (make-variable-buffer-local 'ff-pre-load-hooks)
|
|
280 (make-variable-buffer-local 'ff-post-load-hooks)
|
|
281 (make-variable-buffer-local 'ff-not-found-hooks)
|
|
282 (make-variable-buffer-local 'ff-file-created-hooks)
|
|
283 (make-variable-buffer-local 'ff-case-fold-search)
|
|
284 (make-variable-buffer-local 'ff-always-in-other-window)
|
|
285 (make-variable-buffer-local 'ff-ignore-include)
|
|
286 (make-variable-buffer-local 'ff-quiet-mode)
|
|
287 (make-variable-buffer-local 'ff-other-file-alist)
|
|
288 (make-variable-buffer-local 'ff-search-directories)
|
|
289
|
|
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
291 ;; User entry points
|
|
292
|
|
293 ;;;###autoload
|
|
294 (defun ff-get-other-file (&optional in-other-window)
|
|
295 "Find the corresponding header or source file to this source or header
|
|
296 file. See also the documentation for ff-find-other-file.
|
|
297
|
|
298 If optional IN-OTHER-WINDOW is non-nil, finds the file in another window.
|
|
299
|
|
300 Arguments: (&optional in-other-window)"
|
|
301 (interactive "P")
|
|
302 (let ((ignore ff-ignore-include))
|
|
303 (setq ff-ignore-include t)
|
|
304 (ff-find-the-other-file in-other-window)
|
|
305 (setq ff-ignore-include ignore)))
|
|
306
|
|
307 ;;;###autoload
|
|
308 (defun ff-find-other-file (&optional in-other-window ignore-include)
|
|
309 "Find the corresponding header or source file to this source or header
|
|
310 file; being on a #include line pulls in that file.
|
|
311
|
|
312 If optional IN-OTHER-WINDOW is non-nil, finds the file in the other window.
|
|
313 If optional IGNORE-INCLUDE is non-nil, ignores being on #include lines.
|
|
314
|
|
315 Arguments: (&optional in-other-window ignore-include)
|
|
316
|
|
317 Variables of interest include:
|
|
318
|
|
319 - ff-case-fold-search
|
|
320 Non-nil means ignore cases in matches (see case-fold-search).
|
|
321 If you have extensions in different cases, you will want this to be nil.
|
|
322
|
|
323 - ff-always-in-other-window
|
|
324 If non-nil, always open the other file in another window, unless an
|
|
325 argument is given to ff-find-other-file.
|
|
326
|
|
327 - ff-ignore-include
|
|
328 If non-nil, ignores #include lines.
|
|
329
|
|
330 - ff-always-try-to-create
|
|
331 If non-nil, always attempt to create the other file if it was not found.
|
|
332
|
|
333 - ff-quiet-mode
|
|
334 If non-nil, traces which directories are being searched.
|
|
335
|
|
336 - ff-special-constructs
|
|
337 A list of regular expressions specifying how to recognise special
|
|
338 constructs such as include files etc, and an associated method for
|
|
339 extracting the filename from that construct.
|
|
340
|
|
341 - ff-other-file-alist
|
|
342 Alist of extensions to find given the current file's extension.
|
|
343
|
|
344 - ff-search-directories
|
|
345 List of directories searched through with each extension specified in
|
|
346 ff-other-file-alist that matches this file's extension.
|
|
347
|
|
348 - ff-pre-find-hooks
|
|
349 List of functions to be called before the search for the file starts.
|
|
350
|
|
351 - ff-pre-load-hooks
|
|
352 List of functions to be called before the other file is loaded.
|
|
353
|
|
354 - ff-post-load-hooks
|
|
355 List of functions to be called after the other file is loaded.
|
|
356
|
|
357 - ff-not-found-hooks
|
|
358 List of functions to be called if the other file could not be found.
|
|
359
|
|
360 - ff-file-created-hooks
|
|
361 List of functions to be called if the other file has been created."
|
|
362 (interactive "P")
|
|
363 (let ((ignore ff-ignore-include))
|
|
364 (setq ff-ignore-include ignore-include)
|
|
365 (ff-find-the-other-file in-other-window)
|
|
366 (setq ff-ignore-include ignore)))
|
|
367
|
|
368 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
369 ;; Support functions
|
|
370
|
|
371 (defun ff-gnu-emacs-19 ()
|
|
372 (string-match "^19\\.[0-9]+\\.[0-9]+$" emacs-version))
|
|
373
|
|
374 (defun ff-xemacs ()
|
|
375 (or (string-match "Lucid" emacs-version)
|
|
376 (string-match "XEmacs" emacs-version)))
|
|
377
|
|
378 (defun ff-find-the-other-file (&optional in-other-window)
|
|
379 "Find the corresponding header or source file to this source or header
|
|
380 file; being on a #include line pulls in that file, but see the help on
|
|
381 the ff-ignore-include variable.
|
|
382
|
|
383 If optional IN-OTHER-WINDOW is non-nil, finds the file in another window.
|
|
384
|
|
385 Arguments: (&optional in-other-window)"
|
|
386
|
|
387 (let (match ;; matching regexp for this file
|
|
388 suffixes ;; set of replacing regexps for the matching regexp
|
|
389 action ;; function to generate the names of the other files
|
|
390 fname ;; basename of this file
|
|
391 pos ;; where we start matching filenames
|
|
392 stub ;; name of the file without extension
|
|
393 alist ;; working copy of the list of file extensions
|
|
394 pathname ;; the pathname of the file or the #include line
|
|
395 default-name ;; file we should create if none found
|
|
396 format ;; what we have to match
|
|
397 found ;; name of the file or buffer found - nil if none
|
|
398 dirs ;; local value of ff-search-directories
|
|
399 no-match) ;; whether we know about this kind of file
|
|
400
|
|
401 (if ff-pre-find-hooks
|
|
402 (run-hooks 'ff-pre-find-hooks))
|
|
403
|
|
404 (message "Working...")
|
|
405
|
|
406 (setq dirs
|
|
407 (if (symbolp ff-search-directories)
|
|
408 (ff-list-replace-env-vars (symbol-value ff-search-directories))
|
|
409 (ff-list-replace-env-vars ff-search-directories)))
|
|
410
|
|
411 (save-excursion
|
|
412 (beginning-of-line 1)
|
|
413 (setq fname (ff-treat-as-special)))
|
|
414
|
|
415 (cond
|
|
416 ((and (not ff-ignore-include) fname)
|
|
417 (setq default-name fname)
|
|
418 (setq found (ff-get-file dirs fname nil in-other-window)))
|
|
419
|
|
420 ;; let's just get the corresponding file
|
|
421 (t
|
|
422 (setq alist (if (symbolp ff-other-file-alist)
|
|
423 (symbol-value ff-other-file-alist)
|
|
424 ff-other-file-alist)
|
|
425 pathname (if (buffer-file-name)
|
|
426 (buffer-file-name)
|
|
427 "/none.none"))
|
|
428
|
|
429 (string-match ".*/\\(.+\\)$" pathname)
|
|
430 (setq fname (substring pathname (match-beginning 1) (match-end 1))
|
|
431 no-match nil
|
|
432 match (car alist))
|
|
433
|
|
434 ;; find the table entry corresponding to this file
|
|
435 (setq pos (ff-string-match (car match) fname))
|
|
436 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
|
|
437 (setq alist (cdr alist))
|
|
438 (setq match (car alist))
|
|
439 (setq pos (ff-string-match (car match) fname)))
|
|
440
|
|
441 ;; no point going on if we haven't found anything
|
|
442 (if (not match)
|
|
443 (setq no-match t)
|
|
444
|
|
445 ;; otherwise, suffixes contains what we need
|
|
446 (setq suffixes (car (cdr match))
|
|
447 action (car (cdr match))
|
|
448 found nil)
|
|
449
|
|
450 ;; if we have a function to generate new names,
|
|
451 ;; invoke it with the name of the current file
|
|
452 (if (and (atom action) (fboundp action))
|
|
453 (progn
|
|
454 (setq suffixes (funcall action (buffer-file-name))
|
|
455 match (cons (car match) (list suffixes))
|
|
456 stub nil
|
|
457 default-name (car suffixes)))
|
|
458
|
|
459 ;; otherwise build our filename stub
|
|
460 (cond
|
|
461
|
|
462 ;; get around the problem that 0 and nil both mean false!
|
|
463 ((= pos 0)
|
|
464 (setq format "")
|
|
465 (setq stub "")
|
|
466 )
|
|
467
|
|
468 (t
|
|
469 (setq format (concat "\\(.+\\)" (car match)))
|
|
470 (string-match format fname)
|
|
471 (setq stub (substring fname (match-beginning 1) (match-end 1)))
|
|
472 ))
|
|
473
|
|
474 ;; if we find nothing, we should try to get a file like this one
|
|
475 (setq default-name
|
|
476 (concat stub (car (car (cdr match))))))
|
|
477
|
|
478 ;; do the real work - find the file
|
|
479 (setq found
|
|
480 (ff-get-file dirs
|
|
481 stub
|
|
482 suffixes
|
|
483 in-other-window)))))
|
|
484
|
|
485 (cond
|
|
486 (no-match ;; could not even determine the other file
|
|
487 (message ""))
|
|
488
|
|
489 (t
|
|
490 (cond
|
|
491
|
|
492 ((not found) ;; could not find the other file
|
|
493
|
|
494 (if ff-not-found-hooks ;; run the hooks
|
|
495 (run-hooks 'ff-not-found-hooks))
|
|
496
|
|
497 (cond
|
|
498 (ff-always-try-to-create ;; try to create the file
|
|
499 (let (name pathname)
|
|
500
|
|
501 (setq name
|
|
502 (expand-file-name
|
|
503 (read-file-name
|
|
504 (format "Find or create %s in: " default-name)
|
|
505 default-directory default-name nil)))
|
|
506
|
|
507 (setq pathname
|
|
508 (if (file-directory-p name)
|
|
509 (concat (file-name-as-directory name) default-name)
|
|
510 (setq found name)))
|
|
511
|
|
512 (ff-find-file pathname in-other-window t)))
|
|
513
|
|
514 (t ;; don't create the file, just whinge
|
|
515 (message "no file found for %s" fname))))
|
|
516
|
|
517 (t ;; matching file found
|
|
518 nil))))
|
|
519
|
|
520 found)) ;; return buffer-name or filename
|
|
521
|
|
522 (defun ff-get-file (search-dirs fname-stub &optional suffix-list other-window)
|
|
523 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
|
|
524 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
|
|
525 with each of the given suffixes. Gets the file or the buffer corresponding
|
|
526 to the name of the first file found, or nil.
|
|
527
|
|
528 Arguments: (search-dirs fname-stub &optional suffix-list in-other-window)
|
|
529 "
|
|
530 (let ((filename (ff-get-file-name search-dirs fname-stub suffix-list)))
|
|
531
|
|
532 (cond
|
|
533 ((not filename)
|
|
534 nil)
|
|
535
|
|
536 ((bufferp (get-buffer filename))
|
|
537 (ff-switch-to-buffer filename other-window)
|
|
538 filename)
|
|
539
|
|
540 ((file-exists-p filename)
|
|
541 (ff-find-file filename other-window nil)
|
|
542 filename)
|
|
543
|
|
544 (t
|
|
545 nil))))
|
|
546
|
|
547 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
|
|
548 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
|
|
549 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
|
|
550 with each of the given suffixes. Returns the name of the first file found.
|
|
551
|
|
552 Arguments: (search-dirs fname-stub &optional suffix-list)
|
|
553 "
|
|
554 (let* (dirs ;; working copy of dirs to search
|
|
555 dir ;; the current dir considered
|
|
556 file ;; filename being looked for
|
|
557 rest ;; pathname after first /*
|
|
558 this-suffix ;; the suffix we are currently considering
|
|
559 suffixes ;; working copy of suffix-list
|
|
560 filename ;; built filename
|
|
561 blist ;; list of live buffers
|
|
562 buf ;; current buffer in blist
|
|
563 found) ;; whether we have found anything
|
|
564
|
|
565 (setq suffixes suffix-list)
|
|
566
|
|
567 ;; suffixes is nil => fname-stub is the file we are looking for
|
|
568 ;; otherwise fname-stub is a stub, and we append a suffix
|
|
569 (if suffixes
|
|
570 (setq this-suffix (car suffixes))
|
|
571 (setq this-suffix "")
|
|
572 (setq suffixes (list "")))
|
|
573
|
|
574 ;; find whether the file is in a buffer first
|
|
575 (while (and suffixes (not found))
|
|
576 (setq filename (concat fname-stub this-suffix))
|
|
577
|
|
578 (if (not ff-quiet-mode)
|
|
579 (message "finding buffer %s..." filename))
|
|
580
|
|
581 (if (bufferp (get-buffer filename))
|
|
582 (setq found filename))
|
|
583
|
|
584 (setq blist (buffer-list))
|
|
585 (setq buf (buffer-name (car blist)))
|
|
586 (while (and blist (not found))
|
|
587
|
|
588 (if (string-match (concat filename "<[0-9]+>") buf)
|
|
589 (setq found buf))
|
|
590
|
|
591 (setq blist (cdr blist))
|
|
592 (setq buf (buffer-name (car blist))))
|
|
593
|
|
594 (setq suffixes (cdr suffixes))
|
|
595 (setq this-suffix (car suffixes)))
|
|
596
|
|
597 ;; now look for the real file
|
|
598 (setq dirs search-dirs)
|
|
599 (setq dir (car dirs))
|
|
600 (while (and (not found) dirs)
|
|
601
|
|
602 (setq suffixes suffix-list)
|
|
603
|
|
604 ;; if dir does not contain '/*', look for the file
|
|
605 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir)))
|
|
606 (progn
|
|
607
|
|
608 ;; suffixes is nil => fname-stub is the file we are looking for
|
|
609 ;; otherwise fname-stub is a stub, and we append a suffix
|
|
610 (if suffixes
|
|
611 (setq this-suffix (car suffixes))
|
|
612 (setq this-suffix "")
|
|
613 (setq suffixes (list "")))
|
|
614
|
|
615 (while (and suffixes (not found))
|
|
616
|
|
617 (setq filename (concat fname-stub this-suffix))
|
|
618 (setq file (concat dir "/" filename))
|
|
619
|
|
620 (if (not ff-quiet-mode)
|
|
621 (message "finding %s..." file))
|
|
622
|
|
623 (if (file-exists-p file)
|
|
624 (setq found file))
|
|
625
|
|
626 (setq suffixes (cdr suffixes))
|
|
627 (setq this-suffix (car suffixes))))
|
|
628
|
|
629 ;; otherwise dir matches the '/*', so search each dir separately
|
|
630 (progn
|
|
631 (if (match-beginning 2)
|
|
632 (setq rest (substring dir (match-beginning 2) (match-end 2)))
|
|
633 (setq rest "")
|
|
634 )
|
|
635 (setq dir (substring dir (match-beginning 1) (match-end 1)))
|
|
636
|
|
637 (let ((dirlist (ff-all-dirs-under dir '("..")))
|
|
638 this-dir compl-dirs)
|
|
639
|
|
640 (setq this-dir (car dirlist))
|
|
641 (while dirlist
|
|
642 (setq compl-dirs
|
|
643 (append
|
|
644 compl-dirs
|
|
645 (list (concat this-dir rest))
|
|
646 ))
|
|
647 (setq dirlist (cdr dirlist))
|
|
648 (setq this-dir (car dirlist)))
|
|
649
|
|
650 (if compl-dirs
|
|
651 (setq found (ff-get-file-name compl-dirs
|
|
652 fname-stub
|
|
653 suffix-list))))))
|
|
654 (setq dirs (cdr dirs))
|
|
655 (setq dir (car dirs)))
|
|
656
|
|
657 (if found
|
|
658 (message "%s found" found))
|
|
659
|
|
660 found))
|
|
661
|
|
662 (defun ff-string-match (regexp string &optional start)
|
|
663 "Like string-match (which see), but sets case-fold-search to
|
|
664 ff-case-fold-search before searching, and then resets it back again."
|
|
665 (let ((exact-match case-fold-search)
|
|
666 match)
|
|
667 (if regexp
|
|
668 (progn
|
|
669 (setq case-fold-search ff-case-fold-search)
|
|
670 (setq match (string-match regexp string start))
|
|
671 (setq case-fold-search exact-match)))
|
|
672 match))
|
|
673
|
|
674 (defun ff-list-replace-env-vars (search-list)
|
|
675 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST."
|
|
676 (let (list
|
|
677 (var (car search-list)))
|
|
678 (while search-list
|
|
679 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var)
|
|
680 (setq var
|
|
681 (concat
|
|
682 (substring var (match-beginning 1) (match-end 1))
|
|
683 (getenv (substring var (match-beginning 2) (match-end 2)))
|
|
684 (substring var (match-beginning 3) (match-end 3)))))
|
|
685 (setq search-list (cdr search-list))
|
|
686 (setq list (cons var list))
|
|
687 (setq var (car search-list)))
|
|
688 (setq search-list (reverse list))))
|
|
689
|
|
690 (defun ff-treat-as-special ()
|
|
691 "Returns the file to look for if the construct was special, otherwise
|
|
692 returns nil. The construct is defined in the variable ff-special-constructs
|
|
693 (which see)."
|
|
694 (let* (fname
|
|
695 (list ff-special-constructs)
|
|
696 (elem (car list))
|
|
697 (regexp (car elem))
|
|
698 (match (cdr elem)))
|
|
699 (while (and list (not fname))
|
|
700 (if (and (looking-at regexp) match)
|
|
701 (setq fname (funcall match)))
|
|
702 (setq list (cdr list))
|
|
703 (setq elem (car list))
|
|
704 (setq regexp (car elem))
|
|
705 (setq match (cdr elem)))
|
|
706 fname))
|
|
707
|
|
708 (defun ff-basename (string)
|
|
709 "Returns the basename of PATHNAME."
|
|
710 (setq string (concat "/" string))
|
|
711 (string-match ".*/\\([^/]+\\)$" string)
|
|
712 (setq string (substring string (match-beginning 1) (match-end 1))))
|
|
713
|
|
714 (defun ff-all-dirs-under (here &optional exclude)
|
|
715 "Get all the directory files under DIRECTORY.
|
|
716 Exclude all files in the optional EXCLUDE list."
|
|
717 (if (file-directory-p here)
|
|
718 (condition-case nil
|
|
719 (progn
|
|
720 (let ((files (directory-files here t))
|
|
721 (dirlist (list))
|
|
722 file)
|
|
723 (while files
|
|
724 (setq file (car files))
|
|
725 (if (and
|
|
726 (file-directory-p file)
|
|
727 (not (member (ff-basename file) exclude)))
|
|
728 (setq dirlist (cons file dirlist)))
|
|
729 (setq files (cdr files)))
|
|
730 (setq dirlist (reverse dirlist))))
|
|
731 (error nil))
|
|
732 nil))
|
|
733
|
|
734 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file)
|
|
735 "Calls Function2 or Function1 with FILE as argument, depending on whether
|
|
736 (optional) OTHER-WINDOW is set or not. Function1 and Function2 are typically
|
|
737 find-file / find-file-other-window or switch-to-buffer / switch-to-buffer-
|
|
738 other-window function pairs.
|
|
739
|
|
740 If optional NEW-FILE is t, then a special hook (ff-file-created-hooks) is
|
|
741 called before ff-post-load-hooks.
|
|
742
|
|
743 Arguments: (function1 function2 file &optional in-other-window new-file)
|
|
744 "
|
|
745 (if ff-pre-load-hooks
|
|
746 (run-hooks 'ff-pre-load-hooks))
|
|
747 (if (or
|
|
748 (and in-other-window (not ff-always-in-other-window))
|
|
749 (and (not in-other-window) ff-always-in-other-window))
|
|
750 (funcall f2 file)
|
|
751 (funcall f1 file))
|
|
752 (if new-file
|
|
753 (if ff-file-created-hooks
|
|
754 (run-hooks 'ff-file-created-hooks)))
|
|
755 (if ff-post-load-hooks
|
|
756 (run-hooks 'ff-post-load-hooks)))
|
|
757
|
|
758 (defun ff-find-file (file &optional in-other-window new-file)
|
|
759 "Like find-file (which see), but checks whether the file goes in another
|
|
760 window or not.
|
|
761
|
|
762 Arguments: (file &optional in-other-window new-file)
|
|
763 "
|
|
764 (ff-switch-file 'find-file
|
|
765 'find-file-other-window
|
|
766 file in-other-window new-file))
|
|
767
|
|
768 (defun ff-switch-to-buffer (file &optional in-other-window)
|
|
769 "Like switch-to-buffer (which see), but checks whether the buffer ends up
|
|
770 in another window or not.
|
|
771
|
|
772 Arguments: (file &optional in-other-window)
|
|
773 "
|
|
774 (ff-switch-file 'switch-to-buffer
|
|
775 'switch-to-buffer-other-window
|
|
776 file in-other-window nil))
|
|
777
|
|
778 (cond
|
|
779 ((ff-gnu-emacs-19)
|
|
780 (defun ff-goto-click (event)
|
|
781 (set-buffer (window-buffer (posn-window (event-end event))))
|
|
782 (goto-char (posn-point (event-end event))))
|
|
783
|
|
784 ;;;###autoload
|
|
785 (defun ff-mouse-find-other-file (event)
|
|
786 "Visit the file you click on."
|
|
787 (interactive "e")
|
|
788 (save-excursion
|
|
789 (ff-goto-click event)
|
|
790 (ff-find-other-file nil)))
|
|
791
|
|
792 ;;;###autoload
|
|
793 (defun ff-mouse-find-other-file-other-window (event)
|
|
794 "Visit the file you click on."
|
|
795 (interactive "e")
|
|
796 (save-excursion
|
|
797 (ff-goto-click event)
|
|
798 (ff-find-other-file t)))
|
|
799
|
|
800 ;;;###autoload
|
|
801 (defun locate-file (fname dirs &optional suffix-list ignore-perms)
|
|
802 "Defines XEmacs look-alike locate-file for GNU Emacs-19."
|
|
803 (interactive)
|
|
804 (ff-get-file dirs fname suffix-list))
|
|
805 )
|
|
806
|
|
807 ((ff-xemacs)
|
|
808
|
|
809 ;;;###autoload
|
|
810 (defun ff-mouse-find-other-file (event)
|
|
811 "Visit the file you click on."
|
|
812 (interactive "@e")
|
|
813 (save-excursion
|
|
814 (mouse-set-point event)
|
|
815 (ff-find-other-file nil)))
|
|
816
|
|
817 ;;;###autoload
|
|
818 (defun ff-mouse-find-other-file-other-window (event)
|
|
819 "Visit the file you click on."
|
|
820 (interactive "@e")
|
|
821 (save-excursion
|
|
822 (mouse-set-point event)
|
|
823 (ff-find-other-file t)))
|
|
824 ))
|
|
825
|
|
826 (provide 'find-file)
|
|
827
|
|
828 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
829 ;; This section offers an example of user defined function to select files
|
|
830
|
|
831 (defun upcase-p (string &optional start end)
|
|
832 "Return t if this string is all uppercase. Given START and/or END,
|
|
833 checks between these characters."
|
|
834 (let (match str)
|
|
835 (if (not start)
|
|
836 (setq start 0))
|
|
837 (if (not end)
|
|
838 (setq end (length string)))
|
|
839 (if (= start end)
|
|
840 (setq end (1+ end)))
|
|
841 (setq str (substring string start end))
|
|
842 (if (and
|
|
843 (ff-string-match "[A-Z]+" str)
|
|
844 (setq match (match-data))
|
|
845 (= (car match) 0)
|
|
846 (= (car (cdr match)) (length str)))
|
|
847 t
|
|
848 nil)))
|
|
849
|
|
850 (defun ff-cc-hh-converter (arg)
|
|
851 "Discriminate file extensions and build up a new file list based
|
|
852 possibly on part of the directory name and the name of the file
|
|
853 passed in."
|
|
854 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg)
|
|
855 (let ((path (if (match-beginning 1)
|
|
856 (substring arg (match-beginning 1) (match-end 1)) nil))
|
|
857 (dire (if (match-beginning 2)
|
|
858 (substring arg (match-beginning 2) (match-end 2)) nil))
|
|
859 (file (if (match-beginning 3)
|
|
860 (substring arg (match-beginning 3) (match-end 3)) nil))
|
|
861 (extn (if (match-beginning 4)
|
|
862 (substring arg (match-beginning 4) (match-end 4)) nil))
|
|
863 return-list)
|
|
864 (cond
|
|
865 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h}
|
|
866 ((and (string= extn "cc")
|
|
867 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file))
|
|
868 (let ((stub (substring file (match-beginning 2) (match-end 2))))
|
|
869 (setq dire (upcase (substring file (match-beginning 1) (match-end 1))))
|
|
870 (setq return-list (list (concat stub ".hh")
|
|
871 (concat stub ".h")
|
|
872 (concat file ".hh")
|
|
873 (concat file ".h")))
|
|
874 ))
|
|
875 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C}
|
|
876 ((and (string= extn "hh") (upcase-p dire) file)
|
|
877 (let ((stub (concat (downcase dire) file)))
|
|
878 (setq return-list (list (concat stub ".cc")
|
|
879 (concat stub ".C")
|
|
880 (concat file ".cc")
|
|
881 (concat file ".C")))
|
|
882 ))
|
|
883 ;; zap.cc => zap.hh or zap.h
|
|
884 ((string= extn "cc")
|
|
885 (let ((stub file))
|
|
886 (setq return-list (list (concat stub ".hh")
|
|
887 (concat stub ".h")))
|
|
888 ))
|
|
889 ;; zap.hh => zap.cc or zap.C
|
|
890 ((string= extn "hh")
|
|
891 (let ((stub file))
|
|
892 (setq return-list (list (concat stub ".cc")
|
|
893 (concat stub ".C")))
|
|
894 ))
|
|
895 (t
|
|
896 nil))
|
|
897
|
|
898 return-list))
|
|
899
|
|
900 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
901 ;; This section offers an example of user defined function to place point.
|
|
902 ;; The regexps are Ada specific.
|
|
903
|
|
904 (defvar ff-function-name nil "Name of the function we are in.")
|
|
905
|
|
906 (defvar ada-procedure-start-regexp)
|
|
907 (defvar ada-package-start-regexp)
|
|
908
|
|
909 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in)
|
|
910 ;;
|
|
911 (defun ff-which-function-are-we-in ()
|
|
912 "Determine whether we are on a function definition/declaration and
|
|
913 remember the name of that function."
|
|
914
|
|
915 (setq ff-function-name nil)
|
|
916
|
|
917 (save-excursion
|
|
918 (if (re-search-backward ada-procedure-start-regexp nil t)
|
|
919 (setq ff-function-name (buffer-substring (match-beginning 0)
|
|
920 (match-end 0)))
|
|
921 ; we didn't find a procedure start, perhaps there is a package
|
|
922 (if (re-search-backward ada-package-start-regexp nil t)
|
|
923 (setq ff-function-name (buffer-substring (match-beginning 0)
|
|
924 (match-end 0)))
|
|
925 ))))
|
|
926
|
|
927 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly)
|
|
928 ;;
|
|
929 (defun ff-set-point-accordingly ()
|
|
930 "Find the function specified in ff-function-name, previously
|
|
931 determined by ff-which-function-are-we-in."
|
|
932 (if ff-function-name
|
|
933 (progn
|
|
934 (goto-char (point-min))
|
|
935 (search-forward ff-function-name nil t))))
|
|
936
|
|
937 ;; find-file.el ends here
|
|
938
|