Add copyright notice.
[emacs.git] / lisp / find-file.el
blobd59f10d22bd68638111ae94cb5acf68a850e58d4
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 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 2, or (at your option)
14 ;; 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; 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.
26 ;;; Commentary:
28 ;; PURPOSE:
29 ;; This package features a function called ff-find-other-file, which performs
30 ;; the following function:
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.
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.
40 ;; THE "OTHER FILE", or "corresponding file", generally has the same basename,
41 ;; and just has a different extension as described by the ff-other-file-alist
42 ;; variable:
44 ;; '(("\\.cc$" (".hh" ".h"))
45 ;; ("\\.hh$" (".cc" ".C" ".CC" ".cxx" ".cpp")))
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.
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:
60 ;; '(("\\.cc$" cc-function)
61 ;; ("\\.hh$" hh-function))
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
65 ;; than one function, and one could imagine the following value for cc-other-
66 ;; file-alist:
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:
79 ;; ("." "../../src" "../include/*" "/usr/local/*/src/*" "$PROJECT/src")
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.
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.
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),
94 ;; unless the variable ff-always-try-to-create is set to nil.
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
100 ;; do the opposite of what was described above.
102 ;; THERE ARE FIVE AVAILABLE HOOKS, called in this order if non-nil:
104 ;; - ff-pre-find-hooks - called before the search for the other file starts
105 ;; - ff-not-found-hooks - called when the other file could not be found
106 ;; - ff-pre-load-hooks - called just before the other file is 'loaded'
107 ;; - ff-file-created-hooks - called when the other file is created
108 ;; - ff-post-load-hooks - called just after the other file is 'loaded'
110 ;; The *load-hooks allow you to place point where you want it in the other
111 ;; file.
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.
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,
120 ;; Vasco Lopes Paulo, Mark A. Plaksin, Robert Lang, Trevor West, Kevin
121 ;; Pereira, Benedict Lofstedt & Justin Vallon.
123 ;;; Code:
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 ;; User definable variables:
127 (defgroup ff nil
128 "Find a file corresponding to this one given a pattern."
129 :prefix "ff-"
130 :link '(emacs-commentary-link "find-file")
131 :group 'find-file)
133 (defcustom ff-pre-find-hooks nil
134 "*List of functions to be called before the search for the file starts."
135 :type 'hook
136 :group 'ff)
138 (defcustom ff-pre-load-hooks nil
139 "*List of functions to be called before the other file is loaded."
140 :type 'hook
141 :group 'ff)
143 (defcustom ff-post-load-hooks nil
144 "*List of functions to be called after the other file is loaded."
145 :type 'hook
146 :group 'ff)
148 (defcustom ff-not-found-hooks nil
149 "*List of functions to be called if the other file could not be found."
150 :type 'hook
151 :group 'ff)
153 (defcustom ff-file-created-hooks nil
154 "*List of functions to be called if the other file needs to be created."
155 :type 'hook
156 :group 'ff)
158 (defcustom ff-case-fold-search nil
159 "*Non-nil means ignore cases in matches (see `case-fold-search').
160 If you have extensions in different cases, you will want this to be nil."
161 :type 'boolean
162 :group 'ff)
164 (defcustom ff-always-in-other-window nil
165 "*If non-nil, find the corresponding file in another window by default.
166 To override this, give an argument to `ff-find-other-file'."
167 :type 'boolean
168 :group 'ff)
170 (defcustom ff-ignore-include nil
171 "*If non-nil, ignore `#include' lines."
172 :type 'boolean
173 :group 'ff)
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)
180 (defcustom ff-quiet-mode nil
181 "*If non-nil, trace which directories are being searched."
182 :type 'boolean
183 :group 'ff)
185 (defvar ff-special-constructs
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)))))
192 ;; Ada import
193 ("^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)" .
194 (lambda ()
195 (setq fname (buffer-substring (match-beginning 1) (match-end 1)))
196 (require 'ada-mode)
197 (setq fname (concat (ada-make-filename-from-adaname fname)
198 ada-spec-suffix))))
200 "*A list of regular expressions for `ff-find-file'.
201 Specifies how to recognise special constructs such as include files
202 etc. and an associated method for extracting the filename from that
203 construct.")
205 (defcustom ff-other-file-alist 'cc-other-file-alist
206 "*Alist of extensions to find given the current file's extension.
208 This list should contain the most used extensions before the others,
209 since the search algorithm searches sequentially through each
210 directory specified in `ff-search-directories'. If a file is not found,
211 a new one is created with the first matching extension (`.cc' yields `.hh').
212 This alist should be set by the major mode."
213 :type '(choice (repeat (list regexp (choice (repeat string) function)))
214 symbol)
215 :group 'ff)
217 (defcustom ff-search-directories 'cc-search-directories
218 "*List of directories to search for a specific file.
220 Set by default to `cc-search-directories', expanded at run-time.
222 This list is searched through with each extension specified in
223 `ff-other-file-alist' that matches this file's extension. So the
224 longer the list, the longer it'll take to realise that a file
225 may not exist.
227 A typical format is
229 '(\".\" \"/usr/include\" \"$PROJECT/*/include\")
231 Environment variables can be inserted between slashes (`/').
232 They will be replaced by their definition. If a variable does
233 not exist, it is replaced (silently) with an empty string.
235 The stars are *not* wildcards: they are searched for together with
236 the preceding slash. The star represents all the subdirectories except
237 `..', and each of these subdirectories will be searched in turn."
238 :type '(choice (repeat directory) symbol)
239 :group 'ff)
241 (defcustom cc-search-directories
242 '("." "/usr/include" "/usr/local/include/*")
243 "*See the description of the `ff-search-directories' variable."
244 :type '(repeat directory)
245 :group 'ff)
247 (defcustom cc-other-file-alist
249 ("\\.cc$" (".hh" ".h"))
250 ("\\.hh$" (".cc" ".C"))
252 ("\\.c$" (".h"))
253 ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp"))
255 ("\\.C$" (".H" ".hh" ".h"))
256 ("\\.H$" (".C" ".CC"))
258 ("\\.CC$" (".HH" ".H" ".hh" ".h"))
259 ("\\.HH$" (".CC"))
261 ("\\.cxx$" (".hh" ".h"))
262 ("\\.cpp$" (".hh" ".h"))
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-hooks)
288 (make-variable-buffer-local 'ff-pre-load-hooks)
289 (make-variable-buffer-local 'ff-post-load-hooks)
290 (make-variable-buffer-local 'ff-not-found-hooks)
291 (make-variable-buffer-local 'ff-file-created-hooks)
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 (defun ff-find-other-file (&optional in-other-window ignore-include)
316 "Find the header or source file corresponding to this file.
317 Being on a `#include' line pulls in that file.
319 If optional IN-OTHER-WINDOW is non-nil, find the file in the other window.
320 If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines.
322 Variables of interest include:
324 - `ff-case-fold-search'
325 Non-nil means ignore cases in matches (see `case-fold-search').
326 If you have extensions in different cases, you will want this to be nil.
328 - `ff-always-in-other-window'
329 If non-nil, always open the other file in another window, unless an
330 argument is given to `ff-find-other-file'.
332 - `ff-ignore-include'
333 If non-nil, ignores #include lines.
335 - `ff-always-try-to-create'
336 If non-nil, always attempt to create the other file if it was not found.
338 - `ff-quiet-mode'
339 If non-nil, traces which directories are being searched.
341 - `ff-special-constructs'
342 A list of regular expressions specifying how to recognise special
343 constructs such as include files etc, and an associated method for
344 extracting the filename from that construct.
346 - `ff-other-file-alist'
347 Alist of extensions to find given the current file's extension.
349 - `ff-search-directories'
350 List of directories searched through with each extension specified in
351 `ff-other-file-alist' that matches this file's extension.
353 - `ff-pre-find-hooks'
354 List of functions to be called before the search for the file starts.
356 - `ff-pre-load-hooks'
357 List of functions to be called before the other file is loaded.
359 - `ff-post-load-hooks'
360 List of functions to be called after the other file is loaded.
362 - `ff-not-found-hooks'
363 List of functions to be called if the other file could not be found.
365 - `ff-file-created-hooks'
366 List of functions to be called if the other file has been created."
367 (interactive "P")
368 (let ((ignore ff-ignore-include))
369 (setq ff-ignore-include ignore-include)
370 (ff-find-the-other-file in-other-window)
371 (setq ff-ignore-include ignore)))
373 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
374 ;; Support functions
376 (defun ff-find-the-other-file (&optional in-other-window)
377 "Find the header or source file corresponding to the current file.
378 Being on a `#include' line pulls in that file, but see the help on
379 the `ff-ignore-include' variable.
381 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
383 (let (match ;; matching regexp for this file
384 suffixes ;; set of replacing regexps for the matching regexp
385 action ;; function to generate the names of the other files
386 fname ;; basename of this file
387 pos ;; where we start matching filenames
388 stub ;; name of the file without extension
389 alist ;; working copy of the list of file extensions
390 pathname ;; the pathname of the file or the #include line
391 default-name ;; file we should create if none found
392 format ;; what we have to match
393 found ;; name of the file or buffer found - nil if none
394 dirs ;; local value of ff-search-directories
395 no-match) ;; whether we know about this kind of file
397 (if ff-pre-find-hooks
398 (run-hooks 'ff-pre-find-hooks))
400 (message "Working...")
402 (setq dirs
403 (if (symbolp ff-search-directories)
404 (ff-list-replace-env-vars (symbol-value ff-search-directories))
405 (ff-list-replace-env-vars ff-search-directories)))
407 (save-excursion
408 (beginning-of-line 1)
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 (string-match ".*/\\(.+\\)$" pathname)
426 (setq fname (substring pathname (match-beginning 1) (match-end 1))
427 no-match nil
428 match (car alist))
430 ;; find the table entry corresponding to this file
431 (setq pos (ff-string-match (car match) fname))
432 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
433 (setq alist (cdr alist))
434 (setq match (car alist))
435 (setq pos (ff-string-match (car match) fname)))
437 ;; no point going on if we haven't found anything
438 (if (not match)
439 (setq no-match t)
441 ;; otherwise, suffixes contains what we need
442 (setq suffixes (car (cdr match))
443 action (car (cdr match))
444 found nil)
446 ;; if we have a function to generate new names,
447 ;; invoke it with the name of the current file
448 (if (and (atom action) (fboundp action))
449 (progn
450 (setq suffixes (funcall action (buffer-file-name))
451 match (cons (car match) (list suffixes))
452 stub nil
453 default-name (car suffixes)))
455 ;; otherwise build our filename stub
456 (cond
458 ;; get around the problem that 0 and nil both mean false!
459 ((= pos 0)
460 (setq format "")
461 (setq stub "")
465 (setq format (concat "\\(.+\\)" (car match)))
466 (string-match format fname)
467 (setq stub (substring fname (match-beginning 1) (match-end 1)))
470 ;; if we find nothing, we should try to get a file like this one
471 (setq default-name
472 (concat stub (car (car (cdr match))))))
474 ;; do the real work - find the file
475 (setq found
476 (ff-get-file dirs
477 stub
478 suffixes
479 in-other-window)))))
481 (cond
482 (no-match ;; could not even determine the other file
483 (message ""))
486 (cond
488 ((not found) ;; could not find the other file
490 (if ff-not-found-hooks ;; run the hooks
491 (run-hooks 'ff-not-found-hooks))
493 (cond
494 (ff-always-try-to-create ;; try to create the file
495 (let (name pathname)
497 (setq name
498 (expand-file-name
499 (read-file-name
500 (format "Find or create %s in: " default-name)
501 default-directory default-name nil)))
503 (setq pathname
504 (if (file-directory-p name)
505 (concat (file-name-as-directory name) default-name)
506 (setq found name)))
508 (ff-find-file pathname in-other-window t)))
510 (t ;; don't create the file, just whinge
511 (message "No file found for %s" fname))))
513 (t ;; matching file found
514 nil))))
516 found)) ;; return buffer-name or filename
518 (defun ff-get-file (search-dirs filename &optional suffix-list other-window)
519 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
520 If (optional) SUFFIX-LIST is nil, search for fname, otherwise search
521 for fname with each of the given suffixes. Get the file or the buffer
522 corresponding to the name of the first file found, or nil."
523 (let ((filename (ff-get-file-name search-dirs filename suffix-list)))
525 (cond
526 ((not filename)
527 nil)
529 ((bufferp (get-file-buffer filename))
530 (ff-switch-to-buffer (get-file-buffer filename) other-window)
531 filename)
533 ((file-exists-p filename)
534 (ff-find-file filename other-window nil)
535 filename)
538 nil))))
540 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
541 "Find a file in SEARCH-DIRS with the given name (or stub) FNAME-STUB.
542 If (optional) SUFFIX-LIST is nil, search for FNAME-STUB, otherwise
543 search for FNAME-STUB with each of the given suffixes. Return the
544 name of the first file found."
545 (let* (dirs ;; working copy of dirs to search
546 dir ;; the current dir considered
547 file ;; filename being looked for
548 rest ;; pathname after first /*
549 this-suffix ;; the suffix we are currently considering
550 suffixes ;; working copy of suffix-list
551 filename ;; built filename
552 blist ;; list of live buffers
553 buf ;; current buffer in blist
554 found) ;; whether we have found anything
556 (setq suffixes suffix-list)
558 ;; suffixes is nil => fname-stub is the file we are looking for
559 ;; otherwise fname-stub is a stub, and we append a suffix
560 (if suffixes
561 (setq this-suffix (car suffixes))
562 (setq this-suffix "")
563 (setq suffixes (list "")))
565 ;; find whether the file is in a buffer first
566 (while (and suffixes (not found))
567 (setq filename (concat fname-stub this-suffix))
569 (if (not ff-quiet-mode)
570 (message "Finding buffer %s..." filename))
572 (if (bufferp (get-file-buffer filename))
573 (setq found (buffer-file-name (get-file-buffer filename))))
575 (setq blist (buffer-list))
576 (setq buf (buffer-name (car blist)))
577 (while (and blist (not found))
579 (if (string-match (concat filename "<[0-9]+>") buf)
580 (setq found (buffer-file-name (car blist))))
582 (setq blist (cdr blist))
583 (setq buf (buffer-name (car blist))))
585 (setq suffixes (cdr suffixes))
586 (setq this-suffix (car suffixes)))
588 ;; now look for the real file
589 (setq dirs search-dirs)
590 (setq dir (car dirs))
591 (while (and (not found) dirs)
593 (setq suffixes suffix-list)
595 ;; if dir does not contain '/*', look for the file
596 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir)))
597 (progn
599 ;; suffixes is nil => fname-stub is the file we are looking for
600 ;; otherwise fname-stub is a stub, and we append a suffix
601 (if suffixes
602 (setq this-suffix (car suffixes))
603 (setq this-suffix "")
604 (setq suffixes (list "")))
606 (while (and suffixes (not found))
608 (setq filename (concat fname-stub this-suffix))
609 (setq file (concat dir "/" filename))
611 (if (not ff-quiet-mode)
612 (message "Finding %s..." file))
614 (if (file-exists-p file)
615 (setq found file))
617 (setq suffixes (cdr suffixes))
618 (setq this-suffix (car suffixes))))
620 ;; otherwise dir matches the '/*', so search each dir separately
621 (progn
622 (if (match-beginning 2)
623 (setq rest (substring dir (match-beginning 2) (match-end 2)))
624 (setq rest "")
626 (setq dir (substring dir (match-beginning 1) (match-end 1)))
628 (let ((dirlist (ff-all-dirs-under dir '("..")))
629 this-dir compl-dirs)
631 (setq this-dir (car dirlist))
632 (while dirlist
633 (setq compl-dirs
634 (append
635 compl-dirs
636 (list (concat this-dir rest))
638 (setq dirlist (cdr dirlist))
639 (setq this-dir (car dirlist)))
641 (if compl-dirs
642 (setq found (ff-get-file-name compl-dirs
643 fname-stub
644 suffix-list))))))
645 (setq dirs (cdr dirs))
646 (setq dir (car dirs)))
648 (if found
649 (message "%s found" found))
651 found))
653 (defun ff-string-match (regexp string &optional start)
654 "Like `string-match', but set `case-fold-search' temporarily.
655 The value used comes from `ff-case-fold-search'."
656 (let ((case-fold-search ff-case-fold-search))
657 (if regexp
658 (string-match regexp string start))))
660 (defun ff-list-replace-env-vars (search-list)
661 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST."
662 (let (list
663 (var (car search-list)))
664 (while search-list
665 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var)
666 (setq var
667 (concat
668 (substring var (match-beginning 1) (match-end 1))
669 (getenv (substring var (match-beginning 2) (match-end 2)))
670 (substring var (match-beginning 3) (match-end 3)))))
671 (setq search-list (cdr search-list))
672 (setq list (cons var list))
673 (setq var (car search-list)))
674 (setq search-list (reverse list))))
676 (defun ff-treat-as-special ()
677 "Return the file to look for if the construct was special, else nil.
678 The construct is defined in the variable `ff-special-constructs'."
679 (let* (fname
680 (list ff-special-constructs)
681 (elem (car list))
682 (regexp (car elem))
683 (match (cdr elem)))
684 (while (and list (not fname))
685 (if (and (looking-at regexp) match)
686 (setq fname (funcall match)))
687 (setq list (cdr list))
688 (setq elem (car list))
689 (setq regexp (car elem))
690 (setq match (cdr elem)))
691 fname))
693 (defun ff-basename (string)
694 "Return the basename of pathname STRING."
695 (setq string (concat "/" string))
696 (string-match ".*/\\([^/]+\\)$" string)
697 (setq string (substring string (match-beginning 1) (match-end 1))))
699 (defun ff-all-dirs-under (here &optional exclude)
700 "Get all the directory files under directory HERE.
701 Exclude all files in the optional EXCLUDE list."
702 (if (file-directory-p here)
703 (condition-case nil
704 (progn
705 (let ((files (directory-files here t))
706 (dirlist (list))
707 file)
708 (while files
709 (setq file (car files))
710 (if (and
711 (file-directory-p file)
712 (not (member (ff-basename file) exclude)))
713 (setq dirlist (cons file dirlist)))
714 (setq files (cdr files)))
715 (setq dirlist (reverse dirlist))))
716 (error nil))
717 nil))
719 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file)
720 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW.
721 In addition, this runs various hooks.
723 Either F1 or F2 receives FILE as the sole argument.
724 The decision of which one to call is based on IN-OTHER-WINDOW
725 and on the global variable `ff-always-in-other-window'.
727 F1 and F2 are typically `find-file' / `find-file-other-window'
728 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs.
730 If optional NEW-FILE is t, then a special hook (`ff-file-created-hooks') is
731 called before `ff-post-load-hooks'."
732 (if ff-pre-load-hooks
733 (run-hooks 'ff-pre-load-hooks))
734 (if (or
735 (and in-other-window (not ff-always-in-other-window))
736 (and (not in-other-window) ff-always-in-other-window))
737 (funcall f2 file)
738 (funcall f1 file))
739 (if new-file
740 (if ff-file-created-hooks
741 (run-hooks 'ff-file-created-hooks)))
742 (if ff-post-load-hooks
743 (run-hooks 'ff-post-load-hooks)))
745 (defun ff-find-file (file &optional in-other-window new-file)
746 "Like `find-file', but may show the file in another window."
747 (ff-switch-file 'find-file
748 'find-file-other-window
749 file in-other-window new-file))
751 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window)
752 "Like `switch-to-buffer', but may show the buffer in another window."
754 (ff-switch-file 'switch-to-buffer
755 'switch-to-buffer-other-window
756 buffer-or-name in-other-window nil))
758 ;;;###autoload
759 (defun ff-mouse-find-other-file (event)
760 "Visit the file you click on."
761 (interactive "e")
762 (save-excursion
763 (mouse-set-point event)
764 (ff-find-other-file nil)))
766 ;;;###autoload
767 (defun ff-mouse-find-other-file-other-window (event)
768 "Visit the file you click on in another window."
769 (interactive "e")
770 (save-excursion
771 (mouse-set-point event)
772 (ff-find-other-file t)))
774 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
775 ;; This section offers an example of user defined function to select files
777 (defun ff-upcase-p (string &optional start end)
778 "Return t if STRING is all uppercase.
779 Given START and/or END, checks between these characters."
780 (let (match str)
781 (if (not start)
782 (setq start 0))
783 (if (not end)
784 (setq end (length string)))
785 (if (= start end)
786 (setq end (1+ end)))
787 (setq str (substring string start end))
788 (if (and
789 (ff-string-match "[A-Z]+" str)
790 (setq match (match-data))
791 (= (car match) 0)
792 (= (car (cdr match)) (length str)))
794 nil)))
796 (defun ff-cc-hh-converter (arg)
797 "Discriminate file extensions.
798 Build up a new file list based possibly on part of the directory name
799 and the name of the file passed in."
800 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg)
801 (let ((path (if (match-beginning 1)
802 (substring arg (match-beginning 1) (match-end 1)) nil))
803 (dire (if (match-beginning 2)
804 (substring arg (match-beginning 2) (match-end 2)) nil))
805 (file (if (match-beginning 3)
806 (substring arg (match-beginning 3) (match-end 3)) nil))
807 (extn (if (match-beginning 4)
808 (substring arg (match-beginning 4) (match-end 4)) nil))
809 return-list)
810 (cond
811 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h}
812 ((and (string= extn "cc")
813 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file))
814 (let ((stub (substring file (match-beginning 2) (match-end 2))))
815 (setq dire (upcase (substring file (match-beginning 1) (match-end 1))))
816 (setq return-list (list (concat stub ".hh")
817 (concat stub ".h")
818 (concat file ".hh")
819 (concat file ".h")))
821 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C}
822 ((and (string= extn "hh") (ff-upcase-p dire) file)
823 (let ((stub (concat (downcase dire) file)))
824 (setq return-list (list (concat stub ".cc")
825 (concat stub ".C")
826 (concat file ".cc")
827 (concat file ".C")))
829 ;; zap.cc => zap.hh or zap.h
830 ((string= extn "cc")
831 (let ((stub file))
832 (setq return-list (list (concat stub ".hh")
833 (concat stub ".h")))
835 ;; zap.hh => zap.cc or zap.C
836 ((string= extn "hh")
837 (let ((stub file))
838 (setq return-list (list (concat stub ".cc")
839 (concat stub ".C")))
842 nil))
844 return-list))
846 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
847 ;; This section offers an example of user defined function to place point.
848 ;; The regexps are Ada specific.
850 (defvar ff-function-name nil "Name of the function we are in.")
852 (eval-when-compile (require 'ada-mode))
854 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in)
856 (defun ff-which-function-are-we-in ()
857 "Return the name of the function whose definition/declaration point is in.
858 Also remember that name in `ff-function-name'."
860 (setq ff-function-name nil)
862 (save-excursion
863 (if (re-search-backward ada-procedure-start-regexp nil t)
864 (setq ff-function-name (buffer-substring (match-beginning 0)
865 (match-end 0)))
866 ; we didn't find a procedure start, perhaps there is a package
867 (if (re-search-backward ada-package-start-regexp nil t)
868 (setq ff-function-name (buffer-substring (match-beginning 0)
869 (match-end 0)))
870 ))))
872 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly)
874 (defun ff-set-point-accordingly ()
875 "Find the function specified in `ff-function-name'.
876 That name was previously determined by `ff-which-function-are-we-in'."
877 (if ff-function-name
878 (progn
879 (goto-char (point-min))
880 (search-forward ff-function-name nil t))))
882 (provide 'find-file)
884 ;;; find-file.el ends here