*** empty log message ***
[emacs.git] / lisp / find-file.el
blobebed0b730442cba04ba90889f39dc398ef773f69
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 ;;; Change Log:
115 ;; FEEDBACK:
116 ;; Please send me bug reports, bug fixes, and extensions, so that I can
117 ;; merge them into the master source.
119 ;; CREDITS:
120 ;; Many thanks go to TUSC Computer Systems Pty Ltd for providing an environ-
121 ;; ment that made the development of this package possible.
123 ;; Many thanks also go to all those who provided valuable feedback throughout
124 ;; the development of this package:
125 ;; Rolf Ebert in particular, Fritz Knabe, Heddy Boubaker, Sebastian Kremer,
126 ;; Vasco Lopes Paulo, Mark A. Plaksin, Robert Lang, Trevor West, Kevin
127 ;; Pereira, Benedict Lofstedt & Justin Vallon.
129 ;;; Code:
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
131 ;; User definable variables:
133 (defgroup ff nil
134 "Find a file corresponding to this one given a pattern."
135 :prefix "ff-"
136 :group 'find-file)
138 (defcustom ff-pre-find-hooks nil
139 "*List of functions to be called before the search for the file starts."
140 :type 'hook
141 :group 'ff)
143 (defcustom ff-pre-load-hooks nil
144 "*List of functions to be called before the other file is loaded."
145 :type 'hook
146 :group 'ff)
148 (defcustom ff-post-load-hooks nil
149 "*List of functions to be called after the other file is loaded."
150 :type 'hook
151 :group 'ff)
153 (defcustom ff-not-found-hooks nil
154 "*List of functions to be called if the other file could not be found."
155 :type 'hook
156 :group 'ff)
158 (defcustom ff-file-created-hooks nil
159 "*List of functions to be called if the other file needs to be created."
160 :type 'hook
161 :group 'ff)
163 (defcustom ff-case-fold-search nil
164 "*Non-nil means ignore cases in matches (see `case-fold-search').
165 If you have extensions in different cases, you will want this to be nil."
166 :type 'boolean
167 :group 'ff)
169 (defcustom ff-always-in-other-window nil
170 "*If non-nil, find the corresponding file in another window by default.
171 To override this, give an argument to `ff-find-other-file'."
172 :type 'boolean
173 :group 'ff)
175 (defcustom ff-ignore-include nil
176 "*If non-nil, ignore `#include' lines."
177 :type 'boolean
178 :group 'ff)
180 (defcustom ff-always-try-to-create t
181 "*If non-nil, always attempt to create the other file if it was not found."
182 :type 'boolean
183 :group 'ff)
185 (defcustom ff-quiet-mode nil
186 "*If non-nil, trace which directories are being searched."
187 :type 'boolean
188 :group 'ff)
190 (defvar ff-special-constructs
192 ;; C/C++ include, for NeXTSTEP too
193 ("^\#\\s *\\(include\\|import\\)\\s +[<\"]\\(.*\\)[>\"]" .
194 (lambda ()
195 (setq fname (buffer-substring (match-beginning 2) (match-end 2)))))
197 ;; Ada import
198 ("^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)" .
199 (lambda ()
200 (setq fname (buffer-substring (match-beginning 1) (match-end 1)))
201 (require 'ada-mode)
202 (setq fname (concat (ada-make-filename-from-adaname fname)
203 ada-spec-suffix))))
205 "*A list of regular expressions specifying how to recognise special
206 constructs such as include files etc, and an associated method for
207 extracting the filename from that construct.")
209 (defcustom ff-other-file-alist 'cc-other-file-alist
210 "*Alist of extensions to find given the current file's extension.
212 This list should contain the most used extensions before the others,
213 since the search algorithm searches sequentially through each
214 directory specified in `ff-search-directories'. If a file is not found,
215 a new one is created with the first matching extension (`.cc' yields `.hh').
216 This alist should be set by the major mode."
217 :type '(choice (repeat (list regexp (choice (repeat string) function)))
218 symbol)
219 :group 'ff)
221 (defcustom ff-search-directories 'cc-search-directories
222 "*List of directories to search for a specific file.
224 Set by default to `cc-search-directories', expanded at run-time.
226 This list is searched through with each extension specified in
227 `ff-other-file-alist' that matches this file's extension. So the
228 longer the list, the longer it'll take to realise that a file
229 may not exist.
231 A typical format is
233 '(\".\" \"/usr/include\" \"$PROJECT/*/include\")
235 Environment variables can be inserted between slashes (`/').
236 They will be replaced by their definition. If a variable does
237 not exist, it is replaced (silently) with an empty string.
239 The stars are *not* wildcards: they are searched for together with
240 the preceding slash. The star represents all the subdirectories except
241 `..', and each of these subdirectories will be searched in turn."
242 :type '(choice (repeat directory) symbol)
243 :group 'ff)
245 (defcustom cc-search-directories
246 '("." "/usr/include" "/usr/local/include/*")
247 "*See the description of the `ff-search-directories' variable."
248 :type '(repeat directory)
249 :group 'ff)
251 (defcustom cc-other-file-alist
253 ("\\.cc$" (".hh" ".h"))
254 ("\\.hh$" (".cc" ".C"))
256 ("\\.c$" (".h"))
257 ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp"))
259 ("\\.C$" (".H" ".hh" ".h"))
260 ("\\.H$" (".C" ".CC"))
262 ("\\.CC$" (".HH" ".H" ".hh" ".h"))
263 ("\\.HH$" (".CC"))
265 ("\\.cxx$" (".hh" ".h"))
266 ("\\.cpp$" (".hh" ".h"))
268 "*Alist of extensions to find given the current file's extension.
270 This list should contain the most used extensions before the others,
271 since the search algorithm searches sequentially through each directory
272 specified in `ff-search-directories'. If a file is not found, a new one
273 is created with the first matching extension (`.cc' yields `.hh')."
274 :type '(repeat (list regexp (choice (repeat string) function)))
275 :group 'ff)
277 (defcustom modula2-other-file-alist
279 ("\\.mi$" (".md")) ;; Modula-2 module definition
280 ("\\.md$" (".mi")) ;; and implementation.
282 "*See the description for the `ff-search-directories' variable."
283 :type '(repeat (list regexp (choice (repeat string) function)))
284 :group 'ff)
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288 ;; No user definable variables beyond this point!
289 ;; ==============================================
291 (make-variable-buffer-local 'ff-pre-find-hooks)
292 (make-variable-buffer-local 'ff-pre-load-hooks)
293 (make-variable-buffer-local 'ff-post-load-hooks)
294 (make-variable-buffer-local 'ff-not-found-hooks)
295 (make-variable-buffer-local 'ff-file-created-hooks)
296 (make-variable-buffer-local 'ff-case-fold-search)
297 (make-variable-buffer-local 'ff-always-in-other-window)
298 (make-variable-buffer-local 'ff-ignore-include)
299 (make-variable-buffer-local 'ff-quiet-mode)
300 (make-variable-buffer-local 'ff-other-file-alist)
301 (make-variable-buffer-local 'ff-search-directories)
303 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
304 ;; User entry points
306 ;;;###autoload
307 (defun ff-get-other-file (&optional in-other-window)
308 "Find the header or source file corresponding to this file.
309 See also the documentation for `ff-find-other-file;.
311 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
312 (interactive "P")
313 (let ((ignore ff-ignore-include))
314 (setq ff-ignore-include t)
315 (ff-find-the-other-file in-other-window)
316 (setq ff-ignore-include ignore)))
318 ;;;###autoload
319 (defun ff-find-other-file (&optional in-other-window ignore-include)
320 "Find the header or source file corresponding to this file.
321 Being on a `#include' line pulls in that file.
323 If optional IN-OTHER-WINDOW is non-nil, find the file in the other window.
324 If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines.
326 Variables of interest include:
328 - ff-case-fold-search
329 Non-nil means ignore cases in matches (see case-fold-search).
330 If you have extensions in different cases, you will want this to be nil.
332 - ff-always-in-other-window
333 If non-nil, always open the other file in another window, unless an
334 argument is given to ff-find-other-file.
336 - ff-ignore-include
337 If non-nil, ignores #include lines.
339 - ff-always-try-to-create
340 If non-nil, always attempt to create the other file if it was not found.
342 - ff-quiet-mode
343 If non-nil, traces which directories are being searched.
345 - ff-special-constructs
346 A list of regular expressions specifying how to recognise special
347 constructs such as include files etc, and an associated method for
348 extracting the filename from that construct.
350 - ff-other-file-alist
351 Alist of extensions to find given the current file's extension.
353 - ff-search-directories
354 List of directories searched through with each extension specified in
355 ff-other-file-alist that matches this file's extension.
357 - ff-pre-find-hooks
358 List of functions to be called before the search for the file starts.
360 - ff-pre-load-hooks
361 List of functions to be called before the other file is loaded.
363 - ff-post-load-hooks
364 List of functions to be called after the other file is loaded.
366 - ff-not-found-hooks
367 List of functions to be called if the other file could not be found.
369 - ff-file-created-hooks
370 List of functions to be called if the other file has been created."
371 (interactive "P")
372 (let ((ignore ff-ignore-include))
373 (setq ff-ignore-include ignore-include)
374 (ff-find-the-other-file in-other-window)
375 (setq ff-ignore-include ignore)))
377 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
378 ;; Support functions
380 (defun ff-find-the-other-file (&optional in-other-window)
381 "Find the header or source file corresponding to the current file.
382 Being on a `#include' line pulls in that file, but see the help on
383 the `ff-ignore-include' variable.
385 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
387 (let (match ;; matching regexp for this file
388 suffixes ;; set of replacing regexps for the matching regexp
389 action ;; function to generate the names of the other files
390 fname ;; basename of this file
391 pos ;; where we start matching filenames
392 stub ;; name of the file without extension
393 alist ;; working copy of the list of file extensions
394 pathname ;; the pathname of the file or the #include line
395 default-name ;; file we should create if none found
396 format ;; what we have to match
397 found ;; name of the file or buffer found - nil if none
398 dirs ;; local value of ff-search-directories
399 no-match) ;; whether we know about this kind of file
401 (if ff-pre-find-hooks
402 (run-hooks 'ff-pre-find-hooks))
404 (message "Working...")
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)))
411 (save-excursion
412 (beginning-of-line 1)
413 (setq fname (ff-treat-as-special)))
415 (cond
416 ((and (not ff-ignore-include) fname)
417 (setq default-name fname)
418 (setq found (ff-get-file dirs fname nil in-other-window)))
420 ;; let's just get the corresponding file
422 (setq alist (if (symbolp ff-other-file-alist)
423 (symbol-value ff-other-file-alist)
424 ff-other-file-alist)
425 pathname (if (buffer-file-name)
426 (buffer-file-name)
427 "/none.none"))
429 (string-match ".*/\\(.+\\)$" pathname)
430 (setq fname (substring pathname (match-beginning 1) (match-end 1))
431 no-match nil
432 match (car alist))
434 ;; find the table entry corresponding to this file
435 (setq pos (ff-string-match (car match) fname))
436 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
437 (setq alist (cdr alist))
438 (setq match (car alist))
439 (setq pos (ff-string-match (car match) fname)))
441 ;; no point going on if we haven't found anything
442 (if (not match)
443 (setq no-match t)
445 ;; otherwise, suffixes contains what we need
446 (setq suffixes (car (cdr match))
447 action (car (cdr match))
448 found nil)
450 ;; if we have a function to generate new names,
451 ;; invoke it with the name of the current file
452 (if (and (atom action) (fboundp action))
453 (progn
454 (setq suffixes (funcall action (buffer-file-name))
455 match (cons (car match) (list suffixes))
456 stub nil
457 default-name (car suffixes)))
459 ;; otherwise build our filename stub
460 (cond
462 ;; get around the problem that 0 and nil both mean false!
463 ((= pos 0)
464 (setq format "")
465 (setq stub "")
469 (setq format (concat "\\(.+\\)" (car match)))
470 (string-match format fname)
471 (setq stub (substring fname (match-beginning 1) (match-end 1)))
474 ;; if we find nothing, we should try to get a file like this one
475 (setq default-name
476 (concat stub (car (car (cdr match))))))
478 ;; do the real work - find the file
479 (setq found
480 (ff-get-file dirs
481 stub
482 suffixes
483 in-other-window)))))
485 (cond
486 (no-match ;; could not even determine the other file
487 (message ""))
490 (cond
492 ((not found) ;; could not find the other file
494 (if ff-not-found-hooks ;; run the hooks
495 (run-hooks 'ff-not-found-hooks))
497 (cond
498 (ff-always-try-to-create ;; try to create the file
499 (let (name pathname)
501 (setq name
502 (expand-file-name
503 (read-file-name
504 (format "Find or create %s in: " default-name)
505 default-directory default-name nil)))
507 (setq pathname
508 (if (file-directory-p name)
509 (concat (file-name-as-directory name) default-name)
510 (setq found name)))
512 (ff-find-file pathname in-other-window t)))
514 (t ;; don't create the file, just whinge
515 (message "No file found for %s" fname))))
517 (t ;; matching file found
518 nil))))
520 found)) ;; return buffer-name or filename
522 (defun ff-get-file (search-dirs fname-stub &optional suffix-list other-window)
523 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
524 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
525 with each of the given suffixes. Gets the file or the buffer corresponding
526 to the name of the first file found, or nil.
528 Arguments: (search-dirs fname-stub &optional suffix-list in-other-window)
530 (let ((filename (ff-get-file-name search-dirs fname-stub suffix-list)))
532 (cond
533 ((not filename)
534 nil)
536 ((bufferp (get-file-buffer filename))
537 (ff-switch-to-buffer (get-file-buffer filename) other-window)
538 filename)
540 ((file-exists-p filename)
541 (ff-find-file filename other-window nil)
542 filename)
545 nil))))
547 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
548 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
549 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
550 with each of the given suffixes. Returns the name of the first file found.
552 Arguments: (search-dirs fname-stub &optional suffix-list)
554 (let* (dirs ;; working copy of dirs to search
555 dir ;; the current dir considered
556 file ;; filename being looked for
557 rest ;; pathname after first /*
558 this-suffix ;; the suffix we are currently considering
559 suffixes ;; working copy of suffix-list
560 filename ;; built filename
561 blist ;; list of live buffers
562 buf ;; current buffer in blist
563 found) ;; whether we have found anything
565 (setq suffixes suffix-list)
567 ;; suffixes is nil => fname-stub is the file we are looking for
568 ;; otherwise fname-stub is a stub, and we append a suffix
569 (if suffixes
570 (setq this-suffix (car suffixes))
571 (setq this-suffix "")
572 (setq suffixes (list "")))
574 ;; find whether the file is in a buffer first
575 (while (and suffixes (not found))
576 (setq filename (concat fname-stub this-suffix))
578 (if (not ff-quiet-mode)
579 (message "Finding buffer %s..." filename))
581 (if (bufferp (get-file-buffer filename))
582 (setq found (buffer-file-name (get-file-buffer filename))))
584 (setq blist (buffer-list))
585 (setq buf (buffer-name (car blist)))
586 (while (and blist (not found))
588 (if (string-match (concat filename "<[0-9]+>") buf)
589 (setq found (buffer-file-name (car blist))))
591 (setq blist (cdr blist))
592 (setq buf (buffer-name (car blist))))
594 (setq suffixes (cdr suffixes))
595 (setq this-suffix (car suffixes)))
597 ;; now look for the real file
598 (setq dirs search-dirs)
599 (setq dir (car dirs))
600 (while (and (not found) dirs)
602 (setq suffixes suffix-list)
604 ;; if dir does not contain '/*', look for the file
605 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir)))
606 (progn
608 ;; suffixes is nil => fname-stub is the file we are looking for
609 ;; otherwise fname-stub is a stub, and we append a suffix
610 (if suffixes
611 (setq this-suffix (car suffixes))
612 (setq this-suffix "")
613 (setq suffixes (list "")))
615 (while (and suffixes (not found))
617 (setq filename (concat fname-stub this-suffix))
618 (setq file (concat dir "/" filename))
620 (if (not ff-quiet-mode)
621 (message "Finding %s..." file))
623 (if (file-exists-p file)
624 (setq found file))
626 (setq suffixes (cdr suffixes))
627 (setq this-suffix (car suffixes))))
629 ;; otherwise dir matches the '/*', so search each dir separately
630 (progn
631 (if (match-beginning 2)
632 (setq rest (substring dir (match-beginning 2) (match-end 2)))
633 (setq rest "")
635 (setq dir (substring dir (match-beginning 1) (match-end 1)))
637 (let ((dirlist (ff-all-dirs-under dir '("..")))
638 this-dir compl-dirs)
640 (setq this-dir (car dirlist))
641 (while dirlist
642 (setq compl-dirs
643 (append
644 compl-dirs
645 (list (concat this-dir rest))
647 (setq dirlist (cdr dirlist))
648 (setq this-dir (car dirlist)))
650 (if compl-dirs
651 (setq found (ff-get-file-name compl-dirs
652 fname-stub
653 suffix-list))))))
654 (setq dirs (cdr dirs))
655 (setq dir (car dirs)))
657 (if found
658 (message "%s found" found))
660 found))
662 (defun ff-string-match (regexp string &optional start)
663 "Like `string-match', but set `case-fold-search' temporarily.
664 The value used comes from `ff-case-fold-search'."
665 (let ((case-fold-search ff-case-fold-search))
666 (if regexp
667 (string-match regexp string start))))
669 (defun ff-list-replace-env-vars (search-list)
670 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST."
671 (let (list
672 (var (car search-list)))
673 (while search-list
674 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var)
675 (setq var
676 (concat
677 (substring var (match-beginning 1) (match-end 1))
678 (getenv (substring var (match-beginning 2) (match-end 2)))
679 (substring var (match-beginning 3) (match-end 3)))))
680 (setq search-list (cdr search-list))
681 (setq list (cons var list))
682 (setq var (car search-list)))
683 (setq search-list (reverse list))))
685 (defun ff-treat-as-special ()
686 "Returns the file to look for if the construct was special, else nil.
687 The construct is defined in the variable `ff-special-constructs'."
688 (let* (fname
689 (list ff-special-constructs)
690 (elem (car list))
691 (regexp (car elem))
692 (match (cdr elem)))
693 (while (and list (not fname))
694 (if (and (looking-at regexp) match)
695 (setq fname (funcall match)))
696 (setq list (cdr list))
697 (setq elem (car list))
698 (setq regexp (car elem))
699 (setq match (cdr elem)))
700 fname))
702 (defun ff-basename (string)
703 "Return the basename of PATHNAME."
704 (setq string (concat "/" string))
705 (string-match ".*/\\([^/]+\\)$" string)
706 (setq string (substring string (match-beginning 1) (match-end 1))))
708 (defun ff-all-dirs-under (here &optional exclude)
709 "Get all the directory files under directory HERE.
710 Exclude all files in the optional EXCLUDE list."
711 (if (file-directory-p here)
712 (condition-case nil
713 (progn
714 (let ((files (directory-files here t))
715 (dirlist (list))
716 file)
717 (while files
718 (setq file (car files))
719 (if (and
720 (file-directory-p file)
721 (not (member (ff-basename file) exclude)))
722 (setq dirlist (cons file dirlist)))
723 (setq files (cdr files)))
724 (setq dirlist (reverse dirlist))))
725 (error nil))
726 nil))
728 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file)
729 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW.
730 In addition, this runs various hooks.
732 Either F1 or F2 receives FILE as the sole argument.
733 The decision of which one to call is based on IN-OTHER-WINDOW
734 and on the global variable `ff-always-in-other-window'.
736 F1 and F2 are typically `find-file' / `find-file-other-window'
737 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs.
739 If optional NEW-FILE is t, then a special hook (`ff-file-created-hooks') is
740 called before `ff-post-load-hooks'."
741 (if ff-pre-load-hooks
742 (run-hooks 'ff-pre-load-hooks))
743 (if (or
744 (and in-other-window (not ff-always-in-other-window))
745 (and (not in-other-window) ff-always-in-other-window))
746 (funcall f2 file)
747 (funcall f1 file))
748 (if new-file
749 (if ff-file-created-hooks
750 (run-hooks 'ff-file-created-hooks)))
751 (if ff-post-load-hooks
752 (run-hooks 'ff-post-load-hooks)))
754 (defun ff-find-file (file &optional in-other-window new-file)
755 "Like `find-file', but may show the file in another window."
756 (ff-switch-file 'find-file
757 'find-file-other-window
758 file in-other-window new-file))
760 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window)
761 "Like `switch-to-buffer', but may show the buffer in another window."
763 (ff-switch-file 'switch-to-buffer
764 'switch-to-buffer-other-window
765 buffer-or-name in-other-window nil))
767 (defun ff-goto-click (event)
768 (set-buffer (window-buffer (posn-window (event-end event))))
769 (goto-char (posn-point (event-end event))))
771 ;;;###autoload
772 (defun ff-mouse-find-other-file (event)
773 "Visit the file you click on."
774 (interactive "e")
775 (save-excursion
776 (ff-goto-click event)
777 (ff-find-other-file nil)))
779 ;;;###autoload
780 (defun ff-mouse-find-other-file-other-window (event)
781 "Visit the file you click on."
782 (interactive "e")
783 (save-excursion
784 (ff-goto-click event)
785 (ff-find-other-file t)))
787 (provide 'find-file)
789 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
790 ;; This section offers an example of user defined function to select files
792 (defun ff-upcase-p (string &optional start end)
793 "Return t if this string is all uppercase.
794 Given START and/or END, checks between these characters."
795 (let (match str)
796 (if (not start)
797 (setq start 0))
798 (if (not end)
799 (setq end (length string)))
800 (if (= start end)
801 (setq end (1+ end)))
802 (setq str (substring string start end))
803 (if (and
804 (ff-string-match "[A-Z]+" str)
805 (setq match (match-data))
806 (= (car match) 0)
807 (= (car (cdr match)) (length str)))
809 nil)))
811 (defun ff-cc-hh-converter (arg)
812 "Discriminate file extensions.
813 Build up a new file list based possibly on part of the directory name
814 and the name of the file passed in."
815 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg)
816 (let ((path (if (match-beginning 1)
817 (substring arg (match-beginning 1) (match-end 1)) nil))
818 (dire (if (match-beginning 2)
819 (substring arg (match-beginning 2) (match-end 2)) nil))
820 (file (if (match-beginning 3)
821 (substring arg (match-beginning 3) (match-end 3)) nil))
822 (extn (if (match-beginning 4)
823 (substring arg (match-beginning 4) (match-end 4)) nil))
824 return-list)
825 (cond
826 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h}
827 ((and (string= extn "cc")
828 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file))
829 (let ((stub (substring file (match-beginning 2) (match-end 2))))
830 (setq dire (upcase (substring file (match-beginning 1) (match-end 1))))
831 (setq return-list (list (concat stub ".hh")
832 (concat stub ".h")
833 (concat file ".hh")
834 (concat file ".h")))
836 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C}
837 ((and (string= extn "hh") (ff-upcase-p dire) file)
838 (let ((stub (concat (downcase dire) file)))
839 (setq return-list (list (concat stub ".cc")
840 (concat stub ".C")
841 (concat file ".cc")
842 (concat file ".C")))
844 ;; zap.cc => zap.hh or zap.h
845 ((string= extn "cc")
846 (let ((stub file))
847 (setq return-list (list (concat stub ".hh")
848 (concat stub ".h")))
850 ;; zap.hh => zap.cc or zap.C
851 ((string= extn "hh")
852 (let ((stub file))
853 (setq return-list (list (concat stub ".cc")
854 (concat stub ".C")))
857 nil))
859 return-list))
861 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
862 ;; This section offers an example of user defined function to place point.
863 ;; The regexps are Ada specific.
865 (defvar ff-function-name nil "Name of the function we are in.")
867 (eval-when-compile (require 'ada-mode))
869 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in)
871 (defun ff-which-function-are-we-in ()
872 "Return the name of the function whose definition/declaration point is in.
873 Also remember that name in `ff-function-name'."
875 (setq ff-function-name nil)
877 (save-excursion
878 (if (re-search-backward ada-procedure-start-regexp nil t)
879 (setq ff-function-name (buffer-substring (match-beginning 0)
880 (match-end 0)))
881 ; we didn't find a procedure start, perhaps there is a package
882 (if (re-search-backward ada-package-start-regexp nil t)
883 (setq ff-function-name (buffer-substring (match-beginning 0)
884 (match-end 0)))
885 ))))
887 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly)
889 (defun ff-set-point-accordingly ()
890 "Find the function specified in `ff-function-name'.
891 That name was previously determined by `ff-which-function-are-we-in'."
892 (if ff-function-name
893 (progn
894 (goto-char (point-min))
895 (search-forward ff-function-name nil t))))
897 ;; find-file.el ends here