Mercurial > emacs
annotate lisp/find-file.el @ 22007:92d6cd3eebfa
(find-function-noselect): Autoload it.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 09 May 1998 19:53:47 +0000 |
parents | c297faa167f5 |
children | bf83c23f3300 |
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 | |
11272 | 380 (defun ff-emacs-19 () |
10491 | 381 (string-match "^19\\.[0-9]+\\.[0-9]+$" emacs-version)) |
382 | |
383 (defun ff-xemacs () | |
384 (or (string-match "Lucid" emacs-version) | |
385 (string-match "XEmacs" emacs-version))) | |
386 | |
387 (defun ff-find-the-other-file (&optional in-other-window) | |
11272 | 388 "Find the header or source file corresponding to the current file. |
389 Being on a `#include' line pulls in that file, but see the help on | |
390 the `ff-ignore-include' variable. | |
10491 | 391 |
11272 | 392 If optional IN-OTHER-WINDOW is non-nil, find the file in another window." |
10491 | 393 |
394 (let (match ;; matching regexp for this file | |
395 suffixes ;; set of replacing regexps for the matching regexp | |
396 action ;; function to generate the names of the other files | |
397 fname ;; basename of this file | |
398 pos ;; where we start matching filenames | |
399 stub ;; name of the file without extension | |
400 alist ;; working copy of the list of file extensions | |
401 pathname ;; the pathname of the file or the #include line | |
402 default-name ;; file we should create if none found | |
403 format ;; what we have to match | |
404 found ;; name of the file or buffer found - nil if none | |
405 dirs ;; local value of ff-search-directories | |
406 no-match) ;; whether we know about this kind of file | |
407 | |
408 (if ff-pre-find-hooks | |
409 (run-hooks 'ff-pre-find-hooks)) | |
410 | |
411 (message "Working...") | |
412 | |
413 (setq dirs | |
414 (if (symbolp ff-search-directories) | |
415 (ff-list-replace-env-vars (symbol-value ff-search-directories)) | |
416 (ff-list-replace-env-vars ff-search-directories))) | |
417 | |
418 (save-excursion | |
419 (beginning-of-line 1) | |
420 (setq fname (ff-treat-as-special))) | |
421 | |
422 (cond | |
423 ((and (not ff-ignore-include) fname) | |
424 (setq default-name fname) | |
425 (setq found (ff-get-file dirs fname nil in-other-window))) | |
426 | |
427 ;; let's just get the corresponding file | |
428 (t | |
429 (setq alist (if (symbolp ff-other-file-alist) | |
430 (symbol-value ff-other-file-alist) | |
431 ff-other-file-alist) | |
432 pathname (if (buffer-file-name) | |
433 (buffer-file-name) | |
434 "/none.none")) | |
435 | |
436 (string-match ".*/\\(.+\\)$" pathname) | |
437 (setq fname (substring pathname (match-beginning 1) (match-end 1)) | |
438 no-match nil | |
439 match (car alist)) | |
440 | |
441 ;; find the table entry corresponding to this file | |
442 (setq pos (ff-string-match (car match) fname)) | |
443 (while (and match (if (and pos (>= pos 0)) nil (not pos))) | |
444 (setq alist (cdr alist)) | |
445 (setq match (car alist)) | |
446 (setq pos (ff-string-match (car match) fname))) | |
447 | |
448 ;; no point going on if we haven't found anything | |
449 (if (not match) | |
450 (setq no-match t) | |
451 | |
452 ;; otherwise, suffixes contains what we need | |
453 (setq suffixes (car (cdr match)) | |
454 action (car (cdr match)) | |
455 found nil) | |
456 | |
457 ;; if we have a function to generate new names, | |
458 ;; invoke it with the name of the current file | |
459 (if (and (atom action) (fboundp action)) | |
460 (progn | |
461 (setq suffixes (funcall action (buffer-file-name)) | |
462 match (cons (car match) (list suffixes)) | |
463 stub nil | |
464 default-name (car suffixes))) | |
465 | |
466 ;; otherwise build our filename stub | |
467 (cond | |
468 | |
469 ;; get around the problem that 0 and nil both mean false! | |
470 ((= pos 0) | |
471 (setq format "") | |
472 (setq stub "") | |
473 ) | |
474 | |
475 (t | |
476 (setq format (concat "\\(.+\\)" (car match))) | |
477 (string-match format fname) | |
478 (setq stub (substring fname (match-beginning 1) (match-end 1))) | |
479 )) | |
480 | |
481 ;; if we find nothing, we should try to get a file like this one | |
482 (setq default-name | |
483 (concat stub (car (car (cdr match)))))) | |
484 | |
485 ;; do the real work - find the file | |
486 (setq found | |
487 (ff-get-file dirs | |
488 stub | |
489 suffixes | |
490 in-other-window))))) | |
491 | |
492 (cond | |
493 (no-match ;; could not even determine the other file | |
494 (message "")) | |
495 | |
496 (t | |
497 (cond | |
498 | |
499 ((not found) ;; could not find the other file | |
500 | |
501 (if ff-not-found-hooks ;; run the hooks | |
502 (run-hooks 'ff-not-found-hooks)) | |
503 | |
504 (cond | |
505 (ff-always-try-to-create ;; try to create the file | |
506 (let (name pathname) | |
507 | |
508 (setq name | |
509 (expand-file-name | |
510 (read-file-name | |
511 (format "Find or create %s in: " default-name) | |
512 default-directory default-name nil))) | |
513 | |
514 (setq pathname | |
515 (if (file-directory-p name) | |
516 (concat (file-name-as-directory name) default-name) | |
517 (setq found name))) | |
518 | |
519 (ff-find-file pathname in-other-window t))) | |
520 | |
521 (t ;; don't create the file, just whinge | |
17548 | 522 (message "No file found for %s" fname)))) |
10491 | 523 |
524 (t ;; matching file found | |
525 nil)))) | |
526 | |
527 found)) ;; return buffer-name or filename | |
528 | |
529 (defun ff-get-file (search-dirs fname-stub &optional suffix-list other-window) | |
530 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub). | |
531 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname | |
532 with each of the given suffixes. Gets the file or the buffer corresponding | |
533 to the name of the first file found, or nil. | |
534 | |
535 Arguments: (search-dirs fname-stub &optional suffix-list in-other-window) | |
536 " | |
537 (let ((filename (ff-get-file-name search-dirs fname-stub suffix-list))) | |
538 | |
539 (cond | |
540 ((not filename) | |
541 nil) | |
542 | |
13896 | 543 ((bufferp (get-file-buffer filename)) |
544 (ff-switch-to-buffer (get-file-buffer filename) other-window) | |
10491 | 545 filename) |
546 | |
547 ((file-exists-p filename) | |
548 (ff-find-file filename other-window nil) | |
549 filename) | |
550 | |
551 (t | |
552 nil)))) | |
553 | |
554 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list) | |
555 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub). | |
556 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname | |
557 with each of the given suffixes. Returns the name of the first file found. | |
558 | |
559 Arguments: (search-dirs fname-stub &optional suffix-list) | |
560 " | |
561 (let* (dirs ;; working copy of dirs to search | |
562 dir ;; the current dir considered | |
563 file ;; filename being looked for | |
564 rest ;; pathname after first /* | |
565 this-suffix ;; the suffix we are currently considering | |
566 suffixes ;; working copy of suffix-list | |
567 filename ;; built filename | |
568 blist ;; list of live buffers | |
569 buf ;; current buffer in blist | |
570 found) ;; whether we have found anything | |
571 | |
572 (setq suffixes suffix-list) | |
573 | |
574 ;; suffixes is nil => fname-stub is the file we are looking for | |
575 ;; otherwise fname-stub is a stub, and we append a suffix | |
576 (if suffixes | |
577 (setq this-suffix (car suffixes)) | |
578 (setq this-suffix "") | |
579 (setq suffixes (list ""))) | |
580 | |
581 ;; find whether the file is in a buffer first | |
582 (while (and suffixes (not found)) | |
583 (setq filename (concat fname-stub this-suffix)) | |
584 | |
585 (if (not ff-quiet-mode) | |
17548 | 586 (message "Finding buffer %s..." filename)) |
10491 | 587 |
17548 | 588 (if (bufferp (get-file-buffer filename)) |
589 (setq found (buffer-file-name (get-file-buffer filename)))) | |
10491 | 590 |
591 (setq blist (buffer-list)) | |
592 (setq buf (buffer-name (car blist))) | |
593 (while (and blist (not found)) | |
594 | |
595 (if (string-match (concat filename "<[0-9]+>") buf) | |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
596 (setq found (buffer-file-name (car blist)))) |
10491 | 597 |
598 (setq blist (cdr blist)) | |
599 (setq buf (buffer-name (car blist)))) | |
600 | |
601 (setq suffixes (cdr suffixes)) | |
602 (setq this-suffix (car suffixes))) | |
603 | |
604 ;; now look for the real file | |
605 (setq dirs search-dirs) | |
606 (setq dir (car dirs)) | |
607 (while (and (not found) dirs) | |
608 | |
609 (setq suffixes suffix-list) | |
610 | |
611 ;; if dir does not contain '/*', look for the file | |
612 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir))) | |
613 (progn | |
614 | |
615 ;; suffixes is nil => fname-stub is the file we are looking for | |
616 ;; otherwise fname-stub is a stub, and we append a suffix | |
617 (if suffixes | |
618 (setq this-suffix (car suffixes)) | |
619 (setq this-suffix "") | |
620 (setq suffixes (list ""))) | |
621 | |
622 (while (and suffixes (not found)) | |
623 | |
624 (setq filename (concat fname-stub this-suffix)) | |
625 (setq file (concat dir "/" filename)) | |
626 | |
627 (if (not ff-quiet-mode) | |
17548 | 628 (message "Finding %s..." file)) |
10491 | 629 |
630 (if (file-exists-p file) | |
631 (setq found file)) | |
632 | |
633 (setq suffixes (cdr suffixes)) | |
634 (setq this-suffix (car suffixes)))) | |
635 | |
636 ;; otherwise dir matches the '/*', so search each dir separately | |
637 (progn | |
638 (if (match-beginning 2) | |
639 (setq rest (substring dir (match-beginning 2) (match-end 2))) | |
640 (setq rest "") | |
641 ) | |
642 (setq dir (substring dir (match-beginning 1) (match-end 1))) | |
643 | |
644 (let ((dirlist (ff-all-dirs-under dir '(".."))) | |
645 this-dir compl-dirs) | |
646 | |
647 (setq this-dir (car dirlist)) | |
648 (while dirlist | |
649 (setq compl-dirs | |
650 (append | |
651 compl-dirs | |
652 (list (concat this-dir rest)) | |
653 )) | |
654 (setq dirlist (cdr dirlist)) | |
655 (setq this-dir (car dirlist))) | |
656 | |
657 (if compl-dirs | |
658 (setq found (ff-get-file-name compl-dirs | |
659 fname-stub | |
660 suffix-list)))))) | |
661 (setq dirs (cdr dirs)) | |
662 (setq dir (car dirs))) | |
663 | |
664 (if found | |
665 (message "%s found" found)) | |
666 | |
667 found)) | |
668 | |
669 (defun ff-string-match (regexp string &optional start) | |
13896 | 670 "Like `string-match', but set `case-fold-search' temporarily. |
11272 | 671 The value used comes from `ff-case-fold-search'." |
672 (let ((case-fold-search ff-case-fold-search)) | |
10491 | 673 (if regexp |
11272 | 674 (string-match regexp string start)))) |
10491 | 675 |
676 (defun ff-list-replace-env-vars (search-list) | |
677 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST." | |
678 (let (list | |
679 (var (car search-list))) | |
680 (while search-list | |
681 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var) | |
682 (setq var | |
683 (concat | |
684 (substring var (match-beginning 1) (match-end 1)) | |
685 (getenv (substring var (match-beginning 2) (match-end 2))) | |
686 (substring var (match-beginning 3) (match-end 3))))) | |
687 (setq search-list (cdr search-list)) | |
688 (setq list (cons var list)) | |
689 (setq var (car search-list))) | |
690 (setq search-list (reverse list)))) | |
691 | |
692 (defun ff-treat-as-special () | |
11272 | 693 "Returns the file to look for if the construct was special, else nil. |
13896 | 694 The construct is defined in the variable `ff-special-constructs'." |
10491 | 695 (let* (fname |
696 (list ff-special-constructs) | |
697 (elem (car list)) | |
698 (regexp (car elem)) | |
699 (match (cdr elem))) | |
700 (while (and list (not fname)) | |
701 (if (and (looking-at regexp) match) | |
702 (setq fname (funcall match))) | |
703 (setq list (cdr list)) | |
704 (setq elem (car list)) | |
705 (setq regexp (car elem)) | |
706 (setq match (cdr elem))) | |
707 fname)) | |
708 | |
709 (defun ff-basename (string) | |
11272 | 710 "Return the basename of PATHNAME." |
10491 | 711 (setq string (concat "/" string)) |
712 (string-match ".*/\\([^/]+\\)$" string) | |
713 (setq string (substring string (match-beginning 1) (match-end 1)))) | |
714 | |
715 (defun ff-all-dirs-under (here &optional exclude) | |
11272 | 716 "Get all the directory files under directory HERE. |
10491 | 717 Exclude all files in the optional EXCLUDE list." |
718 (if (file-directory-p here) | |
719 (condition-case nil | |
720 (progn | |
721 (let ((files (directory-files here t)) | |
722 (dirlist (list)) | |
723 file) | |
724 (while files | |
725 (setq file (car files)) | |
726 (if (and | |
727 (file-directory-p file) | |
728 (not (member (ff-basename file) exclude))) | |
729 (setq dirlist (cons file dirlist))) | |
730 (setq files (cdr files))) | |
731 (setq dirlist (reverse dirlist)))) | |
732 (error nil)) | |
733 nil)) | |
734 | |
735 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file) | |
11272 | 736 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW. |
737 In addition, this runs various hooks. | |
10491 | 738 |
11272 | 739 Either F1 or F2 receives FILE as the sole argument. |
740 The decision of which one to call is based on IN-OTHER-WINDOW | |
741 and on the global variable `ff-always-in-other-window'. | |
10491 | 742 |
11272 | 743 F1 and F2 are typically `find-file' / `find-file-other-window' |
744 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs. | |
745 | |
746 If optional NEW-FILE is t, then a special hook (`ff-file-created-hooks') is | |
747 called before `ff-post-load-hooks'." | |
10491 | 748 (if ff-pre-load-hooks |
749 (run-hooks 'ff-pre-load-hooks)) | |
750 (if (or | |
751 (and in-other-window (not ff-always-in-other-window)) | |
752 (and (not in-other-window) ff-always-in-other-window)) | |
753 (funcall f2 file) | |
754 (funcall f1 file)) | |
755 (if new-file | |
756 (if ff-file-created-hooks | |
757 (run-hooks 'ff-file-created-hooks))) | |
758 (if ff-post-load-hooks | |
759 (run-hooks 'ff-post-load-hooks))) | |
760 | |
761 (defun ff-find-file (file &optional in-other-window new-file) | |
13896 | 762 "Like `find-file', but may show the file in another window." |
10491 | 763 (ff-switch-file 'find-file |
764 'find-file-other-window | |
765 file in-other-window new-file)) | |
766 | |
13896 | 767 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window) |
768 "Like `switch-to-buffer', but may show the buffer in another window." | |
10491 | 769 |
770 (ff-switch-file 'switch-to-buffer | |
771 'switch-to-buffer-other-window | |
13896 | 772 buffer-or-name in-other-window nil)) |
10491 | 773 |
774 (cond | |
11272 | 775 ((ff-emacs-19) |
10491 | 776 (defun ff-goto-click (event) |
777 (set-buffer (window-buffer (posn-window (event-end event)))) | |
778 (goto-char (posn-point (event-end event)))) | |
779 | |
780 ;;;###autoload | |
781 (defun ff-mouse-find-other-file (event) | |
782 "Visit the file you click on." | |
783 (interactive "e") | |
784 (save-excursion | |
785 (ff-goto-click event) | |
786 (ff-find-other-file nil))) | |
787 | |
788 ;;;###autoload | |
789 (defun ff-mouse-find-other-file-other-window (event) | |
790 "Visit the file you click on." | |
791 (interactive "e") | |
792 (save-excursion | |
793 (ff-goto-click event) | |
794 (ff-find-other-file t))) | |
795 | |
796 ;;;###autoload | |
797 (defun locate-file (fname dirs &optional suffix-list ignore-perms) | |
798 "Defines XEmacs look-alike locate-file for GNU Emacs-19." | |
799 (interactive) | |
800 (ff-get-file dirs fname suffix-list)) | |
801 ) | |
802 | |
803 ((ff-xemacs) | |
804 | |
805 ;;;###autoload | |
806 (defun ff-mouse-find-other-file (event) | |
807 "Visit the file you click on." | |
808 (interactive "@e") | |
809 (save-excursion | |
810 (mouse-set-point event) | |
811 (ff-find-other-file nil))) | |
812 | |
813 ;;;###autoload | |
814 (defun ff-mouse-find-other-file-other-window (event) | |
815 "Visit the file you click on." | |
816 (interactive "@e") | |
817 (save-excursion | |
818 (mouse-set-point event) | |
819 (ff-find-other-file t))) | |
820 )) | |
821 | |
822 (provide 'find-file) | |
823 | |
824 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
825 ;; This section offers an example of user defined function to select files | |
826 | |
11272 | 827 (defun ff-upcase-p (string &optional start end) |
828 "Return t if this string is all uppercase. | |
829 Given START and/or END, checks between these characters." | |
10491 | 830 (let (match str) |
831 (if (not start) | |
832 (setq start 0)) | |
833 (if (not end) | |
834 (setq end (length string))) | |
835 (if (= start end) | |
836 (setq end (1+ end))) | |
837 (setq str (substring string start end)) | |
838 (if (and | |
839 (ff-string-match "[A-Z]+" str) | |
840 (setq match (match-data)) | |
841 (= (car match) 0) | |
842 (= (car (cdr match)) (length str))) | |
843 t | |
844 nil))) | |
845 | |
846 (defun ff-cc-hh-converter (arg) | |
11272 | 847 "Discriminate file extensions. |
848 Build up a new file list based possibly on part of the directory name | |
849 and the name of the file passed in." | |
10491 | 850 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg) |
851 (let ((path (if (match-beginning 1) | |
852 (substring arg (match-beginning 1) (match-end 1)) nil)) | |
853 (dire (if (match-beginning 2) | |
854 (substring arg (match-beginning 2) (match-end 2)) nil)) | |
855 (file (if (match-beginning 3) | |
856 (substring arg (match-beginning 3) (match-end 3)) nil)) | |
857 (extn (if (match-beginning 4) | |
858 (substring arg (match-beginning 4) (match-end 4)) nil)) | |
859 return-list) | |
860 (cond | |
861 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h} | |
862 ((and (string= extn "cc") | |
863 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file)) | |
864 (let ((stub (substring file (match-beginning 2) (match-end 2)))) | |
865 (setq dire (upcase (substring file (match-beginning 1) (match-end 1)))) | |
866 (setq return-list (list (concat stub ".hh") | |
867 (concat stub ".h") | |
868 (concat file ".hh") | |
869 (concat file ".h"))) | |
870 )) | |
871 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C} | |
11272 | 872 ((and (string= extn "hh") (ff-upcase-p dire) file) |
10491 | 873 (let ((stub (concat (downcase dire) file))) |
874 (setq return-list (list (concat stub ".cc") | |
875 (concat stub ".C") | |
876 (concat file ".cc") | |
877 (concat file ".C"))) | |
878 )) | |
879 ;; zap.cc => zap.hh or zap.h | |
880 ((string= extn "cc") | |
881 (let ((stub file)) | |
882 (setq return-list (list (concat stub ".hh") | |
883 (concat stub ".h"))) | |
884 )) | |
885 ;; zap.hh => zap.cc or zap.C | |
886 ((string= extn "hh") | |
887 (let ((stub file)) | |
888 (setq return-list (list (concat stub ".cc") | |
889 (concat stub ".C"))) | |
890 )) | |
891 (t | |
892 nil)) | |
893 | |
894 return-list)) | |
895 | |
896 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
897 ;; This section offers an example of user defined function to place point. | |
898 ;; The regexps are Ada specific. | |
899 | |
900 (defvar ff-function-name nil "Name of the function we are in.") | |
901 | |
18119
8805be62c8f7
(ada-other-file-alist): Variable definition moved to ada-mode.el.
Richard M. Stallman <rms@gnu.org>
parents:
17548
diff
changeset
|
902 (eval-when-compile (require 'ada-mode)) |
10491 | 903 |
904 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in) | |
905 ;; | |
906 (defun ff-which-function-are-we-in () | |
11272 | 907 "Return the name of the function whose definition/declaration point is in. |
908 Also remember that name in `ff-function-name'." | |
10491 | 909 |
910 (setq ff-function-name nil) | |
911 | |
912 (save-excursion | |
913 (if (re-search-backward ada-procedure-start-regexp nil t) | |
914 (setq ff-function-name (buffer-substring (match-beginning 0) | |
915 (match-end 0))) | |
916 ; we didn't find a procedure start, perhaps there is a package | |
917 (if (re-search-backward ada-package-start-regexp nil t) | |
918 (setq ff-function-name (buffer-substring (match-beginning 0) | |
919 (match-end 0))) | |
920 )))) | |
921 | |
922 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly) | |
923 ;; | |
924 (defun ff-set-point-accordingly () | |
11272 | 925 "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
|
926 That name was previously determined by `ff-which-function-are-we-in'." |
10491 | 927 (if ff-function-name |
928 (progn | |
929 (goto-char (point-min)) | |
930 (search-forward ff-function-name nil t)))) | |
931 | |
932 ;; find-file.el ends here | |
933 |