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