(print): Handle internal display-local object.
[emacs.git] / lisp / find-file.el
blobf4135efa4d0bba052d134898511b4d0097c64543
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 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
22 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Commentary:
26 ;; PURPOSE:
27 ;; This package features a function called ff-find-other-file, which performs
28 ;; the following function:
30 ;; When in a .c file, find the first corresponding .h file in a set
31 ;; of directories and display it, and vice-versa from the .h file.
33 ;; Many people maintain their include file in a directory separate to their
34 ;; src directory, and very often you may be editing a file and have a need to
35 ;; visit the "other file". This package searches through a set of directories
36 ;; to find that file.
38 ;; THE "OTHER FILE", or "corresponding file", generally has the same basename,
39 ;; and just has a different extension as described by the ff-other-file-alist
40 ;; variable:
42 ;; '(("\\.cc$" (".hh" ".h"))
43 ;; ("\\.hh$" (".cc" ".C" ".CC" ".cxx" ".cpp")))
45 ;; If the current file has a .cc extension, ff-find-other-file will attempt
46 ;; to look for a .hh file, and then a .h file in some directory as described
47 ;; below. The mechanism here is to replace the matched part of the original
48 ;; filename with each of the corresponding extensions in turn.
50 ;; Alternatively, there are situations where the filename of the other file
51 ;; cannot be determined easily with regexps. For example, a .c file may
52 ;; have two corresponding .h files, for its public and private parts, or
53 ;; the filename for the .c file contains part of the pathname of the .h
54 ;; file, as between src/fooZap.cc and include/FOO/zap.hh. In that case, the
55 ;; format above can be changed to include a function to be called when the
56 ;; current file matches the regexp:
58 ;; '(("\\.cc$" cc-function)
59 ;; ("\\.hh$" hh-function))
61 ;; These functions must return a list consisting of the possible names of the
62 ;; corresponding file, with or without path. There is no real need for more
63 ;; than one function, and one could imagine the following value for cc-other-
64 ;; file-alist:
66 ;; (setq cc-other-file-alist
67 ;; '(("\\.cc$" ff-cc-hh-converter)
68 ;; ("\\.hh$" ff-cc-hh-converter)
69 ;; ("\\.c$" (".h"))
70 ;; ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp"))))
71 ;;
72 ;; ff-cc-hh-converter is included at the end of this file as a reference.
73 ;;
74 ;; SEARCHING is carried out in a set of directories specified by the
75 ;; ff-search-directories variable:
77 ;; ("." "../../src" "../include/*" "/usr/local/*/src/*" "$PROJECT/src")
79 ;; This means that the corresponding file will be searched for first in
80 ;; the current directory, then in ../../src, then in one of the directories
81 ;; under ../include, and so on. The star is _not_ a general wildcard
82 ;; character: it just indicates that the subdirectories of this directory
83 ;; must each be searched in turn. Environment variables will be expanded in
84 ;; the ff-search-directories variable.
86 ;; If the point is on a #include line, the file to be #included is searched
87 ;; for in the same manner. This can be disabled with the ff-ignore-include
88 ;; variable, or by calling ff-get-other-file instead of ff-find-other-file.
90 ;; If the file was not found, ff-find-other-file will prompt you for where
91 ;; to create the new "corresponding file" (defaults to the current directory),
92 ;; unless the variable ff-always-try-to-create is set to nil.
94 ;; GIVEN AN ARGUMENT (with the ^U prefix), ff-find-other-file will get the
95 ;; other file in another (the other?) window (see find-file-other-window and
96 ;; switch-to-buffer-other-window). This can be set on a more permanent basis
97 ;; by setting ff-always-in-other-window to t in which case the ^U prefix will
98 ;; do the opposite of what was described above.
100 ;; THERE ARE FIVE AVAILABLE HOOKS, called in this order if non-nil:
102 ;; - ff-pre-find-hooks - called before the search for the other file starts
103 ;; - ff-not-found-hooks - called when the other file could not be found
104 ;; - ff-pre-load-hooks - called just before the other file is 'loaded'
105 ;; - ff-file-created-hooks - called when the other file is created
106 ;; - ff-post-load-hooks - called just after the other file is 'loaded'
108 ;; The *load-hooks allow you to place point where you want it in the other
109 ;; file.
111 ;; LCD Archive Entry:
112 ;; find-file|Henry Guillaume|henry@qbd.com.au|
113 ;; Find a file associated with this buffer.|
114 ;; 21-Dec-1994|4.0|~/misc/find-file.el.Z|
116 ;; FEEDBACK:
117 ;; Please send me bug reports, bug fixes, and extensions, so that I can
118 ;; merge them into the master source.
120 ;; CREDITS:
121 ;; Many thanks go to TUSC Computer Systems Pty Ltd for providing an environ-
122 ;; ment that made the development of this package possible.
124 ;; Many thanks also go to all those who provided valuable feedback throughout
125 ;; the development of this package:
126 ;; Rolf Ebert in particular, Fritz Knabe, Heddy Boubaker, Sebastian Kremer,
127 ;; Vasco Lopes Paulo, Mark A. Plaksin, Robert Lang, Trevor West, Kevin
128 ;; Pereira & Benedict Lofstedt.
130 ;; Code:
131 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 ;; User definable variables:
134 (defvar ff-pre-find-hooks nil
135 "*List of functions to be called before the search for the file starts.")
137 (defvar ff-pre-load-hooks nil
138 "*List of functions to be called before the other file is loaded.")
140 (defvar ff-post-load-hooks nil
141 "*List of functions to be called after the other file is loaded.")
143 (defvar ff-not-found-hooks nil
144 "*List of functions to be called if the other file could not be found.")
146 (defvar ff-file-created-hooks nil
147 "*List of functions to be called if the other file needs to be created.")
149 (defvar ff-case-fold-search nil
150 "*Non-nil means ignore cases in matches (see case-fold-search).
151 If you have extensions in different cases, you will want this to be nil.")
153 (defvar ff-always-in-other-window nil
154 "*If non-nil, always open the other file in another window, unless an
155 argument is given to ff-find-other-file.")
157 (defvar ff-ignore-include nil
158 "*If non-nil, ignores include lines.")
160 (defvar ff-always-try-to-create t
161 "*If non-nil, always attempt to create the other file if it was not found.")
163 (defvar ff-quiet-mode nil
164 "*If non-nil, traces which directories are being searched.")
166 (defvar ff-special-constructs
168 ;; C/C++ include, for NeXTSTEP too
169 ("^\#\\s *\\(include\\|import\\)\\s +[<\"]\\(.*\\)[>\"]" .
170 (lambda ()
171 (setq fname (buffer-substring (match-beginning 2) (match-end 2)))))
173 ;; Ada import
174 ("^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)" .
175 (lambda ()
176 (setq fname (buffer-substring (match-beginning 1) (match-end 1)))
177 (setq fname (concat (ada-make-filename-from-adaname fname)
178 ada-spec-suffix))))
180 "*A list of regular expressions specifying how to recognise special
181 constructs such as include files etc, and an associated method for
182 extracting the filename from that construct.")
184 (defvar ff-other-file-alist 'cc-other-file-alist
185 "*Alist of extensions to find given the current file's extension.
187 This list should contain the most used extensions before the others,
188 since the search algorithm searches sequentially through each
189 directory specified in ff-search-directories. If a file is not found,
190 a new one is created with the first matching extension (.cc yields .hh).
191 This alist should be set by the major-mode.")
193 (defvar ff-search-directories 'cc-search-directories
194 "*List of directories to search for a specific file.
196 Set by default to 'cc-search-directories, expanded at run-time.
198 This list is searched through with each extension specified in
199 ff-other-file-alist that matches this file's extension. So the
200 longer the list, the longer it'll take to realise that a file
201 may not exist.
203 A typical format is
205 '(\".\" \"/usr/include/*\" \"$PROJECT/*/include\")
207 Environment variables can be inserted between slashes ('/').
208 They will be replaced by their definition. If a variable does
209 not exist, it will (silently) be replaced with an empty string.
211 The stars are _not_ wildcards: they are searched for together with
212 the preceding slash. The star represents all the subdirectories except
213 '..', and each of these subdirectories will be searched in turn.")
215 (defvar cc-search-directories
216 '("." "/usr/include/*" "/usr/local/include/*")
217 "*See the description of the ff-search-directories variable.")
219 (defvar cc-other-file-alist
221 ("\\.cc$" (".hh" ".h"))
222 ("\\.hh$" (".cc" ".C"))
224 ("\\.c$" (".h"))
225 ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp"))
227 ("\\.C$" (".H" ".hh" ".h"))
228 ("\\.H$" (".C" ".CC"))
230 ("\\.CC$" (".HH" ".H" ".hh" ".h"))
231 ("\\.HH$" (".CC"))
233 ("\\.cxx$" (".hh" ".h"))
234 ("\\.cpp$" (".hh" ".h"))
236 "*Alist of extensions to find given the current file's extension.
238 This list should contain the most used extensions before the others,
239 since the search algorithm searches sequentially through each directory
240 specified in ff-search-directories. If a file is not found, a new one
241 is created with the first matching extension (.cc yields .hh).")
243 (defvar ada-search-directories
244 '("." "/usr/adainclude" "/usr/local/adainclude")
245 "*See the description for the ff-search-directories variable.")
247 (defvar ada-other-file-alist
249 ("\\.ads$" (".adb")) ;; Ada specs and bodies
250 ("\\.adb$" (".ads")) ;; GNAT filename conventions
252 "*Alist of extensions to find given the current file's extension.
254 This list should contain the most used extensions before the others,
255 since the search algorithm searches sequentially through each directory
256 specified in ada-search-directories. If a file is not found, a new one
257 is created with the first matching extension (.adb yields .ads).
260 ;;;### autoload
261 (autoload 'ada-make-filename-from-adaname "ada-mode"
262 "Determine the filename of a package/procedure from its own Ada name.")
263 (defvar ada-spec-suffix ".ads"
264 "*Suffix of Ada specification files.")
265 (make-variable-buffer-local 'ada-spec-suffix)
267 (defvar modula2-other-file-alist
269 ("\\.mi$" (".md")) ;; Modula-2 module definition
270 ("\\.md$" (".mi")) ;; and implementation.
272 "*See the description for the ff-search-directories variable.")
274 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
275 ;; No user definable variables beyond this point!
276 ;; ==============================================
278 (make-variable-buffer-local 'ff-pre-find-hooks)
279 (make-variable-buffer-local 'ff-pre-load-hooks)
280 (make-variable-buffer-local 'ff-post-load-hooks)
281 (make-variable-buffer-local 'ff-not-found-hooks)
282 (make-variable-buffer-local 'ff-file-created-hooks)
283 (make-variable-buffer-local 'ff-case-fold-search)
284 (make-variable-buffer-local 'ff-always-in-other-window)
285 (make-variable-buffer-local 'ff-ignore-include)
286 (make-variable-buffer-local 'ff-quiet-mode)
287 (make-variable-buffer-local 'ff-other-file-alist)
288 (make-variable-buffer-local 'ff-search-directories)
290 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
291 ;; User entry points
293 ;;;###autoload
294 (defun ff-get-other-file (&optional in-other-window)
295 "Find the corresponding header or source file to this source or header
296 file. See also the documentation for ff-find-other-file.
298 If optional IN-OTHER-WINDOW is non-nil, finds the file in another window.
300 Arguments: (&optional in-other-window)"
301 (interactive "P")
302 (let ((ignore ff-ignore-include))
303 (setq ff-ignore-include t)
304 (ff-find-the-other-file in-other-window)
305 (setq ff-ignore-include ignore)))
307 ;;;###autoload
308 (defun ff-find-other-file (&optional in-other-window ignore-include)
309 "Find the corresponding header or source file to this source or header
310 file; being on a #include line pulls in that file.
312 If optional IN-OTHER-WINDOW is non-nil, finds the file in the other window.
313 If optional IGNORE-INCLUDE is non-nil, ignores being on #include lines.
315 Arguments: (&optional in-other-window ignore-include)
317 Variables of interest include:
319 - ff-case-fold-search
320 Non-nil means ignore cases in matches (see case-fold-search).
321 If you have extensions in different cases, you will want this to be nil.
323 - ff-always-in-other-window
324 If non-nil, always open the other file in another window, unless an
325 argument is given to ff-find-other-file.
327 - ff-ignore-include
328 If non-nil, ignores #include lines.
330 - ff-always-try-to-create
331 If non-nil, always attempt to create the other file if it was not found.
333 - ff-quiet-mode
334 If non-nil, traces which directories are being searched.
336 - ff-special-constructs
337 A list of regular expressions specifying how to recognise special
338 constructs such as include files etc, and an associated method for
339 extracting the filename from that construct.
341 - ff-other-file-alist
342 Alist of extensions to find given the current file's extension.
344 - ff-search-directories
345 List of directories searched through with each extension specified in
346 ff-other-file-alist that matches this file's extension.
348 - ff-pre-find-hooks
349 List of functions to be called before the search for the file starts.
351 - ff-pre-load-hooks
352 List of functions to be called before the other file is loaded.
354 - ff-post-load-hooks
355 List of functions to be called after the other file is loaded.
357 - ff-not-found-hooks
358 List of functions to be called if the other file could not be found.
360 - ff-file-created-hooks
361 List of functions to be called if the other file has been created."
362 (interactive "P")
363 (let ((ignore ff-ignore-include))
364 (setq ff-ignore-include ignore-include)
365 (ff-find-the-other-file in-other-window)
366 (setq ff-ignore-include ignore)))
368 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
369 ;; Support functions
371 (defun ff-gnu-emacs-19 ()
372 (string-match "^19\\.[0-9]+\\.[0-9]+$" emacs-version))
374 (defun ff-xemacs ()
375 (or (string-match "Lucid" emacs-version)
376 (string-match "XEmacs" emacs-version)))
378 (defun ff-find-the-other-file (&optional in-other-window)
379 "Find the corresponding header or source file to this source or header
380 file; being on a #include line pulls in that file, but see the help on
381 the ff-ignore-include variable.
383 If optional IN-OTHER-WINDOW is non-nil, finds the file in another window.
385 Arguments: (&optional in-other-window)"
387 (let (match ;; matching regexp for this file
388 suffixes ;; set of replacing regexps for the matching regexp
389 action ;; function to generate the names of the other files
390 fname ;; basename of this file
391 pos ;; where we start matching filenames
392 stub ;; name of the file without extension
393 alist ;; working copy of the list of file extensions
394 pathname ;; the pathname of the file or the #include line
395 default-name ;; file we should create if none found
396 format ;; what we have to match
397 found ;; name of the file or buffer found - nil if none
398 dirs ;; local value of ff-search-directories
399 no-match) ;; whether we know about this kind of file
401 (if ff-pre-find-hooks
402 (run-hooks 'ff-pre-find-hooks))
404 (message "Working...")
406 (setq dirs
407 (if (symbolp ff-search-directories)
408 (ff-list-replace-env-vars (symbol-value ff-search-directories))
409 (ff-list-replace-env-vars ff-search-directories)))
411 (save-excursion
412 (beginning-of-line 1)
413 (setq fname (ff-treat-as-special)))
415 (cond
416 ((and (not ff-ignore-include) fname)
417 (setq default-name fname)
418 (setq found (ff-get-file dirs fname nil in-other-window)))
420 ;; let's just get the corresponding file
422 (setq alist (if (symbolp ff-other-file-alist)
423 (symbol-value ff-other-file-alist)
424 ff-other-file-alist)
425 pathname (if (buffer-file-name)
426 (buffer-file-name)
427 "/none.none"))
429 (string-match ".*/\\(.+\\)$" pathname)
430 (setq fname (substring pathname (match-beginning 1) (match-end 1))
431 no-match nil
432 match (car alist))
434 ;; find the table entry corresponding to this file
435 (setq pos (ff-string-match (car match) fname))
436 (while (and match (if (and pos (>= pos 0)) nil (not pos)))
437 (setq alist (cdr alist))
438 (setq match (car alist))
439 (setq pos (ff-string-match (car match) fname)))
441 ;; no point going on if we haven't found anything
442 (if (not match)
443 (setq no-match t)
445 ;; otherwise, suffixes contains what we need
446 (setq suffixes (car (cdr match))
447 action (car (cdr match))
448 found nil)
450 ;; if we have a function to generate new names,
451 ;; invoke it with the name of the current file
452 (if (and (atom action) (fboundp action))
453 (progn
454 (setq suffixes (funcall action (buffer-file-name))
455 match (cons (car match) (list suffixes))
456 stub nil
457 default-name (car suffixes)))
459 ;; otherwise build our filename stub
460 (cond
462 ;; get around the problem that 0 and nil both mean false!
463 ((= pos 0)
464 (setq format "")
465 (setq stub "")
469 (setq format (concat "\\(.+\\)" (car match)))
470 (string-match format fname)
471 (setq stub (substring fname (match-beginning 1) (match-end 1)))
474 ;; if we find nothing, we should try to get a file like this one
475 (setq default-name
476 (concat stub (car (car (cdr match))))))
478 ;; do the real work - find the file
479 (setq found
480 (ff-get-file dirs
481 stub
482 suffixes
483 in-other-window)))))
485 (cond
486 (no-match ;; could not even determine the other file
487 (message ""))
490 (cond
492 ((not found) ;; could not find the other file
494 (if ff-not-found-hooks ;; run the hooks
495 (run-hooks 'ff-not-found-hooks))
497 (cond
498 (ff-always-try-to-create ;; try to create the file
499 (let (name pathname)
501 (setq name
502 (expand-file-name
503 (read-file-name
504 (format "Find or create %s in: " default-name)
505 default-directory default-name nil)))
507 (setq pathname
508 (if (file-directory-p name)
509 (concat (file-name-as-directory name) default-name)
510 (setq found name)))
512 (ff-find-file pathname in-other-window t)))
514 (t ;; don't create the file, just whinge
515 (message "no file found for %s" fname))))
517 (t ;; matching file found
518 nil))))
520 found)) ;; return buffer-name or filename
522 (defun ff-get-file (search-dirs fname-stub &optional suffix-list other-window)
523 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
524 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
525 with each of the given suffixes. Gets the file or the buffer corresponding
526 to the name of the first file found, or nil.
528 Arguments: (search-dirs fname-stub &optional suffix-list in-other-window)
530 (let ((filename (ff-get-file-name search-dirs fname-stub suffix-list)))
532 (cond
533 ((not filename)
534 nil)
536 ((bufferp (get-buffer filename))
537 (ff-switch-to-buffer filename other-window)
538 filename)
540 ((file-exists-p filename)
541 (ff-find-file filename other-window nil)
542 filename)
545 nil))))
547 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list)
548 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub).
549 If (optional) SUFFIXES is nil, search for fname, otherwise search for fname
550 with each of the given suffixes. Returns the name of the first file found.
552 Arguments: (search-dirs fname-stub &optional suffix-list)
554 (let* (dirs ;; working copy of dirs to search
555 dir ;; the current dir considered
556 file ;; filename being looked for
557 rest ;; pathname after first /*
558 this-suffix ;; the suffix we are currently considering
559 suffixes ;; working copy of suffix-list
560 filename ;; built filename
561 blist ;; list of live buffers
562 buf ;; current buffer in blist
563 found) ;; whether we have found anything
565 (setq suffixes suffix-list)
567 ;; suffixes is nil => fname-stub is the file we are looking for
568 ;; otherwise fname-stub is a stub, and we append a suffix
569 (if suffixes
570 (setq this-suffix (car suffixes))
571 (setq this-suffix "")
572 (setq suffixes (list "")))
574 ;; find whether the file is in a buffer first
575 (while (and suffixes (not found))
576 (setq filename (concat fname-stub this-suffix))
578 (if (not ff-quiet-mode)
579 (message "finding buffer %s..." filename))
581 (if (bufferp (get-buffer filename))
582 (setq found filename))
584 (setq blist (buffer-list))
585 (setq buf (buffer-name (car blist)))
586 (while (and blist (not found))
588 (if (string-match (concat filename "<[0-9]+>") buf)
589 (setq found buf))
591 (setq blist (cdr blist))
592 (setq buf (buffer-name (car blist))))
594 (setq suffixes (cdr suffixes))
595 (setq this-suffix (car suffixes)))
597 ;; now look for the real file
598 (setq dirs search-dirs)
599 (setq dir (car dirs))
600 (while (and (not found) dirs)
602 (setq suffixes suffix-list)
604 ;; if dir does not contain '/*', look for the file
605 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir)))
606 (progn
608 ;; suffixes is nil => fname-stub is the file we are looking for
609 ;; otherwise fname-stub is a stub, and we append a suffix
610 (if suffixes
611 (setq this-suffix (car suffixes))
612 (setq this-suffix "")
613 (setq suffixes (list "")))
615 (while (and suffixes (not found))
617 (setq filename (concat fname-stub this-suffix))
618 (setq file (concat dir "/" filename))
620 (if (not ff-quiet-mode)
621 (message "finding %s..." file))
623 (if (file-exists-p file)
624 (setq found file))
626 (setq suffixes (cdr suffixes))
627 (setq this-suffix (car suffixes))))
629 ;; otherwise dir matches the '/*', so search each dir separately
630 (progn
631 (if (match-beginning 2)
632 (setq rest (substring dir (match-beginning 2) (match-end 2)))
633 (setq rest "")
635 (setq dir (substring dir (match-beginning 1) (match-end 1)))
637 (let ((dirlist (ff-all-dirs-under dir '("..")))
638 this-dir compl-dirs)
640 (setq this-dir (car dirlist))
641 (while dirlist
642 (setq compl-dirs
643 (append
644 compl-dirs
645 (list (concat this-dir rest))
647 (setq dirlist (cdr dirlist))
648 (setq this-dir (car dirlist)))
650 (if compl-dirs
651 (setq found (ff-get-file-name compl-dirs
652 fname-stub
653 suffix-list))))))
654 (setq dirs (cdr dirs))
655 (setq dir (car dirs)))
657 (if found
658 (message "%s found" found))
660 found))
662 (defun ff-string-match (regexp string &optional start)
663 "Like string-match (which see), but sets case-fold-search to
664 ff-case-fold-search before searching, and then resets it back again."
665 (let ((exact-match case-fold-search)
666 match)
667 (if regexp
668 (progn
669 (setq case-fold-search ff-case-fold-search)
670 (setq match (string-match regexp string start))
671 (setq case-fold-search exact-match)))
672 match))
674 (defun ff-list-replace-env-vars (search-list)
675 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST."
676 (let (list
677 (var (car search-list)))
678 (while search-list
679 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var)
680 (setq var
681 (concat
682 (substring var (match-beginning 1) (match-end 1))
683 (getenv (substring var (match-beginning 2) (match-end 2)))
684 (substring var (match-beginning 3) (match-end 3)))))
685 (setq search-list (cdr search-list))
686 (setq list (cons var list))
687 (setq var (car search-list)))
688 (setq search-list (reverse list))))
690 (defun ff-treat-as-special ()
691 "Returns the file to look for if the construct was special, otherwise
692 returns nil. The construct is defined in the variable ff-special-constructs
693 (which see)."
694 (let* (fname
695 (list ff-special-constructs)
696 (elem (car list))
697 (regexp (car elem))
698 (match (cdr elem)))
699 (while (and list (not fname))
700 (if (and (looking-at regexp) match)
701 (setq fname (funcall match)))
702 (setq list (cdr list))
703 (setq elem (car list))
704 (setq regexp (car elem))
705 (setq match (cdr elem)))
706 fname))
708 (defun ff-basename (string)
709 "Returns the basename of PATHNAME."
710 (setq string (concat "/" string))
711 (string-match ".*/\\([^/]+\\)$" string)
712 (setq string (substring string (match-beginning 1) (match-end 1))))
714 (defun ff-all-dirs-under (here &optional exclude)
715 "Get all the directory files under DIRECTORY.
716 Exclude all files in the optional EXCLUDE list."
717 (if (file-directory-p here)
718 (condition-case nil
719 (progn
720 (let ((files (directory-files here t))
721 (dirlist (list))
722 file)
723 (while files
724 (setq file (car files))
725 (if (and
726 (file-directory-p file)
727 (not (member (ff-basename file) exclude)))
728 (setq dirlist (cons file dirlist)))
729 (setq files (cdr files)))
730 (setq dirlist (reverse dirlist))))
731 (error nil))
732 nil))
734 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file)
735 "Calls Function2 or Function1 with FILE as argument, depending on whether
736 (optional) OTHER-WINDOW is set or not. Function1 and Function2 are typically
737 find-file / find-file-other-window or switch-to-buffer / switch-to-buffer-
738 other-window function pairs.
740 If optional NEW-FILE is t, then a special hook (ff-file-created-hooks) is
741 called before ff-post-load-hooks.
743 Arguments: (function1 function2 file &optional in-other-window new-file)
745 (if ff-pre-load-hooks
746 (run-hooks 'ff-pre-load-hooks))
747 (if (or
748 (and in-other-window (not ff-always-in-other-window))
749 (and (not in-other-window) ff-always-in-other-window))
750 (funcall f2 file)
751 (funcall f1 file))
752 (if new-file
753 (if ff-file-created-hooks
754 (run-hooks 'ff-file-created-hooks)))
755 (if ff-post-load-hooks
756 (run-hooks 'ff-post-load-hooks)))
758 (defun ff-find-file (file &optional in-other-window new-file)
759 "Like find-file (which see), but checks whether the file goes in another
760 window or not.
762 Arguments: (file &optional in-other-window new-file)
764 (ff-switch-file 'find-file
765 'find-file-other-window
766 file in-other-window new-file))
768 (defun ff-switch-to-buffer (file &optional in-other-window)
769 "Like switch-to-buffer (which see), but checks whether the buffer ends up
770 in another window or not.
772 Arguments: (file &optional in-other-window)
774 (ff-switch-file 'switch-to-buffer
775 'switch-to-buffer-other-window
776 file in-other-window nil))
778 (cond
779 ((ff-gnu-emacs-19)
780 (defun ff-goto-click (event)
781 (set-buffer (window-buffer (posn-window (event-end event))))
782 (goto-char (posn-point (event-end event))))
784 ;;;###autoload
785 (defun ff-mouse-find-other-file (event)
786 "Visit the file you click on."
787 (interactive "e")
788 (save-excursion
789 (ff-goto-click 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 (ff-goto-click event)
798 (ff-find-other-file t)))
800 ;;;###autoload
801 (defun locate-file (fname dirs &optional suffix-list ignore-perms)
802 "Defines XEmacs look-alike locate-file for GNU Emacs-19."
803 (interactive)
804 (ff-get-file dirs fname suffix-list))
807 ((ff-xemacs)
809 ;;;###autoload
810 (defun ff-mouse-find-other-file (event)
811 "Visit the file you click on."
812 (interactive "@e")
813 (save-excursion
814 (mouse-set-point event)
815 (ff-find-other-file nil)))
817 ;;;###autoload
818 (defun ff-mouse-find-other-file-other-window (event)
819 "Visit the file you click on."
820 (interactive "@e")
821 (save-excursion
822 (mouse-set-point event)
823 (ff-find-other-file t)))
826 (provide 'find-file)
828 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
829 ;; This section offers an example of user defined function to select files
831 (defun upcase-p (string &optional start end)
832 "Return t if this string is all uppercase. Given START and/or END,
833 checks between these characters."
834 (let (match str)
835 (if (not start)
836 (setq start 0))
837 (if (not end)
838 (setq end (length string)))
839 (if (= start end)
840 (setq end (1+ end)))
841 (setq str (substring string start end))
842 (if (and
843 (ff-string-match "[A-Z]+" str)
844 (setq match (match-data))
845 (= (car match) 0)
846 (= (car (cdr match)) (length str)))
848 nil)))
850 (defun ff-cc-hh-converter (arg)
851 "Discriminate file extensions and build up a new file list based
852 possibly on part of the directory name and the name of the file
853 passed in."
854 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg)
855 (let ((path (if (match-beginning 1)
856 (substring arg (match-beginning 1) (match-end 1)) nil))
857 (dire (if (match-beginning 2)
858 (substring arg (match-beginning 2) (match-end 2)) nil))
859 (file (if (match-beginning 3)
860 (substring arg (match-beginning 3) (match-end 3)) nil))
861 (extn (if (match-beginning 4)
862 (substring arg (match-beginning 4) (match-end 4)) nil))
863 return-list)
864 (cond
865 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h}
866 ((and (string= extn "cc")
867 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file))
868 (let ((stub (substring file (match-beginning 2) (match-end 2))))
869 (setq dire (upcase (substring file (match-beginning 1) (match-end 1))))
870 (setq return-list (list (concat stub ".hh")
871 (concat stub ".h")
872 (concat file ".hh")
873 (concat file ".h")))
875 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C}
876 ((and (string= extn "hh") (upcase-p dire) file)
877 (let ((stub (concat (downcase dire) file)))
878 (setq return-list (list (concat stub ".cc")
879 (concat stub ".C")
880 (concat file ".cc")
881 (concat file ".C")))
883 ;; zap.cc => zap.hh or zap.h
884 ((string= extn "cc")
885 (let ((stub file))
886 (setq return-list (list (concat stub ".hh")
887 (concat stub ".h")))
889 ;; zap.hh => zap.cc or zap.C
890 ((string= extn "hh")
891 (let ((stub file))
892 (setq return-list (list (concat stub ".cc")
893 (concat stub ".C")))
896 nil))
898 return-list))
900 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
901 ;; This section offers an example of user defined function to place point.
902 ;; The regexps are Ada specific.
904 (defvar ff-function-name nil "Name of the function we are in.")
906 (defvar ada-procedure-start-regexp)
907 (defvar ada-package-start-regexp)
909 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in)
911 (defun ff-which-function-are-we-in ()
912 "Determine whether we are on a function definition/declaration and
913 remember the name of that function."
915 (setq ff-function-name nil)
917 (save-excursion
918 (if (re-search-backward ada-procedure-start-regexp nil t)
919 (setq ff-function-name (buffer-substring (match-beginning 0)
920 (match-end 0)))
921 ; we didn't find a procedure start, perhaps there is a package
922 (if (re-search-backward ada-package-start-regexp nil t)
923 (setq ff-function-name (buffer-substring (match-beginning 0)
924 (match-end 0)))
925 ))))
927 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly)
929 (defun ff-set-point-accordingly ()
930 "Find the function specified in ff-function-name, previously
931 determined by ff-which-function-are-we-in."
932 (if ff-function-name
933 (progn
934 (goto-char (point-min))
935 (search-forward ff-function-name nil t))))
937 ;; find-file.el ends here