(bookmark-locate): ;;;###autoload this alias.
[emacs.git] / lisp / find-file.el
blob3f0ac1ea0d9064343630039443992082abbe77f1
1 ;;; find-file.el --- find a file corresponding to this one given a pattern
3 ;; Author: Henry Guillaume <henry@qbd.com.au>
4 ;; Keywords: c, matching, tools
6 ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; PURPOSE:
28 ;; This package features a function called ff-find-other-file, which performs
29 ;; the following function:
31 ;; When in a .c file, find the first corresponding .h file in a set
32 ;; of directories and display it, and vice-versa from the .h file.
34 ;; Many people maintain their include file in a directory separate to their
35 ;; src directory, and very often you may be editing a file and have a need to
36 ;; visit the "other file". This package searches through a set of directories
37 ;; to find that file.
39 ;; THE "OTHER FILE", or "corresponding file", generally has the same basename,
40 ;; and just has a different extension as described by the ff-other-file-alist
41 ;; variable:
43 ;; '(("\\.cc$" (".hh" ".h"))
44 ;; ("\\.hh$" (".cc" ".C" ".CC" ".cxx" ".cpp")))
46 ;; If the current file has a .cc extension, ff-find-other-file will attempt
47 ;; to look for a .hh file, and then a .h file in some directory as described
48 ;; below. The mechanism here is to replace the matched part of the original
49 ;; filename with each of the corresponding extensions in turn.
51 ;; Alternatively, there are situations where the filename of the other file
52 ;; cannot be determined easily with regexps. For example, a .c file may
53 ;; have two corresponding .h files, for its public and private parts, or
54 ;; the filename for the .c file contains part of the pathname of the .h
55 ;; file, as between src/fooZap.cc and include/FOO/zap.hh. In that case, the
56 ;; format above can be changed to include a function to be called when the
57 ;; current file matches the regexp:
59 ;; '(("\\.cc$" cc-function)
60 ;; ("\\.hh$" hh-function))
62 ;; These functions must return a list consisting of the possible names of the
63 ;; corresponding file, with or without path. There is no real need for more
64 ;; than one function, and one could imagine the following value for cc-other-
65 ;; file-alist:
67 ;; (setq cc-other-file-alist
68 ;; '(("\\.cc$" ff-cc-hh-converter)
69 ;; ("\\.hh$" ff-cc-hh-converter)
70 ;; ("\\.c$" (".h"))
71 ;; ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp"))))
72 ;;
73 ;; ff-cc-hh-converter is included at the end of this file as a reference.
74 ;;
75 ;; SEARCHING is carried out in a set of directories specified by the
76 ;; ff-search-directories variable:
78 ;; ("." "../../src" "../include/*" "/usr/local/*/src/*" "$PROJECT/src")
80 ;; This means that the corresponding file will be searched for first in
81 ;; the current directory, then in ../../src, then in one of the directories
82 ;; under ../include, and so on. The star is _not_ a general wildcard
83 ;; character: it just indicates that the subdirectories of this directory
84 ;; must each be searched in turn. Environment variables will be expanded in
85 ;; the ff-search-directories variable.
87 ;; If the point is on a #include line, the file to be #included is searched
88 ;; for in the same manner. This can be disabled with the ff-ignore-include
89 ;; variable, or by calling ff-get-other-file instead of ff-find-other-file.
91 ;; If the file was not found, ff-find-other-file will prompt you for where
92 ;; to create the new "corresponding file" (defaults to the current directory),
93 ;; unless the variable ff-always-try-to-create is set to nil.
95 ;; GIVEN AN ARGUMENT (with the ^U prefix), ff-find-other-file will get the
96 ;; other file in another (the other?) window (see find-file-other-window and
97 ;; switch-to-buffer-other-window). This can be set on a more permanent basis
98 ;; by setting ff-always-in-other-window to t in which case the ^U prefix will
99 ;; do the opposite of what was described above.
101 ;; THERE ARE FIVE AVAILABLE HOOKS, called in this order if non-nil:
103 ;; - ff-pre-find-hooks - called before the search for the other file starts
104 ;; - ff-not-found-hooks - called when the other file could not be found
105 ;; - ff-pre-load-hooks - called just before the other file is 'loaded'
106 ;; - ff-file-created-hooks - called when the other file is created
107 ;; - ff-post-load-hooks - called just after the other file is 'loaded'
109 ;; The *load-hooks allow you to place point where you want it in the other
110 ;; file.
112 ;; FEEDBACK:
113 ;; Please send me bug reports, bug fixes, and extensions, so that I can
114 ;; merge them into the master source.
116 ;; CREDITS:
117 ;; Many thanks go to TUSC Computer Systems Pty Ltd for providing an environ-
118 ;; ment that made the development of this package possible.
120 ;; Many thanks also go to all those who provided valuable feedback throughout
121 ;; the development of this package:
122 ;; Rolf Ebert in particular, Fritz Knabe, Heddy Boubaker, Sebastian Kremer,
123 ;; Vasco Lopes Paulo, Mark A. Plaksin, Robert Lang, Trevor West, Kevin
124 ;; Pereira & Benedict Lofstedt.
126 ;; Code:
127 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
128 ;; User definable variables:
130 (defvar ff-pre-find-hooks nil
131 "*List of functions to be called before the search for the file starts.")
133 (defvar ff-pre-load-hooks nil
134 "*List of functions to be called before the other file is loaded.")
136 (defvar ff-post-load-hooks nil
137 "*List of functions to be called after the other file is loaded.")
139 (defvar ff-not-found-hooks nil
140 "*List of functions to be called if the other file could not be found.")
142 (defvar ff-file-created-hooks nil
143 "*List of functions to be called if the other file needs to be created.")
145 (defvar ff-case-fold-search nil
146 "*Non-nil means ignore cases in matches (see `case-fold-search').
147 If you have extensions in different cases, you will want this to be nil.")
149 (defvar ff-always-in-other-window nil
150 "*If non-nil, find the corresponding file in another window by default.
151 To override this, give an argument to `ff-find-other-file'.")
153 (defvar ff-ignore-include nil
154 "*If non-nil, ignore `#include' lines.")
156 (defvar ff-always-try-to-create t
157 "*If non-nil, always attempt to create the other file if it was not found.")
159 (defvar ff-quiet-mode nil
160 "*If non-nil, trace which directories are being searched.")
162 (defvar ff-special-constructs
164 ;; C/C++ include, for NeXTSTEP too
165 ("^\#\\s *\\(include\\|import\\)\\s +[<\"]\\(.*\\)[>\"]" .
166 (lambda ()
167 (setq fname (buffer-substring (match-beginning 2) (match-end 2)))))
169 ;; Ada import
170 ("^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)" .
171 (lambda ()
172 (setq fname (buffer-substring (match-beginning 1) (match-end 1)))
173 (require 'ada-mode)
174 (setq fname (concat (ada-make-filename-from-adaname fname)
175 ada-spec-suffix))))
177 "*A list of regular expressions specifying how to recognise special
178 constructs such as include files etc, and an associated method for
179 extracting the filename from that construct.")
181 (defvar ff-other-file-alist 'cc-other-file-alist
182 "*Alist of extensions to find given the current file's extension.
184 This list should contain the most used extensions before the others,
185 since the search algorithm searches sequentially through each
186 directory specified in `ff-search-directories'. If a file is not found,
187 a new one is created with the first matching extension (`.cc' yields `.hh').
188 This alist should be set by the major mode.")
190 (defvar ff-search-directories 'cc-search-directories
191 "*List of directories to search for a specific file.
193 Set by default to `cc-search-directories', expanded at run-time.
195 This list is searched through with each extension specified in
196 `ff-other-file-alist' that matches this file's extension. So the
197 longer the list, the longer it'll take to realise that a file
198 may not exist.
200 A typical format is
202 '(\".\" \"/usr/include/*\" \"$PROJECT/*/include\")
204 Environment variables can be inserted between slashes (`/').
205 They will be replaced by their definition. If a variable does
206 not exist, it is replaced (silently) with an empty string.
208 The stars are *not* wildcards: they are searched for together with
209 the preceding slash. The star represents all the subdirectories except
210 `..', and each of these subdirectories will be searched in turn.")
212 (defvar cc-search-directories
213 '("." "/usr/include/*" "/usr/local/include/*")
214 "*See the description of the `ff-search-directories' variable.")
216 (defvar cc-other-file-alist
218 ("\\.cc$" (".hh" ".h"))
219 ("\\.hh$" (".cc" ".C"))
221 ("\\.c$" (".h"))
222 ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp"))
224 ("\\.C$" (".H" ".hh" ".h"))
225 ("\\.H$" (".C" ".CC"))
227 ("\\.CC$" (".HH" ".H" ".hh" ".h"))
228 ("\\.HH$" (".CC"))
230 ("\\.cxx$" (".hh" ".h"))
231 ("\\.cpp$" (".hh" ".h"))
233 "*Alist of extensions to find given the current file's extension.
235 This list should contain the most used extensions before the others,
236 since the search algorithm searches sequentially through each directory
237 specified in `ff-search-directories'. If a file is not found, a new one
238 is created with the first matching extension (`.cc' yields `.hh').")
240 (defvar ada-search-directories
241 '("." "/usr/adainclude" "/usr/local/adainclude")
242 "*See the description for the `ff-search-directories' variable.")
244 (defvar ada-other-file-alist
246 ("\\.ads$" (".adb")) ;; Ada specs and bodies
247 ("\\.adb$" (".ads")) ;; GNAT filename conventions
249 "*Alist of extensions to find given the current file's extension.
251 This list should contain the most used extensions before the others,
252 since the search algorithm searches sequentially through each directory
253 specified in `ada-search-directories'. If a file is not found, a new one
254 is created with the first matching extension (`.adb' yields `.ads').
257 (defvar modula2-other-file-alist
259 ("\\.mi$" (".md")) ;; Modula-2 module definition
260 ("\\.md$" (".mi")) ;; and implementation.
262 "*See the description for the `ff-search-directories' variable.")
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
265 ;; No user definable variables beyond this point!
266 ;; ==============================================
268 (make-variable-buffer-local 'ff-pre-find-hooks)
269 (make-variable-buffer-local 'ff-pre-load-hooks)
270 (make-variable-buffer-local 'ff-post-load-hooks)
271 (make-variable-buffer-local 'ff-not-found-hooks)
272 (make-variable-buffer-local 'ff-file-created-hooks)
273 (make-variable-buffer-local 'ff-case-fold-search)
274 (make-variable-buffer-local 'ff-always-in-other-window)
275 (make-variable-buffer-local 'ff-ignore-include)
276 (make-variable-buffer-local 'ff-quiet-mode)
277 (make-variable-buffer-local 'ff-other-file-alist)
278 (make-variable-buffer-local 'ff-search-directories)
280 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
281 ;; User entry points
283 ;;;###autoload
284 (defun ff-get-other-file (&optional in-other-window)
285 "Find the header or source file corresponding to this file.
286 See also the documentation for `ff-find-other-file;.
288 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
289 (interactive "P")
290 (let ((ignore ff-ignore-include))
291 (setq ff-ignore-include t)
292 (ff-find-the-other-file in-other-window)
293 (setq ff-ignore-include ignore)))
295 ;;;###autoload
296 (defun ff-find-other-file (&optional in-other-window ignore-include)
297 "Find the header or source file corresponding to this file.
298 Being on a `#include' line pulls in that file.
300 If optional IN-OTHER-WINDOW is non-nil, find the file in the other window.
301 If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines.
303 Variables of interest include:
305 - ff-case-fold-search
306 Non-nil means ignore cases in matches (see case-fold-search).
307 If you have extensions in different cases, you will want this to be nil.
309 - ff-always-in-other-window
310 If non-nil, always open the other file in another window, unless an
311 argument is given to ff-find-other-file.
313 - ff-ignore-include
314 If non-nil, ignores #include lines.
316 - ff-always-try-to-create
317 If non-nil, always attempt to create the other file if it was not found.
319 - ff-quiet-mode
320 If non-nil, traces which directories are being searched.
322 - ff-special-constructs
323 A list of regular expressions specifying how to recognise special
324 constructs such as include files etc, and an associated method for
325 extracting the filename from that construct.
327 - ff-other-file-alist
328 Alist of extensions to find given the current file's extension.
330 - ff-search-directories
331 List of directories searched through with each extension specified in
332 ff-other-file-alist that matches this file's extension.
334 - ff-pre-find-hooks
335 List of functions to be called before the search for the file starts.
337 - ff-pre-load-hooks
338 List of functions to be called before the other file is loaded.
340 - ff-post-load-hooks
341 List of functions to be called after the other file is loaded.
343 - ff-not-found-hooks
344 List of functions to be called if the other file could not be found.
346 - ff-file-created-hooks
347 List of functions to be called if the other file has been created."
348 (interactive "P")
349 (let ((ignore ff-ignore-include))
350 (setq ff-ignore-include ignore-include)
351 (ff-find-the-other-file in-other-window)
352 (setq ff-ignore-include ignore)))
354 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
355 ;; Support functions
357 (defun ff-emacs-19 ()
358 (string-match "^19\\.[0-9]+\\.[0-9]+$" emacs-version))
360 (defun ff-xemacs ()
361 (or (string-match "Lucid" emacs-version)
362 (string-match "XEmacs" emacs-version)))
364 (defun ff-find-the-other-file (&optional in-other-window)
365 "Find the header or source file corresponding to the current file.
366 Being on a `#include' line pulls in that file, but see the help on
367 the `ff-ignore-include' variable.
369 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
371 (let (match ;; matching regexp for this file
372 suffixes ;; set of replacing regexps for the matching regexp
373 action ;; function to generate the names of the other files
374 fname ;; basename of this file
375 pos ;; where we start matching filenames
376 stub ;; name of the file without extension
377 alist ;; working copy of the list of file extensions
378 pathname ;; the pathname of the file or the #include line
379 default-name ;; file we should create if none found
380 format ;; what we have to match
381 found ;; name of the file or buffer found - nil if none
382 dirs ;; local value of ff-search-directories
383 no-match) ;; whether we know about this kind of file
385 (if ff-pre-find-hooks
386 (run-hooks 'ff-pre-find-hooks))
388 (message "Working...")
390 (setq dirs
391 (if (symbolp ff-search-directories)
392 (ff-list-replace-env-vars (symbol-value ff-search-directories))
393 (ff-list-replace-env-vars ff-search-directories)))
395 (save-excursion
396 (beginning-of-line 1)
397 (setq fname (ff-treat-as-special)))
399 (cond
400 ((and (not ff-ignore-include) fname)
401 (setq default-name fname)
402 (setq found (ff-get-file dirs fname nil in-other-window)))
404 ;; let's just get the corresponding file
406 (setq alist (if (symbolp ff-other-file-alist)
407 (symbol-value ff-other-file-alist)
408 ff-other-file-alist)
409 pathname (if (buffer-file-name)
410 (buffer-file-name)
411 "/none.none"))
413 (string-match ".*/\\(.+\\)$" pathname)
414 (setq fname (substring pathname (match-beginning 1) (match-end 1))
415 no-match nil
416 match (car alist))
418 ;; find the table entry corresponding to this file
419 (setq pos (ff-string-match (car match) fname))
420 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
421 (setq alist (cdr alist))
422 (setq match (car alist))
423 (setq pos (ff-string-match (car match) fname)))
425 ;; no point going on if we haven't found anything
426 (if (not match)
427 (setq no-match t)
429 ;; otherwise, suffixes contains what we need
430 (setq suffixes (car (cdr match))
431 action (car (cdr match))
432 found nil)
434 ;; if we have a function to generate new names,
435 ;; invoke it with the name of the current file
436 (if (and (atom action) (fboundp action))
437 (progn
438 (setq suffixes (funcall action (buffer-file-name))
439 match (cons (car match) (list suffixes))
440 stub nil
441 default-name (car suffixes)))
443 ;; otherwise build our filename stub
444 (cond
446 ;; get around the problem that 0 and nil both mean false!
447 ((= pos 0)
448 (setq format "")
449 (setq stub "")
453 (setq format (concat "\\(.+\\)" (car match)))
454 (string-match format fname)
455 (setq stub (substring fname (match-beginning 1) (match-end 1)))
458 ;; if we find nothing, we should try to get a file like this one
459 (setq default-name
460 (concat stub (car (car (cdr match))))))
462 ;; do the real work - find the file
463 (setq found
464 (ff-get-file dirs
465 stub
466 suffixes
467 in-other-window)))))
469 (cond
470 (no-match ;; could not even determine the other file
471 (message ""))
474 (cond
476 ((not found) ;; could not find the other file
478 (if ff-not-found-hooks ;; run the hooks
479 (run-hooks 'ff-not-found-hooks))
481 (cond
482 (ff-always-try-to-create ;; try to create the file
483 (let (name pathname)
485 (setq name
486 (expand-file-name
487 (read-file-name
488 (format "Find or create %s in: " default-name)
489 default-directory default-name nil)))
491 (setq pathname
492 (if (file-directory-p name)
493 (concat (file-name-as-directory name) default-name)
494 (setq found name)))
496 (ff-find-file pathname in-other-window t)))
498 (t ;; don't create the file, just whinge
499 (message "no file found for %s" fname))))
501 (t ;; matching file found
502 nil))))
504 found)) ;; return buffer-name or filename
506 (defun ff-get-file (search-dirs fname-stub &optional suffix-list other-window)
507 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
508 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
509 with each of the given suffixes. Gets the file or the buffer corresponding
510 to the name of the first file found, or nil.
512 Arguments: (search-dirs fname-stub &optional suffix-list in-other-window)
514 (let ((filename (ff-get-file-name search-dirs fname-stub suffix-list)))
516 (cond
517 ((not filename)
518 nil)
520 ((bufferp (get-file-buffer filename))
521 (ff-switch-to-buffer (get-file-buffer filename) other-window)
522 filename)
524 ((file-exists-p filename)
525 (ff-find-file filename other-window nil)
526 filename)
529 nil))))
531 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
532 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
533 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
534 with each of the given suffixes. Returns the name of the first file found.
536 Arguments: (search-dirs fname-stub &optional suffix-list)
538 (let* (dirs ;; working copy of dirs to search
539 dir ;; the current dir considered
540 file ;; filename being looked for
541 rest ;; pathname after first /*
542 this-suffix ;; the suffix we are currently considering
543 suffixes ;; working copy of suffix-list
544 filename ;; built filename
545 blist ;; list of live buffers
546 buf ;; current buffer in blist
547 found) ;; whether we have found anything
549 (setq suffixes suffix-list)
551 ;; suffixes is nil => fname-stub is the file we are looking for
552 ;; otherwise fname-stub is a stub, and we append a suffix
553 (if suffixes
554 (setq this-suffix (car suffixes))
555 (setq this-suffix "")
556 (setq suffixes (list "")))
558 ;; find whether the file is in a buffer first
559 (while (and suffixes (not found))
560 (setq filename (concat fname-stub this-suffix))
562 (if (not ff-quiet-mode)
563 (message "finding buffer %s..." filename))
565 (if (bufferp (get-buffer filename))
566 (setq found filename))
568 (setq blist (buffer-list))
569 (setq buf (buffer-name (car blist)))
570 (while (and blist (not found))
572 (if (string-match (concat filename "<[0-9]+>") buf)
573 (setq found buf))
575 (setq blist (cdr blist))
576 (setq buf (buffer-name (car blist))))
578 (setq suffixes (cdr suffixes))
579 (setq this-suffix (car suffixes)))
581 ;; now look for the real file
582 (setq dirs search-dirs)
583 (setq dir (car dirs))
584 (while (and (not found) dirs)
586 (setq suffixes suffix-list)
588 ;; if dir does not contain '/*', look for the file
589 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir)))
590 (progn
592 ;; suffixes is nil => fname-stub is the file we are looking for
593 ;; otherwise fname-stub is a stub, and we append a suffix
594 (if suffixes
595 (setq this-suffix (car suffixes))
596 (setq this-suffix "")
597 (setq suffixes (list "")))
599 (while (and suffixes (not found))
601 (setq filename (concat fname-stub this-suffix))
602 (setq file (concat dir "/" filename))
604 (if (not ff-quiet-mode)
605 (message "finding %s..." file))
607 (if (file-exists-p file)
608 (setq found file))
610 (setq suffixes (cdr suffixes))
611 (setq this-suffix (car suffixes))))
613 ;; otherwise dir matches the '/*', so search each dir separately
614 (progn
615 (if (match-beginning 2)
616 (setq rest (substring dir (match-beginning 2) (match-end 2)))
617 (setq rest "")
619 (setq dir (substring dir (match-beginning 1) (match-end 1)))
621 (let ((dirlist (ff-all-dirs-under dir '("..")))
622 this-dir compl-dirs)
624 (setq this-dir (car dirlist))
625 (while dirlist
626 (setq compl-dirs
627 (append
628 compl-dirs
629 (list (concat this-dir rest))
631 (setq dirlist (cdr dirlist))
632 (setq this-dir (car dirlist)))
634 (if compl-dirs
635 (setq found (ff-get-file-name compl-dirs
636 fname-stub
637 suffix-list))))))
638 (setq dirs (cdr dirs))
639 (setq dir (car dirs)))
641 (if found
642 (message "%s found" found))
644 found))
646 (defun ff-string-match (regexp string &optional start)
647 "Like `string-match', but set `case-fold-search' temporarily.
648 The value used comes from `ff-case-fold-search'."
649 (let ((case-fold-search ff-case-fold-search))
650 (if regexp
651 (string-match regexp string start))))
653 (defun ff-list-replace-env-vars (search-list)
654 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST."
655 (let (list
656 (var (car search-list)))
657 (while search-list
658 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var)
659 (setq var
660 (concat
661 (substring var (match-beginning 1) (match-end 1))
662 (getenv (substring var (match-beginning 2) (match-end 2)))
663 (substring var (match-beginning 3) (match-end 3)))))
664 (setq search-list (cdr search-list))
665 (setq list (cons var list))
666 (setq var (car search-list)))
667 (setq search-list (reverse list))))
669 (defun ff-treat-as-special ()
670 "Returns the file to look for if the construct was special, else nil.
671 The construct is defined in the variable `ff-special-constructs'."
672 (let* (fname
673 (list ff-special-constructs)
674 (elem (car list))
675 (regexp (car elem))
676 (match (cdr elem)))
677 (while (and list (not fname))
678 (if (and (looking-at regexp) match)
679 (setq fname (funcall match)))
680 (setq list (cdr list))
681 (setq elem (car list))
682 (setq regexp (car elem))
683 (setq match (cdr elem)))
684 fname))
686 (defun ff-basename (string)
687 "Return the basename of PATHNAME."
688 (setq string (concat "/" string))
689 (string-match ".*/\\([^/]+\\)$" string)
690 (setq string (substring string (match-beginning 1) (match-end 1))))
692 (defun ff-all-dirs-under (here &optional exclude)
693 "Get all the directory files under directory HERE.
694 Exclude all files in the optional EXCLUDE list."
695 (if (file-directory-p here)
696 (condition-case nil
697 (progn
698 (let ((files (directory-files here t))
699 (dirlist (list))
700 file)
701 (while files
702 (setq file (car files))
703 (if (and
704 (file-directory-p file)
705 (not (member (ff-basename file) exclude)))
706 (setq dirlist (cons file dirlist)))
707 (setq files (cdr files)))
708 (setq dirlist (reverse dirlist))))
709 (error nil))
710 nil))
712 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file)
713 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW.
714 In addition, this runs various hooks.
716 Either F1 or F2 receives FILE as the sole argument.
717 The decision of which one to call is based on IN-OTHER-WINDOW
718 and on the global variable `ff-always-in-other-window'.
720 F1 and F2 are typically `find-file' / `find-file-other-window'
721 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs.
723 If optional NEW-FILE is t, then a special hook (`ff-file-created-hooks') is
724 called before `ff-post-load-hooks'."
725 (if ff-pre-load-hooks
726 (run-hooks 'ff-pre-load-hooks))
727 (if (or
728 (and in-other-window (not ff-always-in-other-window))
729 (and (not in-other-window) ff-always-in-other-window))
730 (funcall f2 file)
731 (funcall f1 file))
732 (if new-file
733 (if ff-file-created-hooks
734 (run-hooks 'ff-file-created-hooks)))
735 (if ff-post-load-hooks
736 (run-hooks 'ff-post-load-hooks)))
738 (defun ff-find-file (file &optional in-other-window new-file)
739 "Like `find-file', but may show the file in another window."
740 (ff-switch-file 'find-file
741 'find-file-other-window
742 file in-other-window new-file))
744 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window)
745 "Like `switch-to-buffer', but may show the buffer in another window."
747 (ff-switch-file 'switch-to-buffer
748 'switch-to-buffer-other-window
749 buffer-or-name in-other-window nil))
751 (cond
752 ((ff-emacs-19)
753 (defun ff-goto-click (event)
754 (set-buffer (window-buffer (posn-window (event-end event))))
755 (goto-char (posn-point (event-end event))))
757 ;;;###autoload
758 (defun ff-mouse-find-other-file (event)
759 "Visit the file you click on."
760 (interactive "e")
761 (save-excursion
762 (ff-goto-click event)
763 (ff-find-other-file nil)))
765 ;;;###autoload
766 (defun ff-mouse-find-other-file-other-window (event)
767 "Visit the file you click on."
768 (interactive "e")
769 (save-excursion
770 (ff-goto-click event)
771 (ff-find-other-file t)))
773 ;;;###autoload
774 (defun locate-file (fname dirs &optional suffix-list ignore-perms)
775 "Defines XEmacs look-alike locate-file for GNU Emacs-19."
776 (interactive)
777 (ff-get-file dirs fname suffix-list))
780 ((ff-xemacs)
782 ;;;###autoload
783 (defun ff-mouse-find-other-file (event)
784 "Visit the file you click on."
785 (interactive "@e")
786 (save-excursion
787 (mouse-set-point event)
788 (ff-find-other-file nil)))
790 ;;;###autoload
791 (defun ff-mouse-find-other-file-other-window (event)
792 "Visit the file you click on."
793 (interactive "@e")
794 (save-excursion
795 (mouse-set-point event)
796 (ff-find-other-file t)))
799 (provide 'find-file)
801 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
802 ;; This section offers an example of user defined function to select files
804 (defun ff-upcase-p (string &optional start end)
805 "Return t if this string is all uppercase.
806 Given START and/or END, checks between these characters."
807 (let (match str)
808 (if (not start)
809 (setq start 0))
810 (if (not end)
811 (setq end (length string)))
812 (if (= start end)
813 (setq end (1+ end)))
814 (setq str (substring string start end))
815 (if (and
816 (ff-string-match "[A-Z]+" str)
817 (setq match (match-data))
818 (= (car match) 0)
819 (= (car (cdr match)) (length str)))
821 nil)))
823 (defun ff-cc-hh-converter (arg)
824 "Discriminate file extensions.
825 Build up a new file list based possibly on part of the directory name
826 and the name of the file passed in."
827 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg)
828 (let ((path (if (match-beginning 1)
829 (substring arg (match-beginning 1) (match-end 1)) nil))
830 (dire (if (match-beginning 2)
831 (substring arg (match-beginning 2) (match-end 2)) nil))
832 (file (if (match-beginning 3)
833 (substring arg (match-beginning 3) (match-end 3)) nil))
834 (extn (if (match-beginning 4)
835 (substring arg (match-beginning 4) (match-end 4)) nil))
836 return-list)
837 (cond
838 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h}
839 ((and (string= extn "cc")
840 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file))
841 (let ((stub (substring file (match-beginning 2) (match-end 2))))
842 (setq dire (upcase (substring file (match-beginning 1) (match-end 1))))
843 (setq return-list (list (concat stub ".hh")
844 (concat stub ".h")
845 (concat file ".hh")
846 (concat file ".h")))
848 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C}
849 ((and (string= extn "hh") (ff-upcase-p dire) file)
850 (let ((stub (concat (downcase dire) file)))
851 (setq return-list (list (concat stub ".cc")
852 (concat stub ".C")
853 (concat file ".cc")
854 (concat file ".C")))
856 ;; zap.cc => zap.hh or zap.h
857 ((string= extn "cc")
858 (let ((stub file))
859 (setq return-list (list (concat stub ".hh")
860 (concat stub ".h")))
862 ;; zap.hh => zap.cc or zap.C
863 ((string= extn "hh")
864 (let ((stub file))
865 (setq return-list (list (concat stub ".cc")
866 (concat stub ".C")))
869 nil))
871 return-list))
873 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
874 ;; This section offers an example of user defined function to place point.
875 ;; The regexps are Ada specific.
877 (defvar ff-function-name nil "Name of the function we are in.")
879 (defvar ada-procedure-start-regexp)
880 (defvar ada-package-start-regexp)
882 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in)
884 (defun ff-which-function-are-we-in ()
885 "Return the name of the function whose definition/declaration point is in.
886 Also remember that name in `ff-function-name'."
888 (setq ff-function-name nil)
890 (save-excursion
891 (if (re-search-backward ada-procedure-start-regexp nil t)
892 (setq ff-function-name (buffer-substring (match-beginning 0)
893 (match-end 0)))
894 ; we didn't find a procedure start, perhaps there is a package
895 (if (re-search-backward ada-package-start-regexp nil t)
896 (setq ff-function-name (buffer-substring (match-beginning 0)
897 (match-end 0)))
898 ))))
900 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly)
902 (defun ff-set-point-accordingly ()
903 "Find the function specified in `ff-function-name'.
904 That name was previously determined by `ff-which-function-are-we-in'."
905 (if ff-function-name
906 (progn
907 (goto-char (point-min))
908 (search-forward ff-function-name nil t))))
910 ;; find-file.el ends here