Mercurial > emacs
annotate lisp/find-file.el @ 30515:6165183bc490
(remove, remq): New functions.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Thu, 27 Jul 2000 20:08:47 +0000 |
parents | bf83c23f3300 |
children | e06965792db4 |
rev | line source |
---|---|
10491 | 1 ;;; find-file.el --- find a file corresponding to this one given a pattern |
2 | |
18127 | 3 ;; Author: Henry Guillaume <henri@tibco.com, henry@c032.aone.net.au> |
21057 | 4 ;; Maintainer: FSF |
10491 | 5 ;; Keywords: c, matching, tools |
6 | |
11234 | 7 ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc. |
10491 | 8 |
14169 | 9 ;; This file is part of GNU Emacs. |
10491 | 10 |
14169 | 11 ;; GNU Emacs is free software; you can redistribute it and/or modify |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
10491 | 15 |
14169 | 16 ;; GNU Emacs is distributed in the hope that it will be useful, |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
10491 | 20 |
14169 | 21 ;; You should have received a copy of the GNU General Public License |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
10491 | 25 |
26 ;;; Commentary: | |
27 | |
28 ;; PURPOSE: | |
29 ;; This package features a function called ff-find-other-file, which performs | |
30 ;; the following function: | |
31 ;; | |
32 ;; When in a .c file, find the first corresponding .h file in a set | |
33 ;; of directories and display it, and vice-versa from the .h file. | |
34 ;; | |
35 ;; Many people maintain their include file in a directory separate to their | |
36 ;; src directory, and very often you may be editing a file and have a need to | |
37 ;; visit the "other file". This package searches through a set of directories | |
38 ;; to find that file. | |
39 ;; | |
40 ;; THE "OTHER FILE", or "corresponding file", generally has the same basename, | |
41 ;; and just has a different extension as described by the ff-other-file-alist | |
42 ;; variable: | |
43 ;; | |
44 ;; '(("\\.cc$" (".hh" ".h")) | |
45 ;; ("\\.hh$" (".cc" ".C" ".CC" ".cxx" ".cpp"))) | |
46 ;; | |
47 ;; If the current file has a .cc extension, ff-find-other-file will attempt | |
48 ;; to look for a .hh file, and then a .h file in some directory as described | |
49 ;; below. The mechanism here is to replace the matched part of the original | |
50 ;; filename with each of the corresponding extensions in turn. | |
51 ;; | |
52 ;; Alternatively, there are situations where the filename of the other file | |
53 ;; cannot be determined easily with regexps. For example, a .c file may | |
54 ;; have two corresponding .h files, for its public and private parts, or | |
55 ;; the filename for the .c file contains part of the pathname of the .h | |
56 ;; file, as between src/fooZap.cc and include/FOO/zap.hh. In that case, the | |
57 ;; format above can be changed to include a function to be called when the | |
58 ;; current file matches the regexp: | |
59 ;; | |
60 ;; '(("\\.cc$" cc-function) | |
61 ;; ("\\.hh$" hh-function)) | |
62 ;; | |
63 ;; These functions must return a list consisting of the possible names of the | |
64 ;; corresponding file, with or without path. There is no real need for more | |
65 ;; than one function, and one could imagine the following value for cc-other- | |
66 ;; file-alist: | |
67 ;; | |
68 ;; (setq cc-other-file-alist | |
69 ;; '(("\\.cc$" ff-cc-hh-converter) | |
70 ;; ("\\.hh$" ff-cc-hh-converter) | |
71 ;; ("\\.c$" (".h")) | |
72 ;; ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp")))) | |
73 ;; | |
74 ;; ff-cc-hh-converter is included at the end of this file as a reference. | |
75 ;; | |
76 ;; SEARCHING is carried out in a set of directories specified by the | |
77 ;; ff-search-directories variable: | |
78 ;; | |
79 ;; ("." "../../src" "../include/*" "/usr/local/*/src/*" "$PROJECT/src") | |
80 ;; | |
81 ;; This means that the corresponding file will be searched for first in | |
82 ;; the current directory, then in ../../src, then in one of the directories | |
83 ;; under ../include, and so on. The star is _not_ a general wildcard | |
84 ;; character: it just indicates that the subdirectories of this directory | |
85 ;; must each be searched in turn. Environment variables will be expanded in | |
86 ;; the ff-search-directories variable. | |
87 ;; | |
88 ;; If the point is on a #include line, the file to be #included is searched | |
89 ;; for in the same manner. This can be disabled with the ff-ignore-include | |
90 ;; variable, or by calling ff-get-other-file instead of ff-find-other-file. | |
91 ;; | |
92 ;; If the file was not found, ff-find-other-file will prompt you for where | |
93 ;; to create the new "corresponding file" (defaults to the current directory), | |
94 ;; unless the variable ff-always-try-to-create is set to nil. | |
95 ;; | |
96 ;; GIVEN AN ARGUMENT (with the ^U prefix), ff-find-other-file will get the | |
97 ;; other file in another (the other?) window (see find-file-other-window and | |
98 ;; switch-to-buffer-other-window). This can be set on a more permanent basis | |
99 ;; by setting ff-always-in-other-window to t in which case the ^U prefix will | |
100 ;; do the opposite of what was described above. | |
101 ;; | |
102 ;; THERE ARE FIVE AVAILABLE HOOKS, called in this order if non-nil: | |
103 ;; | |
104 ;; - ff-pre-find-hooks - called before the search for the other file starts | |
105 ;; - ff-not-found-hooks - called when the other file could not be found | |
106 ;; - ff-pre-load-hooks - called just before the other file is 'loaded' | |
107 ;; - ff-file-created-hooks - called when the other file is created | |
108 ;; - ff-post-load-hooks - called just after the other file is 'loaded' | |
109 ;; | |
110 ;; The *load-hooks allow you to place point where you want it in the other | |
111 ;; file. | |
112 | |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
113 ;;; Change Log: |
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
114 ;; |
10491 | 115 ;; FEEDBACK: |
116 ;; Please send me bug reports, bug fixes, and extensions, so that I can | |
117 ;; merge them into the master source. | |
118 | |
119 ;; CREDITS: | |
120 ;; Many thanks go to TUSC Computer Systems Pty Ltd for providing an environ- | |
121 ;; ment that made the development of this package possible. | |
122 ;; | |
123 ;; Many thanks also go to all those who provided valuable feedback throughout | |
124 ;; the development of this package: | |
125 ;; Rolf Ebert in particular, Fritz Knabe, Heddy Boubaker, Sebastian Kremer, | |
126 ;; Vasco Lopes Paulo, Mark A. Plaksin, Robert Lang, Trevor West, Kevin | |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
127 ;; Pereira, Benedict Lofstedt & Justin Vallon. |
10491 | 128 |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
129 ;;; Code: |
10491 | 130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
131 ;; User definable variables: | |
132 | |
21087 | 133 (defgroup ff nil |
134 "Find a file corresponding to this one given a pattern." | |
135 :prefix "ff-" | |
136 :group 'find-file) | |
10491 | 137 |
21087 | 138 (defcustom ff-pre-find-hooks nil |
139 "*List of functions to be called before the search for the file starts." | |
140 :type 'hook | |
141 :group 'ff) | |
10491 | 142 |
21087 | 143 (defcustom ff-pre-load-hooks nil |
144 "*List of functions to be called before the other file is loaded." | |
145 :type 'hook | |
146 :group 'ff) | |
10491 | 147 |
21087 | 148 (defcustom ff-post-load-hooks nil |
149 "*List of functions to be called after the other file is loaded." | |
150 :type 'hook | |
151 :group 'ff) | |
10491 | 152 |
21087 | 153 (defcustom ff-not-found-hooks nil |
154 "*List of functions to be called if the other file could not be found." | |
155 :type 'hook | |
156 :group 'ff) | |
10491 | 157 |
21087 | 158 (defcustom ff-file-created-hooks nil |
159 "*List of functions to be called if the other file needs to be created." | |
160 :type 'hook | |
161 :group 'ff) | |
10491 | 162 |
21087 | 163 (defcustom ff-case-fold-search nil |
164 "*Non-nil means ignore cases in matches (see `case-fold-search'). | |
165 If you have extensions in different cases, you will want this to be nil." | |
166 :type 'boolean | |
167 :group 'ff) | |
10491 | 168 |
21087 | 169 (defcustom ff-always-in-other-window nil |
170 "*If non-nil, find the corresponding file in another window by default. | |
171 To override this, give an argument to `ff-find-other-file'." | |
172 :type 'boolean | |
173 :group 'ff) | |
10491 | 174 |
21087 | 175 (defcustom ff-ignore-include nil |
176 "*If non-nil, ignore `#include' lines." | |
177 :type 'boolean | |
178 :group 'ff) | |
10491 | 179 |
21087 | 180 (defcustom ff-always-try-to-create t |
181 "*If non-nil, always attempt to create the other file if it was not found." | |
182 :type 'boolean | |
183 :group 'ff) | |
184 | |
185 (defcustom ff-quiet-mode nil | |
186 "*If non-nil, trace which directories are being searched." | |
187 :type 'boolean | |
188 :group 'ff) | |
10491 | 189 |
190 (defvar ff-special-constructs | |
191 '( | |
192 ;; C/C++ include, for NeXTSTEP too | |
193 ("^\#\\s *\\(include\\|import\\)\\s +[<\"]\\(.*\\)[>\"]" . | |
194 (lambda () | |
195 (setq fname (buffer-substring (match-beginning 2) (match-end 2))))) | |
196 | |
197 ;; Ada import | |
198 ("^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)" . | |
199 (lambda () | |
200 (setq fname (buffer-substring (match-beginning 1) (match-end 1))) | |
12949
f6e5a73b96e4
(ada-spec-suffix): Definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
12567
diff
changeset
|
201 (require 'ada-mode) |
10491 | 202 (setq fname (concat (ada-make-filename-from-adaname fname) |
12949
f6e5a73b96e4
(ada-spec-suffix): Definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
12567
diff
changeset
|
203 ada-spec-suffix)))) |
10491 | 204 ) |
205 "*A list of regular expressions specifying how to recognise special | |
206 constructs such as include files etc, and an associated method for | |
207 extracting the filename from that construct.") | |
208 | |
21087 | 209 (defcustom ff-other-file-alist 'cc-other-file-alist |
10491 | 210 "*Alist of extensions to find given the current file's extension. |
211 | |
212 This list should contain the most used extensions before the others, | |
213 since the search algorithm searches sequentially through each | |
11272 | 214 directory specified in `ff-search-directories'. If a file is not found, |
215 a new one is created with the first matching extension (`.cc' yields `.hh'). | |
21087 | 216 This alist should be set by the major mode." |
217 :type '(choice (repeat (list regexp (choice (repeat string) function))) | |
218 symbol) | |
219 :group 'ff) | |
10491 | 220 |
21087 | 221 (defcustom ff-search-directories 'cc-search-directories |
10491 | 222 "*List of directories to search for a specific file. |
223 | |
11272 | 224 Set by default to `cc-search-directories', expanded at run-time. |
10491 | 225 |
226 This list is searched through with each extension specified in | |
11272 | 227 `ff-other-file-alist' that matches this file's extension. So the |
10491 | 228 longer the list, the longer it'll take to realise that a file |
229 may not exist. | |
230 | |
231 A typical format is | |
232 | |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
233 '(\".\" \"/usr/include\" \"$PROJECT/*/include\") |
10491 | 234 |
11272 | 235 Environment variables can be inserted between slashes (`/'). |
10491 | 236 They will be replaced by their definition. If a variable does |
11272 | 237 not exist, it is replaced (silently) with an empty string. |
10491 | 238 |
11272 | 239 The stars are *not* wildcards: they are searched for together with |
240 the preceding slash. The star represents all the subdirectories except | |
21087 | 241 `..', and each of these subdirectories will be searched in turn." |
242 :type '(choice (repeat directory) symbol) | |
243 :group 'ff) | |
10491 | 244 |
21087 | 245 (defcustom cc-search-directories |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
246 '("." "/usr/include" "/usr/local/include/*") |
21087 | 247 "*See the description of the `ff-search-directories' variable." |
248 :type '(repeat directory) | |
249 :group 'ff) | |
10491 | 250 |
21087 | 251 (defcustom cc-other-file-alist |
10491 | 252 '( |
253 ("\\.cc$" (".hh" ".h")) | |
254 ("\\.hh$" (".cc" ".C")) | |
255 | |
256 ("\\.c$" (".h")) | |
257 ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp")) | |
258 | |
259 ("\\.C$" (".H" ".hh" ".h")) | |
260 ("\\.H$" (".C" ".CC")) | |
261 | |
262 ("\\.CC$" (".HH" ".H" ".hh" ".h")) | |
263 ("\\.HH$" (".CC")) | |
264 | |
265 ("\\.cxx$" (".hh" ".h")) | |
266 ("\\.cpp$" (".hh" ".h")) | |
267 ) | |
268 "*Alist of extensions to find given the current file's extension. | |
269 | |
270 This list should contain the most used extensions before the others, | |
271 since the search algorithm searches sequentially through each directory | |
11272 | 272 specified in `ff-search-directories'. If a file is not found, a new one |
21087 | 273 is created with the first matching extension (`.cc' yields `.hh')." |
274 :type '(repeat (list regexp (choice (repeat string) function))) | |
275 :group 'ff) | |
10491 | 276 |
21087 | 277 (defcustom modula2-other-file-alist |
10491 | 278 '( |
279 ("\\.mi$" (".md")) ;; Modula-2 module definition | |
280 ("\\.md$" (".mi")) ;; and implementation. | |
281 ) | |
21087 | 282 "*See the description for the `ff-search-directories' variable." |
283 :type '(repeat (list regexp (choice (repeat string) function))) | |
284 :group 'ff) | |
285 | |
10491 | 286 |
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
288 ;; No user definable variables beyond this point! | |
289 ;; ============================================== | |
290 | |
291 (make-variable-buffer-local 'ff-pre-find-hooks) | |
292 (make-variable-buffer-local 'ff-pre-load-hooks) | |
293 (make-variable-buffer-local 'ff-post-load-hooks) | |
294 (make-variable-buffer-local 'ff-not-found-hooks) | |
295 (make-variable-buffer-local 'ff-file-created-hooks) | |
296 (make-variable-buffer-local 'ff-case-fold-search) | |
297 (make-variable-buffer-local 'ff-always-in-other-window) | |
298 (make-variable-buffer-local 'ff-ignore-include) | |
299 (make-variable-buffer-local 'ff-quiet-mode) | |
300 (make-variable-buffer-local 'ff-other-file-alist) | |
301 (make-variable-buffer-local 'ff-search-directories) | |
302 | |
303 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
304 ;; User entry points | |
305 | |
306 ;;;###autoload | |
307 (defun ff-get-other-file (&optional in-other-window) | |
11272 | 308 "Find the header or source file corresponding to this file. |
309 See also the documentation for `ff-find-other-file;. | |
10491 | 310 |
11272 | 311 If optional IN-OTHER-WINDOW is non-nil, find the file in another window." |
10491 | 312 (interactive "P") |
313 (let ((ignore ff-ignore-include)) | |
314 (setq ff-ignore-include t) | |
315 (ff-find-the-other-file in-other-window) | |
316 (setq ff-ignore-include ignore))) | |
317 | |
318 ;;;###autoload | |
319 (defun ff-find-other-file (&optional in-other-window ignore-include) | |
11272 | 320 "Find the header or source file corresponding to this file. |
321 Being on a `#include' line pulls in that file. | |
10491 | 322 |
11272 | 323 If optional IN-OTHER-WINDOW is non-nil, find the file in the other window. |
324 If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines. | |
10491 | 325 |
326 Variables of interest include: | |
327 | |
328 - ff-case-fold-search | |
329 Non-nil means ignore cases in matches (see case-fold-search). | |
330 If you have extensions in different cases, you will want this to be nil. | |
331 | |
332 - ff-always-in-other-window | |
333 If non-nil, always open the other file in another window, unless an | |
334 argument is given to ff-find-other-file. | |
335 | |
336 - ff-ignore-include | |
337 If non-nil, ignores #include lines. | |
338 | |
339 - ff-always-try-to-create | |
340 If non-nil, always attempt to create the other file if it was not found. | |
341 | |
342 - ff-quiet-mode | |
343 If non-nil, traces which directories are being searched. | |
344 | |
345 - ff-special-constructs | |
346 A list of regular expressions specifying how to recognise special | |
347 constructs such as include files etc, and an associated method for | |
348 extracting the filename from that construct. | |
349 | |
350 - ff-other-file-alist | |
351 Alist of extensions to find given the current file's extension. | |
352 | |
353 - ff-search-directories | |
354 List of directories searched through with each extension specified in | |
355 ff-other-file-alist that matches this file's extension. | |
356 | |
357 - ff-pre-find-hooks | |
358 List of functions to be called before the search for the file starts. | |
359 | |
360 - ff-pre-load-hooks | |
361 List of functions to be called before the other file is loaded. | |
362 | |
363 - ff-post-load-hooks | |
364 List of functions to be called after the other file is loaded. | |
365 | |
366 - ff-not-found-hooks | |
367 List of functions to be called if the other file could not be found. | |
368 | |
369 - ff-file-created-hooks | |
370 List of functions to be called if the other file has been created." | |
371 (interactive "P") | |
372 (let ((ignore ff-ignore-include)) | |
373 (setq ff-ignore-include ignore-include) | |
374 (ff-find-the-other-file in-other-window) | |
375 (setq ff-ignore-include ignore))) | |
376 | |
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
378 ;; Support functions | |
379 | |
380 (defun ff-find-the-other-file (&optional in-other-window) | |
11272 | 381 "Find the header or source file corresponding to the current file. |
382 Being on a `#include' line pulls in that file, but see the help on | |
383 the `ff-ignore-include' variable. | |
10491 | 384 |
11272 | 385 If optional IN-OTHER-WINDOW is non-nil, find the file in another window." |
10491 | 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 | |
17548 | 515 (message "No file found for %s" fname)))) |
10491 | 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 | |
13896 | 536 ((bufferp (get-file-buffer filename)) |
537 (ff-switch-to-buffer (get-file-buffer filename) other-window) | |
10491 | 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) | |
17548 | 579 (message "Finding buffer %s..." filename)) |
10491 | 580 |
17548 | 581 (if (bufferp (get-file-buffer filename)) |
582 (setq found (buffer-file-name (get-file-buffer filename)))) | |
10491 | 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) | |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
589 (setq found (buffer-file-name (car blist)))) |
10491 | 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) | |
17548 | 621 (message "Finding %s..." file)) |
10491 | 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) | |
13896 | 663 "Like `string-match', but set `case-fold-search' temporarily. |
11272 | 664 The value used comes from `ff-case-fold-search'." |
665 (let ((case-fold-search ff-case-fold-search)) | |
10491 | 666 (if regexp |
11272 | 667 (string-match regexp string start)))) |
10491 | 668 |
669 (defun ff-list-replace-env-vars (search-list) | |
670 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST." | |
671 (let (list | |
672 (var (car search-list))) | |
673 (while search-list | |
674 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var) | |
675 (setq var | |
676 (concat | |
677 (substring var (match-beginning 1) (match-end 1)) | |
678 (getenv (substring var (match-beginning 2) (match-end 2))) | |
679 (substring var (match-beginning 3) (match-end 3))))) | |
680 (setq search-list (cdr search-list)) | |
681 (setq list (cons var list)) | |
682 (setq var (car search-list))) | |
683 (setq search-list (reverse list)))) | |
684 | |
685 (defun ff-treat-as-special () | |
11272 | 686 "Returns the file to look for if the construct was special, else nil. |
13896 | 687 The construct is defined in the variable `ff-special-constructs'." |
10491 | 688 (let* (fname |
689 (list ff-special-constructs) | |
690 (elem (car list)) | |
691 (regexp (car elem)) | |
692 (match (cdr elem))) | |
693 (while (and list (not fname)) | |
694 (if (and (looking-at regexp) match) | |
695 (setq fname (funcall match))) | |
696 (setq list (cdr list)) | |
697 (setq elem (car list)) | |
698 (setq regexp (car elem)) | |
699 (setq match (cdr elem))) | |
700 fname)) | |
701 | |
702 (defun ff-basename (string) | |
11272 | 703 "Return the basename of PATHNAME." |
10491 | 704 (setq string (concat "/" string)) |
705 (string-match ".*/\\([^/]+\\)$" string) | |
706 (setq string (substring string (match-beginning 1) (match-end 1)))) | |
707 | |
708 (defun ff-all-dirs-under (here &optional exclude) | |
11272 | 709 "Get all the directory files under directory HERE. |
10491 | 710 Exclude all files in the optional EXCLUDE list." |
711 (if (file-directory-p here) | |
712 (condition-case nil | |
713 (progn | |
714 (let ((files (directory-files here t)) | |
715 (dirlist (list)) | |
716 file) | |
717 (while files | |
718 (setq file (car files)) | |
719 (if (and | |
720 (file-directory-p file) | |
721 (not (member (ff-basename file) exclude))) | |
722 (setq dirlist (cons file dirlist))) | |
723 (setq files (cdr files))) | |
724 (setq dirlist (reverse dirlist)))) | |
725 (error nil)) | |
726 nil)) | |
727 | |
728 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file) | |
11272 | 729 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW. |
730 In addition, this runs various hooks. | |
10491 | 731 |
11272 | 732 Either F1 or F2 receives FILE as the sole argument. |
733 The decision of which one to call is based on IN-OTHER-WINDOW | |
734 and on the global variable `ff-always-in-other-window'. | |
10491 | 735 |
11272 | 736 F1 and F2 are typically `find-file' / `find-file-other-window' |
737 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs. | |
738 | |
739 If optional NEW-FILE is t, then a special hook (`ff-file-created-hooks') is | |
740 called before `ff-post-load-hooks'." | |
10491 | 741 (if ff-pre-load-hooks |
742 (run-hooks 'ff-pre-load-hooks)) | |
743 (if (or | |
744 (and in-other-window (not ff-always-in-other-window)) | |
745 (and (not in-other-window) ff-always-in-other-window)) | |
746 (funcall f2 file) | |
747 (funcall f1 file)) | |
748 (if new-file | |
749 (if ff-file-created-hooks | |
750 (run-hooks 'ff-file-created-hooks))) | |
751 (if ff-post-load-hooks | |
752 (run-hooks 'ff-post-load-hooks))) | |
753 | |
754 (defun ff-find-file (file &optional in-other-window new-file) | |
13896 | 755 "Like `find-file', but may show the file in another window." |
10491 | 756 (ff-switch-file 'find-file |
757 'find-file-other-window | |
758 file in-other-window new-file)) | |
759 | |
13896 | 760 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window) |
761 "Like `switch-to-buffer', but may show the buffer in another window." | |
10491 | 762 |
763 (ff-switch-file 'switch-to-buffer | |
764 'switch-to-buffer-other-window | |
13896 | 765 buffer-or-name in-other-window nil)) |
10491 | 766 |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
767 (defun ff-goto-click (event) |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
768 (set-buffer (window-buffer (posn-window (event-end event)))) |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
769 (goto-char (posn-point (event-end event)))) |
10491 | 770 |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
771 ;;;###autoload |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
772 (defun ff-mouse-find-other-file (event) |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
773 "Visit the file you click on." |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
774 (interactive "e") |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
775 (save-excursion |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
776 (ff-goto-click event) |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
777 (ff-find-other-file nil))) |
10491 | 778 |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
779 ;;;###autoload |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
780 (defun ff-mouse-find-other-file-other-window (event) |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
781 "Visit the file you click on." |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
782 (interactive "e") |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
783 (save-excursion |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
784 (ff-goto-click event) |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
785 (ff-find-other-file t))) |
10491 | 786 |
787 (provide 'find-file) | |
788 | |
789 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
790 ;; This section offers an example of user defined function to select files | |
791 | |
11272 | 792 (defun ff-upcase-p (string &optional start end) |
793 "Return t if this string is all uppercase. | |
794 Given START and/or END, checks between these characters." | |
10491 | 795 (let (match str) |
796 (if (not start) | |
797 (setq start 0)) | |
798 (if (not end) | |
799 (setq end (length string))) | |
800 (if (= start end) | |
801 (setq end (1+ end))) | |
802 (setq str (substring string start end)) | |
803 (if (and | |
804 (ff-string-match "[A-Z]+" str) | |
805 (setq match (match-data)) | |
806 (= (car match) 0) | |
807 (= (car (cdr match)) (length str))) | |
808 t | |
809 nil))) | |
810 | |
811 (defun ff-cc-hh-converter (arg) | |
11272 | 812 "Discriminate file extensions. |
813 Build up a new file list based possibly on part of the directory name | |
814 and the name of the file passed in." | |
10491 | 815 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg) |
816 (let ((path (if (match-beginning 1) | |
817 (substring arg (match-beginning 1) (match-end 1)) nil)) | |
818 (dire (if (match-beginning 2) | |
819 (substring arg (match-beginning 2) (match-end 2)) nil)) | |
820 (file (if (match-beginning 3) | |
821 (substring arg (match-beginning 3) (match-end 3)) nil)) | |
822 (extn (if (match-beginning 4) | |
823 (substring arg (match-beginning 4) (match-end 4)) nil)) | |
824 return-list) | |
825 (cond | |
826 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h} | |
827 ((and (string= extn "cc") | |
828 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file)) | |
829 (let ((stub (substring file (match-beginning 2) (match-end 2)))) | |
830 (setq dire (upcase (substring file (match-beginning 1) (match-end 1)))) | |
831 (setq return-list (list (concat stub ".hh") | |
832 (concat stub ".h") | |
833 (concat file ".hh") | |
834 (concat file ".h"))) | |
835 )) | |
836 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C} | |
11272 | 837 ((and (string= extn "hh") (ff-upcase-p dire) file) |
10491 | 838 (let ((stub (concat (downcase dire) file))) |
839 (setq return-list (list (concat stub ".cc") | |
840 (concat stub ".C") | |
841 (concat file ".cc") | |
842 (concat file ".C"))) | |
843 )) | |
844 ;; zap.cc => zap.hh or zap.h | |
845 ((string= extn "cc") | |
846 (let ((stub file)) | |
847 (setq return-list (list (concat stub ".hh") | |
848 (concat stub ".h"))) | |
849 )) | |
850 ;; zap.hh => zap.cc or zap.C | |
851 ((string= extn "hh") | |
852 (let ((stub file)) | |
853 (setq return-list (list (concat stub ".cc") | |
854 (concat stub ".C"))) | |
855 )) | |
856 (t | |
857 nil)) | |
858 | |
859 return-list)) | |
860 | |
861 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
862 ;; This section offers an example of user defined function to place point. | |
863 ;; The regexps are Ada specific. | |
864 | |
865 (defvar ff-function-name nil "Name of the function we are in.") | |
866 | |
18119
8805be62c8f7
(ada-other-file-alist): Variable definition moved to ada-mode.el.
Richard M. Stallman <rms@gnu.org>
parents:
17548
diff
changeset
|
867 (eval-when-compile (require 'ada-mode)) |
10491 | 868 |
869 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in) | |
870 ;; | |
871 (defun ff-which-function-are-we-in () | |
11272 | 872 "Return the name of the function whose definition/declaration point is in. |
873 Also remember that name in `ff-function-name'." | |
10491 | 874 |
875 (setq ff-function-name nil) | |
876 | |
877 (save-excursion | |
878 (if (re-search-backward ada-procedure-start-regexp nil t) | |
879 (setq ff-function-name (buffer-substring (match-beginning 0) | |
880 (match-end 0))) | |
881 ; we didn't find a procedure start, perhaps there is a package | |
882 (if (re-search-backward ada-package-start-regexp nil t) | |
883 (setq ff-function-name (buffer-substring (match-beginning 0) | |
884 (match-end 0))) | |
885 )))) | |
886 | |
887 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly) | |
888 ;; | |
889 (defun ff-set-point-accordingly () | |
11272 | 890 "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
|
891 That name was previously determined by `ff-which-function-are-we-in'." |
10491 | 892 (if ff-function-name |
893 (progn | |
894 (goto-char (point-min)) | |
895 (search-forward ff-function-name nil t)))) | |
896 | |
897 ;; find-file.el ends here | |
898 |