Mercurial > emacs
annotate lisp/find-file.el @ 20068:9c4fd611c3ec
(narrow-to-page): Check whether forward-page
actually set the match data.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Thu, 16 Oct 1997 18:09:31 +0000 |
parents | 6316f98dda40 |
children | 9b9c929675eb |
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> |
10491 | 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 modula2-other-file-alist | |
243 '( | |
244 ("\\.mi$" (".md")) ;; Modula-2 module definition | |
245 ("\\.md$" (".mi")) ;; and implementation. | |
246 ) | |
11272 | 247 "*See the description for the `ff-search-directories' variable.") |
10491 | 248 |
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
250 ;; No user definable variables beyond this point! | |
251 ;; ============================================== | |
252 | |
253 (make-variable-buffer-local 'ff-pre-find-hooks) | |
254 (make-variable-buffer-local 'ff-pre-load-hooks) | |
255 (make-variable-buffer-local 'ff-post-load-hooks) | |
256 (make-variable-buffer-local 'ff-not-found-hooks) | |
257 (make-variable-buffer-local 'ff-file-created-hooks) | |
258 (make-variable-buffer-local 'ff-case-fold-search) | |
259 (make-variable-buffer-local 'ff-always-in-other-window) | |
260 (make-variable-buffer-local 'ff-ignore-include) | |
261 (make-variable-buffer-local 'ff-quiet-mode) | |
262 (make-variable-buffer-local 'ff-other-file-alist) | |
263 (make-variable-buffer-local 'ff-search-directories) | |
264 | |
265 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
266 ;; User entry points | |
267 | |
268 ;;;###autoload | |
269 (defun ff-get-other-file (&optional in-other-window) | |
11272 | 270 "Find the header or source file corresponding to this file. |
271 See also the documentation for `ff-find-other-file;. | |
10491 | 272 |
11272 | 273 If optional IN-OTHER-WINDOW is non-nil, find the file in another window." |
10491 | 274 (interactive "P") |
275 (let ((ignore ff-ignore-include)) | |
276 (setq ff-ignore-include t) | |
277 (ff-find-the-other-file in-other-window) | |
278 (setq ff-ignore-include ignore))) | |
279 | |
280 ;;;###autoload | |
281 (defun ff-find-other-file (&optional in-other-window ignore-include) | |
11272 | 282 "Find the header or source file corresponding to this file. |
283 Being on a `#include' line pulls in that file. | |
10491 | 284 |
11272 | 285 If optional IN-OTHER-WINDOW is non-nil, find the file in the other window. |
286 If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines. | |
10491 | 287 |
288 Variables of interest include: | |
289 | |
290 - ff-case-fold-search | |
291 Non-nil means ignore cases in matches (see case-fold-search). | |
292 If you have extensions in different cases, you will want this to be nil. | |
293 | |
294 - ff-always-in-other-window | |
295 If non-nil, always open the other file in another window, unless an | |
296 argument is given to ff-find-other-file. | |
297 | |
298 - ff-ignore-include | |
299 If non-nil, ignores #include lines. | |
300 | |
301 - ff-always-try-to-create | |
302 If non-nil, always attempt to create the other file if it was not found. | |
303 | |
304 - ff-quiet-mode | |
305 If non-nil, traces which directories are being searched. | |
306 | |
307 - ff-special-constructs | |
308 A list of regular expressions specifying how to recognise special | |
309 constructs such as include files etc, and an associated method for | |
310 extracting the filename from that construct. | |
311 | |
312 - ff-other-file-alist | |
313 Alist of extensions to find given the current file's extension. | |
314 | |
315 - ff-search-directories | |
316 List of directories searched through with each extension specified in | |
317 ff-other-file-alist that matches this file's extension. | |
318 | |
319 - ff-pre-find-hooks | |
320 List of functions to be called before the search for the file starts. | |
321 | |
322 - ff-pre-load-hooks | |
323 List of functions to be called before the other file is loaded. | |
324 | |
325 - ff-post-load-hooks | |
326 List of functions to be called after the other file is loaded. | |
327 | |
328 - ff-not-found-hooks | |
329 List of functions to be called if the other file could not be found. | |
330 | |
331 - ff-file-created-hooks | |
332 List of functions to be called if the other file has been created." | |
333 (interactive "P") | |
334 (let ((ignore ff-ignore-include)) | |
335 (setq ff-ignore-include ignore-include) | |
336 (ff-find-the-other-file in-other-window) | |
337 (setq ff-ignore-include ignore))) | |
338 | |
339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
340 ;; Support functions | |
341 | |
11272 | 342 (defun ff-emacs-19 () |
10491 | 343 (string-match "^19\\.[0-9]+\\.[0-9]+$" emacs-version)) |
344 | |
345 (defun ff-xemacs () | |
346 (or (string-match "Lucid" emacs-version) | |
347 (string-match "XEmacs" emacs-version))) | |
348 | |
349 (defun ff-find-the-other-file (&optional in-other-window) | |
11272 | 350 "Find the header or source file corresponding to the current file. |
351 Being on a `#include' line pulls in that file, but see the help on | |
352 the `ff-ignore-include' variable. | |
10491 | 353 |
11272 | 354 If optional IN-OTHER-WINDOW is non-nil, find the file in another window." |
10491 | 355 |
356 (let (match ;; matching regexp for this file | |
357 suffixes ;; set of replacing regexps for the matching regexp | |
358 action ;; function to generate the names of the other files | |
359 fname ;; basename of this file | |
360 pos ;; where we start matching filenames | |
361 stub ;; name of the file without extension | |
362 alist ;; working copy of the list of file extensions | |
363 pathname ;; the pathname of the file or the #include line | |
364 default-name ;; file we should create if none found | |
365 format ;; what we have to match | |
366 found ;; name of the file or buffer found - nil if none | |
367 dirs ;; local value of ff-search-directories | |
368 no-match) ;; whether we know about this kind of file | |
369 | |
370 (if ff-pre-find-hooks | |
371 (run-hooks 'ff-pre-find-hooks)) | |
372 | |
373 (message "Working...") | |
374 | |
375 (setq dirs | |
376 (if (symbolp ff-search-directories) | |
377 (ff-list-replace-env-vars (symbol-value ff-search-directories)) | |
378 (ff-list-replace-env-vars ff-search-directories))) | |
379 | |
380 (save-excursion | |
381 (beginning-of-line 1) | |
382 (setq fname (ff-treat-as-special))) | |
383 | |
384 (cond | |
385 ((and (not ff-ignore-include) fname) | |
386 (setq default-name fname) | |
387 (setq found (ff-get-file dirs fname nil in-other-window))) | |
388 | |
389 ;; let's just get the corresponding file | |
390 (t | |
391 (setq alist (if (symbolp ff-other-file-alist) | |
392 (symbol-value ff-other-file-alist) | |
393 ff-other-file-alist) | |
394 pathname (if (buffer-file-name) | |
395 (buffer-file-name) | |
396 "/none.none")) | |
397 | |
398 (string-match ".*/\\(.+\\)$" pathname) | |
399 (setq fname (substring pathname (match-beginning 1) (match-end 1)) | |
400 no-match nil | |
401 match (car alist)) | |
402 | |
403 ;; find the table entry corresponding to this file | |
404 (setq pos (ff-string-match (car match) fname)) | |
405 (while (and match (if (and pos (>= pos 0)) nil (not pos))) | |
406 (setq alist (cdr alist)) | |
407 (setq match (car alist)) | |
408 (setq pos (ff-string-match (car match) fname))) | |
409 | |
410 ;; no point going on if we haven't found anything | |
411 (if (not match) | |
412 (setq no-match t) | |
413 | |
414 ;; otherwise, suffixes contains what we need | |
415 (setq suffixes (car (cdr match)) | |
416 action (car (cdr match)) | |
417 found nil) | |
418 | |
419 ;; if we have a function to generate new names, | |
420 ;; invoke it with the name of the current file | |
421 (if (and (atom action) (fboundp action)) | |
422 (progn | |
423 (setq suffixes (funcall action (buffer-file-name)) | |
424 match (cons (car match) (list suffixes)) | |
425 stub nil | |
426 default-name (car suffixes))) | |
427 | |
428 ;; otherwise build our filename stub | |
429 (cond | |
430 | |
431 ;; get around the problem that 0 and nil both mean false! | |
432 ((= pos 0) | |
433 (setq format "") | |
434 (setq stub "") | |
435 ) | |
436 | |
437 (t | |
438 (setq format (concat "\\(.+\\)" (car match))) | |
439 (string-match format fname) | |
440 (setq stub (substring fname (match-beginning 1) (match-end 1))) | |
441 )) | |
442 | |
443 ;; if we find nothing, we should try to get a file like this one | |
444 (setq default-name | |
445 (concat stub (car (car (cdr match)))))) | |
446 | |
447 ;; do the real work - find the file | |
448 (setq found | |
449 (ff-get-file dirs | |
450 stub | |
451 suffixes | |
452 in-other-window))))) | |
453 | |
454 (cond | |
455 (no-match ;; could not even determine the other file | |
456 (message "")) | |
457 | |
458 (t | |
459 (cond | |
460 | |
461 ((not found) ;; could not find the other file | |
462 | |
463 (if ff-not-found-hooks ;; run the hooks | |
464 (run-hooks 'ff-not-found-hooks)) | |
465 | |
466 (cond | |
467 (ff-always-try-to-create ;; try to create the file | |
468 (let (name pathname) | |
469 | |
470 (setq name | |
471 (expand-file-name | |
472 (read-file-name | |
473 (format "Find or create %s in: " default-name) | |
474 default-directory default-name nil))) | |
475 | |
476 (setq pathname | |
477 (if (file-directory-p name) | |
478 (concat (file-name-as-directory name) default-name) | |
479 (setq found name))) | |
480 | |
481 (ff-find-file pathname in-other-window t))) | |
482 | |
483 (t ;; don't create the file, just whinge | |
17548 | 484 (message "No file found for %s" fname)))) |
10491 | 485 |
486 (t ;; matching file found | |
487 nil)))) | |
488 | |
489 found)) ;; return buffer-name or filename | |
490 | |
491 (defun ff-get-file (search-dirs fname-stub &optional suffix-list other-window) | |
492 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub). | |
493 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname | |
494 with each of the given suffixes. Gets the file or the buffer corresponding | |
495 to the name of the first file found, or nil. | |
496 | |
497 Arguments: (search-dirs fname-stub &optional suffix-list in-other-window) | |
498 " | |
499 (let ((filename (ff-get-file-name search-dirs fname-stub suffix-list))) | |
500 | |
501 (cond | |
502 ((not filename) | |
503 nil) | |
504 | |
13896 | 505 ((bufferp (get-file-buffer filename)) |
506 (ff-switch-to-buffer (get-file-buffer filename) other-window) | |
10491 | 507 filename) |
508 | |
509 ((file-exists-p filename) | |
510 (ff-find-file filename other-window nil) | |
511 filename) | |
512 | |
513 (t | |
514 nil)))) | |
515 | |
516 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list) | |
517 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub). | |
518 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname | |
519 with each of the given suffixes. Returns the name of the first file found. | |
520 | |
521 Arguments: (search-dirs fname-stub &optional suffix-list) | |
522 " | |
523 (let* (dirs ;; working copy of dirs to search | |
524 dir ;; the current dir considered | |
525 file ;; filename being looked for | |
526 rest ;; pathname after first /* | |
527 this-suffix ;; the suffix we are currently considering | |
528 suffixes ;; working copy of suffix-list | |
529 filename ;; built filename | |
530 blist ;; list of live buffers | |
531 buf ;; current buffer in blist | |
532 found) ;; whether we have found anything | |
533 | |
534 (setq suffixes suffix-list) | |
535 | |
536 ;; suffixes is nil => fname-stub is the file we are looking for | |
537 ;; otherwise fname-stub is a stub, and we append a suffix | |
538 (if suffixes | |
539 (setq this-suffix (car suffixes)) | |
540 (setq this-suffix "") | |
541 (setq suffixes (list ""))) | |
542 | |
543 ;; find whether the file is in a buffer first | |
544 (while (and suffixes (not found)) | |
545 (setq filename (concat fname-stub this-suffix)) | |
546 | |
547 (if (not ff-quiet-mode) | |
17548 | 548 (message "Finding buffer %s..." filename)) |
10491 | 549 |
17548 | 550 (if (bufferp (get-file-buffer filename)) |
551 (setq found (buffer-file-name (get-file-buffer filename)))) | |
10491 | 552 |
553 (setq blist (buffer-list)) | |
554 (setq buf (buffer-name (car blist))) | |
555 (while (and blist (not found)) | |
556 | |
557 (if (string-match (concat filename "<[0-9]+>") buf) | |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
558 (setq found (buffer-file-name (car blist)))) |
10491 | 559 |
560 (setq blist (cdr blist)) | |
561 (setq buf (buffer-name (car blist)))) | |
562 | |
563 (setq suffixes (cdr suffixes)) | |
564 (setq this-suffix (car suffixes))) | |
565 | |
566 ;; now look for the real file | |
567 (setq dirs search-dirs) | |
568 (setq dir (car dirs)) | |
569 (while (and (not found) dirs) | |
570 | |
571 (setq suffixes suffix-list) | |
572 | |
573 ;; if dir does not contain '/*', look for the file | |
574 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir))) | |
575 (progn | |
576 | |
577 ;; suffixes is nil => fname-stub is the file we are looking for | |
578 ;; otherwise fname-stub is a stub, and we append a suffix | |
579 (if suffixes | |
580 (setq this-suffix (car suffixes)) | |
581 (setq this-suffix "") | |
582 (setq suffixes (list ""))) | |
583 | |
584 (while (and suffixes (not found)) | |
585 | |
586 (setq filename (concat fname-stub this-suffix)) | |
587 (setq file (concat dir "/" filename)) | |
588 | |
589 (if (not ff-quiet-mode) | |
17548 | 590 (message "Finding %s..." file)) |
10491 | 591 |
592 (if (file-exists-p file) | |
593 (setq found file)) | |
594 | |
595 (setq suffixes (cdr suffixes)) | |
596 (setq this-suffix (car suffixes)))) | |
597 | |
598 ;; otherwise dir matches the '/*', so search each dir separately | |
599 (progn | |
600 (if (match-beginning 2) | |
601 (setq rest (substring dir (match-beginning 2) (match-end 2))) | |
602 (setq rest "") | |
603 ) | |
604 (setq dir (substring dir (match-beginning 1) (match-end 1))) | |
605 | |
606 (let ((dirlist (ff-all-dirs-under dir '(".."))) | |
607 this-dir compl-dirs) | |
608 | |
609 (setq this-dir (car dirlist)) | |
610 (while dirlist | |
611 (setq compl-dirs | |
612 (append | |
613 compl-dirs | |
614 (list (concat this-dir rest)) | |
615 )) | |
616 (setq dirlist (cdr dirlist)) | |
617 (setq this-dir (car dirlist))) | |
618 | |
619 (if compl-dirs | |
620 (setq found (ff-get-file-name compl-dirs | |
621 fname-stub | |
622 suffix-list)))))) | |
623 (setq dirs (cdr dirs)) | |
624 (setq dir (car dirs))) | |
625 | |
626 (if found | |
627 (message "%s found" found)) | |
628 | |
629 found)) | |
630 | |
631 (defun ff-string-match (regexp string &optional start) | |
13896 | 632 "Like `string-match', but set `case-fold-search' temporarily. |
11272 | 633 The value used comes from `ff-case-fold-search'." |
634 (let ((case-fold-search ff-case-fold-search)) | |
10491 | 635 (if regexp |
11272 | 636 (string-match regexp string start)))) |
10491 | 637 |
638 (defun ff-list-replace-env-vars (search-list) | |
639 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST." | |
640 (let (list | |
641 (var (car search-list))) | |
642 (while search-list | |
643 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var) | |
644 (setq var | |
645 (concat | |
646 (substring var (match-beginning 1) (match-end 1)) | |
647 (getenv (substring var (match-beginning 2) (match-end 2))) | |
648 (substring var (match-beginning 3) (match-end 3))))) | |
649 (setq search-list (cdr search-list)) | |
650 (setq list (cons var list)) | |
651 (setq var (car search-list))) | |
652 (setq search-list (reverse list)))) | |
653 | |
654 (defun ff-treat-as-special () | |
11272 | 655 "Returns the file to look for if the construct was special, else nil. |
13896 | 656 The construct is defined in the variable `ff-special-constructs'." |
10491 | 657 (let* (fname |
658 (list ff-special-constructs) | |
659 (elem (car list)) | |
660 (regexp (car elem)) | |
661 (match (cdr elem))) | |
662 (while (and list (not fname)) | |
663 (if (and (looking-at regexp) match) | |
664 (setq fname (funcall match))) | |
665 (setq list (cdr list)) | |
666 (setq elem (car list)) | |
667 (setq regexp (car elem)) | |
668 (setq match (cdr elem))) | |
669 fname)) | |
670 | |
671 (defun ff-basename (string) | |
11272 | 672 "Return the basename of PATHNAME." |
10491 | 673 (setq string (concat "/" string)) |
674 (string-match ".*/\\([^/]+\\)$" string) | |
675 (setq string (substring string (match-beginning 1) (match-end 1)))) | |
676 | |
677 (defun ff-all-dirs-under (here &optional exclude) | |
11272 | 678 "Get all the directory files under directory HERE. |
10491 | 679 Exclude all files in the optional EXCLUDE list." |
680 (if (file-directory-p here) | |
681 (condition-case nil | |
682 (progn | |
683 (let ((files (directory-files here t)) | |
684 (dirlist (list)) | |
685 file) | |
686 (while files | |
687 (setq file (car files)) | |
688 (if (and | |
689 (file-directory-p file) | |
690 (not (member (ff-basename file) exclude))) | |
691 (setq dirlist (cons file dirlist))) | |
692 (setq files (cdr files))) | |
693 (setq dirlist (reverse dirlist)))) | |
694 (error nil)) | |
695 nil)) | |
696 | |
697 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file) | |
11272 | 698 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW. |
699 In addition, this runs various hooks. | |
10491 | 700 |
11272 | 701 Either F1 or F2 receives FILE as the sole argument. |
702 The decision of which one to call is based on IN-OTHER-WINDOW | |
703 and on the global variable `ff-always-in-other-window'. | |
10491 | 704 |
11272 | 705 F1 and F2 are typically `find-file' / `find-file-other-window' |
706 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs. | |
707 | |
708 If optional NEW-FILE is t, then a special hook (`ff-file-created-hooks') is | |
709 called before `ff-post-load-hooks'." | |
10491 | 710 (if ff-pre-load-hooks |
711 (run-hooks 'ff-pre-load-hooks)) | |
712 (if (or | |
713 (and in-other-window (not ff-always-in-other-window)) | |
714 (and (not in-other-window) ff-always-in-other-window)) | |
715 (funcall f2 file) | |
716 (funcall f1 file)) | |
717 (if new-file | |
718 (if ff-file-created-hooks | |
719 (run-hooks 'ff-file-created-hooks))) | |
720 (if ff-post-load-hooks | |
721 (run-hooks 'ff-post-load-hooks))) | |
722 | |
723 (defun ff-find-file (file &optional in-other-window new-file) | |
13896 | 724 "Like `find-file', but may show the file in another window." |
10491 | 725 (ff-switch-file 'find-file |
726 'find-file-other-window | |
727 file in-other-window new-file)) | |
728 | |
13896 | 729 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window) |
730 "Like `switch-to-buffer', but may show the buffer in another window." | |
10491 | 731 |
732 (ff-switch-file 'switch-to-buffer | |
733 'switch-to-buffer-other-window | |
13896 | 734 buffer-or-name in-other-window nil)) |
10491 | 735 |
736 (cond | |
11272 | 737 ((ff-emacs-19) |
10491 | 738 (defun ff-goto-click (event) |
739 (set-buffer (window-buffer (posn-window (event-end event)))) | |
740 (goto-char (posn-point (event-end event)))) | |
741 | |
742 ;;;###autoload | |
743 (defun ff-mouse-find-other-file (event) | |
744 "Visit the file you click on." | |
745 (interactive "e") | |
746 (save-excursion | |
747 (ff-goto-click event) | |
748 (ff-find-other-file nil))) | |
749 | |
750 ;;;###autoload | |
751 (defun ff-mouse-find-other-file-other-window (event) | |
752 "Visit the file you click on." | |
753 (interactive "e") | |
754 (save-excursion | |
755 (ff-goto-click event) | |
756 (ff-find-other-file t))) | |
757 | |
758 ;;;###autoload | |
759 (defun locate-file (fname dirs &optional suffix-list ignore-perms) | |
760 "Defines XEmacs look-alike locate-file for GNU Emacs-19." | |
761 (interactive) | |
762 (ff-get-file dirs fname suffix-list)) | |
763 ) | |
764 | |
765 ((ff-xemacs) | |
766 | |
767 ;;;###autoload | |
768 (defun ff-mouse-find-other-file (event) | |
769 "Visit the file you click on." | |
770 (interactive "@e") | |
771 (save-excursion | |
772 (mouse-set-point event) | |
773 (ff-find-other-file nil))) | |
774 | |
775 ;;;###autoload | |
776 (defun ff-mouse-find-other-file-other-window (event) | |
777 "Visit the file you click on." | |
778 (interactive "@e") | |
779 (save-excursion | |
780 (mouse-set-point event) | |
781 (ff-find-other-file t))) | |
782 )) | |
783 | |
784 (provide 'find-file) | |
785 | |
786 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
787 ;; This section offers an example of user defined function to select files | |
788 | |
11272 | 789 (defun ff-upcase-p (string &optional start end) |
790 "Return t if this string is all uppercase. | |
791 Given START and/or END, checks between these characters." | |
10491 | 792 (let (match str) |
793 (if (not start) | |
794 (setq start 0)) | |
795 (if (not end) | |
796 (setq end (length string))) | |
797 (if (= start end) | |
798 (setq end (1+ end))) | |
799 (setq str (substring string start end)) | |
800 (if (and | |
801 (ff-string-match "[A-Z]+" str) | |
802 (setq match (match-data)) | |
803 (= (car match) 0) | |
804 (= (car (cdr match)) (length str))) | |
805 t | |
806 nil))) | |
807 | |
808 (defun ff-cc-hh-converter (arg) | |
11272 | 809 "Discriminate file extensions. |
810 Build up a new file list based possibly on part of the directory name | |
811 and the name of the file passed in." | |
10491 | 812 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg) |
813 (let ((path (if (match-beginning 1) | |
814 (substring arg (match-beginning 1) (match-end 1)) nil)) | |
815 (dire (if (match-beginning 2) | |
816 (substring arg (match-beginning 2) (match-end 2)) nil)) | |
817 (file (if (match-beginning 3) | |
818 (substring arg (match-beginning 3) (match-end 3)) nil)) | |
819 (extn (if (match-beginning 4) | |
820 (substring arg (match-beginning 4) (match-end 4)) nil)) | |
821 return-list) | |
822 (cond | |
823 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h} | |
824 ((and (string= extn "cc") | |
825 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file)) | |
826 (let ((stub (substring file (match-beginning 2) (match-end 2)))) | |
827 (setq dire (upcase (substring file (match-beginning 1) (match-end 1)))) | |
828 (setq return-list (list (concat stub ".hh") | |
829 (concat stub ".h") | |
830 (concat file ".hh") | |
831 (concat file ".h"))) | |
832 )) | |
833 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C} | |
11272 | 834 ((and (string= extn "hh") (ff-upcase-p dire) file) |
10491 | 835 (let ((stub (concat (downcase dire) file))) |
836 (setq return-list (list (concat stub ".cc") | |
837 (concat stub ".C") | |
838 (concat file ".cc") | |
839 (concat file ".C"))) | |
840 )) | |
841 ;; zap.cc => zap.hh or zap.h | |
842 ((string= extn "cc") | |
843 (let ((stub file)) | |
844 (setq return-list (list (concat stub ".hh") | |
845 (concat stub ".h"))) | |
846 )) | |
847 ;; zap.hh => zap.cc or zap.C | |
848 ((string= extn "hh") | |
849 (let ((stub file)) | |
850 (setq return-list (list (concat stub ".cc") | |
851 (concat stub ".C"))) | |
852 )) | |
853 (t | |
854 nil)) | |
855 | |
856 return-list)) | |
857 | |
858 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
859 ;; This section offers an example of user defined function to place point. | |
860 ;; The regexps are Ada specific. | |
861 | |
862 (defvar ff-function-name nil "Name of the function we are in.") | |
863 | |
18119
8805be62c8f7
(ada-other-file-alist): Variable definition moved to ada-mode.el.
Richard M. Stallman <rms@gnu.org>
parents:
17548
diff
changeset
|
864 (eval-when-compile (require 'ada-mode)) |
10491 | 865 |
866 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in) | |
867 ;; | |
868 (defun ff-which-function-are-we-in () | |
11272 | 869 "Return the name of the function whose definition/declaration point is in. |
870 Also remember that name in `ff-function-name'." | |
10491 | 871 |
872 (setq ff-function-name nil) | |
873 | |
874 (save-excursion | |
875 (if (re-search-backward ada-procedure-start-regexp nil t) | |
876 (setq ff-function-name (buffer-substring (match-beginning 0) | |
877 (match-end 0))) | |
878 ; we didn't find a procedure start, perhaps there is a package | |
879 (if (re-search-backward ada-package-start-regexp nil t) | |
880 (setq ff-function-name (buffer-substring (match-beginning 0) | |
881 (match-end 0))) | |
882 )))) | |
883 | |
884 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly) | |
885 ;; | |
886 (defun ff-set-point-accordingly () | |
11272 | 887 "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
|
888 That name was previously determined by `ff-which-function-are-we-in'." |
10491 | 889 (if ff-function-name |
890 (progn | |
891 (goto-char (point-min)) | |
892 (search-forward ff-function-name nil t)))) | |
893 | |
894 ;; find-file.el ends here | |
895 |