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