(set-case-syntax-charset, set-case-syntax-1)
[bpt/emacs.git] / lisp / find-file.el
index c484374..cd53d82 100644 (file)
@@ -1,31 +1,33 @@
 ;;; find-file.el --- find a file corresponding to this one given a pattern
 
-;; Author:         Henry Guillaume <henry@qbd.com.au>
+;; Author: Henry Guillaume <henri@tibco.com, henry@c032.aone.net.au>
+;; Maintainer: FSF
 ;; Keywords: c, matching, tools
 
 ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
 
-;;; This file is part of GNU Emacs.
+;; This file is part of GNU Emacs.
 
-;;; GNU Emacs is free software; you can redistribute it and/or modify
-;;; it under the terms of the GNU General Public License as published by
-;;; the Free Software Foundation; either version 2, or (at your option)
-;;; any later version.
+;; GNU Emacs is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
 
-;;; GNU Emacs is distributed in the hope that it will be useful,
-;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
-;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-;;; GNU General Public License for more details.
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
 
-;;; You should have received a copy of the GNU General Public License
-;;; along with GNU Emacs; see the file COPYING.  If not, write to
-;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
 ;; PURPOSE:
-;; This package features a function called ff-find-other-file, which performs 
-;; the following function: 
+;; This package features a function called ff-find-other-file, which performs
+;; the following function:
 ;;
 ;;     When in a .c file, find the first corresponding .h file in a set
 ;;     of directories and display it, and vice-versa from the .h file.
@@ -36,7 +38,7 @@
 ;; to find that file.
 ;;
 ;; THE "OTHER FILE", or "corresponding file", generally has the same basename,
-;; and just has a different extension as described by the ff-other-file-alist 
+;; and just has a different extension as described by the ff-other-file-alist
 ;; variable:
 ;;
 ;;   '(("\\.cc$"  (".hh" ".h"))
@@ -58,8 +60,8 @@
 ;;   '(("\\.cc$"  cc-function)
 ;;     ("\\.hh$"  hh-function))
 ;;
-;; These functions must return a list consisting of the possible names of the 
-;; corresponding file, with or without path. There is no real need for more 
+;; These functions must return a list consisting of the possible names of the
+;; corresponding file, with or without path. There is no real need for more
 ;; than one function, and one could imagine the following value for cc-other-
 ;; file-alist:
 ;;
 ;;
 ;; If the file was not found, ff-find-other-file will prompt you for where
 ;; to create the new "corresponding file" (defaults to the current directory),
-;; unless the variable ff-always-try-to-create is set to nil. 
+;; unless the variable ff-always-try-to-create is set to nil.
 ;;
-;; GIVEN AN ARGUMENT (with the ^U prefix), ff-find-other-file will get the 
-;; other file in another (the other?) window (see find-file-other-window and 
-;; switch-to-buffer-other-window). This can be set on a more permanent basis 
-;; by setting ff-always-in-other-window to t in which case the ^U prefix will 
+;; GIVEN AN ARGUMENT (with the ^U prefix), ff-find-other-file will get the
+;; other file in another (the other?) window (see find-file-other-window and
+;; switch-to-buffer-other-window). This can be set on a more permanent basis
+;; by setting ff-always-in-other-window to t in which case the ^U prefix will
 ;; do the opposite of what was described above.
 ;;
 ;; THERE ARE FIVE AVAILABLE HOOKS, called in this order if non-nil:
 ;; - ff-post-load-hooks    - called just after the other file is 'loaded'
 ;;
 ;; The *load-hooks allow you to place point where you want it in the other
-;; file. 
-
-;; LCD Archive Entry:
-;; find-file|Henry Guillaume|henry@qbd.com.au|
-;; Find a file associated with this buffer.|
-;; 21-Dec-1994|4.0|~/misc/find-file.el.Z|
-
-;; FEEDBACK:
-;; Please send me bug reports, bug fixes, and extensions, so that I can
-;; merge them into the master source.
+;; file.
 
 ;; CREDITS:
 ;; Many thanks go to TUSC Computer Systems Pty Ltd for providing an environ-
 ;; Many thanks also go to all those who provided valuable feedback throughout
 ;; the development of this package:
 ;;     Rolf Ebert in particular, Fritz Knabe, Heddy Boubaker, Sebastian Kremer,
-;;     Vasco Lopes Paulo, Mark A. Plaksin, Robert Lang, Trevor West, Kevin 
-;;     Pereira & Benedict Lofstedt.
+;;     Vasco Lopes Paulo, Mark A. Plaksin, Robert Lang, Trevor West, Kevin
+;;     Pereira, Benedict Lofstedt & Justin Vallon.
 
-;; Code:
+;;; Code:
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; User definable variables:
 
-(defvar ff-pre-find-hooks nil
-  "*List of functions to be called before the search for the file starts.")
-
-(defvar ff-pre-load-hooks nil
-  "*List of functions to be called before the other file is loaded.")
-
-(defvar ff-post-load-hooks nil
-  "*List of functions to be called after the other file is loaded.")
-
-(defvar ff-not-found-hooks nil
-  "*List of functions to be called if the other file could not be found.")
-
-(defvar ff-file-created-hooks nil
-  "*List of functions to be called if the other file needs to be created.")
-
-(defvar ff-case-fold-search nil
-  "*Non-nil means ignore cases in matches (see case-fold-search).
-If you have extensions in different cases, you will want this to be nil.")
-
-(defvar ff-always-in-other-window nil
-  "*If non-nil, always open the other file in another window, unless an
-argument is given to ff-find-other-file.")
-
-(defvar ff-ignore-include nil
-  "*If non-nil, ignores include lines.")
-
-(defvar ff-always-try-to-create t
-  "*If non-nil, always attempt to create the other file if it was not found.")
-
-(defvar ff-quiet-mode nil
-  "*If non-nil, traces which directories are being searched.")
-
-(defvar ff-special-constructs 
+(defgroup ff nil
+  "Find a file corresponding to this one given a pattern."
+  :prefix "ff-"
+  :link '(emacs-commentary-link "find-file")
+  :group 'find-file)
+
+(defcustom ff-pre-find-hooks nil
+  "*List of functions to be called before the search for the file starts."
+  :type 'hook
+  :group 'ff)
+
+(defcustom ff-pre-load-hooks nil
+  "*List of functions to be called before the other file is loaded."
+  :type 'hook
+  :group 'ff)
+
+(defcustom ff-post-load-hooks nil
+  "*List of functions to be called after the other file is loaded."
+  :type 'hook
+  :group 'ff)
+
+(defcustom ff-not-found-hooks nil
+  "*List of functions to be called if the other file could not be found."
+  :type 'hook
+  :group 'ff)
+
+(defcustom ff-file-created-hooks nil
+  "*List of functions to be called if the other file needs to be created."
+  :type 'hook
+  :group 'ff)
+
+(defcustom ff-case-fold-search nil
+  "*Non-nil means ignore cases in matches (see `case-fold-search').
+If you have extensions in different cases, you will want this to be nil."
+  :type 'boolean
+  :group 'ff)
+
+(defcustom ff-always-in-other-window nil
+  "*If non-nil, find the corresponding file in another window by default.
+To override this, give an argument to `ff-find-other-file'."
+  :type 'boolean
+  :group 'ff)
+
+(defcustom ff-ignore-include nil
+  "*If non-nil, ignore `#include' lines."
+  :type 'boolean
+  :group 'ff)
+
+(defcustom ff-always-try-to-create t
+  "*If non-nil, always attempt to create the other file if it was not found."
+  :type 'boolean
+  :group 'ff)
+
+(defcustom ff-quiet-mode nil
+  "*If non-nil, trace which directories are being searched."
+  :type 'boolean
+  :group 'ff)
+
+(defvar ff-special-constructs
   '(
     ;; C/C++ include, for NeXTSTEP too
     ("^\#\\s *\\(include\\|import\\)\\s +[<\"]\\(.*\\)[>\"]" .
@@ -174,49 +193,58 @@ argument is given to ff-find-other-file.")
     ("^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)" .
      (lambda ()
        (setq fname (buffer-substring (match-beginning 1) (match-end 1)))
+       (require 'ada-mode)
        (setq fname (concat (ada-make-filename-from-adaname fname)
-                           ada-spec-suffix))))
+                          ada-spec-suffix))))
     )
-  "*A list of regular expressions specifying how to recognise special 
-constructs such as include files etc, and an associated method for 
-extracting the filename from that construct.")
+  "*A list of regular expressions for `ff-find-file'.
+Specifies how to recognise special constructs such as include files
+etc. and an associated method for extracting the filename from that
+construct.")
 
-(defvar ff-other-file-alist 'cc-other-file-alist
+(defcustom ff-other-file-alist 'cc-other-file-alist
   "*Alist of extensions to find given the current file's extension.
 
 This list should contain the most used extensions before the others,
 since the search algorithm searches sequentially through each
-directory specified in ff-search-directories. If a file is not found,
-a new one is created with the first matching extension (.cc yields .hh).
-This alist should be set by the major-mode.")
-
-(defvar ff-search-directories 'cc-search-directories
+directory specified in `ff-search-directories'.  If a file is not found,
+a new one is created with the first matching extension (`.cc' yields `.hh').
+This alist should be set by the major mode."
+  :type '(choice (repeat (list regexp (choice (repeat string) function)))
+                symbol)
+  :group 'ff)
+
+(defcustom ff-search-directories 'cc-search-directories
   "*List of directories to search for a specific file.
 
-Set by default to 'cc-search-directories, expanded at run-time.
+Set by default to `cc-search-directories', expanded at run-time.
 
 This list is searched through with each extension specified in
-ff-other-file-alist that matches this file's extension. So the
+`ff-other-file-alist' that matches this file's extension.  So the
 longer the list, the longer it'll take to realise that a file
 may not exist.
 
-A typical format is 
+A typical format is
 
-    '(\".\" \"/usr/include/*\" \"$PROJECT/*/include\")
+    '(\".\" \"/usr/include\" \"$PROJECT/*/include\")
 
-Environment variables can be inserted between slashes ('/').
+Environment variables can be inserted between slashes (`/').
 They will be replaced by their definition. If a variable does
-not exist, it will (silently) be replaced with an empty string.
+not exist, it is replaced (silently) with an empty string.
 
-The stars are _not_ wildcards: they are searched for together with
-the preceding slash. The star represents all the subdirectories except
-'..', and each of these subdirectories will be searched in turn.")
+The stars are *not* wildcards: they are searched for together with
+the preceding slash.  The star represents all the subdirectories except
+`..', and each of these subdirectories will be searched in turn."
+  :type '(choice (repeat directory) symbol)
+  :group 'ff)
 
-(defvar cc-search-directories
-  '("." "/usr/include/*" "/usr/local/include/*")
-  "*See the description of the ff-search-directories variable.")
+(defcustom cc-search-directories
+  '("." "/usr/include" "/usr/local/include/*")
+  "*See the description of the `ff-search-directories' variable."
+  :type '(repeat directory)
+  :group 'ff)
 
-(defvar cc-other-file-alist
+(defcustom cc-other-file-alist
   '(
     ("\\.cc$"  (".hh" ".h"))
     ("\\.hh$"  (".cc" ".C"))
@@ -237,39 +265,20 @@ the preceding slash. The star represents all the subdirectories except
 
 This list should contain the most used extensions before the others,
 since the search algorithm searches sequentially through each directory
-specified in ff-search-directories. If a file is not found, a new one
-is created with the first matching extension (.cc yields .hh).")
+specified in `ff-search-directories'.  If a file is not found, a new one
+is created with the first matching extension (`.cc' yields `.hh')."
+  :type '(repeat (list regexp (choice (repeat string) function)))
+  :group 'ff)
 
-(defvar ada-search-directories
-  '("." "/usr/adainclude" "/usr/local/adainclude")
-  "*See the description for the ff-search-directories variable.")
-
-(defvar ada-other-file-alist
-  '(
-    ("\\.ads$" (".adb")) ;; Ada specs and bodies
-    ("\\.adb$" (".ads")) ;; GNAT filename conventions
-    )
-  "*Alist of extensions to find given the current file's extension.
-
-This list should contain the most used extensions before the others,
-since the search algorithm searches sequentially through each directory
-specified in ada-search-directories. If a file is not found, a new one
-is created with the first matching extension (.adb yields .ads).
-")
-
-;;;### autoload
-(autoload 'ada-make-filename-from-adaname "ada-mode"
-  "Determine the filename of a package/procedure from its own Ada name.")
-(defvar ada-spec-suffix ".ads"  
-  "*Suffix of Ada specification files.")
-(make-variable-buffer-local 'ada-spec-suffix)
-
-(defvar modula2-other-file-alist
+(defcustom modula2-other-file-alist
   '(
     ("\\.mi$" (".md")) ;; Modula-2 module definition
     ("\\.md$" (".mi")) ;; and implementation.
     )
-  "*See the description for the ff-search-directories variable.")
+  "*See the description for the `ff-search-directories' variable."
+  :type '(repeat (list regexp (choice (repeat string) function)))
+  :group 'ff)
+
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; No user definable variables beyond this point!
@@ -292,12 +301,10 @@ is created with the first matching extension (.adb yields .ads).
 
 ;;;###autoload
 (defun ff-get-other-file (&optional in-other-window)
-  "Find the corresponding header or source file to this source or header
-file. See also the documentation for ff-find-other-file.
+  "Find the header or source file corresponding to this file.
+See also the documentation for `ff-find-other-file'.
 
-If optional IN-OTHER-WINDOW is non-nil, finds the file in another window.
-
-Arguments: (&optional in-other-window)"
+If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
   (interactive "P")
   (let ((ignore ff-ignore-include))
     (setq ff-ignore-include t)
@@ -306,58 +313,56 @@ Arguments: (&optional in-other-window)"
 
 ;;;###autoload
 (defun ff-find-other-file (&optional in-other-window ignore-include)
-  "Find the corresponding header or source file to this source or header
-file; being on a #include line pulls in that file.
-
-If optional IN-OTHER-WINDOW is non-nil, finds the file in the other window.
-If optional IGNORE-INCLUDE is non-nil, ignores being on #include lines.
+  "Find the header or source file corresponding to this file.
+Being on a `#include' line pulls in that file.
 
-Arguments: (&optional in-other-window ignore-include)
+If optional IN-OTHER-WINDOW is non-nil, find the file in the other window.
+If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines.
 
 Variables of interest include:
 
- - ff-case-fold-search
-   Non-nil means ignore cases in matches (see case-fold-search).
+ - `ff-case-fold-search'
+   Non-nil means ignore cases in matches (see `case-fold-search').
    If you have extensions in different cases, you will want this to be nil.
 
- - ff-always-in-other-window 
+ - `ff-always-in-other-window'
    If non-nil, always open the other file in another window, unless an
-   argument is given to ff-find-other-file.
+   argument is given to `ff-find-other-file'.
 
- - ff-ignore-include 
+ - `ff-ignore-include'
    If non-nil, ignores #include lines.
 
- - ff-always-try-to-create 
+ - `ff-always-try-to-create'
    If non-nil, always attempt to create the other file if it was not found.
 
- - ff-quiet-mode 
+ - `ff-quiet-mode'
    If non-nil, traces which directories are being searched.
 
- - ff-special-constructs 
-   A list of regular expressions specifying how to recognise special 
-   constructs such as include files etc, and an associated method for 
+ - `ff-special-constructs'
+   A list of regular expressions specifying how to recognise special
+   constructs such as include files etc, and an associated method for
    extracting the filename from that construct.
 
- - ff-other-file-alist
+ - `ff-other-file-alist'
    Alist of extensions to find given the current file's extension.
 
- - ff-search-directories 
+ - `ff-search-directories'
    List of directories searched through with each extension specified in
-   ff-other-file-alist that matches this file's extension.
+   `ff-other-file-alist' that matches this file's extension.
 
- - ff-pre-find-hooks 
+ - `ff-pre-find-hooks'
    List of functions to be called before the search for the file starts.
 
- - ff-pre-load-hooks 
+ - `ff-pre-load-hooks'
    List of functions to be called before the other file is loaded.
 
- - ff-post-load-hooks
+ - `ff-post-load-hooks'
    List of functions to be called after the other file is loaded.
 
- - ff-not-found-hooks
+ - `ff-not-found-hooks'
    List of functions to be called if the other file could not be found.
 
- - ff-file-created-hooks
+ - `ff-file-created-hooks'
    List of functions to be called if the other file has been created."
   (interactive "P")
   (let ((ignore ff-ignore-include))
@@ -368,21 +373,12 @@ Variables of interest include:
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Support functions
 
-(defun ff-gnu-emacs-19 ()
-  (string-match "^19\\.[0-9]+\\.[0-9]+$" emacs-version))
-
-(defun ff-xemacs ()
-  (or (string-match "Lucid"  emacs-version)
-      (string-match "XEmacs" emacs-version)))
-
 (defun ff-find-the-other-file (&optional in-other-window)
-  "Find the corresponding header or source file to this source or header
-file; being on a #include line pulls in that file, but see the help on
-the ff-ignore-include variable.
+  "Find the header or source file corresponding to the current file.
+Being on a `#include' line pulls in that file, but see the help on
+the `ff-ignore-include' variable.
 
-If optional IN-OTHER-WINDOW is non-nil, finds the file in another window.
-
-Arguments: (&optional in-other-window)"
+If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
 
   (let (match           ;; matching regexp for this file
         suffixes        ;; set of replacing regexps for the matching regexp
@@ -393,8 +389,8 @@ Arguments: (&optional in-other-window)"
         alist           ;; working copy of the list of file extensions
         pathname        ;; the pathname of the file or the #include line
         default-name    ;; file we should create if none found
-        format          ;; what we have to match    
-        found           ;; name of the file or buffer found - nil if none 
+        format          ;; what we have to match
+        found           ;; name of the file or buffer found - nil if none
         dirs            ;; local value of ff-search-directories
         no-match)       ;; whether we know about this kind of file
 
@@ -426,8 +422,7 @@ Arguments: (&optional in-other-window)"
                          (buffer-file-name)
                        "/none.none"))
 
-      (string-match ".*/\\(.+\\)$" pathname)
-      (setq fname (substring pathname (match-beginning 1) (match-end 1))
+      (setq fname (file-name-nondirectory pathname)
             no-match nil
             match (car alist))
 
@@ -447,7 +442,7 @@ Arguments: (&optional in-other-window)"
               action (car (cdr match))
               found nil)
 
-        ;; if we have a function to generate new names, 
+        ;; if we have a function to generate new names,
         ;; invoke it with the name of the current file
         (if (and (atom action) (fboundp action))
             (progn
@@ -457,7 +452,7 @@ Arguments: (&optional in-other-window)"
                     default-name (car suffixes)))
 
           ;; otherwise build our filename stub
-          (cond 
+          (cond
 
            ;; get around the problem that 0 and nil both mean false!
            ((= pos 0)
@@ -476,17 +471,17 @@ Arguments: (&optional in-other-window)"
                 (concat stub (car (car (cdr match))))))
 
         ;; do the real work - find the file
-        (setq found 
+        (setq found
               (ff-get-file dirs
                            stub
-                           suffixes 
+                           suffixes
                            in-other-window)))))
 
     (cond
      (no-match                     ;; could not even determine the other file
       (message ""))
 
-     (t 
+     (t
       (cond
 
        ((not found)                ;; could not find the other file
@@ -494,7 +489,7 @@ Arguments: (&optional in-other-window)"
         (if ff-not-found-hooks     ;; run the hooks
             (run-hooks 'ff-not-found-hooks))
 
-        (cond 
+        (cond
          (ff-always-try-to-create  ;; try to create the file
           (let (name pathname)
 
@@ -512,29 +507,123 @@ Arguments: (&optional in-other-window)"
             (ff-find-file pathname in-other-window t)))
 
          (t                        ;; don't create the file, just whinge
-          (message "no file found for %s" fname))))
+          (message "No file found for %s" fname))))
 
        (t                          ;; matching file found
         nil))))
 
     found))                        ;; return buffer-name or filename
 
-(defun ff-get-file (search-dirs fname-stub &optional suffix-list other-window)
-  "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub). 
-If (optional) SUFFIXES is nil, search for fname, otherwise search for fname 
-with each of the given suffixes. Gets the file or the buffer corresponding 
-to the name of the first file found, or nil.
+(defun ff-other-file-name ()
+  "Return name of the header or source file corresponding to the current file.
+Being on a `#include' line pulls in that file, but see the help on
+the `ff-ignore-include' variable."
 
-Arguments: (search-dirs fname-stub &optional suffix-list in-other-window)
-"
-  (let ((filename (ff-get-file-name search-dirs fname-stub suffix-list)))
+  (let (match           ;; matching regexp for this file
+        suffixes        ;; set of replacing regexps for the matching regexp
+        action          ;; function to generate the names of the other files
+        fname           ;; basename of this file
+        pos             ;; where we start matching filenames
+        stub            ;; name of the file without extension
+        alist           ;; working copy of the list of file extensions
+        pathname        ;; the pathname of the file or the #include line
+        default-name    ;; file we should create if none found
+        format          ;; what we have to match
+        found           ;; name of the file or buffer found - nil if none
+        dirs            ;; local value of ff-search-directories
+        no-match)       ;; whether we know about this kind of file
+
+    (message "Working...")
+
+    (setq dirs
+          (if (symbolp ff-search-directories)
+              (ff-list-replace-env-vars (symbol-value ff-search-directories))
+            (ff-list-replace-env-vars ff-search-directories)))
+
+    (save-excursion
+      (beginning-of-line 1)
+      (setq fname (ff-treat-as-special)))
+
+    (cond
+     ((and (not ff-ignore-include) fname)
+      (setq default-name fname)
+      (setq found (ff-get-file-name dirs fname nil)))
+
+     ;; let's just get the corresponding file
+     (t
+      (setq alist (if (symbolp ff-other-file-alist)
+                      (symbol-value ff-other-file-alist)
+                    ff-other-file-alist)
+            pathname (if (buffer-file-name)
+                         (buffer-file-name)
+                       "/none.none"))
+
+      (setq fname (file-name-nondirectory pathname)
+            no-match nil
+            match (car alist))
+
+      ;; find the table entry corresponding to this file
+      (setq pos (ff-string-match (car match) fname))
+      (while (and match (if (and pos (>= pos 0)) nil (not pos)))
+        (setq alist (cdr alist))
+        (setq match (car alist))
+        (setq pos (ff-string-match (car match) fname)))
+
+      ;; no point going on if we haven't found anything
+      (if (not match)
+          (setq no-match t)
+
+        ;; otherwise, suffixes contains what we need
+        (setq suffixes (car (cdr match))
+              action (car (cdr match))
+              found nil)
+
+        ;; if we have a function to generate new names,
+        ;; invoke it with the name of the current file
+        (if (and (atom action) (fboundp action))
+            (progn
+              (setq suffixes (funcall action (buffer-file-name))
+                    match (cons (car match) (list suffixes))
+                    stub nil
+                    default-name (car suffixes)))
+
+          ;; otherwise build our filename stub
+          (cond
+
+           ;; get around the problem that 0 and nil both mean false!
+           ((= pos 0)
+            (setq format "")
+            (setq stub "")
+            )
+
+           (t
+            (setq format (concat "\\(.+\\)" (car match)))
+            (string-match format fname)
+            (setq stub (substring fname (match-beginning 1) (match-end 1)))
+            ))
+
+          ;; if we find nothing, we should try to get a file like this one
+          (setq default-name
+                (concat stub (car (car (cdr match))))))
+
+        ;; do the real work - find the file
+        (setq found
+              (ff-get-file-name dirs stub suffixes)))))
+    found))                        ;; return buffer-name or filename
+
+(defun ff-get-file (search-dirs filename &optional suffix-list other-window)
+  "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
+If (optional) SUFFIX-LIST is nil, search for fname, otherwise search
+for fname with each of the given suffixes.  Get the file or the buffer
+corresponding to the name of the first file found, or nil."
+  (let ((filename (ff-get-file-name search-dirs filename suffix-list)))
             
-    (cond 
+    (cond
      ((not filename)
       nil)
 
-     ((bufferp (get-buffer filename))
-      (ff-switch-to-buffer filename other-window)
+     ((bufferp (get-file-buffer filename))
+      (ff-switch-to-buffer (get-file-buffer filename) other-window)
       filename)
                
      ((file-exists-p filename)
@@ -545,12 +634,10 @@ Arguments: (search-dirs fname-stub &optional suffix-list in-other-window)
       nil))))
 
 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
-  "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub). 
-If (optional) SUFFIXES is nil, search for fname, otherwise search for fname 
-with each of the given suffixes. Returns the name of the first file found.
-
-Arguments: (search-dirs fname-stub &optional suffix-list)
-"
+  "Find a file in SEARCH-DIRS with the given name (or stub) FNAME-STUB.
+If (optional) SUFFIX-LIST is nil, search for FNAME-STUB, otherwise
+search for FNAME-STUB with each of the given suffixes.  Return the
+name of the first file found."
   (let* (dirs         ;; working copy of dirs to search
          dir          ;; the current dir considered
          file         ;; filename being looked for
@@ -576,17 +663,17 @@ Arguments: (search-dirs fname-stub &optional suffix-list)
       (setq filename (concat fname-stub this-suffix))
 
       (if (not ff-quiet-mode)
-          (message "finding buffer %s..." filename))
+          (message "Finding buffer %s..." filename))
 
-      (if (bufferp (get-buffer filename))
-          (setq found filename))
+      (if (bufferp (get-file-buffer filename))
+          (setq found (buffer-file-name (get-file-buffer filename))))
 
       (setq blist (buffer-list))
       (setq buf (buffer-name (car blist)))
       (while (and blist (not found))
 
         (if (string-match (concat filename "<[0-9]+>") buf)
-            (setq found buf))
+            (setq found (buffer-file-name (car blist))))
 
         (setq blist (cdr blist))
         (setq buf (buffer-name (car blist))))
@@ -603,7 +690,7 @@ Arguments: (search-dirs fname-stub &optional suffix-list)
 
       ;; if dir does not contain '/*', look for the file
       (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir)))
-          (progn 
+          (progn
             
             ;; suffixes is nil => fname-stub is the file we are looking for
             ;; otherwise fname-stub is a stub, and we append a suffix
@@ -618,7 +705,7 @@ Arguments: (search-dirs fname-stub &optional suffix-list)
               (setq file (concat dir "/" filename))
               
               (if (not ff-quiet-mode)
-                  (message "finding %s..." file))
+                  (message "Finding %s..." file))
 
               (if (file-exists-p file)
                   (setq found file))
@@ -660,16 +747,11 @@ Arguments: (search-dirs fname-stub &optional suffix-list)
     found))
 
 (defun ff-string-match (regexp string &optional start)
-  "Like string-match (which see), but sets case-fold-search to 
-ff-case-fold-search before searching, and then resets it back again."
-  (let ((exact-match case-fold-search)
-        match)
+  "Like `string-match', but set `case-fold-search' temporarily.
+The value used comes from `ff-case-fold-search'."
+  (let ((case-fold-search ff-case-fold-search))
     (if regexp
-        (progn
-          (setq case-fold-search ff-case-fold-search)
-          (setq match (string-match regexp string start))
-          (setq case-fold-search exact-match)))
-    match))
+       (string-match regexp string start))))
 
 (defun ff-list-replace-env-vars (search-list)
   "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST."
@@ -688,9 +770,8 @@ ff-case-fold-search before searching, and then resets it back again."
     (setq search-list (reverse list))))
 
 (defun ff-treat-as-special ()
-  "Returns the file to look for if the construct was special, otherwise
-returns nil. The construct is defined in the variable ff-special-constructs 
-(which see)."
+  "Return the file to look for if the construct was special, else nil.
+The construct is defined in the variable `ff-special-constructs'."
   (let* (fname
          (list ff-special-constructs)
          (elem (car list))
@@ -706,13 +787,13 @@ returns nil. The construct is defined in the variable ff-special-constructs
     fname))
 
 (defun ff-basename (string)
-  "Returns the basename of PATHNAME."
+  "Return the basename of pathname STRING."
   (setq string (concat "/" string))
   (string-match ".*/\\([^/]+\\)$" string)
   (setq string (substring string (match-beginning 1) (match-end 1))))
 
 (defun ff-all-dirs-under (here &optional exclude)
-  "Get all the directory files under DIRECTORY.
+  "Get all the directory files under directory HERE.
 Exclude all files in the optional EXCLUDE list."
   (if (file-directory-p here)
       (condition-case nil
@@ -732,16 +813,18 @@ Exclude all files in the optional EXCLUDE list."
     nil))
 
 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file)
-  "Calls Function2 or Function1 with FILE as argument, depending on whether 
-(optional) OTHER-WINDOW is set or not. Function1 and Function2 are typically 
-find-file / find-file-other-window or switch-to-buffer / switch-to-buffer-
-other-window function pairs.
+  "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW.
+In addition, this runs various hooks.
 
-If optional NEW-FILE is t, then a special hook (ff-file-created-hooks) is 
-called before ff-post-load-hooks.
+Either F1 or F2 receives FILE as the sole argument.
+The decision of which one to call is based on IN-OTHER-WINDOW
+and on the global variable `ff-always-in-other-window'.
 
-Arguments: (function1 function2 file &optional in-other-window new-file)
-"
+F1 and F2 are typically `find-file' / `find-file-other-window'
+or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs.
+
+If optional NEW-FILE is t, then a special hook (`ff-file-created-hooks') is
+called before `ff-post-load-hooks'."
   (if ff-pre-load-hooks
       (run-hooks 'ff-pre-load-hooks))
   (if (or
@@ -756,81 +839,40 @@ Arguments: (function1 function2 file &optional in-other-window new-file)
       (run-hooks 'ff-post-load-hooks)))
 
 (defun ff-find-file (file &optional in-other-window new-file)
-  "Like find-file (which see), but checks whether the file goes in another 
-window or not.
-
-Arguments: (file &optional in-other-window new-file)
-"
-  (ff-switch-file 'find-file 
-                  'find-file-other-window 
+  "Like `find-file', but may show the file in another window."
+  (ff-switch-file 'find-file
+                  'find-file-other-window
                   file in-other-window new-file))
 
-(defun ff-switch-to-buffer (file &optional in-other-window)
-  "Like switch-to-buffer (which see), but checks whether the buffer ends up 
-in another window or not.
-
-Arguments: (file &optional in-other-window)
-"
-  (ff-switch-file 'switch-to-buffer 
-                  'switch-to-buffer-other-window 
-                  file in-other-window nil))
-
-(cond 
- ((ff-gnu-emacs-19)
-  (defun ff-goto-click (event)
-    (set-buffer (window-buffer (posn-window (event-end event))))
-    (goto-char (posn-point (event-end event))))
-
-  ;;;###autoload
-  (defun ff-mouse-find-other-file (event)
-    "Visit the file you click on."
-    (interactive "e")
-    (save-excursion
-      (ff-goto-click event)
-      (ff-find-other-file nil)))
+(defun ff-switch-to-buffer (buffer-or-name &optional in-other-window)
+  "Like `switch-to-buffer', but may show the buffer in another window."
 
-  ;;;###autoload
-  (defun ff-mouse-find-other-file-other-window (event)
-    "Visit the file you click on."
-    (interactive "e")
-    (save-excursion
-      (ff-goto-click event)
-      (ff-find-other-file t)))
-
-  ;;;###autoload
-  (defun locate-file (fname dirs &optional suffix-list ignore-perms)
-    "Defines XEmacs look-alike locate-file for GNU Emacs-19."
-    (interactive)
-    (ff-get-file dirs fname suffix-list)) 
-  )
-
- ((ff-xemacs)
-
-  ;;;###autoload
-  (defun ff-mouse-find-other-file (event)
-    "Visit the file you click on."
-    (interactive "@e")
-    (save-excursion
-      (mouse-set-point event)
-      (ff-find-other-file nil)))
+  (ff-switch-file 'switch-to-buffer
+                  'switch-to-buffer-other-window
+                  buffer-or-name in-other-window nil))
 
-  ;;;###autoload
-  (defun ff-mouse-find-other-file-other-window (event)
-    "Visit the file you click on."
-    (interactive "@e")
-    (save-excursion
-      (mouse-set-point event)
-      (ff-find-other-file t))) 
-  ))
+;;;###autoload
+(defun ff-mouse-find-other-file (event)
+  "Visit the file you click on."
+  (interactive "e")
+  (save-excursion
+    (mouse-set-point event)
+    (ff-find-other-file nil)))
 
-(provide 'find-file)
+;;;###autoload
+(defun ff-mouse-find-other-file-other-window (event)
+  "Visit the file you click on in another window."
+  (interactive "e")
+  (save-excursion
+    (mouse-set-point event)
+    (ff-find-other-file t)))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; This section offers an example of user defined function to select files
 
-(defun upcase-p (string &optional start end)
-  "Return t if this string is all uppercase. Given START and/or END,
-checks between these characters."
+(defun ff-upcase-p (string &optional start end)
+  "Return t if STRING is all uppercase.
+Given START and/or END, checks between these characters."
   (let (match str)
     (if (not start)
         (setq start 0))
@@ -839,7 +881,7 @@ checks between these characters."
     (if (= start end)
         (setq end (1+ end)))
     (setq str (substring string start end))
-    (if (and 
+    (if (and
          (ff-string-match "[A-Z]+" str)
          (setq match (match-data))
          (= (car match) 0)
@@ -848,22 +890,22 @@ checks between these characters."
       nil)))
 
 (defun ff-cc-hh-converter (arg)
-  "Discriminate file extensions and build up a new file list based 
-possibly on part of the directory name and the name of the file 
-passed in."
+  "Discriminate file extensions.
+Build up a new file list based possibly on part of the directory name
+and the name of the file passed in."
   (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg)
-  (let ((path (if (match-beginning 1) 
+  (let ((path (if (match-beginning 1)
                   (substring arg (match-beginning 1) (match-end 1)) nil))
-        (dire (if (match-beginning 2) 
+        (dire (if (match-beginning 2)
                   (substring arg (match-beginning 2) (match-end 2)) nil))
-        (file (if (match-beginning 3) 
+        (file (if (match-beginning 3)
                   (substring arg (match-beginning 3) (match-end 3)) nil))
-        (extn (if (match-beginning 4) 
+        (extn (if (match-beginning 4)
                   (substring arg (match-beginning 4) (match-end 4)) nil))
         return-list)
     (cond
      ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h}
-     ((and (string= extn "cc") 
+     ((and (string= extn "cc")
            (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file))
       (let ((stub  (substring file (match-beginning 2) (match-end 2))))
         (setq dire (upcase (substring file (match-beginning 1) (match-end 1))))
@@ -873,9 +915,9 @@ passed in."
                                 (concat file ".h")))
         ))
      ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C}
-     ((and (string= extn "hh") (upcase-p dire) file)
+     ((and (string= extn "hh") (ff-upcase-p dire) file)
       (let ((stub (concat (downcase dire) file)))
-        (setq return-list (list (concat stub ".cc")           
+        (setq return-list (list (concat stub ".cc")
                                 (concat stub ".C")
                                 (concat file ".cc")
                                 (concat file ".C")))
@@ -892,7 +934,7 @@ passed in."
         (setq return-list (list (concat stub ".cc")
                                 (concat stub ".C")))
         ))
-     (t 
+     (t
       nil))
     
     return-list))
@@ -903,14 +945,13 @@ passed in."
 
 (defvar ff-function-name nil "Name of the function we are in.")
 
-(defvar ada-procedure-start-regexp)
-(defvar ada-package-start-regexp)
+(eval-when-compile (require 'ada-mode))
 
 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in)
 ;;
 (defun ff-which-function-are-we-in ()
-  "Determine whether we are on a function definition/declaration and 
-remember the name of that function."
+  "Return the name of the function whose definition/declaration point is in.
+Also remember that name in `ff-function-name'."
 
   (setq ff-function-name nil)
 
@@ -927,12 +968,13 @@ remember the name of that function."
 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly)
 ;;
 (defun ff-set-point-accordingly ()
-  "Find the function specified in ff-function-name, previously 
-determined by ff-which-function-are-we-in."
+  "Find the function specified in `ff-function-name'.
+That name was previously determined by `ff-which-function-are-we-in'."
   (if ff-function-name
       (progn
         (goto-char (point-min))
         (search-forward ff-function-name nil t))))
 
-;; find-file.el ends here
+(provide 'find-file)
 
+;;; find-file.el ends here