(r_alloc_reinit): New function.
[emacs.git] / lisp / find-file.el
blob44aa85e5699a2eb3250c10cdd8e93f4879e87e5a
1 ;;; find-file.el --- find a file corresponding to this one given a pattern
3 ;; Author: Henry Guillaume <henri@tibco.com, henry@c032.aone.net.au>
4 ;; 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 modula2-other-file-alist
244 ("\\.mi$" (".md")) ;; Modula-2 module definition
245 ("\\.md$" (".mi")) ;; and implementation.
247 "*See the description for the `ff-search-directories' variable.")
249 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
250 ;; No user definable variables beyond this point!
251 ;; ==============================================
253 (make-variable-buffer-local 'ff-pre-find-hooks)
254 (make-variable-buffer-local 'ff-pre-load-hooks)
255 (make-variable-buffer-local 'ff-post-load-hooks)
256 (make-variable-buffer-local 'ff-not-found-hooks)
257 (make-variable-buffer-local 'ff-file-created-hooks)
258 (make-variable-buffer-local 'ff-case-fold-search)
259 (make-variable-buffer-local 'ff-always-in-other-window)
260 (make-variable-buffer-local 'ff-ignore-include)
261 (make-variable-buffer-local 'ff-quiet-mode)
262 (make-variable-buffer-local 'ff-other-file-alist)
263 (make-variable-buffer-local 'ff-search-directories)
265 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 ;; User entry points
268 ;;;###autoload
269 (defun ff-get-other-file (&optional in-other-window)
270 "Find the header or source file corresponding to this file.
271 See also the documentation for `ff-find-other-file;.
273 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
274 (interactive "P")
275 (let ((ignore ff-ignore-include))
276 (setq ff-ignore-include t)
277 (ff-find-the-other-file in-other-window)
278 (setq ff-ignore-include ignore)))
280 ;;;###autoload
281 (defun ff-find-other-file (&optional in-other-window ignore-include)
282 "Find the header or source file corresponding to this file.
283 Being on a `#include' line pulls in that file.
285 If optional IN-OTHER-WINDOW is non-nil, find the file in the other window.
286 If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines.
288 Variables of interest include:
290 - ff-case-fold-search
291 Non-nil means ignore cases in matches (see case-fold-search).
292 If you have extensions in different cases, you will want this to be nil.
294 - ff-always-in-other-window
295 If non-nil, always open the other file in another window, unless an
296 argument is given to ff-find-other-file.
298 - ff-ignore-include
299 If non-nil, ignores #include lines.
301 - ff-always-try-to-create
302 If non-nil, always attempt to create the other file if it was not found.
304 - ff-quiet-mode
305 If non-nil, traces which directories are being searched.
307 - ff-special-constructs
308 A list of regular expressions specifying how to recognise special
309 constructs such as include files etc, and an associated method for
310 extracting the filename from that construct.
312 - ff-other-file-alist
313 Alist of extensions to find given the current file's extension.
315 - ff-search-directories
316 List of directories searched through with each extension specified in
317 ff-other-file-alist that matches this file's extension.
319 - ff-pre-find-hooks
320 List of functions to be called before the search for the file starts.
322 - ff-pre-load-hooks
323 List of functions to be called before the other file is loaded.
325 - ff-post-load-hooks
326 List of functions to be called after the other file is loaded.
328 - ff-not-found-hooks
329 List of functions to be called if the other file could not be found.
331 - ff-file-created-hooks
332 List of functions to be called if the other file has been created."
333 (interactive "P")
334 (let ((ignore ff-ignore-include))
335 (setq ff-ignore-include ignore-include)
336 (ff-find-the-other-file in-other-window)
337 (setq ff-ignore-include ignore)))
339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
340 ;; Support functions
342 (defun ff-emacs-19 ()
343 (string-match "^19\\.[0-9]+\\.[0-9]+$" emacs-version))
345 (defun ff-xemacs ()
346 (or (string-match "Lucid" emacs-version)
347 (string-match "XEmacs" emacs-version)))
349 (defun ff-find-the-other-file (&optional in-other-window)
350 "Find the header or source file corresponding to the current file.
351 Being on a `#include' line pulls in that file, but see the help on
352 the `ff-ignore-include' variable.
354 If optional IN-OTHER-WINDOW is non-nil, find the file in another window."
356 (let (match ;; matching regexp for this file
357 suffixes ;; set of replacing regexps for the matching regexp
358 action ;; function to generate the names of the other files
359 fname ;; basename of this file
360 pos ;; where we start matching filenames
361 stub ;; name of the file without extension
362 alist ;; working copy of the list of file extensions
363 pathname ;; the pathname of the file or the #include line
364 default-name ;; file we should create if none found
365 format ;; what we have to match
366 found ;; name of the file or buffer found - nil if none
367 dirs ;; local value of ff-search-directories
368 no-match) ;; whether we know about this kind of file
370 (if ff-pre-find-hooks
371 (run-hooks 'ff-pre-find-hooks))
373 (message "Working...")
375 (setq dirs
376 (if (symbolp ff-search-directories)
377 (ff-list-replace-env-vars (symbol-value ff-search-directories))
378 (ff-list-replace-env-vars ff-search-directories)))
380 (save-excursion
381 (beginning-of-line 1)
382 (setq fname (ff-treat-as-special)))
384 (cond
385 ((and (not ff-ignore-include) fname)
386 (setq default-name fname)
387 (setq found (ff-get-file dirs fname nil in-other-window)))
389 ;; let's just get the corresponding file
391 (setq alist (if (symbolp ff-other-file-alist)
392 (symbol-value ff-other-file-alist)
393 ff-other-file-alist)
394 pathname (if (buffer-file-name)
395 (buffer-file-name)
396 "/none.none"))
398 (string-match ".*/\\(.+\\)$" pathname)
399 (setq fname (substring pathname (match-beginning 1) (match-end 1))
400 no-match nil
401 match (car alist))
403 ;; find the table entry corresponding to this file
404 (setq pos (ff-string-match (car match) fname))
405 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
406 (setq alist (cdr alist))
407 (setq match (car alist))
408 (setq pos (ff-string-match (car match) fname)))
410 ;; no point going on if we haven't found anything
411 (if (not match)
412 (setq no-match t)
414 ;; otherwise, suffixes contains what we need
415 (setq suffixes (car (cdr match))
416 action (car (cdr match))
417 found nil)
419 ;; if we have a function to generate new names,
420 ;; invoke it with the name of the current file
421 (if (and (atom action) (fboundp action))
422 (progn
423 (setq suffixes (funcall action (buffer-file-name))
424 match (cons (car match) (list suffixes))
425 stub nil
426 default-name (car suffixes)))
428 ;; otherwise build our filename stub
429 (cond
431 ;; get around the problem that 0 and nil both mean false!
432 ((= pos 0)
433 (setq format "")
434 (setq stub "")
438 (setq format (concat "\\(.+\\)" (car match)))
439 (string-match format fname)
440 (setq stub (substring fname (match-beginning 1) (match-end 1)))
443 ;; if we find nothing, we should try to get a file like this one
444 (setq default-name
445 (concat stub (car (car (cdr match))))))
447 ;; do the real work - find the file
448 (setq found
449 (ff-get-file dirs
450 stub
451 suffixes
452 in-other-window)))))
454 (cond
455 (no-match ;; could not even determine the other file
456 (message ""))
459 (cond
461 ((not found) ;; could not find the other file
463 (if ff-not-found-hooks ;; run the hooks
464 (run-hooks 'ff-not-found-hooks))
466 (cond
467 (ff-always-try-to-create ;; try to create the file
468 (let (name pathname)
470 (setq name
471 (expand-file-name
472 (read-file-name
473 (format "Find or create %s in: " default-name)
474 default-directory default-name nil)))
476 (setq pathname
477 (if (file-directory-p name)
478 (concat (file-name-as-directory name) default-name)
479 (setq found name)))
481 (ff-find-file pathname in-other-window t)))
483 (t ;; don't create the file, just whinge
484 (message "No file found for %s" fname))))
486 (t ;; matching file found
487 nil))))
489 found)) ;; return buffer-name or filename
491 (defun ff-get-file (search-dirs fname-stub &optional suffix-list other-window)
492 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
493 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
494 with each of the given suffixes. Gets the file or the buffer corresponding
495 to the name of the first file found, or nil.
497 Arguments: (search-dirs fname-stub &optional suffix-list in-other-window)
499 (let ((filename (ff-get-file-name search-dirs fname-stub suffix-list)))
501 (cond
502 ((not filename)
503 nil)
505 ((bufferp (get-file-buffer filename))
506 (ff-switch-to-buffer (get-file-buffer filename) other-window)
507 filename)
509 ((file-exists-p filename)
510 (ff-find-file filename other-window nil)
511 filename)
514 nil))))
516 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
517 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
518 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
519 with each of the given suffixes. Returns the name of the first file found.
521 Arguments: (search-dirs fname-stub &optional suffix-list)
523 (let* (dirs ;; working copy of dirs to search
524 dir ;; the current dir considered
525 file ;; filename being looked for
526 rest ;; pathname after first /*
527 this-suffix ;; the suffix we are currently considering
528 suffixes ;; working copy of suffix-list
529 filename ;; built filename
530 blist ;; list of live buffers
531 buf ;; current buffer in blist
532 found) ;; whether we have found anything
534 (setq suffixes suffix-list)
536 ;; suffixes is nil => fname-stub is the file we are looking for
537 ;; otherwise fname-stub is a stub, and we append a suffix
538 (if suffixes
539 (setq this-suffix (car suffixes))
540 (setq this-suffix "")
541 (setq suffixes (list "")))
543 ;; find whether the file is in a buffer first
544 (while (and suffixes (not found))
545 (setq filename (concat fname-stub this-suffix))
547 (if (not ff-quiet-mode)
548 (message "Finding buffer %s..." filename))
550 (if (bufferp (get-file-buffer filename))
551 (setq found (buffer-file-name (get-file-buffer filename))))
553 (setq blist (buffer-list))
554 (setq buf (buffer-name (car blist)))
555 (while (and blist (not found))
557 (if (string-match (concat filename "<[0-9]+>") buf)
558 (setq found (buffer-file-name (car blist))))
560 (setq blist (cdr blist))
561 (setq buf (buffer-name (car blist))))
563 (setq suffixes (cdr suffixes))
564 (setq this-suffix (car suffixes)))
566 ;; now look for the real file
567 (setq dirs search-dirs)
568 (setq dir (car dirs))
569 (while (and (not found) dirs)
571 (setq suffixes suffix-list)
573 ;; if dir does not contain '/*', look for the file
574 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir)))
575 (progn
577 ;; suffixes is nil => fname-stub is the file we are looking for
578 ;; otherwise fname-stub is a stub, and we append a suffix
579 (if suffixes
580 (setq this-suffix (car suffixes))
581 (setq this-suffix "")
582 (setq suffixes (list "")))
584 (while (and suffixes (not found))
586 (setq filename (concat fname-stub this-suffix))
587 (setq file (concat dir "/" filename))
589 (if (not ff-quiet-mode)
590 (message "Finding %s..." file))
592 (if (file-exists-p file)
593 (setq found file))
595 (setq suffixes (cdr suffixes))
596 (setq this-suffix (car suffixes))))
598 ;; otherwise dir matches the '/*', so search each dir separately
599 (progn
600 (if (match-beginning 2)
601 (setq rest (substring dir (match-beginning 2) (match-end 2)))
602 (setq rest "")
604 (setq dir (substring dir (match-beginning 1) (match-end 1)))
606 (let ((dirlist (ff-all-dirs-under dir '("..")))
607 this-dir compl-dirs)
609 (setq this-dir (car dirlist))
610 (while dirlist
611 (setq compl-dirs
612 (append
613 compl-dirs
614 (list (concat this-dir rest))
616 (setq dirlist (cdr dirlist))
617 (setq this-dir (car dirlist)))
619 (if compl-dirs
620 (setq found (ff-get-file-name compl-dirs
621 fname-stub
622 suffix-list))))))
623 (setq dirs (cdr dirs))
624 (setq dir (car dirs)))
626 (if found
627 (message "%s found" found))
629 found))
631 (defun ff-string-match (regexp string &optional start)
632 "Like `string-match', but set `case-fold-search' temporarily.
633 The value used comes from `ff-case-fold-search'."
634 (let ((case-fold-search ff-case-fold-search))
635 (if regexp
636 (string-match regexp string start))))
638 (defun ff-list-replace-env-vars (search-list)
639 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST."
640 (let (list
641 (var (car search-list)))
642 (while search-list
643 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var)
644 (setq var
645 (concat
646 (substring var (match-beginning 1) (match-end 1))
647 (getenv (substring var (match-beginning 2) (match-end 2)))
648 (substring var (match-beginning 3) (match-end 3)))))
649 (setq search-list (cdr search-list))
650 (setq list (cons var list))
651 (setq var (car search-list)))
652 (setq search-list (reverse list))))
654 (defun ff-treat-as-special ()
655 "Returns the file to look for if the construct was special, else nil.
656 The construct is defined in the variable `ff-special-constructs'."
657 (let* (fname
658 (list ff-special-constructs)
659 (elem (car list))
660 (regexp (car elem))
661 (match (cdr elem)))
662 (while (and list (not fname))
663 (if (and (looking-at regexp) match)
664 (setq fname (funcall match)))
665 (setq list (cdr list))
666 (setq elem (car list))
667 (setq regexp (car elem))
668 (setq match (cdr elem)))
669 fname))
671 (defun ff-basename (string)
672 "Return the basename of PATHNAME."
673 (setq string (concat "/" string))
674 (string-match ".*/\\([^/]+\\)$" string)
675 (setq string (substring string (match-beginning 1) (match-end 1))))
677 (defun ff-all-dirs-under (here &optional exclude)
678 "Get all the directory files under directory HERE.
679 Exclude all files in the optional EXCLUDE list."
680 (if (file-directory-p here)
681 (condition-case nil
682 (progn
683 (let ((files (directory-files here t))
684 (dirlist (list))
685 file)
686 (while files
687 (setq file (car files))
688 (if (and
689 (file-directory-p file)
690 (not (member (ff-basename file) exclude)))
691 (setq dirlist (cons file dirlist)))
692 (setq files (cdr files)))
693 (setq dirlist (reverse dirlist))))
694 (error nil))
695 nil))
697 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file)
698 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW.
699 In addition, this runs various hooks.
701 Either F1 or F2 receives FILE as the sole argument.
702 The decision of which one to call is based on IN-OTHER-WINDOW
703 and on the global variable `ff-always-in-other-window'.
705 F1 and F2 are typically `find-file' / `find-file-other-window'
706 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs.
708 If optional NEW-FILE is t, then a special hook (`ff-file-created-hooks') is
709 called before `ff-post-load-hooks'."
710 (if ff-pre-load-hooks
711 (run-hooks 'ff-pre-load-hooks))
712 (if (or
713 (and in-other-window (not ff-always-in-other-window))
714 (and (not in-other-window) ff-always-in-other-window))
715 (funcall f2 file)
716 (funcall f1 file))
717 (if new-file
718 (if ff-file-created-hooks
719 (run-hooks 'ff-file-created-hooks)))
720 (if ff-post-load-hooks
721 (run-hooks 'ff-post-load-hooks)))
723 (defun ff-find-file (file &optional in-other-window new-file)
724 "Like `find-file', but may show the file in another window."
725 (ff-switch-file 'find-file
726 'find-file-other-window
727 file in-other-window new-file))
729 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window)
730 "Like `switch-to-buffer', but may show the buffer in another window."
732 (ff-switch-file 'switch-to-buffer
733 'switch-to-buffer-other-window
734 buffer-or-name in-other-window nil))
736 (cond
737 ((ff-emacs-19)
738 (defun ff-goto-click (event)
739 (set-buffer (window-buffer (posn-window (event-end event))))
740 (goto-char (posn-point (event-end event))))
742 ;;;###autoload
743 (defun ff-mouse-find-other-file (event)
744 "Visit the file you click on."
745 (interactive "e")
746 (save-excursion
747 (ff-goto-click event)
748 (ff-find-other-file nil)))
750 ;;;###autoload
751 (defun ff-mouse-find-other-file-other-window (event)
752 "Visit the file you click on."
753 (interactive "e")
754 (save-excursion
755 (ff-goto-click event)
756 (ff-find-other-file t)))
758 ;;;###autoload
759 (defun locate-file (fname dirs &optional suffix-list ignore-perms)
760 "Defines XEmacs look-alike locate-file for GNU Emacs-19."
761 (interactive)
762 (ff-get-file dirs fname suffix-list))
765 ((ff-xemacs)
767 ;;;###autoload
768 (defun ff-mouse-find-other-file (event)
769 "Visit the file you click on."
770 (interactive "@e")
771 (save-excursion
772 (mouse-set-point event)
773 (ff-find-other-file nil)))
775 ;;;###autoload
776 (defun ff-mouse-find-other-file-other-window (event)
777 "Visit the file you click on."
778 (interactive "@e")
779 (save-excursion
780 (mouse-set-point event)
781 (ff-find-other-file t)))
784 (provide 'find-file)
786 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
787 ;; This section offers an example of user defined function to select files
789 (defun ff-upcase-p (string &optional start end)
790 "Return t if this string is all uppercase.
791 Given START and/or END, checks between these characters."
792 (let (match str)
793 (if (not start)
794 (setq start 0))
795 (if (not end)
796 (setq end (length string)))
797 (if (= start end)
798 (setq end (1+ end)))
799 (setq str (substring string start end))
800 (if (and
801 (ff-string-match "[A-Z]+" str)
802 (setq match (match-data))
803 (= (car match) 0)
804 (= (car (cdr match)) (length str)))
806 nil)))
808 (defun ff-cc-hh-converter (arg)
809 "Discriminate file extensions.
810 Build up a new file list based possibly on part of the directory name
811 and the name of the file passed in."
812 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg)
813 (let ((path (if (match-beginning 1)
814 (substring arg (match-beginning 1) (match-end 1)) nil))
815 (dire (if (match-beginning 2)
816 (substring arg (match-beginning 2) (match-end 2)) nil))
817 (file (if (match-beginning 3)
818 (substring arg (match-beginning 3) (match-end 3)) nil))
819 (extn (if (match-beginning 4)
820 (substring arg (match-beginning 4) (match-end 4)) nil))
821 return-list)
822 (cond
823 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h}
824 ((and (string= extn "cc")
825 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file))
826 (let ((stub (substring file (match-beginning 2) (match-end 2))))
827 (setq dire (upcase (substring file (match-beginning 1) (match-end 1))))
828 (setq return-list (list (concat stub ".hh")
829 (concat stub ".h")
830 (concat file ".hh")
831 (concat file ".h")))
833 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C}
834 ((and (string= extn "hh") (ff-upcase-p dire) file)
835 (let ((stub (concat (downcase dire) file)))
836 (setq return-list (list (concat stub ".cc")
837 (concat stub ".C")
838 (concat file ".cc")
839 (concat file ".C")))
841 ;; zap.cc => zap.hh or zap.h
842 ((string= extn "cc")
843 (let ((stub file))
844 (setq return-list (list (concat stub ".hh")
845 (concat stub ".h")))
847 ;; zap.hh => zap.cc or zap.C
848 ((string= extn "hh")
849 (let ((stub file))
850 (setq return-list (list (concat stub ".cc")
851 (concat stub ".C")))
854 nil))
856 return-list))
858 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
859 ;; This section offers an example of user defined function to place point.
860 ;; The regexps are Ada specific.
862 (defvar ff-function-name nil "Name of the function we are in.")
864 (eval-when-compile (require 'ada-mode))
866 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in)
868 (defun ff-which-function-are-we-in ()
869 "Return the name of the function whose definition/declaration point is in.
870 Also remember that name in `ff-function-name'."
872 (setq ff-function-name nil)
874 (save-excursion
875 (if (re-search-backward ada-procedure-start-regexp nil t)
876 (setq ff-function-name (buffer-substring (match-beginning 0)
877 (match-end 0)))
878 ; we didn't find a procedure start, perhaps there is a package
879 (if (re-search-backward ada-package-start-regexp nil t)
880 (setq ff-function-name (buffer-substring (match-beginning 0)
881 (match-end 0)))
882 ))))
884 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly)
886 (defun ff-set-point-accordingly ()
887 "Find the function specified in `ff-function-name'.
888 That name was previously determined by `ff-which-function-are-we-in'."
889 (if ff-function-name
890 (progn
891 (goto-char (point-min))
892 (search-forward ff-function-name nil t))))
894 ;; find-file.el ends here