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