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