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