(telnet-initial-filter): Fix error message.
[emacs.git] / lisp / find-file.el
blobb8aee680516f67599abc96b679c6860bc51e102d
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 ;;; Change Log:
114 ;; FEEDBACK:
115 ;; Please send me bug reports, bug fixes, and extensions, so that I can
116 ;; merge them into the master source.
118 ;; CREDITS:
119 ;; Many thanks go to TUSC Computer Systems Pty Ltd for providing an environ-
120 ;; ment that made the development of this package possible.
122 ;; Many thanks also go to all those who provided valuable feedback throughout
123 ;; the development of this package:
124 ;; Rolf Ebert in particular, Fritz Knabe, Heddy Boubaker, Sebastian Kremer,
125 ;; Vasco Lopes Paulo, Mark A. Plaksin, Robert Lang, Trevor West, Kevin
126 ;; Pereira, Benedict Lofstedt & Justin Vallon.
128 ;;; Code:
129 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
130 ;; User definable variables:
132 (defvar ff-pre-find-hooks nil
133 "*List of functions to be called before the search for the file starts.")
135 (defvar ff-pre-load-hooks nil
136 "*List of functions to be called before the other file is loaded.")
138 (defvar ff-post-load-hooks nil
139 "*List of functions to be called after the other file is loaded.")
141 (defvar ff-not-found-hooks nil
142 "*List of functions to be called if the other file could not be found.")
144 (defvar ff-file-created-hooks nil
145 "*List of functions to be called if the other file needs to be created.")
147 (defvar ff-case-fold-search nil
148 "*Non-nil means ignore cases in matches (see `case-fold-search').
149 If you have extensions in different cases, you will want this to be nil.")
151 (defvar ff-always-in-other-window nil
152 "*If non-nil, find the corresponding file in another window by default.
153 To override this, give an argument to `ff-find-other-file'.")
155 (defvar ff-ignore-include nil
156 "*If non-nil, ignore `#include' lines.")
158 (defvar ff-always-try-to-create t
159 "*If non-nil, always attempt to create the other file if it was not found.")
161 (defvar ff-quiet-mode nil
162 "*If non-nil, trace which directories are being searched.")
164 (defvar ff-special-constructs
166 ;; C/C++ include, for NeXTSTEP too
167 ("^\#\\s *\\(include\\|import\\)\\s +[<\"]\\(.*\\)[>\"]" .
168 (lambda ()
169 (setq fname (buffer-substring (match-beginning 2) (match-end 2)))))
171 ;; Ada import
172 ("^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)" .
173 (lambda ()
174 (setq fname (buffer-substring (match-beginning 1) (match-end 1)))
175 (require 'ada-mode)
176 (setq fname (concat (ada-make-filename-from-adaname fname)
177 ada-spec-suffix))))
179 "*A list of regular expressions specifying how to recognise special
180 constructs such as include files etc, and an associated method for
181 extracting the filename from that construct.")
183 (defvar ff-other-file-alist 'cc-other-file-alist
184 "*Alist of extensions to find given the current file's extension.
186 This list should contain the most used extensions before the others,
187 since the search algorithm searches sequentially through each
188 directory specified in `ff-search-directories'. If a file is not found,
189 a new one is created with the first matching extension (`.cc' yields `.hh').
190 This alist should be set by the major mode.")
192 (defvar ff-search-directories 'cc-search-directories
193 "*List of directories to search for a specific file.
195 Set by default to `cc-search-directories', expanded at run-time.
197 This list is searched through with each extension specified in
198 `ff-other-file-alist' that matches this file's extension. So the
199 longer the list, the longer it'll take to realise that a file
200 may not exist.
202 A typical format is
204 '(\".\" \"/usr/include\" \"$PROJECT/*/include\")
206 Environment variables can be inserted between slashes (`/').
207 They will be replaced by their definition. If a variable does
208 not exist, it is replaced (silently) with an empty string.
210 The stars are *not* wildcards: they are searched for together with
211 the preceding slash. The star represents all the subdirectories except
212 `..', and each of these subdirectories will be searched in turn.")
214 (defvar cc-search-directories
215 '("." "/usr/include" "/usr/local/include/*")
216 "*See the description of the `ff-search-directories' variable.")
218 (defvar cc-other-file-alist
220 ("\\.cc$" (".hh" ".h"))
221 ("\\.hh$" (".cc" ".C"))
223 ("\\.c$" (".h"))
224 ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp"))
226 ("\\.C$" (".H" ".hh" ".h"))
227 ("\\.H$" (".C" ".CC"))
229 ("\\.CC$" (".HH" ".H" ".hh" ".h"))
230 ("\\.HH$" (".CC"))
232 ("\\.cxx$" (".hh" ".h"))
233 ("\\.cpp$" (".hh" ".h"))
235 "*Alist of extensions to find given the current file's extension.
237 This list should contain the most used extensions before the others,
238 since the search algorithm searches sequentially through each directory
239 specified in `ff-search-directories'. If a file is not found, a new one
240 is created with the first matching extension (`.cc' yields `.hh').")
242 (defvar ada-search-directories
243 '("." "/usr/adainclude" "/usr/local/adainclude")
244 "*See the description for the `ff-search-directories' variable.")
246 (defvar ada-other-file-alist
248 ("\\.ads$" (".adb")) ;; Ada specs and bodies
249 ("\\.adb$" (".ads")) ;; GNAT filename conventions
251 "*Alist of extensions to find given the current file's extension.
253 This list should contain the most used extensions before the others,
254 since the search algorithm searches sequentially through each directory
255 specified in `ada-search-directories'. If a file is not found, a new one
256 is created with the first matching extension (`.adb' yields `.ads').
259 (defvar modula2-other-file-alist
261 ("\\.mi$" (".md")) ;; Modula-2 module definition
262 ("\\.md$" (".mi")) ;; and implementation.
264 "*See the description for the `ff-search-directories' variable.")
266 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
267 ;; No user definable variables beyond this point!
268 ;; ==============================================
270 (make-variable-buffer-local 'ff-pre-find-hooks)
271 (make-variable-buffer-local 'ff-pre-load-hooks)
272 (make-variable-buffer-local 'ff-post-load-hooks)
273 (make-variable-buffer-local 'ff-not-found-hooks)
274 (make-variable-buffer-local 'ff-file-created-hooks)
275 (make-variable-buffer-local 'ff-case-fold-search)
276 (make-variable-buffer-local 'ff-always-in-other-window)
277 (make-variable-buffer-local 'ff-ignore-include)
278 (make-variable-buffer-local 'ff-quiet-mode)
279 (make-variable-buffer-local 'ff-other-file-alist)
280 (make-variable-buffer-local 'ff-search-directories)
282 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283 ;; User entry points
285 ;;;###autoload
286 (defun ff-get-other-file (&optional in-other-window)
287 "Find the header or source file corresponding to this file.
288 See also the documentation for `ff-find-other-file;.
290 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
291 (interactive "P")
292 (let ((ignore ff-ignore-include))
293 (setq ff-ignore-include t)
294 (ff-find-the-other-file in-other-window)
295 (setq ff-ignore-include ignore)))
297 ;;;###autoload
298 (defun ff-find-other-file (&optional in-other-window ignore-include)
299 "Find the header or source file corresponding to this file.
300 Being on a `#include' line pulls in that file.
302 If optional IN-OTHER-WINDOW is non-nil, find the file in the other window.
303 If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines.
305 Variables of interest include:
307 - ff-case-fold-search
308 Non-nil means ignore cases in matches (see case-fold-search).
309 If you have extensions in different cases, you will want this to be nil.
311 - ff-always-in-other-window
312 If non-nil, always open the other file in another window, unless an
313 argument is given to ff-find-other-file.
315 - ff-ignore-include
316 If non-nil, ignores #include lines.
318 - ff-always-try-to-create
319 If non-nil, always attempt to create the other file if it was not found.
321 - ff-quiet-mode
322 If non-nil, traces which directories are being searched.
324 - ff-special-constructs
325 A list of regular expressions specifying how to recognise special
326 constructs such as include files etc, and an associated method for
327 extracting the filename from that construct.
329 - ff-other-file-alist
330 Alist of extensions to find given the current file's extension.
332 - ff-search-directories
333 List of directories searched through with each extension specified in
334 ff-other-file-alist that matches this file's extension.
336 - ff-pre-find-hooks
337 List of functions to be called before the search for the file starts.
339 - ff-pre-load-hooks
340 List of functions to be called before the other file is loaded.
342 - ff-post-load-hooks
343 List of functions to be called after the other file is loaded.
345 - ff-not-found-hooks
346 List of functions to be called if the other file could not be found.
348 - ff-file-created-hooks
349 List of functions to be called if the other file has been created."
350 (interactive "P")
351 (let ((ignore ff-ignore-include))
352 (setq ff-ignore-include ignore-include)
353 (ff-find-the-other-file in-other-window)
354 (setq ff-ignore-include ignore)))
356 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
357 ;; Support functions
359 (defun ff-emacs-19 ()
360 (string-match "^19\\.[0-9]+\\.[0-9]+$" emacs-version))
362 (defun ff-xemacs ()
363 (or (string-match "Lucid" emacs-version)
364 (string-match "XEmacs" emacs-version)))
366 (defun ff-find-the-other-file (&optional in-other-window)
367 "Find the header or source file corresponding to the current file.
368 Being on a `#include' line pulls in that file, but see the help on
369 the `ff-ignore-include' variable.
371 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
373 (let (match ;; matching regexp for this file
374 suffixes ;; set of replacing regexps for the matching regexp
375 action ;; function to generate the names of the other files
376 fname ;; basename of this file
377 pos ;; where we start matching filenames
378 stub ;; name of the file without extension
379 alist ;; working copy of the list of file extensions
380 pathname ;; the pathname of the file or the #include line
381 default-name ;; file we should create if none found
382 format ;; what we have to match
383 found ;; name of the file or buffer found - nil if none
384 dirs ;; local value of ff-search-directories
385 no-match) ;; whether we know about this kind of file
387 (if ff-pre-find-hooks
388 (run-hooks 'ff-pre-find-hooks))
390 (message "Working...")
392 (setq dirs
393 (if (symbolp ff-search-directories)
394 (ff-list-replace-env-vars (symbol-value ff-search-directories))
395 (ff-list-replace-env-vars ff-search-directories)))
397 (save-excursion
398 (beginning-of-line 1)
399 (setq fname (ff-treat-as-special)))
401 (cond
402 ((and (not ff-ignore-include) fname)
403 (setq default-name fname)
404 (setq found (ff-get-file dirs fname nil in-other-window)))
406 ;; let's just get the corresponding file
408 (setq alist (if (symbolp ff-other-file-alist)
409 (symbol-value ff-other-file-alist)
410 ff-other-file-alist)
411 pathname (if (buffer-file-name)
412 (buffer-file-name)
413 "/none.none"))
415 (string-match ".*/\\(.+\\)$" pathname)
416 (setq fname (substring pathname (match-beginning 1) (match-end 1))
417 no-match nil
418 match (car alist))
420 ;; find the table entry corresponding to this file
421 (setq pos (ff-string-match (car match) fname))
422 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
423 (setq alist (cdr alist))
424 (setq match (car alist))
425 (setq pos (ff-string-match (car match) fname)))
427 ;; no point going on if we haven't found anything
428 (if (not match)
429 (setq no-match t)
431 ;; otherwise, suffixes contains what we need
432 (setq suffixes (car (cdr match))
433 action (car (cdr match))
434 found nil)
436 ;; if we have a function to generate new names,
437 ;; invoke it with the name of the current file
438 (if (and (atom action) (fboundp action))
439 (progn
440 (setq suffixes (funcall action (buffer-file-name))
441 match (cons (car match) (list suffixes))
442 stub nil
443 default-name (car suffixes)))
445 ;; otherwise build our filename stub
446 (cond
448 ;; get around the problem that 0 and nil both mean false!
449 ((= pos 0)
450 (setq format "")
451 (setq stub "")
455 (setq format (concat "\\(.+\\)" (car match)))
456 (string-match format fname)
457 (setq stub (substring fname (match-beginning 1) (match-end 1)))
460 ;; if we find nothing, we should try to get a file like this one
461 (setq default-name
462 (concat stub (car (car (cdr match))))))
464 ;; do the real work - find the file
465 (setq found
466 (ff-get-file dirs
467 stub
468 suffixes
469 in-other-window)))))
471 (cond
472 (no-match ;; could not even determine the other file
473 (message ""))
476 (cond
478 ((not found) ;; could not find the other file
480 (if ff-not-found-hooks ;; run the hooks
481 (run-hooks 'ff-not-found-hooks))
483 (cond
484 (ff-always-try-to-create ;; try to create the file
485 (let (name pathname)
487 (setq name
488 (expand-file-name
489 (read-file-name
490 (format "Find or create %s in: " default-name)
491 default-directory default-name nil)))
493 (setq pathname
494 (if (file-directory-p name)
495 (concat (file-name-as-directory name) default-name)
496 (setq found name)))
498 (ff-find-file pathname in-other-window t)))
500 (t ;; don't create the file, just whinge
501 (message "no file found for %s" fname))))
503 (t ;; matching file found
504 nil))))
506 found)) ;; return buffer-name or filename
508 (defun ff-get-file (search-dirs fname-stub &optional suffix-list other-window)
509 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
510 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
511 with each of the given suffixes. Gets the file or the buffer corresponding
512 to the name of the first file found, or nil.
514 Arguments: (search-dirs fname-stub &optional suffix-list in-other-window)
516 (let ((filename (ff-get-file-name search-dirs fname-stub suffix-list)))
518 (cond
519 ((not filename)
520 nil)
522 ((bufferp (get-file-buffer filename))
523 (ff-switch-to-buffer (get-file-buffer filename) other-window)
524 filename)
526 ((file-exists-p filename)
527 (ff-find-file filename other-window nil)
528 filename)
531 nil))))
533 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
534 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
535 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
536 with each of the given suffixes. Returns the name of the first file found.
538 Arguments: (search-dirs fname-stub &optional suffix-list)
540 (let* (dirs ;; working copy of dirs to search
541 dir ;; the current dir considered
542 file ;; filename being looked for
543 rest ;; pathname after first /*
544 this-suffix ;; the suffix we are currently considering
545 suffixes ;; working copy of suffix-list
546 filename ;; built filename
547 blist ;; list of live buffers
548 buf ;; current buffer in blist
549 found) ;; whether we have found anything
551 (setq suffixes suffix-list)
553 ;; suffixes is nil => fname-stub is the file we are looking for
554 ;; otherwise fname-stub is a stub, and we append a suffix
555 (if suffixes
556 (setq this-suffix (car suffixes))
557 (setq this-suffix "")
558 (setq suffixes (list "")))
560 ;; find whether the file is in a buffer first
561 (while (and suffixes (not found))
562 (setq filename (concat fname-stub this-suffix))
564 (if (not ff-quiet-mode)
565 (message "finding buffer %s..." filename))
567 (if (bufferp (get-buffer filename))
568 (setq found (buffer-file-name (get-buffer filename))))
570 (setq blist (buffer-list))
571 (setq buf (buffer-name (car blist)))
572 (while (and blist (not found))
574 (if (string-match (concat filename "<[0-9]+>") buf)
575 (setq found (buffer-file-name (car blist))))
577 (setq blist (cdr blist))
578 (setq buf (buffer-name (car blist))))
580 (setq suffixes (cdr suffixes))
581 (setq this-suffix (car suffixes)))
583 ;; now look for the real file
584 (setq dirs search-dirs)
585 (setq dir (car dirs))
586 (while (and (not found) dirs)
588 (setq suffixes suffix-list)
590 ;; if dir does not contain '/*', look for the file
591 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir)))
592 (progn
594 ;; suffixes is nil => fname-stub is the file we are looking for
595 ;; otherwise fname-stub is a stub, and we append a suffix
596 (if suffixes
597 (setq this-suffix (car suffixes))
598 (setq this-suffix "")
599 (setq suffixes (list "")))
601 (while (and suffixes (not found))
603 (setq filename (concat fname-stub this-suffix))
604 (setq file (concat dir "/" filename))
606 (if (not ff-quiet-mode)
607 (message "finding %s..." file))
609 (if (file-exists-p file)
610 (setq found file))
612 (setq suffixes (cdr suffixes))
613 (setq this-suffix (car suffixes))))
615 ;; otherwise dir matches the '/*', so search each dir separately
616 (progn
617 (if (match-beginning 2)
618 (setq rest (substring dir (match-beginning 2) (match-end 2)))
619 (setq rest "")
621 (setq dir (substring dir (match-beginning 1) (match-end 1)))
623 (let ((dirlist (ff-all-dirs-under dir '("..")))
624 this-dir compl-dirs)
626 (setq this-dir (car dirlist))
627 (while dirlist
628 (setq compl-dirs
629 (append
630 compl-dirs
631 (list (concat this-dir rest))
633 (setq dirlist (cdr dirlist))
634 (setq this-dir (car dirlist)))
636 (if compl-dirs
637 (setq found (ff-get-file-name compl-dirs
638 fname-stub
639 suffix-list))))))
640 (setq dirs (cdr dirs))
641 (setq dir (car dirs)))
643 (if found
644 (message "%s found" found))
646 found))
648 (defun ff-string-match (regexp string &optional start)
649 "Like `string-match', but set `case-fold-search' temporarily.
650 The value used comes from `ff-case-fold-search'."
651 (let ((case-fold-search ff-case-fold-search))
652 (if regexp
653 (string-match regexp string start))))
655 (defun ff-list-replace-env-vars (search-list)
656 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST."
657 (let (list
658 (var (car search-list)))
659 (while search-list
660 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var)
661 (setq var
662 (concat
663 (substring var (match-beginning 1) (match-end 1))
664 (getenv (substring var (match-beginning 2) (match-end 2)))
665 (substring var (match-beginning 3) (match-end 3)))))
666 (setq search-list (cdr search-list))
667 (setq list (cons var list))
668 (setq var (car search-list)))
669 (setq search-list (reverse list))))
671 (defun ff-treat-as-special ()
672 "Returns the file to look for if the construct was special, else nil.
673 The construct is defined in the variable `ff-special-constructs'."
674 (let* (fname
675 (list ff-special-constructs)
676 (elem (car list))
677 (regexp (car elem))
678 (match (cdr elem)))
679 (while (and list (not fname))
680 (if (and (looking-at regexp) match)
681 (setq fname (funcall match)))
682 (setq list (cdr list))
683 (setq elem (car list))
684 (setq regexp (car elem))
685 (setq match (cdr elem)))
686 fname))
688 (defun ff-basename (string)
689 "Return the basename of PATHNAME."
690 (setq string (concat "/" string))
691 (string-match ".*/\\([^/]+\\)$" string)
692 (setq string (substring string (match-beginning 1) (match-end 1))))
694 (defun ff-all-dirs-under (here &optional exclude)
695 "Get all the directory files under directory HERE.
696 Exclude all files in the optional EXCLUDE list."
697 (if (file-directory-p here)
698 (condition-case nil
699 (progn
700 (let ((files (directory-files here t))
701 (dirlist (list))
702 file)
703 (while files
704 (setq file (car files))
705 (if (and
706 (file-directory-p file)
707 (not (member (ff-basename file) exclude)))
708 (setq dirlist (cons file dirlist)))
709 (setq files (cdr files)))
710 (setq dirlist (reverse dirlist))))
711 (error nil))
712 nil))
714 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file)
715 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW.
716 In addition, this runs various hooks.
718 Either F1 or F2 receives FILE as the sole argument.
719 The decision of which one to call is based on IN-OTHER-WINDOW
720 and on the global variable `ff-always-in-other-window'.
722 F1 and F2 are typically `find-file' / `find-file-other-window'
723 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs.
725 If optional NEW-FILE is t, then a special hook (`ff-file-created-hooks') is
726 called before `ff-post-load-hooks'."
727 (if ff-pre-load-hooks
728 (run-hooks 'ff-pre-load-hooks))
729 (if (or
730 (and in-other-window (not ff-always-in-other-window))
731 (and (not in-other-window) ff-always-in-other-window))
732 (funcall f2 file)
733 (funcall f1 file))
734 (if new-file
735 (if ff-file-created-hooks
736 (run-hooks 'ff-file-created-hooks)))
737 (if ff-post-load-hooks
738 (run-hooks 'ff-post-load-hooks)))
740 (defun ff-find-file (file &optional in-other-window new-file)
741 "Like `find-file', but may show the file in another window."
742 (ff-switch-file 'find-file
743 'find-file-other-window
744 file in-other-window new-file))
746 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window)
747 "Like `switch-to-buffer', but may show the buffer in another window."
749 (ff-switch-file 'switch-to-buffer
750 'switch-to-buffer-other-window
751 buffer-or-name in-other-window nil))
753 (cond
754 ((ff-emacs-19)
755 (defun ff-goto-click (event)
756 (set-buffer (window-buffer (posn-window (event-end event))))
757 (goto-char (posn-point (event-end event))))
759 ;;;###autoload
760 (defun ff-mouse-find-other-file (event)
761 "Visit the file you click on."
762 (interactive "e")
763 (save-excursion
764 (ff-goto-click event)
765 (ff-find-other-file nil)))
767 ;;;###autoload
768 (defun ff-mouse-find-other-file-other-window (event)
769 "Visit the file you click on."
770 (interactive "e")
771 (save-excursion
772 (ff-goto-click event)
773 (ff-find-other-file t)))
775 ;;;###autoload
776 (defun locate-file (fname dirs &optional suffix-list ignore-perms)
777 "Defines XEmacs look-alike locate-file for GNU Emacs-19."
778 (interactive)
779 (ff-get-file dirs fname suffix-list))
782 ((ff-xemacs)
784 ;;;###autoload
785 (defun ff-mouse-find-other-file (event)
786 "Visit the file you click on."
787 (interactive "@e")
788 (save-excursion
789 (mouse-set-point event)
790 (ff-find-other-file nil)))
792 ;;;###autoload
793 (defun ff-mouse-find-other-file-other-window (event)
794 "Visit the file you click on."
795 (interactive "@e")
796 (save-excursion
797 (mouse-set-point event)
798 (ff-find-other-file t)))
801 (provide 'find-file)
803 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
804 ;; This section offers an example of user defined function to select files
806 (defun ff-upcase-p (string &optional start end)
807 "Return t if this string is all uppercase.
808 Given START and/or END, checks between these characters."
809 (let (match str)
810 (if (not start)
811 (setq start 0))
812 (if (not end)
813 (setq end (length string)))
814 (if (= start end)
815 (setq end (1+ end)))
816 (setq str (substring string start end))
817 (if (and
818 (ff-string-match "[A-Z]+" str)
819 (setq match (match-data))
820 (= (car match) 0)
821 (= (car (cdr match)) (length str)))
823 nil)))
825 (defun ff-cc-hh-converter (arg)
826 "Discriminate file extensions.
827 Build up a new file list based possibly on part of the directory name
828 and the name of the file passed in."
829 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg)
830 (let ((path (if (match-beginning 1)
831 (substring arg (match-beginning 1) (match-end 1)) nil))
832 (dire (if (match-beginning 2)
833 (substring arg (match-beginning 2) (match-end 2)) nil))
834 (file (if (match-beginning 3)
835 (substring arg (match-beginning 3) (match-end 3)) nil))
836 (extn (if (match-beginning 4)
837 (substring arg (match-beginning 4) (match-end 4)) nil))
838 return-list)
839 (cond
840 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h}
841 ((and (string= extn "cc")
842 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file))
843 (let ((stub (substring file (match-beginning 2) (match-end 2))))
844 (setq dire (upcase (substring file (match-beginning 1) (match-end 1))))
845 (setq return-list (list (concat stub ".hh")
846 (concat stub ".h")
847 (concat file ".hh")
848 (concat file ".h")))
850 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C}
851 ((and (string= extn "hh") (ff-upcase-p dire) file)
852 (let ((stub (concat (downcase dire) file)))
853 (setq return-list (list (concat stub ".cc")
854 (concat stub ".C")
855 (concat file ".cc")
856 (concat file ".C")))
858 ;; zap.cc => zap.hh or zap.h
859 ((string= extn "cc")
860 (let ((stub file))
861 (setq return-list (list (concat stub ".hh")
862 (concat stub ".h")))
864 ;; zap.hh => zap.cc or zap.C
865 ((string= extn "hh")
866 (let ((stub file))
867 (setq return-list (list (concat stub ".cc")
868 (concat stub ".C")))
871 nil))
873 return-list))
875 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
876 ;; This section offers an example of user defined function to place point.
877 ;; The regexps are Ada specific.
879 (defvar ff-function-name nil "Name of the function we are in.")
881 (defvar ada-procedure-start-regexp)
882 (defvar ada-package-start-regexp)
884 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in)
886 (defun ff-which-function-are-we-in ()
887 "Return the name of the function whose definition/declaration point is in.
888 Also remember that name in `ff-function-name'."
890 (setq ff-function-name nil)
892 (save-excursion
893 (if (re-search-backward ada-procedure-start-regexp nil t)
894 (setq ff-function-name (buffer-substring (match-beginning 0)
895 (match-end 0)))
896 ; we didn't find a procedure start, perhaps there is a package
897 (if (re-search-backward ada-package-start-regexp nil t)
898 (setq ff-function-name (buffer-substring (match-beginning 0)
899 (match-end 0)))
900 ))))
902 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly)
904 (defun ff-set-point-accordingly ()
905 "Find the function specified in `ff-function-name'.
906 That name was previously determined by `ff-which-function-are-we-in'."
907 (if ff-function-name
908 (progn
909 (goto-char (point-min))
910 (search-forward ff-function-name nil t))))
912 ;; find-file.el ends here