* lisp/files.el (locate-dominating-file): Doc fix.
[emacs.git] / lisp / find-file.el
blob19fa8283b0613fc79de9c6e98a08d92c45ba8f79
1 ;;; find-file.el --- find a file corresponding to this one given a pattern
3 ;; Author: Henry Guillaume <henri@tibco.com, henry@c032.aone.net.au>
4 ;; Maintainer: FSF
5 ;; Keywords: c, matching, tools
7 ;; Copyright (C) 1994-1995, 2001-2012 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
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 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; PURPOSE:
27 ;; This package features a function called ff-find-other-file, which performs
28 ;; the following function:
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.
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.
38 ;; THE "OTHER FILE", or "corresponding file", generally has the same basename,
39 ;; and just has a different extension as described by the ff-other-file-alist
40 ;; variable:
42 ;; '(("\\.cc$" (".hh" ".h"))
43 ;; ("\\.hh$" (".cc" ".C" ".CC" ".cxx" ".cpp")))
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.
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:
58 ;; '(("\\.cc$" cc--function)
59 ;; ("\\.hh$" hh-function))
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
63 ;; than one function, and one could imagine the following value for cc-other-
64 ;; file-alist:
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"))))
72 ;; ff-cc-hh-converter is included at the end of this file as a reference.
74 ;; SEARCHING is carried out in a set of directories specified by the
75 ;; ff-search-directories variable:
77 ;; ("." "../../src" "../include/*" "/usr/local/*/src/*" "$PROJECT/src")
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.
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.
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),
92 ;; unless the variable ff-always-try-to-create is set to nil.
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
98 ;; do the opposite of what was described above.
100 ;; THERE ARE FIVE AVAILABLE HOOKS, called in this order if non-nil:
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'
108 ;; The *load-hook allow you to place point where you want it in the other
109 ;; file.
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.
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,
118 ;; Vasco Lopes Paulo, Mark A. Plaksin, Robert Lang, Trevor West, Kevin
119 ;; Pereira, Benedict Lofstedt & Justin Vallon.
121 ;;; Code:
122 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
123 ;; User definable variables:
125 (defgroup ff nil
126 "Find a file corresponding to this one given a pattern."
127 :prefix "ff-"
128 :link '(emacs-commentary-link "find-file")
129 :group 'find-file)
131 (defcustom ff-pre-find-hook nil
132 "List of functions to be called before the search for the file starts."
133 :type 'hook
134 :group 'ff)
136 (defcustom ff-pre-load-hook nil
137 "List of functions to be called before the other file is loaded."
138 :type 'hook
139 :group 'ff)
141 (defcustom ff-post-load-hook nil
142 "List of functions to be called after the other file is loaded."
143 :type 'hook
144 :group 'ff)
146 (defcustom ff-not-found-hook nil
147 "List of functions to be called if the other file could not be found."
148 :type 'hook
149 :group 'ff)
151 (defcustom ff-file-created-hook nil
152 "List of functions to be called if the other file needs to be created."
153 :type 'hook
154 :group 'ff)
156 (defcustom ff-case-fold-search nil
157 "Non-nil means ignore cases in matches (see `case-fold-search').
158 If you have extensions in different cases, you will want this to be nil."
159 :type 'boolean
160 :group 'ff)
162 (defcustom ff-always-in-other-window nil
163 "If non-nil, find the corresponding file in another window by default.
164 To override this, give an argument to `ff-find-other-file'."
165 :type 'boolean
166 :group 'ff)
168 (defcustom ff-ignore-include nil
169 "If non-nil, ignore `#include' lines."
170 :type 'boolean
171 :group 'ff)
173 (defcustom ff-always-try-to-create t
174 "If non-nil, always attempt to create the other file if it was not found."
175 :type 'boolean
176 :group 'ff)
178 (defcustom ff-quiet-mode nil
179 "If non-nil, trace which directories are being searched."
180 :type 'boolean
181 :group 'ff)
183 ;;;###autoload
184 (defvar ff-special-constructs
186 ;; C/C++ include, for NeXTstep too
187 (,(purecopy "^\#\\s *\\(include\\|import\\)\\s +[<\"]\\(.*\\)[>\"]") .
188 (lambda ()
189 (buffer-substring (match-beginning 2) (match-end 2))))
191 ;; We include `ff-treat-as-special' documentation here so that autoload
192 ;; can make it available to be read prior to loading this file.
193 "*List of special constructs for `ff-treat-as-special' to recognize.
194 Each element, tried in order, has the form (REGEXP . EXTRACT).
195 If REGEXP matches the current line (from the beginning of the line),
196 `ff-treat-as-special' calls function EXTRACT with no args.
197 If EXTRACT returns nil, keep trying. Otherwise, return the
198 filename that EXTRACT returned.")
200 (defvaralias 'ff-related-file-alist 'ff-other-file-alist)
201 (defcustom ff-other-file-alist 'cc-other-file-alist
202 "Alist of extensions to find given the current file's extension.
204 This list should contain the most used extensions before the others,
205 since the search algorithm searches sequentially through each
206 directory specified in `ff-search-directories'. If a file is not found,
207 a new one is created with the first matching extension (`.cc' yields `.hh').
208 This alist should be set by the major mode."
209 :type '(choice (repeat (list regexp (choice (repeat string) function)))
210 symbol)
211 :group 'ff)
213 (defcustom ff-search-directories 'cc-search-directories
214 "List of directories to search for a specific file.
216 Set by default to `cc-search-directories', expanded at run-time.
218 This list is searched through with each extension specified in
219 `ff-other-file-alist' that matches this file's extension. So the
220 longer the list, the longer it'll take to realize that a file
221 may not exist.
223 A typical format is
225 '(\".\" \"/usr/include\" \"$PROJECT/*/include\")
227 Environment variables can be inserted between slashes (`/').
228 They will be replaced by their definition. If a variable does
229 not exist, it is replaced (silently) with an empty string.
231 The stars are *not* wildcards: they are searched for together with
232 the preceding slash. The star represents all the subdirectories except
233 `..', and each of these subdirectories will be searched in turn."
234 :type '(choice (repeat directory) symbol)
235 :group 'ff)
237 (defcustom cc-search-directories
238 '("." "/usr/include" "/usr/local/include/*")
239 "See the description of the `ff-search-directories' variable."
240 :type '(repeat directory)
241 :group 'ff)
243 (defcustom cc-other-file-alist
244 '(("\\.cc\\'" (".hh" ".h"))
245 ("\\.hh\\'" (".cc" ".C"))
247 ("\\.c\\'" (".h"))
248 ("\\.h\\'" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp"))
250 ("\\.C\\'" (".H" ".hh" ".h"))
251 ("\\.H\\'" (".C" ".CC"))
253 ("\\.CC\\'" (".HH" ".H" ".hh" ".h"))
254 ("\\.HH\\'" (".CC"))
256 ("\\.c\\+\\+\\'" (".h++" ".hh" ".h"))
257 ("\\.h\\+\\+\\'" (".c++"))
259 ("\\.cpp\\'" (".hpp" ".hh" ".h"))
260 ("\\.hpp\\'" (".cpp"))
262 ("\\.cxx\\'" (".hxx" ".hh" ".h"))
263 ("\\.hxx\\'" (".cxx")))
264 "Alist of extensions to find given the current file's extension.
266 This list should contain the most used extensions before the others,
267 since the search algorithm searches sequentially through each directory
268 specified in `ff-search-directories'. If a file is not found, a new one
269 is created with the first matching extension (`.cc' yields `.hh')."
270 :type '(repeat (list regexp (choice (repeat string) function)))
271 :group 'ff)
273 (defcustom modula2-other-file-alist
275 ("\\.mi$" (".md")) ;; Modula-2 module definition
276 ("\\.md$" (".mi")) ;; and implementation.
278 "See the description for the `ff-search-directories' variable."
279 :type '(repeat (list regexp (choice (repeat string) function)))
280 :group 'ff)
283 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 ;; No user definable variables beyond this point!
285 ;; ==============================================
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)
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)
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
300 ;; User entry points
302 ;;;###autoload
303 (defun ff-get-other-file (&optional in-other-window)
304 "Find the header or source file corresponding to this file.
305 See also the documentation for `ff-find-other-file'.
307 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
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)))
314 ;;;###autoload
315 (defalias 'ff-find-related-file 'ff-find-other-file)
317 ;;;###autoload
318 (defun ff-find-other-file (&optional in-other-window ignore-include)
319 "Find the header or source file corresponding to this file.
320 Being on a `#include' line pulls in that file.
322 If optional IN-OTHER-WINDOW is non-nil, find the file in the other window.
323 If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines.
325 Variables of interest include:
327 - `ff-case-fold-search'
328 Non-nil means ignore cases in matches (see `case-fold-search').
329 If you have extensions in different cases, you will want this to be nil.
331 - `ff-always-in-other-window'
332 If non-nil, always open the other file in another window, unless an
333 argument is given to `ff-find-other-file'.
335 - `ff-ignore-include'
336 If non-nil, ignores #include lines.
338 - `ff-always-try-to-create'
339 If non-nil, always attempt to create the other file if it was not found.
341 - `ff-quiet-mode'
342 If non-nil, traces which directories are being searched.
344 - `ff-special-constructs'
345 A list of regular expressions specifying how to recognize special
346 constructs such as include files etc, and an associated method for
347 extracting the filename from that construct.
349 - `ff-other-file-alist'
350 Alist of extensions to find given the current file's extension.
352 - `ff-search-directories'
353 List of directories searched through with each extension specified in
354 `ff-other-file-alist' that matches this file's extension.
356 - `ff-pre-find-hook'
357 List of functions to be called before the search for the file starts.
359 - `ff-pre-load-hook'
360 List of functions to be called before the other file is loaded.
362 - `ff-post-load-hook'
363 List of functions to be called after the other file is loaded.
365 - `ff-not-found-hook'
366 List of functions to be called if the other file could not be found.
368 - `ff-file-created-hook'
369 List of functions to be called if the other file has been created."
370 (interactive "P")
371 (let ((ignore ff-ignore-include))
372 (setq ff-ignore-include ignore-include)
373 (ff-find-the-other-file in-other-window)
374 (setq ff-ignore-include ignore)))
376 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
377 ;; Support functions
379 (defun ff-find-the-other-file (&optional in-other-window)
380 "Find the header or source file corresponding to the current file.
381 Being on a `#include' line pulls in that file, but see the help on
382 the `ff-ignore-include' variable.
384 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
386 (let (match ;; matching regexp for this file
387 suffixes ;; set of replacing regexps for the matching regexp
388 action ;; function to generate the names of the other files
389 fname ;; basename of this file
390 pos ;; where we start matching filenames
391 stub ;; name of the file without extension
392 alist ;; working copy of the list of file extensions
393 pathname ;; the pathname of the file or the #include line
394 default-name ;; file we should create if none found
395 format ;; what we have to match
396 found ;; name of the file or buffer found - nil if none
397 dirs ;; local value of ff-search-directories
398 no-match) ;; whether we know about this kind of file
400 (run-hooks 'ff-pre-find-hook 'ff-pre-find-hooks)
402 (message "Working...")
404 (setq dirs
405 (if (symbolp ff-search-directories)
406 (ff-list-replace-env-vars (symbol-value ff-search-directories))
407 (ff-list-replace-env-vars ff-search-directories)))
409 (setq fname (ff-treat-as-special))
411 (cond
412 ((and (not ff-ignore-include) fname)
413 (setq default-name fname)
414 (setq found (ff-get-file dirs fname nil in-other-window)))
416 ;; let's just get the corresponding file
418 (setq alist (if (symbolp ff-other-file-alist)
419 (symbol-value ff-other-file-alist)
420 ff-other-file-alist)
421 pathname (if (buffer-file-name)
422 (buffer-file-name)
423 "/none.none"))
425 (setq fname (file-name-nondirectory pathname)
426 no-match nil
427 match (car alist))
429 ;; find the table entry corresponding to this file
430 (setq pos (ff-string-match (car match) fname))
431 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
432 (setq alist (cdr alist))
433 (setq match (car alist))
434 (setq pos (ff-string-match (car match) fname)))
436 ;; no point going on if we haven't found anything
437 (if (not match)
438 (setq no-match t)
440 ;; otherwise, suffixes contains what we need
441 (setq suffixes (car (cdr match))
442 action (car (cdr match))
443 found nil)
445 ;; if we have a function to generate new names,
446 ;; invoke it with the name of the current file
447 (if (and (atom action) (fboundp action))
448 (progn
449 (setq suffixes (funcall action (buffer-file-name))
450 match (cons (car match) (list suffixes))
451 stub nil
452 default-name (car suffixes)))
454 ;; otherwise build our filename stub
455 (cond
457 ;; get around the problem that 0 and nil both mean false!
458 ((= pos 0)
459 (setq format "")
460 (setq stub "")
464 (setq format (concat "\\(.+\\)" (car match)))
465 (string-match format fname)
466 (setq stub (substring fname (match-beginning 1) (match-end 1)))
469 ;; if we find nothing, we should try to get a file like this one
470 (setq default-name
471 (concat stub (car (car (cdr match))))))
473 ;; do the real work - find the file
474 (setq found
475 (ff-get-file dirs
476 stub
477 suffixes
478 in-other-window)))))
480 (cond
481 (no-match ;; could not even determine the other file
482 (message ""))
485 (cond
487 ((not found) ;; could not find the other file
489 (run-hooks 'ff-not-found-hook 'ff-not-found-hooks)
491 (cond
492 (ff-always-try-to-create ;; try to create the file
493 (let (name pathname)
495 (setq name
496 (expand-file-name
497 (read-directory-name
498 (format "Find or create %s in: " default-name)
499 default-directory default-name nil)))
501 (setq pathname
502 (if (file-directory-p name)
503 (concat (file-name-as-directory name) default-name)
504 (setq found name)))
506 (ff-find-file pathname in-other-window t)))
508 (t ;; don't create the file, just whinge
509 (message "No file found for %s" fname))))
511 (t ;; matching file found
512 nil))))
514 found)) ;; return buffer-name or filename
516 (defun ff-other-file-name ()
517 "Return name of the header or source file corresponding to the current file.
518 Being on a `#include' line pulls in that file, but see the help on
519 the `ff-ignore-include' variable."
521 (let (match ;; matching regexp for this file
522 suffixes ;; set of replacing regexps for the matching regexp
523 action ;; function to generate the names of the other files
524 fname ;; basename of this file
525 pos ;; where we start matching filenames
526 stub ;; name of the file without extension
527 alist ;; working copy of the list of file extensions
528 pathname ;; the pathname of the file or the #include line
529 default-name ;; file we should create if none found
530 format ;; what we have to match
531 found ;; name of the file or buffer found - nil if none
532 dirs ;; local value of ff-search-directories
533 no-match) ;; whether we know about this kind of file
535 (message "Working...")
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)))
542 (setq fname (ff-treat-as-special))
544 (cond
545 ((and (not ff-ignore-include) fname)
546 (setq default-name fname)
547 (setq found (ff-get-file-name dirs fname nil)))
549 ;; let's just get the corresponding file
551 (setq alist (if (symbolp ff-other-file-alist)
552 (symbol-value ff-other-file-alist)
553 ff-other-file-alist)
554 pathname (if (buffer-file-name)
555 (buffer-file-name)
556 "/none.none"))
558 (setq fname (file-name-nondirectory pathname)
559 no-match nil
560 match (car alist))
562 ;; find the table entry corresponding to this file
563 (setq pos (ff-string-match (car match) fname))
564 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
565 (setq alist (cdr alist))
566 (setq match (car alist))
567 (setq pos (ff-string-match (car match) fname)))
569 ;; no point going on if we haven't found anything
570 (if (not match)
571 (setq no-match t)
573 ;; otherwise, suffixes contains what we need
574 (setq suffixes (car (cdr match))
575 action (car (cdr match))
576 found nil)
578 ;; if we have a function to generate new names,
579 ;; invoke it with the name of the current file
580 (if (and (atom action) (fboundp action))
581 (progn
582 (setq suffixes (funcall action (buffer-file-name))
583 match (cons (car match) (list suffixes))
584 stub nil
585 default-name (car suffixes)))
587 ;; otherwise build our filename stub
588 (cond
590 ;; get around the problem that 0 and nil both mean false!
591 ((= pos 0)
592 (setq format "")
593 (setq stub "")
597 (setq format (concat "\\(.+\\)" (car match)))
598 (string-match format fname)
599 (setq stub (substring fname (match-beginning 1) (match-end 1)))
602 ;; if we find nothing, we should try to get a file like this one
603 (setq default-name
604 (concat stub (car (car (cdr match))))))
606 ;; do the real work - find the file
607 (setq found
608 (ff-get-file-name dirs stub suffixes)))))
609 found)) ;; return buffer-name or filename
611 (defun ff-get-file (search-dirs filename &optional suffix-list other-window)
612 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
613 If (optional) SUFFIX-LIST is nil, search for FILENAME, otherwise search
614 for FILENAME with each of the given suffixes. Get the file or the buffer
615 corresponding to the name of the first file found, or nil."
616 (let ((filename (ff-get-file-name search-dirs filename suffix-list)))
618 (cond
619 ((not filename)
620 nil)
622 ((bufferp (get-file-buffer filename))
623 (ff-switch-to-buffer (get-file-buffer filename) other-window)
624 filename)
626 ((file-exists-p filename)
627 (ff-find-file filename other-window nil)
628 filename)
631 nil))))
633 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
634 "Find a file in SEARCH-DIRS with the given name (or stub) FNAME-STUB.
635 If (optional) SUFFIX-LIST is nil, search for FNAME-STUB, otherwise
636 search for FNAME-STUB with each of the given suffixes. Return the
637 name of the first file found."
638 (let (dirs ;; working copy of dirs to search
639 dir ;; the current dir considered
640 file ;; filename being looked for
641 rest ;; pathname after first /*
642 this-suffix ;; the suffix we are currently considering
643 suffixes ;; working copy of suffix-list
644 filename ;; built filename
645 blist ;; list of live buffers
646 buf ;; current buffer in blist
647 found) ;; whether we have found anything
649 (setq suffixes suffix-list)
651 ;; suffixes is nil => fname-stub is the file we are looking for
652 ;; otherwise fname-stub is a stub, and we append a suffix
653 (if suffixes
654 (setq this-suffix (car suffixes))
655 (setq this-suffix "")
656 (setq suffixes (list "")))
658 ;; find whether the file is in a buffer first
659 (while (and suffixes (not found))
660 (setq filename (concat fname-stub this-suffix))
662 (if (not ff-quiet-mode)
663 (message "Finding buffer %s..." filename))
665 (if (bufferp (get-file-buffer filename))
666 (setq found (buffer-file-name (get-file-buffer filename))))
668 (setq blist (buffer-list))
669 (setq buf (buffer-name (car blist)))
670 (while (and blist (not found))
672 (if (string-match (concat filename "<[0-9]+>") buf)
673 (setq found (buffer-file-name (car blist))))
675 (setq blist (cdr blist))
676 (setq buf (buffer-name (car blist))))
678 (setq suffixes (cdr suffixes))
679 (setq this-suffix (car suffixes)))
681 ;; now look for the real file
682 (setq dirs search-dirs)
683 (setq dir (car dirs))
684 (while (and (not found) dirs)
686 (setq suffixes suffix-list)
688 ;; if dir does not contain '/*', look for the file
689 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir)))
690 (progn
692 ;; suffixes is nil => fname-stub is the file we are looking for
693 ;; otherwise fname-stub is a stub, and we append a suffix
694 (if suffixes
695 (setq this-suffix (car suffixes))
696 (setq this-suffix "")
697 (setq suffixes (list "")))
699 (while (and suffixes (not found))
701 (setq filename (concat fname-stub this-suffix))
702 (setq file (concat dir "/" filename))
704 (if (not ff-quiet-mode)
705 (message "Finding %s..." file))
707 (if (file-exists-p file)
708 (setq found file))
710 (setq suffixes (cdr suffixes))
711 (setq this-suffix (car suffixes))))
713 ;; otherwise dir matches the '/*', so search each dir separately
714 (progn
715 (if (match-beginning 2)
716 (setq rest (substring dir (match-beginning 2) (match-end 2)))
717 (setq rest "")
719 (setq dir (substring dir (match-beginning 1) (match-end 1)))
721 (let ((dirlist (ff-all-dirs-under dir '("..")))
722 this-dir compl-dirs)
724 (setq this-dir (car dirlist))
725 (while dirlist
726 (setq compl-dirs
727 (append
728 compl-dirs
729 (list (concat this-dir rest))
731 (setq dirlist (cdr dirlist))
732 (setq this-dir (car dirlist)))
734 (if compl-dirs
735 (setq found (ff-get-file-name compl-dirs
736 fname-stub
737 suffix-list))))))
738 (setq dirs (cdr dirs))
739 (setq dir (car dirs)))
741 (if found
742 (message "%s found" found))
744 found))
746 (defun ff-string-match (regexp string &optional start)
747 "Like `string-match', but set `case-fold-search' temporarily.
748 The value used comes from `ff-case-fold-search'."
749 (let ((case-fold-search ff-case-fold-search))
750 (if regexp
751 (string-match regexp string start))))
753 (defun ff-list-replace-env-vars (search-list)
754 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST."
755 (let (list
756 (var (car search-list)))
757 (while search-list
758 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var)
759 (setq var
760 (concat
761 (substring var (match-beginning 1) (match-end 1))
762 (getenv (substring var (match-beginning 2) (match-end 2)))
763 (substring var (match-beginning 3) (match-end 3)))))
764 (setq search-list (cdr search-list))
765 (setq list (cons var list))
766 (setq var (car search-list)))
767 (setq search-list (reverse list))))
769 (defun ff-treat-as-special ()
770 "Return the file to look for if the construct was special, else nil.
771 See variable `ff-special-constructs'."
772 (save-excursion
773 (beginning-of-line 1)
774 (let* (fname
775 (list ff-special-constructs)
776 (elem (car list))
777 (regexp (car elem))
778 (match (cdr elem)))
779 (while (and list (not fname))
780 (if (and (looking-at regexp) match)
781 (setq fname (funcall match)))
782 (setq list (cdr list))
783 (setq elem (car list))
784 (setq regexp (car elem))
785 (setq match (cdr elem)))
786 fname)))
788 (defun ff-basename (string)
789 "Return the basename of pathname STRING."
790 (setq string (concat "/" string))
791 (string-match ".*/\\([^/]+\\)$" string)
792 (setq string (substring string (match-beginning 1) (match-end 1))))
794 (defun ff-all-dirs-under (here &optional exclude)
795 "Get all the directory files under directory HERE.
796 Exclude all files in the optional EXCLUDE list."
797 (if (file-directory-p here)
798 (condition-case nil
799 (progn
800 (let ((files (directory-files here t))
801 (dirlist (list))
802 file)
803 (while files
804 (setq file (car files))
805 (if (and
806 (file-directory-p file)
807 (not (member (ff-basename file) exclude)))
808 (setq dirlist (cons file dirlist)))
809 (setq files (cdr files)))
810 (setq dirlist (reverse dirlist))))
811 (error nil))
812 nil))
814 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file)
815 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW.
816 In addition, this runs various hooks.
818 Either F1 or F2 receives FILE as the sole argument.
819 The decision of which one to call is based on IN-OTHER-WINDOW
820 and on the global variable `ff-always-in-other-window'.
822 F1 and F2 are typically `find-file' / `find-file-other-window'
823 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs.
825 If optional NEW-FILE is t, then a special hook (`ff-file-created-hook') is
826 called before `ff-post-load-hook'."
827 (run-hooks 'ff-pre-load-hook 'ff-pre-load-hooks)
828 (if (or
829 (and in-other-window (not ff-always-in-other-window))
830 (and (not in-other-window) ff-always-in-other-window))
831 (funcall f2 file)
832 (funcall f1 file))
833 (if new-file
834 (run-hooks 'ff-file-created-hook 'ff-file-created-hooks))
835 (run-hooks 'ff-post-load-hook 'ff-post-load-hooks))
837 (defun ff-find-file (file &optional in-other-window new-file)
838 "Like `find-file', but may show the file in another window."
839 (ff-switch-file 'find-file
840 'find-file-other-window
841 file in-other-window new-file))
843 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window)
844 "Like `switch-to-buffer', but may show the buffer in another window."
846 (ff-switch-file 'switch-to-buffer
847 'switch-to-buffer-other-window
848 buffer-or-name in-other-window nil))
850 ;;;###autoload
851 (defun ff-mouse-find-other-file (event)
852 "Visit the file you click on."
853 (interactive "e")
854 (save-excursion
855 (mouse-set-point event)
856 (ff-find-other-file nil)))
858 ;;;###autoload
859 (defun ff-mouse-find-other-file-other-window (event)
860 "Visit the file you click on in another window."
861 (interactive "e")
862 (save-excursion
863 (mouse-set-point event)
864 (ff-find-other-file t)))
866 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
867 ;; This section offers an example of user defined function to select files
869 (defun ff-upcase-p (string &optional start end)
870 "Return t if STRING is all uppercase.
871 Given START and/or END, checks between these characters."
872 (let (match str)
873 (if (not start)
874 (setq start 0))
875 (if (not end)
876 (setq end (length string)))
877 (if (= start end)
878 (setq end (1+ end)))
879 (setq str (substring string start end))
880 (if (and
881 (ff-string-match "[A-Z]+" str)
882 (setq match (match-data))
883 (= (car match) 0)
884 (= (car (cdr match)) (length str)))
886 nil)))
888 (defun ff-cc-hh-converter (arg)
889 "Discriminate file extensions.
890 Build up a new file list based possibly on part of the directory name
891 and the name of the file passed in."
892 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg)
893 (let ((path (if (match-beginning 1)
894 (substring arg (match-beginning 1) (match-end 1)) nil))
895 (dire (if (match-beginning 2)
896 (substring arg (match-beginning 2) (match-end 2)) nil))
897 (file (if (match-beginning 3)
898 (substring arg (match-beginning 3) (match-end 3)) nil))
899 (extn (if (match-beginning 4)
900 (substring arg (match-beginning 4) (match-end 4)) nil))
901 return-list)
902 (cond
903 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h}
904 ((and (string= extn "cc")
905 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file))
906 (let ((stub (substring file (match-beginning 2) (match-end 2))))
907 (setq dire (upcase (substring file (match-beginning 1) (match-end 1))))
908 (setq return-list (list (concat stub ".hh")
909 (concat stub ".h")
910 (concat file ".hh")
911 (concat file ".h")))
913 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C}
914 ((and (string= extn "hh") (ff-upcase-p dire) file)
915 (let ((stub (concat (downcase dire) file)))
916 (setq return-list (list (concat stub ".cc")
917 (concat stub ".C")
918 (concat file ".cc")
919 (concat file ".C")))
921 ;; zap.cc => zap.hh or zap.h
922 ((string= extn "cc")
923 (let ((stub file))
924 (setq return-list (list (concat stub ".hh")
925 (concat stub ".h")))
927 ;; zap.hh => zap.cc or zap.C
928 ((string= extn "hh")
929 (let ((stub file))
930 (setq return-list (list (concat stub ".cc")
931 (concat stub ".C")))
934 nil))
936 return-list))
938 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
939 ;; This section offers an example of user defined function to place point.
940 ;; The regexps are Ada specific.
942 (defvar ff-function-name nil "Name of the function we are in.")
944 ;; bind with (setq ff-pre-load-hook 'ff-which-function-are-we-in)
946 (defvar ada-procedure-start-regexp)
947 (defvar ada-package-start-regexp)
949 (defun ff-which-function-are-we-in ()
950 "Return the name of the function whose definition/declaration point is in.
951 Also remember that name in `ff-function-name'."
952 (setq ff-function-name
953 (save-excursion
954 (if (or (re-search-backward ada-procedure-start-regexp nil t)
955 (re-search-backward ada-package-start-regexp nil t))
956 (match-string 0)))))
958 ;; bind with (setq ff-post-load-hook 'ff-set-point-accordingly)
960 (defun ff-set-point-accordingly ()
961 "Find the function specified in `ff-function-name'.
962 That name was previously determined by `ff-which-function-are-we-in'."
963 (if ff-function-name
964 (progn
965 (goto-char (point-min))
966 (search-forward ff-function-name nil t))))
968 (provide 'find-file)
970 ;;; find-file.el ends here