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