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