Unbreak macOS build
[emacs.git] / lisp / find-file.el
blobe02bea06fc06a7e61a2e71f2d260e5d93ce8eafe
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: emacs-devel@gnu.org
5 ;; Keywords: c, matching, tools
7 ;; Copyright (C) 1994-1995, 2001-2017 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 (defcustom ff-special-constructs
185 ;; C/C++ include, for NeXTstep too
186 `((,(purecopy "^#\\s *\\(include\\|import\\)\\s +[<\"]\\(.*\\)[>\"]") .
187 (lambda ()
188 (buffer-substring (match-beginning 2) (match-end 2)))))
189 ;; We include `ff-treat-as-special' documentation here so that autoload
190 ;; can make it available to be read prior to loading this file.
191 "List of special constructs recognized by `ff-treat-as-special'.
192 Each element, tried in order, has the form (REGEXP . EXTRACT).
193 If REGEXP matches the current line (from the beginning of the line),
194 `ff-treat-as-special' calls function EXTRACT with no args.
195 If EXTRACT returns nil, keep trying. Otherwise, return the
196 filename that EXTRACT returned."
197 :type '(repeat (cons regexp function))
198 :group 'ff)
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 ("\\.m\\'" (".h"))
249 ("\\.h\\'" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp" ".m"))
251 ("\\.C\\'" (".H" ".hh" ".h"))
252 ("\\.H\\'" (".C" ".CC"))
254 ("\\.CC\\'" (".HH" ".H" ".hh" ".h"))
255 ("\\.HH\\'" (".CC"))
257 ("\\.c\\+\\+\\'" (".h++" ".hh" ".h"))
258 ("\\.h\\+\\+\\'" (".c++"))
260 ("\\.cpp\\'" (".hpp" ".hh" ".h"))
261 ("\\.hpp\\'" (".cpp"))
263 ("\\.cxx\\'" (".hxx" ".hh" ".h"))
264 ("\\.hxx\\'" (".cxx")))
265 "Alist of extensions to find given the current file's extension.
267 This list should contain the most used extensions before the others,
268 since the search algorithm searches sequentially through each directory
269 specified in `ff-search-directories'. If a file is not found, a new one
270 is created with the first matching extension (`.cc' yields `.hh')."
271 :version "24.4" ; add .m
272 :type '(repeat (list regexp (choice (repeat string) function)))
273 :group 'ff)
275 (defcustom modula2-other-file-alist
277 ("\\.mi$" (".md")) ;; Modula-2 module definition
278 ("\\.md$" (".mi")) ;; and implementation.
280 "See the description for the `ff-search-directories' variable."
281 :type '(repeat (list regexp (choice (repeat string) function)))
282 :group 'ff)
285 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
286 ;; No user definable variables beyond this point!
287 ;; ==============================================
289 (make-variable-buffer-local 'ff-pre-find-hook)
290 (make-variable-buffer-local 'ff-pre-load-hook)
291 (make-variable-buffer-local 'ff-post-load-hook)
292 (make-variable-buffer-local 'ff-not-found-hook)
293 (make-variable-buffer-local 'ff-file-created-hook)
294 (make-variable-buffer-local 'ff-case-fold-search)
295 (make-variable-buffer-local 'ff-always-in-other-window)
296 (make-variable-buffer-local 'ff-ignore-include)
297 (make-variable-buffer-local 'ff-quiet-mode)
298 (make-variable-buffer-local 'ff-other-file-alist)
299 (make-variable-buffer-local 'ff-search-directories)
301 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
302 ;; User entry points
304 ;;;###autoload
305 (defun ff-get-other-file (&optional in-other-window)
306 "Find the header or source file corresponding to this file.
307 See also the documentation for `ff-find-other-file'.
309 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
310 (interactive "P")
311 (let ((ignore ff-ignore-include))
312 (setq ff-ignore-include t)
313 (ff-find-the-other-file in-other-window)
314 (setq ff-ignore-include ignore)))
316 ;;;###autoload
317 (defalias 'ff-find-related-file 'ff-find-other-file)
319 ;;;###autoload
320 (defun ff-find-other-file (&optional in-other-window ignore-include)
321 "Find the header or source file corresponding to this file.
322 Being on a `#include' line pulls in that file.
324 If optional IN-OTHER-WINDOW is non-nil, find the file in the other window.
325 If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines.
327 Variables of interest include:
329 - `ff-case-fold-search'
330 Non-nil means ignore cases in matches (see `case-fold-search').
331 If you have extensions in different cases, you will want this to be nil.
333 - `ff-always-in-other-window'
334 If non-nil, always open the other file in another window, unless an
335 argument is given to `ff-find-other-file'.
337 - `ff-ignore-include'
338 If non-nil, ignores #include lines.
340 - `ff-always-try-to-create'
341 If non-nil, always attempt to create the other file if it was not found.
343 - `ff-quiet-mode'
344 If non-nil, traces which directories are being searched.
346 - `ff-special-constructs'
347 A list of regular expressions specifying how to recognize special
348 constructs such as include files etc, and an associated method for
349 extracting the filename from that construct.
351 - `ff-other-file-alist'
352 Alist of extensions to find given the current file's extension.
354 - `ff-search-directories'
355 List of directories searched through with each extension specified in
356 `ff-other-file-alist' that matches this file's extension.
358 - `ff-pre-find-hook'
359 List of functions to be called before the search for the file starts.
361 - `ff-pre-load-hook'
362 List of functions to be called before the other file is loaded.
364 - `ff-post-load-hook'
365 List of functions to be called after the other file is loaded.
367 - `ff-not-found-hook'
368 List of functions to be called if the other file could not be found.
370 - `ff-file-created-hook'
371 List of functions to be called if the other file has been created."
372 (interactive "P")
373 (let ((ignore ff-ignore-include))
374 (setq ff-ignore-include ignore-include)
375 (ff-find-the-other-file in-other-window)
376 (setq ff-ignore-include ignore)))
378 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
379 ;; Support functions
381 (defun ff-buffer-file-name (&optional buf)
382 "Like `buffer-file-name' but works with indirect buffers as well.
383 If BUF is nil, uses the current buffer."
384 (unless buf
385 (setq buf (current-buffer)))
386 (or (buffer-file-name buf)
387 (when (buffer-base-buffer buf)
388 (buffer-file-name (buffer-base-buffer buf)))))
390 (defun ff-find-the-other-file (&optional in-other-window)
391 "Find the header or source file corresponding to the current file.
392 Being on a `#include' line pulls in that file, but see the help on
393 the `ff-ignore-include' variable.
395 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
397 (let (match ;; matching regexp for this file
398 suffixes ;; set of replacing regexps for the matching regexp
399 action ;; function to generate the names of the other files
400 fname ;; basename of this file
401 pos ;; where we start matching filenames
402 stub ;; name of the file without extension
403 alist ;; working copy of the list of file extensions
404 pathname ;; the pathname of the file or the #include line
405 default-name ;; file we should create if none found
406 format ;; what we have to match
407 found ;; name of the file or buffer found - nil if none
408 dirs ;; local value of ff-search-directories
409 no-match) ;; whether we know about this kind of file
411 (run-hooks 'ff-pre-find-hook 'ff-pre-find-hooks)
413 (message "Working...")
415 (setq dirs
416 (if (symbolp ff-search-directories)
417 (ff-list-replace-env-vars (symbol-value ff-search-directories))
418 (ff-list-replace-env-vars ff-search-directories)))
420 (setq fname (ff-treat-as-special))
422 (cond
423 ((and (not ff-ignore-include) fname)
424 (setq default-name fname)
425 (setq found (ff-get-file dirs fname nil in-other-window)))
427 ;; let's just get the corresponding file
429 (setq alist (if (symbolp ff-other-file-alist)
430 (symbol-value ff-other-file-alist)
431 ff-other-file-alist)
432 pathname (or (ff-buffer-file-name) "/none.none"))
434 (setq fname (file-name-nondirectory pathname)
435 no-match nil
436 match (car alist))
438 ;; find the table entry corresponding to this file
439 (setq pos (ff-string-match (car match) fname))
440 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
441 (setq alist (cdr alist))
442 (setq match (car alist))
443 (setq pos (ff-string-match (car match) fname)))
445 ;; no point going on if we haven't found anything
446 (if (not match)
447 (setq no-match t)
449 ;; otherwise, suffixes contains what we need
450 (setq suffixes (car (cdr match))
451 action (car (cdr match))
452 found nil)
454 ;; if we have a function to generate new names,
455 ;; invoke it with the name of the current file
456 (if (and (atom action) (fboundp action))
457 (progn
458 (setq suffixes (funcall action (ff-buffer-file-name))
459 match (cons (car match) (list suffixes))
460 stub nil
461 default-name (car suffixes)))
463 ;; otherwise build our filename stub
464 (cond
466 ;; get around the problem that 0 and nil both mean false!
467 ((= pos 0)
468 (setq format "")
469 (setq stub "")
473 (setq format (concat "\\(.+\\)" (car match)))
474 (string-match format fname)
475 (setq stub (substring fname (match-beginning 1) (match-end 1)))
478 ;; if we find nothing, we should try to get a file like this one
479 (setq default-name
480 (concat stub (car (car (cdr match))))))
482 ;; do the real work - find the file
483 (setq found
484 (ff-get-file dirs
485 stub
486 suffixes
487 in-other-window)))))
489 (cond
490 (no-match ;; could not even determine the other file
491 (message ""))
494 (cond
496 ((not found) ;; could not find the other file
498 (run-hooks 'ff-not-found-hook 'ff-not-found-hooks)
500 (cond
501 (ff-always-try-to-create ;; try to create the file
502 (let (name pathname)
504 (setq name
505 (expand-file-name
506 (read-directory-name
507 (format "Find or create %s in: " default-name)
508 default-directory default-name nil)))
510 (setq pathname
511 (if (file-directory-p name)
512 (concat (file-name-as-directory name) default-name)
513 (setq found name)))
515 (ff-find-file pathname in-other-window t)))
517 (t ;; don't create the file, just whinge
518 (message "No file found for %s" fname))))
520 (t ;; matching file found
521 nil))))
523 found)) ;; return buffer-name or filename
525 (defun ff-other-file-name ()
526 "Return name of the header or source file corresponding to the current file.
527 Being on a `#include' line pulls in that file, but see the help on
528 the `ff-ignore-include' variable."
530 (let (match ;; matching regexp for this file
531 suffixes ;; set of replacing regexps for the matching regexp
532 action ;; function to generate the names of the other files
533 fname ;; basename of this file
534 pos ;; where we start matching filenames
535 stub ;; name of the file without extension
536 alist ;; working copy of the list of file extensions
537 pathname ;; the pathname of the file or the #include line
538 format ;; what we have to match
539 found ;; name of the file or buffer found - nil if none
540 dirs) ;; local value of ff-search-directories
542 (message "Working...")
544 (setq dirs
545 (if (symbolp ff-search-directories)
546 (ff-list-replace-env-vars (symbol-value ff-search-directories))
547 (ff-list-replace-env-vars ff-search-directories)))
549 (setq fname (ff-treat-as-special))
551 (cond
552 ((and (not ff-ignore-include) fname)
553 (setq found (ff-get-file-name dirs fname nil)))
555 ;; let's just get the corresponding file
557 (setq alist (if (symbolp ff-other-file-alist)
558 (symbol-value ff-other-file-alist)
559 ff-other-file-alist)
560 pathname (or (ff-buffer-file-name) "/none.none"))
562 (setq fname (file-name-nondirectory pathname)
563 match (car alist))
565 ;; find the table entry corresponding to this file
566 (setq pos (ff-string-match (car match) fname))
567 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
568 (setq alist (cdr alist))
569 (setq match (car alist))
570 (setq pos (ff-string-match (car match) fname)))
572 ;; no point going on if we haven't found anything
573 (when match
575 ;; otherwise, suffixes contains what we need
576 (setq suffixes (car (cdr match))
577 action (car (cdr match))
578 found nil)
580 ;; if we have a function to generate new names,
581 ;; invoke it with the name of the current file
582 (if (and (atom action) (fboundp action))
583 (progn
584 (setq suffixes (funcall action (ff-buffer-file-name))
585 match (cons (car match) (list suffixes))
586 stub nil))
588 ;; otherwise build our filename stub
589 (cond
591 ;; get around the problem that 0 and nil both mean false!
592 ((= pos 0)
593 (setq format "")
594 (setq stub "")
598 (setq format (concat "\\(.+\\)" (car match)))
599 (string-match format fname)
600 (setq stub (substring fname (match-beginning 1) (match-end 1)))
603 ;; do the real work - find the file
604 (setq found
605 (ff-get-file-name dirs stub suffixes)))))
606 found)) ;; return buffer-name or filename
608 (defun ff-get-file (search-dirs filename &optional suffix-list other-window)
609 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
610 If (optional) SUFFIX-LIST is nil, search for FILENAME, otherwise search
611 for FILENAME with each of the given suffixes. Get the file or the buffer
612 corresponding to the name of the first file found, or nil."
613 (let ((filename (ff-get-file-name search-dirs filename suffix-list)))
615 (cond
616 ((not filename)
617 nil)
619 ((bufferp (get-file-buffer filename))
620 (ff-switch-to-buffer (get-file-buffer filename) other-window)
621 filename)
623 ((file-exists-p filename)
624 (ff-find-file filename other-window nil)
625 filename)
628 nil))))
630 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
631 "Find a file in SEARCH-DIRS with the given name (or stub) FNAME-STUB.
632 If (optional) SUFFIX-LIST is nil, search for FNAME-STUB, otherwise
633 search for FNAME-STUB with each of the given suffixes. Return the
634 name of the first file found."
635 (let (dirs ;; working copy of dirs to search
636 dir ;; the current dir considered
637 file ;; filename being looked for
638 rest ;; pathname after first /*
639 this-suffix ;; the suffix we are currently considering
640 suffixes ;; working copy of suffix-list
641 filename ;; built filename
642 blist ;; list of live buffers
643 buf ;; current buffer in blist
644 found) ;; whether we have found anything
646 (setq suffixes suffix-list)
648 ;; suffixes is nil => fname-stub is the file we are looking for
649 ;; otherwise fname-stub is a stub, and we append a suffix
650 (if suffixes
651 (setq this-suffix (car suffixes))
652 (setq this-suffix "")
653 (setq suffixes (list "")))
655 ;; find whether the file is in a buffer first
656 (while (and suffixes (not found))
657 (setq filename (concat fname-stub this-suffix))
659 (if (not ff-quiet-mode)
660 (message "Finding buffer %s..." filename))
662 (if (bufferp (get-file-buffer filename))
663 (setq found (ff-buffer-file-name (get-file-buffer filename))))
665 (setq blist (buffer-list))
666 (setq buf (buffer-name (car blist)))
667 (while (and blist (not found))
669 (if (string-match-p (concat filename "<[0-9]+>") buf)
670 (setq found (ff-buffer-file-name (car blist))))
672 (setq blist (cdr blist))
673 (setq buf (buffer-name (car blist))))
675 (setq suffixes (cdr suffixes))
676 (setq this-suffix (car suffixes)))
678 ;; now look for the real file
679 (setq dirs search-dirs)
680 (setq dir (car dirs))
681 (while (and (not found) dirs)
683 (setq suffixes suffix-list)
685 ;; if dir does not contain '/*', look for the file
686 (if (and dir (not (string-match "\\([^*]*\\)/\\*\\(/.*\\)*" dir)))
687 (progn
689 ;; suffixes is nil => fname-stub is the file we are looking for
690 ;; otherwise fname-stub is a stub, and we append a suffix
691 (if suffixes
692 (setq this-suffix (car suffixes))
693 (setq this-suffix "")
694 (setq suffixes (list "")))
696 (while (and suffixes (not found))
698 (setq filename (concat fname-stub this-suffix))
699 (setq file (concat dir "/" filename))
701 (if (not ff-quiet-mode)
702 (message "Finding %s..." file))
704 (if (file-exists-p file)
705 (setq found file))
707 (setq suffixes (cdr suffixes))
708 (setq this-suffix (car suffixes))))
710 ;; otherwise dir matches the '/*', so search each dir separately
711 (progn
712 (if (match-beginning 2)
713 (setq rest (substring dir (match-beginning 2) (match-end 2)))
714 (setq rest "")
716 (setq dir (substring dir (match-beginning 1) (match-end 1)))
718 (let ((dirlist (ff-all-dirs-under dir '("..")))
719 this-dir compl-dirs)
721 (setq this-dir (car dirlist))
722 (while dirlist
723 (setq compl-dirs
724 (append
725 compl-dirs
726 (list (concat this-dir rest))
728 (setq dirlist (cdr dirlist))
729 (setq this-dir (car dirlist)))
731 (if compl-dirs
732 (setq found (ff-get-file-name compl-dirs
733 fname-stub
734 suffix-list))))))
735 (setq dirs (cdr dirs))
736 (setq dir (car dirs)))
738 (if found
739 (message "%s found" found))
741 found))
743 (defun ff-string-match (regexp string &optional start)
744 "Like `string-match', but set `case-fold-search' temporarily.
745 The value used comes from `ff-case-fold-search'."
746 (let ((case-fold-search ff-case-fold-search))
747 (if regexp
748 (string-match regexp string start))))
750 (defun ff-list-replace-env-vars (search-list)
751 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST."
752 (let (list
753 (var (car search-list)))
754 (while search-list
755 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var)
756 (setq var
757 (concat
758 (substring var (match-beginning 1) (match-end 1))
759 (getenv (substring var (match-beginning 2) (match-end 2)))
760 (substring var (match-beginning 3) (match-end 3)))))
761 (setq search-list (cdr search-list))
762 (setq list (cons var list))
763 (setq var (car search-list)))
764 (setq search-list (reverse list))))
766 (defun ff-treat-as-special ()
767 "Return the file to look for if the construct was special, else nil.
768 See variable `ff-special-constructs'."
769 (save-excursion
770 (beginning-of-line 1)
771 (let* (fname
772 (list ff-special-constructs)
773 (elem (car list))
774 (regexp (car elem))
775 (match (cdr elem)))
776 (while (and list (not fname))
777 (if (and (looking-at regexp) match)
778 (setq fname (funcall match)))
779 (setq list (cdr list))
780 (setq elem (car list))
781 (setq regexp (car elem))
782 (setq match (cdr elem)))
783 fname)))
785 (defun ff-basename (string)
786 "Return the basename of pathname STRING."
787 (setq string (concat "/" string))
788 (string-match ".*/\\([^/]+\\)$" string)
789 (setq string (substring string (match-beginning 1) (match-end 1))))
791 (defun ff-all-dirs-under (here &optional exclude)
792 "Get all the directory files under directory HERE.
793 Exclude all files in the optional EXCLUDE list."
794 (if (file-directory-p here)
795 (ignore-errors
796 (let ((files (directory-files here t))
797 (dirlist (list))
798 file)
799 (while files
800 (setq file (car files))
801 (if (and
802 (file-directory-p file)
803 (not (member (ff-basename file) exclude)))
804 (setq dirlist (cons file dirlist)))
805 (setq files (cdr files)))
806 (setq dirlist (reverse dirlist))))
807 nil))
809 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file)
810 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW.
811 In addition, this runs various hooks.
813 Either F1 or F2 receives FILE as the sole argument.
814 The decision of which one to call is based on IN-OTHER-WINDOW
815 and on the global variable `ff-always-in-other-window'.
817 F1 and F2 are typically `find-file' / `find-file-other-window'
818 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs.
820 If optional NEW-FILE is t, then a special hook (`ff-file-created-hook') is
821 called before `ff-post-load-hook'."
822 (run-hooks 'ff-pre-load-hook 'ff-pre-load-hooks)
823 (if (or
824 (and in-other-window (not ff-always-in-other-window))
825 (and (not in-other-window) ff-always-in-other-window))
826 (funcall f2 file)
827 (funcall f1 file))
828 (if new-file
829 (run-hooks 'ff-file-created-hook 'ff-file-created-hooks))
830 (run-hooks 'ff-post-load-hook 'ff-post-load-hooks))
832 (defun ff-find-file (file &optional in-other-window new-file)
833 "Like `find-file', but may show the file in another window."
834 (ff-switch-file 'find-file
835 'find-file-other-window
836 file in-other-window new-file))
838 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window)
839 "Like `switch-to-buffer', but may show the buffer in another window."
841 (ff-switch-file 'switch-to-buffer
842 'switch-to-buffer-other-window
843 buffer-or-name in-other-window nil))
845 ;;;###autoload
846 (defun ff-mouse-find-other-file (event)
847 "Visit the file you click on."
848 (interactive "e")
849 (save-excursion
850 (mouse-set-point event)
851 (ff-find-other-file nil)))
853 ;;;###autoload
854 (defun ff-mouse-find-other-file-other-window (event)
855 "Visit the file you click on in another window."
856 (interactive "e")
857 (save-excursion
858 (mouse-set-point event)
859 (ff-find-other-file t)))
861 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
862 ;; This section offers an example of user defined function to select files
864 (defun ff-upcase-p (string &optional start end)
865 "Return t if STRING is all uppercase.
866 Given START and/or END, checks between these characters."
867 (let (match str)
868 (if (not start)
869 (setq start 0))
870 (if (not end)
871 (setq end (length string)))
872 (if (= start end)
873 (setq end (1+ end)))
874 (setq str (substring string start end))
875 (if (and
876 (ff-string-match "[A-Z]+" str)
877 (setq match (match-data))
878 (= (car match) 0)
879 (= (car (cdr match)) (length str)))
881 nil)))
883 (defun ff-cc-hh-converter (arg)
884 "Discriminate file extensions.
885 Build up a new file list based possibly on part of the directory name
886 and the name of the file passed in."
887 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg)
888 (let ((dire (if (match-beginning 2)
889 (substring arg (match-beginning 2) (match-end 2)) nil))
890 (file (if (match-beginning 3)
891 (substring arg (match-beginning 3) (match-end 3)) nil))
892 (extn (if (match-beginning 4)
893 (substring arg (match-beginning 4) (match-end 4)) nil))
894 return-list)
895 (cond
896 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h}
897 ((and (string= extn "cc")
898 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file))
899 (let ((stub (substring file (match-beginning 2) (match-end 2))))
900 (setq dire (upcase (substring file (match-beginning 1) (match-end 1))))
901 (setq return-list (list (concat stub ".hh")
902 (concat stub ".h")
903 (concat file ".hh")
904 (concat file ".h")))
906 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C}
907 ((and (string= extn "hh") (ff-upcase-p dire) file)
908 (let ((stub (concat (downcase dire) file)))
909 (setq return-list (list (concat stub ".cc")
910 (concat stub ".C")
911 (concat file ".cc")
912 (concat file ".C")))
914 ;; zap.cc => zap.hh or zap.h
915 ((string= extn "cc")
916 (let ((stub file))
917 (setq return-list (list (concat stub ".hh")
918 (concat stub ".h")))
920 ;; zap.hh => zap.cc or zap.C
921 ((string= extn "hh")
922 (let ((stub file))
923 (setq return-list (list (concat stub ".cc")
924 (concat stub ".C")))
927 nil))
929 return-list))
931 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
932 ;; This section offers an example of user defined function to place point.
933 ;; The regexps are Ada specific.
935 (defvar ff-function-name nil "Name of the function we are in.")
937 ;; bind with (setq ff-pre-load-hook 'ff-which-function-are-we-in)
939 (defvar ada-procedure-start-regexp)
940 (defvar ada-package-start-regexp)
942 (defun ff-which-function-are-we-in ()
943 "Return the name of the function whose definition/declaration point is in.
944 Also remember that name in `ff-function-name'."
945 (setq ff-function-name
946 (save-excursion
947 (if (or (re-search-backward ada-procedure-start-regexp nil t)
948 (re-search-backward ada-package-start-regexp nil t))
949 (match-string 0)))))
951 ;; bind with (setq ff-post-load-hook 'ff-set-point-accordingly)
953 (defun ff-set-point-accordingly ()
954 "Find the function specified in `ff-function-name'.
955 That name was previously determined by `ff-which-function-are-we-in'."
956 (if ff-function-name
957 (progn
958 (goto-char (point-min))
959 (search-forward ff-function-name nil t))))
961 (provide 'find-file)
963 ;;; find-file.el ends here