(unsafep-variable): Pass second arg to risky-local-variable-p.
[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
f7583fb6 205(defvaralias 'ff-related-file-alist 'ff-other-file-alist)
14d4446b 206(defcustom ff-other-file-alist 'cc-other-file-alist
2be55c9c
RS
207 "*Alist of extensions to find given the current file's extension.
208
209This list should contain the most used extensions before the others,
210since the search algorithm searches sequentially through each
8c17509e
RS
211directory specified in `ff-search-directories'. If a file is not found,
212a new one is created with the first matching extension (`.cc' yields `.hh').
14d4446b
SE
213This alist should be set by the major mode."
214 :type '(choice (repeat (list regexp (choice (repeat string) function)))
215 symbol)
216 :group 'ff)
2be55c9c 217
14d4446b 218(defcustom ff-search-directories 'cc-search-directories
2be55c9c
RS
219 "*List of directories to search for a specific file.
220
8c17509e 221Set by default to `cc-search-directories', expanded at run-time.
2be55c9c
RS
222
223This list is searched through with each extension specified in
8c17509e 224`ff-other-file-alist' that matches this file's extension. So the
2be55c9c
RS
225longer the list, the longer it'll take to realise that a file
226may not exist.
227
98d775e5 228A typical format is
2be55c9c 229
17feeeb2 230 '(\".\" \"/usr/include\" \"$PROJECT/*/include\")
2be55c9c 231
8c17509e 232Environment variables can be inserted between slashes (`/').
2be55c9c 233They will be replaced by their definition. If a variable does
8c17509e 234not exist, it is replaced (silently) with an empty string.
2be55c9c 235
8c17509e
RS
236The stars are *not* wildcards: they are searched for together with
237the preceding slash. The star represents all the subdirectories except
14d4446b
SE
238`..', and each of these subdirectories will be searched in turn."
239 :type '(choice (repeat directory) symbol)
240 :group 'ff)
2be55c9c 241
14d4446b 242(defcustom cc-search-directories
17feeeb2 243 '("." "/usr/include" "/usr/local/include/*")
14d4446b
SE
244 "*See the description of the `ff-search-directories' variable."
245 :type '(repeat directory)
246 :group 'ff)
2be55c9c 247
14d4446b 248(defcustom cc-other-file-alist
2be55c9c
RS
249 '(
250 ("\\.cc$" (".hh" ".h"))
251 ("\\.hh$" (".cc" ".C"))
252
253 ("\\.c$" (".h"))
254 ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp"))
255
256 ("\\.C$" (".H" ".hh" ".h"))
257 ("\\.H$" (".C" ".CC"))
258
259 ("\\.CC$" (".HH" ".H" ".hh" ".h"))
260 ("\\.HH$" (".CC"))
261
262 ("\\.cxx$" (".hh" ".h"))
263 ("\\.cpp$" (".hh" ".h"))
264 )
265 "*Alist of extensions to find given the current file's extension.
266
267This list should contain the most used extensions before the others,
268since the search algorithm searches sequentially through each directory
8c17509e 269specified in `ff-search-directories'. If a file is not found, a new one
14d4446b
SE
270is created with the first matching extension (`.cc' yields `.hh')."
271 :type '(repeat (list regexp (choice (repeat string) function)))
272 :group 'ff)
2be55c9c 273
14d4446b 274(defcustom modula2-other-file-alist
2be55c9c
RS
275 '(
276 ("\\.mi$" (".md")) ;; Modula-2 module definition
277 ("\\.md$" (".mi")) ;; and implementation.
278 )
14d4446b
SE
279 "*See the description for the `ff-search-directories' variable."
280 :type '(repeat (list regexp (choice (repeat string) function)))
281 :group 'ff)
282
2be55c9c
RS
283
284;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
285;; No user definable variables beyond this point!
286;; ==============================================
287
03741cc5
SM
288(make-variable-buffer-local 'ff-pre-find-hook)
289(make-variable-buffer-local 'ff-pre-load-hook)
290(make-variable-buffer-local 'ff-post-load-hook)
291(make-variable-buffer-local 'ff-not-found-hook)
292(make-variable-buffer-local 'ff-file-created-hook)
2be55c9c
RS
293(make-variable-buffer-local 'ff-case-fold-search)
294(make-variable-buffer-local 'ff-always-in-other-window)
295(make-variable-buffer-local 'ff-ignore-include)
296(make-variable-buffer-local 'ff-quiet-mode)
297(make-variable-buffer-local 'ff-other-file-alist)
298(make-variable-buffer-local 'ff-search-directories)
299
300;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
301;; User entry points
302
303;;;###autoload
304(defun ff-get-other-file (&optional in-other-window)
8c17509e 305 "Find the header or source file corresponding to this file.
98d775e5 306See also the documentation for `ff-find-other-file'.
2be55c9c 307
8c17509e 308If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
2be55c9c
RS
309 (interactive "P")
310 (let ((ignore ff-ignore-include))
311 (setq ff-ignore-include t)
312 (ff-find-the-other-file in-other-window)
313 (setq ff-ignore-include ignore)))
314
f7583fb6
RS
315;;;###
316(defalias 'ff-find-related-file 'ff-find-other-file)
317
2be55c9c
RS
318;;;###autoload
319(defun ff-find-other-file (&optional in-other-window ignore-include)
8c17509e
RS
320 "Find the header or source file corresponding to this file.
321Being on a `#include' line pulls in that file.
2be55c9c 322
8c17509e
RS
323If optional IN-OTHER-WINDOW is non-nil, find the file in the other window.
324If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines.
2be55c9c
RS
325
326Variables of interest include:
327
98d775e5
DL
328 - `ff-case-fold-search'
329 Non-nil means ignore cases in matches (see `case-fold-search').
2be55c9c
RS
330 If you have extensions in different cases, you will want this to be nil.
331
98d775e5 332 - `ff-always-in-other-window'
2be55c9c 333 If non-nil, always open the other file in another window, unless an
98d775e5 334 argument is given to `ff-find-other-file'.
2be55c9c 335
98d775e5 336 - `ff-ignore-include'
2be55c9c
RS
337 If non-nil, ignores #include lines.
338
98d775e5 339 - `ff-always-try-to-create'
2be55c9c
RS
340 If non-nil, always attempt to create the other file if it was not found.
341
98d775e5 342 - `ff-quiet-mode'
2be55c9c
RS
343 If non-nil, traces which directories are being searched.
344
98d775e5
DL
345 - `ff-special-constructs'
346 A list of regular expressions specifying how to recognise special
347 constructs such as include files etc, and an associated method for
2be55c9c
RS
348 extracting the filename from that construct.
349
98d775e5 350 - `ff-other-file-alist'
2be55c9c
RS
351 Alist of extensions to find given the current file's extension.
352
98d775e5 353 - `ff-search-directories'
2be55c9c 354 List of directories searched through with each extension specified in
98d775e5 355 `ff-other-file-alist' that matches this file's extension.
2be55c9c 356
03741cc5 357 - `ff-pre-find-hook'
2be55c9c
RS
358 List of functions to be called before the search for the file starts.
359
03741cc5 360 - `ff-pre-load-hook'
2be55c9c
RS
361 List of functions to be called before the other file is loaded.
362
03741cc5 363 - `ff-post-load-hook'
2be55c9c
RS
364 List of functions to be called after the other file is loaded.
365
03741cc5 366 - `ff-not-found-hook'
2be55c9c
RS
367 List of functions to be called if the other file could not be found.
368
03741cc5 369 - `ff-file-created-hook'
2be55c9c
RS
370 List of functions to be called if the other file has been created."
371 (interactive "P")
372 (let ((ignore ff-ignore-include))
373 (setq ff-ignore-include ignore-include)
374 (ff-find-the-other-file in-other-window)
375 (setq ff-ignore-include ignore)))
376
377;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378;; Support functions
379
2be55c9c 380(defun ff-find-the-other-file (&optional in-other-window)
8c17509e
RS
381 "Find the header or source file corresponding to the current file.
382Being on a `#include' line pulls in that file, but see the help on
383the `ff-ignore-include' variable.
2be55c9c 384
8c17509e 385If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
2be55c9c
RS
386
387 (let (match ;; matching regexp for this file
388 suffixes ;; set of replacing regexps for the matching regexp
389 action ;; function to generate the names of the other files
390 fname ;; basename of this file
391 pos ;; where we start matching filenames
392 stub ;; name of the file without extension
393 alist ;; working copy of the list of file extensions
394 pathname ;; the pathname of the file or the #include line
395 default-name ;; file we should create if none found
98d775e5
DL
396 format ;; what we have to match
397 found ;; name of the file or buffer found - nil if none
2be55c9c
RS
398 dirs ;; local value of ff-search-directories
399 no-match) ;; whether we know about this kind of file
400
03741cc5 401 (run-hooks 'ff-pre-find-hook 'ff-pre-find-hooks)
2be55c9c
RS
402
403 (message "Working...")
404
405 (setq dirs
406 (if (symbolp ff-search-directories)
407 (ff-list-replace-env-vars (symbol-value ff-search-directories))
408 (ff-list-replace-env-vars ff-search-directories)))
409
410 (save-excursion
411 (beginning-of-line 1)
412 (setq fname (ff-treat-as-special)))
413
414 (cond
415 ((and (not ff-ignore-include) fname)
416 (setq default-name fname)
417 (setq found (ff-get-file dirs fname nil in-other-window)))
418
419 ;; let's just get the corresponding file
420 (t
421 (setq alist (if (symbolp ff-other-file-alist)
422 (symbol-value ff-other-file-alist)
423 ff-other-file-alist)
424 pathname (if (buffer-file-name)
425 (buffer-file-name)
426 "/none.none"))
427
f4a97216 428 (setq fname (file-name-nondirectory pathname)
2be55c9c
RS
429 no-match nil
430 match (car alist))
431
432 ;; find the table entry corresponding to this file
433 (setq pos (ff-string-match (car match) fname))
434 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
435 (setq alist (cdr alist))
436 (setq match (car alist))
437 (setq pos (ff-string-match (car match) fname)))
438
439 ;; no point going on if we haven't found anything
440 (if (not match)
441 (setq no-match t)
442
443 ;; otherwise, suffixes contains what we need
444 (setq suffixes (car (cdr match))
445 action (car (cdr match))
446 found nil)
447
98d775e5 448 ;; if we have a function to generate new names,
2be55c9c
RS
449 ;; invoke it with the name of the current file
450 (if (and (atom action) (fboundp action))
451 (progn
452 (setq suffixes (funcall action (buffer-file-name))
453 match (cons (car match) (list suffixes))
454 stub nil
455 default-name (car suffixes)))
456
457 ;; otherwise build our filename stub
98d775e5 458 (cond
2be55c9c
RS
459
460 ;; get around the problem that 0 and nil both mean false!
461 ((= pos 0)
462 (setq format "")
463 (setq stub "")
464 )
465
466 (t
467 (setq format (concat "\\(.+\\)" (car match)))
468 (string-match format fname)
469 (setq stub (substring fname (match-beginning 1) (match-end 1)))
470 ))
471
472 ;; if we find nothing, we should try to get a file like this one
473 (setq default-name
474 (concat stub (car (car (cdr match))))))
475
476 ;; do the real work - find the file
98d775e5 477 (setq found
2be55c9c
RS
478 (ff-get-file dirs
479 stub
98d775e5 480 suffixes
2be55c9c
RS
481 in-other-window)))))
482
483 (cond
484 (no-match ;; could not even determine the other file
485 (message ""))
486
98d775e5 487 (t
2be55c9c
RS
488 (cond
489
490 ((not found) ;; could not find the other file
491
03741cc5 492 (run-hooks 'ff-not-found-hook 'ff-not-found-hooks)
2be55c9c 493
98d775e5 494 (cond
2be55c9c
RS
495 (ff-always-try-to-create ;; try to create the file
496 (let (name pathname)
497
498 (setq name
499 (expand-file-name
500 (read-file-name
501 (format "Find or create %s in: " default-name)
502 default-directory default-name nil)))
503
504 (setq pathname
505 (if (file-directory-p name)
506 (concat (file-name-as-directory name) default-name)
507 (setq found name)))
508
509 (ff-find-file pathname in-other-window t)))
510
511 (t ;; don't create the file, just whinge
988021a7 512 (message "No file found for %s" fname))))
2be55c9c
RS
513
514 (t ;; matching file found
515 nil))))
516
517 found)) ;; return buffer-name or filename
518
274890d9
RS
519(defun ff-other-file-name ()
520 "Return name of the header or source file corresponding to the current file.
521Being on a `#include' line pulls in that file, but see the help on
522the `ff-ignore-include' variable."
523
524 (let (match ;; matching regexp for this file
525 suffixes ;; set of replacing regexps for the matching regexp
526 action ;; function to generate the names of the other files
527 fname ;; basename of this file
528 pos ;; where we start matching filenames
529 stub ;; name of the file without extension
530 alist ;; working copy of the list of file extensions
531 pathname ;; the pathname of the file or the #include line
532 default-name ;; file we should create if none found
533 format ;; what we have to match
534 found ;; name of the file or buffer found - nil if none
535 dirs ;; local value of ff-search-directories
536 no-match) ;; whether we know about this kind of file
537
538 (message "Working...")
539
540 (setq dirs
541 (if (symbolp ff-search-directories)
542 (ff-list-replace-env-vars (symbol-value ff-search-directories))
543 (ff-list-replace-env-vars ff-search-directories)))
544
545 (save-excursion
546 (beginning-of-line 1)
547 (setq fname (ff-treat-as-special)))
548
549 (cond
550 ((and (not ff-ignore-include) fname)
551 (setq default-name fname)
552 (setq found (ff-get-file-name dirs fname nil)))
553
554 ;; let's just get the corresponding file
555 (t
556 (setq alist (if (symbolp ff-other-file-alist)
557 (symbol-value ff-other-file-alist)
558 ff-other-file-alist)
559 pathname (if (buffer-file-name)
560 (buffer-file-name)
561 "/none.none"))
562
563 (setq fname (file-name-nondirectory pathname)
564 no-match nil
565 match (car alist))
566
567 ;; find the table entry corresponding to this file
568 (setq pos (ff-string-match (car match) fname))
569 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
570 (setq alist (cdr alist))
571 (setq match (car alist))
572 (setq pos (ff-string-match (car match) fname)))
573
574 ;; no point going on if we haven't found anything
575 (if (not match)
576 (setq no-match t)
577
578 ;; otherwise, suffixes contains what we need
579 (setq suffixes (car (cdr match))
580 action (car (cdr match))
581 found nil)
582
583 ;; if we have a function to generate new names,
584 ;; invoke it with the name of the current file
585 (if (and (atom action) (fboundp action))
586 (progn
587 (setq suffixes (funcall action (buffer-file-name))
588 match (cons (car match) (list suffixes))
589 stub nil
590 default-name (car suffixes)))
591
592 ;; otherwise build our filename stub
593 (cond
594
595 ;; get around the problem that 0 and nil both mean false!
596 ((= pos 0)
597 (setq format "")
598 (setq stub "")
599 )
600
601 (t
602 (setq format (concat "\\(.+\\)" (car match)))
603 (string-match format fname)
604 (setq stub (substring fname (match-beginning 1) (match-end 1)))
605 ))
606
607 ;; if we find nothing, we should try to get a file like this one
608 (setq default-name
609 (concat stub (car (car (cdr match))))))
610
611 ;; do the real work - find the file
612 (setq found
613 (ff-get-file-name dirs stub suffixes)))))
614 found)) ;; return buffer-name or filename
615
98d775e5
DL
616(defun ff-get-file (search-dirs filename &optional suffix-list other-window)
617 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
618If (optional) SUFFIX-LIST is nil, search for fname, otherwise search
619for fname with each of the given suffixes. Get the file or the buffer
620corresponding to the name of the first file found, or nil."
621 (let ((filename (ff-get-file-name search-dirs filename suffix-list)))
2be55c9c 622
98d775e5 623 (cond
2be55c9c
RS
624 ((not filename)
625 nil)
626
91afecb3
RS
627 ((bufferp (get-file-buffer filename))
628 (ff-switch-to-buffer (get-file-buffer filename) other-window)
2be55c9c
RS
629 filename)
630
631 ((file-exists-p filename)
632 (ff-find-file filename other-window nil)
633 filename)
634
635 (t
636 nil))))
637
638(defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
98d775e5
DL
639 "Find a file in SEARCH-DIRS with the given name (or stub) FNAME-STUB.
640If (optional) SUFFIX-LIST is nil, search for FNAME-STUB, otherwise
641search for FNAME-STUB with each of the given suffixes. Return the
642name of the first file found."
2be55c9c
RS
643 (let* (dirs ;; working copy of dirs to search
644 dir ;; the current dir considered
645 file ;; filename being looked for
646 rest ;; pathname after first /*
647 this-suffix ;; the suffix we are currently considering
648 suffixes ;; working copy of suffix-list
649 filename ;; built filename
650 blist ;; list of live buffers
651 buf ;; current buffer in blist
652 found) ;; whether we have found anything
653
654 (setq suffixes suffix-list)
655
656 ;; suffixes is nil => fname-stub is the file we are looking for
657 ;; otherwise fname-stub is a stub, and we append a suffix
658 (if suffixes
659 (setq this-suffix (car suffixes))
660 (setq this-suffix "")
661 (setq suffixes (list "")))
662
663 ;; find whether the file is in a buffer first
664 (while (and suffixes (not found))
665 (setq filename (concat fname-stub this-suffix))
666
667 (if (not ff-quiet-mode)
988021a7 668 (message "Finding buffer %s..." filename))
2be55c9c 669
988021a7
RS
670 (if (bufferp (get-file-buffer filename))
671 (setq found (buffer-file-name (get-file-buffer filename))))
2be55c9c
RS
672
673 (setq blist (buffer-list))
674 (setq buf (buffer-name (car blist)))
675 (while (and blist (not found))
676
677 (if (string-match (concat filename "<[0-9]+>") buf)
17feeeb2 678 (setq found (buffer-file-name (car blist))))
2be55c9c
RS
679
680 (setq blist (cdr blist))
681 (setq buf (buffer-name (car blist))))
682
683 (setq suffixes (cdr suffixes))
684 (setq this-suffix (car suffixes)))
685
686 ;; now look for the real file
687 (setq dirs search-dirs)
688 (setq dir (car dirs))
689 (while (and (not found) dirs)
690
691 (setq suffixes suffix-list)
692
693 ;; if dir does not contain '/*', look for the file
694 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir)))
98d775e5 695 (progn
2be55c9c
RS
696
697 ;; suffixes is nil => fname-stub is the file we are looking for
698 ;; otherwise fname-stub is a stub, and we append a suffix
699 (if suffixes
700 (setq this-suffix (car suffixes))
701 (setq this-suffix "")
702 (setq suffixes (list "")))
703
704 (while (and suffixes (not found))
705
706 (setq filename (concat fname-stub this-suffix))
707 (setq file (concat dir "/" filename))
708
709 (if (not ff-quiet-mode)
988021a7 710 (message "Finding %s..." file))
2be55c9c
RS
711
712 (if (file-exists-p file)
713 (setq found file))
714
715 (setq suffixes (cdr suffixes))
716 (setq this-suffix (car suffixes))))
717
718 ;; otherwise dir matches the '/*', so search each dir separately
719 (progn
720 (if (match-beginning 2)
721 (setq rest (substring dir (match-beginning 2) (match-end 2)))
722 (setq rest "")
723 )
724 (setq dir (substring dir (match-beginning 1) (match-end 1)))
725
726 (let ((dirlist (ff-all-dirs-under dir '("..")))
727 this-dir compl-dirs)
728
729 (setq this-dir (car dirlist))
730 (while dirlist
731 (setq compl-dirs
732 (append
733 compl-dirs
734 (list (concat this-dir rest))
735 ))
736 (setq dirlist (cdr dirlist))
737 (setq this-dir (car dirlist)))
738
739 (if compl-dirs
740 (setq found (ff-get-file-name compl-dirs
741 fname-stub
742 suffix-list))))))
743 (setq dirs (cdr dirs))
744 (setq dir (car dirs)))
745
746 (if found
747 (message "%s found" found))
748
749 found))
750
751(defun ff-string-match (regexp string &optional start)
91afecb3 752 "Like `string-match', but set `case-fold-search' temporarily.
8c17509e
RS
753The value used comes from `ff-case-fold-search'."
754 (let ((case-fold-search ff-case-fold-search))
2be55c9c 755 (if regexp
8c17509e 756 (string-match regexp string start))))
2be55c9c
RS
757
758(defun ff-list-replace-env-vars (search-list)
759 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST."
760 (let (list
761 (var (car search-list)))
762 (while search-list
763 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var)
764 (setq var
765 (concat
766 (substring var (match-beginning 1) (match-end 1))
767 (getenv (substring var (match-beginning 2) (match-end 2)))
768 (substring var (match-beginning 3) (match-end 3)))))
769 (setq search-list (cdr search-list))
770 (setq list (cons var list))
771 (setq var (car search-list)))
772 (setq search-list (reverse list))))
773
774(defun ff-treat-as-special ()
98d775e5 775 "Return the file to look for if the construct was special, else nil.
91afecb3 776The construct is defined in the variable `ff-special-constructs'."
2be55c9c
RS
777 (let* (fname
778 (list ff-special-constructs)
779 (elem (car list))
780 (regexp (car elem))
781 (match (cdr elem)))
782 (while (and list (not fname))
783 (if (and (looking-at regexp) match)
784 (setq fname (funcall match)))
785 (setq list (cdr list))
786 (setq elem (car list))
787 (setq regexp (car elem))
788 (setq match (cdr elem)))
789 fname))
790
791(defun ff-basename (string)
98d775e5 792 "Return the basename of pathname STRING."
2be55c9c
RS
793 (setq string (concat "/" string))
794 (string-match ".*/\\([^/]+\\)$" string)
795 (setq string (substring string (match-beginning 1) (match-end 1))))
796
797(defun ff-all-dirs-under (here &optional exclude)
8c17509e 798 "Get all the directory files under directory HERE.
2be55c9c
RS
799Exclude all files in the optional EXCLUDE list."
800 (if (file-directory-p here)
801 (condition-case nil
802 (progn
803 (let ((files (directory-files here t))
804 (dirlist (list))
805 file)
806 (while files
807 (setq file (car files))
808 (if (and
809 (file-directory-p file)
810 (not (member (ff-basename file) exclude)))
811 (setq dirlist (cons file dirlist)))
812 (setq files (cdr files)))
813 (setq dirlist (reverse dirlist))))
814 (error nil))
815 nil))
816
817(defun ff-switch-file (f1 f2 file &optional in-other-window new-file)
8c17509e
RS
818 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW.
819In addition, this runs various hooks.
2be55c9c 820
8c17509e
RS
821Either F1 or F2 receives FILE as the sole argument.
822The decision of which one to call is based on IN-OTHER-WINDOW
823and on the global variable `ff-always-in-other-window'.
2be55c9c 824
8c17509e
RS
825F1 and F2 are typically `find-file' / `find-file-other-window'
826or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs.
827
03741cc5
SM
828If optional NEW-FILE is t, then a special hook (`ff-file-created-hook') is
829called before `ff-post-load-hook'."
830 (run-hooks 'ff-pre-load-hook 'ff-pre-load-hooks)
2be55c9c
RS
831 (if (or
832 (and in-other-window (not ff-always-in-other-window))
833 (and (not in-other-window) ff-always-in-other-window))
834 (funcall f2 file)
835 (funcall f1 file))
836 (if new-file
03741cc5
SM
837 (run-hooks 'ff-file-created-hook 'ff-file-created-hooks))
838 (run-hooks 'ff-post-load-hook 'ff-post-load-hooks))
2be55c9c
RS
839
840(defun ff-find-file (file &optional in-other-window new-file)
91afecb3 841 "Like `find-file', but may show the file in another window."
98d775e5
DL
842 (ff-switch-file 'find-file
843 'find-file-other-window
2be55c9c
RS
844 file in-other-window new-file))
845
91afecb3
RS
846(defun ff-switch-to-buffer (buffer-or-name &optional in-other-window)
847 "Like `switch-to-buffer', but may show the buffer in another window."
2be55c9c 848
98d775e5
DL
849 (ff-switch-file 'switch-to-buffer
850 'switch-to-buffer-other-window
91afecb3 851 buffer-or-name in-other-window nil))
2be55c9c 852
6e9aac74
RS
853;;;###autoload
854(defun ff-mouse-find-other-file (event)
855 "Visit the file you click on."
856 (interactive "e")
857 (save-excursion
98d775e5 858 (mouse-set-point event)
6e9aac74 859 (ff-find-other-file nil)))
2be55c9c 860
6e9aac74
RS
861;;;###autoload
862(defun ff-mouse-find-other-file-other-window (event)
98d775e5 863 "Visit the file you click on in another window."
6e9aac74
RS
864 (interactive "e")
865 (save-excursion
98d775e5 866 (mouse-set-point event)
6e9aac74 867 (ff-find-other-file t)))
2be55c9c 868
2be55c9c
RS
869;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
870;; This section offers an example of user defined function to select files
871
8c17509e 872(defun ff-upcase-p (string &optional start end)
98d775e5 873 "Return t if STRING is all uppercase.
8c17509e 874Given START and/or END, checks between these characters."
2be55c9c
RS
875 (let (match str)
876 (if (not start)
877 (setq start 0))
878 (if (not end)
879 (setq end (length string)))
880 (if (= start end)
881 (setq end (1+ end)))
882 (setq str (substring string start end))
98d775e5 883 (if (and
2be55c9c
RS
884 (ff-string-match "[A-Z]+" str)
885 (setq match (match-data))
886 (= (car match) 0)
887 (= (car (cdr match)) (length str)))
888 t
889 nil)))
890
891(defun ff-cc-hh-converter (arg)
8c17509e
RS
892 "Discriminate file extensions.
893Build up a new file list based possibly on part of the directory name
894and the name of the file passed in."
2be55c9c 895 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg)
98d775e5 896 (let ((path (if (match-beginning 1)
2be55c9c 897 (substring arg (match-beginning 1) (match-end 1)) nil))
98d775e5 898 (dire (if (match-beginning 2)
2be55c9c 899 (substring arg (match-beginning 2) (match-end 2)) nil))
98d775e5 900 (file (if (match-beginning 3)
2be55c9c 901 (substring arg (match-beginning 3) (match-end 3)) nil))
98d775e5 902 (extn (if (match-beginning 4)
2be55c9c
RS
903 (substring arg (match-beginning 4) (match-end 4)) nil))
904 return-list)
905 (cond
906 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h}
98d775e5 907 ((and (string= extn "cc")
2be55c9c
RS
908 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file))
909 (let ((stub (substring file (match-beginning 2) (match-end 2))))
910 (setq dire (upcase (substring file (match-beginning 1) (match-end 1))))
911 (setq return-list (list (concat stub ".hh")
912 (concat stub ".h")
913 (concat file ".hh")
914 (concat file ".h")))
915 ))
916 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C}
8c17509e 917 ((and (string= extn "hh") (ff-upcase-p dire) file)
2be55c9c 918 (let ((stub (concat (downcase dire) file)))
98d775e5 919 (setq return-list (list (concat stub ".cc")
2be55c9c
RS
920 (concat stub ".C")
921 (concat file ".cc")
922 (concat file ".C")))
923 ))
924 ;; zap.cc => zap.hh or zap.h
925 ((string= extn "cc")
926 (let ((stub file))
927 (setq return-list (list (concat stub ".hh")
928 (concat stub ".h")))
929 ))
930 ;; zap.hh => zap.cc or zap.C
931 ((string= extn "hh")
932 (let ((stub file))
933 (setq return-list (list (concat stub ".cc")
934 (concat stub ".C")))
935 ))
98d775e5 936 (t
2be55c9c
RS
937 nil))
938
939 return-list))
940
941;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
942;; This section offers an example of user defined function to place point.
943;; The regexps are Ada specific.
944
945(defvar ff-function-name nil "Name of the function we are in.")
946
6cc6a2e9 947;(eval-when-compile (require 'ada-mode))
2be55c9c 948
03741cc5 949;; bind with (setq ff-pre-load-hook 'ff-which-function-are-we-in)
2be55c9c
RS
950;;
951(defun ff-which-function-are-we-in ()
8c17509e
RS
952 "Return the name of the function whose definition/declaration point is in.
953Also remember that name in `ff-function-name'."
2be55c9c
RS
954
955 (setq ff-function-name nil)
956
957 (save-excursion
958 (if (re-search-backward ada-procedure-start-regexp nil t)
959 (setq ff-function-name (buffer-substring (match-beginning 0)
960 (match-end 0)))
961 ; we didn't find a procedure start, perhaps there is a package
962 (if (re-search-backward ada-package-start-regexp nil t)
963 (setq ff-function-name (buffer-substring (match-beginning 0)
964 (match-end 0)))
965 ))))
966
03741cc5 967;; bind with (setq ff-post-load-hook 'ff-set-point-accordingly)
2be55c9c
RS
968;;
969(defun ff-set-point-accordingly ()
8c17509e 970 "Find the function specified in `ff-function-name'.
2ab6bb14 971That name was previously determined by `ff-which-function-are-we-in'."
2be55c9c
RS
972 (if ff-function-name
973 (progn
974 (goto-char (point-min))
975 (search-forward ff-function-name nil t))))
976
98d775e5 977(provide 'find-file)
2be55c9c 978
98d775e5 979;;; find-file.el ends here