Mercurial > emacs
annotate lisp/find-file.el @ 43584:15030afab27a
(vc-revert-buffer): If the buffer is not saved, prompt the user to do
that first.
author | André Spiegel <spiegel@gnu.org> |
---|---|
date | Thu, 28 Feb 2002 09:59:08 +0000 |
parents | e12d545c9a48 |
children | e772dea9fb2e |
rev | line source |
---|---|
10491 | 1 ;;; find-file.el --- find a file corresponding to this one given a pattern |
2 | |
18127 | 3 ;; Author: Henry Guillaume <henri@tibco.com, henry@c032.aone.net.au> |
21057 | 4 ;; Maintainer: FSF |
10491 | 5 ;; Keywords: c, matching, tools |
6 | |
11234 | 7 ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc. |
10491 | 8 |
14169 | 9 ;; This file is part of GNU Emacs. |
10491 | 10 |
14169 | 11 ;; GNU Emacs is free software; you can redistribute it and/or modify |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2, or (at your option) | |
14 ;; any later version. | |
10491 | 15 |
14169 | 16 ;; GNU Emacs is distributed in the hope that it will be useful, |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
10491 | 20 |
14169 | 21 ;; You should have received a copy of the GNU General Public License |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
10491 | 25 |
26 ;;; Commentary: | |
27 | |
28 ;; PURPOSE: | |
30816 | 29 ;; This package features a function called ff-find-other-file, which performs |
30 ;; the following function: | |
10491 | 31 ;; |
32 ;; When in a .c file, find the first corresponding .h file in a set | |
33 ;; of directories and display it, and vice-versa from the .h file. | |
34 ;; | |
35 ;; Many people maintain their include file in a directory separate to their | |
36 ;; src directory, and very often you may be editing a file and have a need to | |
37 ;; visit the "other file". This package searches through a set of directories | |
38 ;; to find that file. | |
39 ;; | |
40 ;; THE "OTHER FILE", or "corresponding file", generally has the same basename, | |
30816 | 41 ;; and just has a different extension as described by the ff-other-file-alist |
10491 | 42 ;; variable: |
43 ;; | |
44 ;; '(("\\.cc$" (".hh" ".h")) | |
45 ;; ("\\.hh$" (".cc" ".C" ".CC" ".cxx" ".cpp"))) | |
46 ;; | |
47 ;; If the current file has a .cc extension, ff-find-other-file will attempt | |
48 ;; to look for a .hh file, and then a .h file in some directory as described | |
49 ;; below. The mechanism here is to replace the matched part of the original | |
50 ;; filename with each of the corresponding extensions in turn. | |
51 ;; | |
52 ;; Alternatively, there are situations where the filename of the other file | |
53 ;; cannot be determined easily with regexps. For example, a .c file may | |
54 ;; have two corresponding .h files, for its public and private parts, or | |
55 ;; the filename for the .c file contains part of the pathname of the .h | |
56 ;; file, as between src/fooZap.cc and include/FOO/zap.hh. In that case, the | |
57 ;; format above can be changed to include a function to be called when the | |
58 ;; current file matches the regexp: | |
59 ;; | |
60 ;; '(("\\.cc$" cc-function) | |
61 ;; ("\\.hh$" hh-function)) | |
62 ;; | |
30816 | 63 ;; These functions must return a list consisting of the possible names of the |
64 ;; corresponding file, with or without path. There is no real need for more | |
10491 | 65 ;; than one function, and one could imagine the following value for cc-other- |
66 ;; file-alist: | |
67 ;; | |
68 ;; (setq cc-other-file-alist | |
69 ;; '(("\\.cc$" ff-cc-hh-converter) | |
70 ;; ("\\.hh$" ff-cc-hh-converter) | |
71 ;; ("\\.c$" (".h")) | |
72 ;; ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp")))) | |
73 ;; | |
74 ;; ff-cc-hh-converter is included at the end of this file as a reference. | |
75 ;; | |
76 ;; SEARCHING is carried out in a set of directories specified by the | |
77 ;; ff-search-directories variable: | |
78 ;; | |
79 ;; ("." "../../src" "../include/*" "/usr/local/*/src/*" "$PROJECT/src") | |
80 ;; | |
81 ;; This means that the corresponding file will be searched for first in | |
82 ;; the current directory, then in ../../src, then in one of the directories | |
83 ;; under ../include, and so on. The star is _not_ a general wildcard | |
84 ;; character: it just indicates that the subdirectories of this directory | |
85 ;; must each be searched in turn. Environment variables will be expanded in | |
86 ;; the ff-search-directories variable. | |
87 ;; | |
88 ;; If the point is on a #include line, the file to be #included is searched | |
89 ;; for in the same manner. This can be disabled with the ff-ignore-include | |
90 ;; variable, or by calling ff-get-other-file instead of ff-find-other-file. | |
91 ;; | |
92 ;; If the file was not found, ff-find-other-file will prompt you for where | |
93 ;; to create the new "corresponding file" (defaults to the current directory), | |
30816 | 94 ;; unless the variable ff-always-try-to-create is set to nil. |
10491 | 95 ;; |
30816 | 96 ;; GIVEN AN ARGUMENT (with the ^U prefix), ff-find-other-file will get the |
97 ;; other file in another (the other?) window (see find-file-other-window and | |
98 ;; switch-to-buffer-other-window). This can be set on a more permanent basis | |
99 ;; by setting ff-always-in-other-window to t in which case the ^U prefix will | |
10491 | 100 ;; do the opposite of what was described above. |
101 ;; | |
102 ;; THERE ARE FIVE AVAILABLE HOOKS, called in this order if non-nil: | |
103 ;; | |
104 ;; - ff-pre-find-hooks - called before the search for the other file starts | |
105 ;; - ff-not-found-hooks - called when the other file could not be found | |
106 ;; - ff-pre-load-hooks - called just before the other file is 'loaded' | |
107 ;; - ff-file-created-hooks - called when the other file is created | |
108 ;; - ff-post-load-hooks - called just after the other file is 'loaded' | |
109 ;; | |
110 ;; The *load-hooks allow you to place point where you want it in the other | |
30816 | 111 ;; file. |
10491 | 112 |
113 ;; CREDITS: | |
114 ;; Many thanks go to TUSC Computer Systems Pty Ltd for providing an environ- | |
115 ;; ment that made the development of this package possible. | |
116 ;; | |
117 ;; Many thanks also go to all those who provided valuable feedback throughout | |
118 ;; the development of this package: | |
119 ;; Rolf Ebert in particular, Fritz Knabe, Heddy Boubaker, Sebastian Kremer, | |
30816 | 120 ;; Vasco Lopes Paulo, Mark A. Plaksin, Robert Lang, Trevor West, Kevin |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
121 ;; Pereira, Benedict Lofstedt & Justin Vallon. |
10491 | 122 |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
123 ;;; Code: |
10491 | 124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
125 ;; User definable variables: | |
126 | |
21087 | 127 (defgroup ff nil |
128 "Find a file corresponding to this one given a pattern." | |
129 :prefix "ff-" | |
30816 | 130 :link '(emacs-commentary-link "find-file") |
21087 | 131 :group 'find-file) |
10491 | 132 |
21087 | 133 (defcustom ff-pre-find-hooks nil |
134 "*List of functions to be called before the search for the file starts." | |
135 :type 'hook | |
136 :group 'ff) | |
10491 | 137 |
21087 | 138 (defcustom ff-pre-load-hooks nil |
139 "*List of functions to be called before the other file is loaded." | |
140 :type 'hook | |
141 :group 'ff) | |
10491 | 142 |
21087 | 143 (defcustom ff-post-load-hooks nil |
144 "*List of functions to be called after the other file is loaded." | |
145 :type 'hook | |
146 :group 'ff) | |
10491 | 147 |
21087 | 148 (defcustom ff-not-found-hooks nil |
149 "*List of functions to be called if the other file could not be found." | |
150 :type 'hook | |
151 :group 'ff) | |
10491 | 152 |
21087 | 153 (defcustom ff-file-created-hooks nil |
154 "*List of functions to be called if the other file needs to be created." | |
155 :type 'hook | |
156 :group 'ff) | |
10491 | 157 |
21087 | 158 (defcustom ff-case-fold-search nil |
159 "*Non-nil means ignore cases in matches (see `case-fold-search'). | |
160 If you have extensions in different cases, you will want this to be nil." | |
161 :type 'boolean | |
162 :group 'ff) | |
10491 | 163 |
21087 | 164 (defcustom ff-always-in-other-window nil |
165 "*If non-nil, find the corresponding file in another window by default. | |
166 To override this, give an argument to `ff-find-other-file'." | |
167 :type 'boolean | |
168 :group 'ff) | |
10491 | 169 |
21087 | 170 (defcustom ff-ignore-include nil |
171 "*If non-nil, ignore `#include' lines." | |
172 :type 'boolean | |
173 :group 'ff) | |
10491 | 174 |
21087 | 175 (defcustom ff-always-try-to-create t |
176 "*If non-nil, always attempt to create the other file if it was not found." | |
177 :type 'boolean | |
178 :group 'ff) | |
179 | |
180 (defcustom ff-quiet-mode nil | |
181 "*If non-nil, trace which directories are being searched." | |
182 :type 'boolean | |
183 :group 'ff) | |
10491 | 184 |
30816 | 185 (defvar ff-special-constructs |
10491 | 186 '( |
187 ;; C/C++ include, for NeXTSTEP too | |
188 ("^\#\\s *\\(include\\|import\\)\\s +[<\"]\\(.*\\)[>\"]" . | |
189 (lambda () | |
190 (setq fname (buffer-substring (match-beginning 2) (match-end 2))))) | |
191 | |
192 ;; Ada import | |
193 ("^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)" . | |
194 (lambda () | |
195 (setq fname (buffer-substring (match-beginning 1) (match-end 1))) | |
12949
f6e5a73b96e4
(ada-spec-suffix): Definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
12567
diff
changeset
|
196 (require 'ada-mode) |
10491 | 197 (setq fname (concat (ada-make-filename-from-adaname fname) |
12949
f6e5a73b96e4
(ada-spec-suffix): Definition deleted.
Richard M. Stallman <rms@gnu.org>
parents:
12567
diff
changeset
|
198 ada-spec-suffix)))) |
10491 | 199 ) |
30816 | 200 "*A list of regular expressions for `ff-find-file'. |
201 Specifies how to recognise special constructs such as include files | |
202 etc. and an associated method for extracting the filename from that | |
203 construct.") | |
10491 | 204 |
21087 | 205 (defcustom ff-other-file-alist 'cc-other-file-alist |
10491 | 206 "*Alist of extensions to find given the current file's extension. |
207 | |
208 This list should contain the most used extensions before the others, | |
209 since the search algorithm searches sequentially through each | |
11272 | 210 directory specified in `ff-search-directories'. If a file is not found, |
211 a new one is created with the first matching extension (`.cc' yields `.hh'). | |
21087 | 212 This alist should be set by the major mode." |
213 :type '(choice (repeat (list regexp (choice (repeat string) function))) | |
214 symbol) | |
215 :group 'ff) | |
10491 | 216 |
21087 | 217 (defcustom ff-search-directories 'cc-search-directories |
10491 | 218 "*List of directories to search for a specific file. |
219 | |
11272 | 220 Set by default to `cc-search-directories', expanded at run-time. |
10491 | 221 |
222 This list is searched through with each extension specified in | |
11272 | 223 `ff-other-file-alist' that matches this file's extension. So the |
10491 | 224 longer the list, the longer it'll take to realise that a file |
225 may not exist. | |
226 | |
30816 | 227 A typical format is |
10491 | 228 |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
229 '(\".\" \"/usr/include\" \"$PROJECT/*/include\") |
10491 | 230 |
11272 | 231 Environment variables can be inserted between slashes (`/'). |
10491 | 232 They will be replaced by their definition. If a variable does |
11272 | 233 not exist, it is replaced (silently) with an empty string. |
10491 | 234 |
11272 | 235 The stars are *not* wildcards: they are searched for together with |
236 the preceding slash. The star represents all the subdirectories except | |
21087 | 237 `..', and each of these subdirectories will be searched in turn." |
238 :type '(choice (repeat directory) symbol) | |
239 :group 'ff) | |
10491 | 240 |
21087 | 241 (defcustom cc-search-directories |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
242 '("." "/usr/include" "/usr/local/include/*") |
21087 | 243 "*See the description of the `ff-search-directories' variable." |
244 :type '(repeat directory) | |
245 :group 'ff) | |
10491 | 246 |
21087 | 247 (defcustom cc-other-file-alist |
10491 | 248 '( |
249 ("\\.cc$" (".hh" ".h")) | |
250 ("\\.hh$" (".cc" ".C")) | |
251 | |
252 ("\\.c$" (".h")) | |
253 ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp")) | |
254 | |
255 ("\\.C$" (".H" ".hh" ".h")) | |
256 ("\\.H$" (".C" ".CC")) | |
257 | |
258 ("\\.CC$" (".HH" ".H" ".hh" ".h")) | |
259 ("\\.HH$" (".CC")) | |
260 | |
261 ("\\.cxx$" (".hh" ".h")) | |
262 ("\\.cpp$" (".hh" ".h")) | |
263 ) | |
264 "*Alist of extensions to find given the current file's extension. | |
265 | |
266 This list should contain the most used extensions before the others, | |
267 since the search algorithm searches sequentially through each directory | |
11272 | 268 specified in `ff-search-directories'. If a file is not found, a new one |
21087 | 269 is created with the first matching extension (`.cc' yields `.hh')." |
270 :type '(repeat (list regexp (choice (repeat string) function))) | |
271 :group 'ff) | |
10491 | 272 |
21087 | 273 (defcustom modula2-other-file-alist |
10491 | 274 '( |
275 ("\\.mi$" (".md")) ;; Modula-2 module definition | |
276 ("\\.md$" (".mi")) ;; and implementation. | |
277 ) | |
21087 | 278 "*See the description for the `ff-search-directories' variable." |
279 :type '(repeat (list regexp (choice (repeat string) function))) | |
280 :group 'ff) | |
281 | |
10491 | 282 |
283 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
284 ;; No user definable variables beyond this point! | |
285 ;; ============================================== | |
286 | |
287 (make-variable-buffer-local 'ff-pre-find-hooks) | |
288 (make-variable-buffer-local 'ff-pre-load-hooks) | |
289 (make-variable-buffer-local 'ff-post-load-hooks) | |
290 (make-variable-buffer-local 'ff-not-found-hooks) | |
291 (make-variable-buffer-local 'ff-file-created-hooks) | |
292 (make-variable-buffer-local 'ff-case-fold-search) | |
293 (make-variable-buffer-local 'ff-always-in-other-window) | |
294 (make-variable-buffer-local 'ff-ignore-include) | |
295 (make-variable-buffer-local 'ff-quiet-mode) | |
296 (make-variable-buffer-local 'ff-other-file-alist) | |
297 (make-variable-buffer-local 'ff-search-directories) | |
298 | |
299 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
300 ;; User entry points | |
301 | |
302 ;;;###autoload | |
303 (defun ff-get-other-file (&optional in-other-window) | |
11272 | 304 "Find the header or source file corresponding to this file. |
30816 | 305 See also the documentation for `ff-find-other-file'. |
10491 | 306 |
11272 | 307 If optional IN-OTHER-WINDOW is non-nil, find the file in another window." |
10491 | 308 (interactive "P") |
309 (let ((ignore ff-ignore-include)) | |
310 (setq ff-ignore-include t) | |
311 (ff-find-the-other-file in-other-window) | |
312 (setq ff-ignore-include ignore))) | |
313 | |
314 ;;;###autoload | |
315 (defun ff-find-other-file (&optional in-other-window ignore-include) | |
11272 | 316 "Find the header or source file corresponding to this file. |
317 Being on a `#include' line pulls in that file. | |
10491 | 318 |
11272 | 319 If optional IN-OTHER-WINDOW is non-nil, find the file in the other window. |
320 If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines. | |
10491 | 321 |
322 Variables of interest include: | |
323 | |
30816 | 324 - `ff-case-fold-search' |
325 Non-nil means ignore cases in matches (see `case-fold-search'). | |
10491 | 326 If you have extensions in different cases, you will want this to be nil. |
327 | |
30816 | 328 - `ff-always-in-other-window' |
10491 | 329 If non-nil, always open the other file in another window, unless an |
30816 | 330 argument is given to `ff-find-other-file'. |
10491 | 331 |
30816 | 332 - `ff-ignore-include' |
10491 | 333 If non-nil, ignores #include lines. |
334 | |
30816 | 335 - `ff-always-try-to-create' |
10491 | 336 If non-nil, always attempt to create the other file if it was not found. |
337 | |
30816 | 338 - `ff-quiet-mode' |
10491 | 339 If non-nil, traces which directories are being searched. |
340 | |
30816 | 341 - `ff-special-constructs' |
342 A list of regular expressions specifying how to recognise special | |
343 constructs such as include files etc, and an associated method for | |
10491 | 344 extracting the filename from that construct. |
345 | |
30816 | 346 - `ff-other-file-alist' |
10491 | 347 Alist of extensions to find given the current file's extension. |
348 | |
30816 | 349 - `ff-search-directories' |
10491 | 350 List of directories searched through with each extension specified in |
30816 | 351 `ff-other-file-alist' that matches this file's extension. |
10491 | 352 |
30816 | 353 - `ff-pre-find-hooks' |
10491 | 354 List of functions to be called before the search for the file starts. |
355 | |
30816 | 356 - `ff-pre-load-hooks' |
10491 | 357 List of functions to be called before the other file is loaded. |
358 | |
30816 | 359 - `ff-post-load-hooks' |
10491 | 360 List of functions to be called after the other file is loaded. |
361 | |
30816 | 362 - `ff-not-found-hooks' |
10491 | 363 List of functions to be called if the other file could not be found. |
364 | |
30816 | 365 - `ff-file-created-hooks' |
10491 | 366 List of functions to be called if the other file has been created." |
367 (interactive "P") | |
368 (let ((ignore ff-ignore-include)) | |
369 (setq ff-ignore-include ignore-include) | |
370 (ff-find-the-other-file in-other-window) | |
371 (setq ff-ignore-include ignore))) | |
372 | |
373 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
374 ;; Support functions | |
375 | |
376 (defun ff-find-the-other-file (&optional in-other-window) | |
11272 | 377 "Find the header or source file corresponding to the current file. |
378 Being on a `#include' line pulls in that file, but see the help on | |
379 the `ff-ignore-include' variable. | |
10491 | 380 |
11272 | 381 If optional IN-OTHER-WINDOW is non-nil, find the file in another window." |
10491 | 382 |
383 (let (match ;; matching regexp for this file | |
384 suffixes ;; set of replacing regexps for the matching regexp | |
385 action ;; function to generate the names of the other files | |
386 fname ;; basename of this file | |
387 pos ;; where we start matching filenames | |
388 stub ;; name of the file without extension | |
389 alist ;; working copy of the list of file extensions | |
390 pathname ;; the pathname of the file or the #include line | |
391 default-name ;; file we should create if none found | |
30816 | 392 format ;; what we have to match |
393 found ;; name of the file or buffer found - nil if none | |
10491 | 394 dirs ;; local value of ff-search-directories |
395 no-match) ;; whether we know about this kind of file | |
396 | |
397 (if ff-pre-find-hooks | |
398 (run-hooks 'ff-pre-find-hooks)) | |
399 | |
400 (message "Working...") | |
401 | |
402 (setq dirs | |
403 (if (symbolp ff-search-directories) | |
404 (ff-list-replace-env-vars (symbol-value ff-search-directories)) | |
405 (ff-list-replace-env-vars ff-search-directories))) | |
406 | |
407 (save-excursion | |
408 (beginning-of-line 1) | |
409 (setq fname (ff-treat-as-special))) | |
410 | |
411 (cond | |
412 ((and (not ff-ignore-include) fname) | |
413 (setq default-name fname) | |
414 (setq found (ff-get-file dirs fname nil in-other-window))) | |
415 | |
416 ;; let's just get the corresponding file | |
417 (t | |
418 (setq alist (if (symbolp ff-other-file-alist) | |
419 (symbol-value ff-other-file-alist) | |
420 ff-other-file-alist) | |
421 pathname (if (buffer-file-name) | |
422 (buffer-file-name) | |
423 "/none.none")) | |
424 | |
38273
910cd30b1586
(ff-find-the-other-file): Use file-name-nondirectory
Eli Zaretskii <eliz@gnu.org>
parents:
30816
diff
changeset
|
425 (setq fname (file-name-nondirectory pathname) |
10491 | 426 no-match nil |
427 match (car alist)) | |
428 | |
429 ;; find the table entry corresponding to this file | |
430 (setq pos (ff-string-match (car match) fname)) | |
431 (while (and match (if (and pos (>= pos 0)) nil (not pos))) | |
432 (setq alist (cdr alist)) | |
433 (setq match (car alist)) | |
434 (setq pos (ff-string-match (car match) fname))) | |
435 | |
436 ;; no point going on if we haven't found anything | |
437 (if (not match) | |
438 (setq no-match t) | |
439 | |
440 ;; otherwise, suffixes contains what we need | |
441 (setq suffixes (car (cdr match)) | |
442 action (car (cdr match)) | |
443 found nil) | |
444 | |
30816 | 445 ;; if we have a function to generate new names, |
10491 | 446 ;; invoke it with the name of the current file |
447 (if (and (atom action) (fboundp action)) | |
448 (progn | |
449 (setq suffixes (funcall action (buffer-file-name)) | |
450 match (cons (car match) (list suffixes)) | |
451 stub nil | |
452 default-name (car suffixes))) | |
453 | |
454 ;; otherwise build our filename stub | |
30816 | 455 (cond |
10491 | 456 |
457 ;; get around the problem that 0 and nil both mean false! | |
458 ((= pos 0) | |
459 (setq format "") | |
460 (setq stub "") | |
461 ) | |
462 | |
463 (t | |
464 (setq format (concat "\\(.+\\)" (car match))) | |
465 (string-match format fname) | |
466 (setq stub (substring fname (match-beginning 1) (match-end 1))) | |
467 )) | |
468 | |
469 ;; if we find nothing, we should try to get a file like this one | |
470 (setq default-name | |
471 (concat stub (car (car (cdr match)))))) | |
472 | |
473 ;; do the real work - find the file | |
30816 | 474 (setq found |
10491 | 475 (ff-get-file dirs |
476 stub | |
30816 | 477 suffixes |
10491 | 478 in-other-window))))) |
479 | |
480 (cond | |
481 (no-match ;; could not even determine the other file | |
482 (message "")) | |
483 | |
30816 | 484 (t |
10491 | 485 (cond |
486 | |
487 ((not found) ;; could not find the other file | |
488 | |
489 (if ff-not-found-hooks ;; run the hooks | |
490 (run-hooks 'ff-not-found-hooks)) | |
491 | |
30816 | 492 (cond |
10491 | 493 (ff-always-try-to-create ;; try to create the file |
494 (let (name pathname) | |
495 | |
496 (setq name | |
497 (expand-file-name | |
498 (read-file-name | |
499 (format "Find or create %s in: " default-name) | |
500 default-directory default-name nil))) | |
501 | |
502 (setq pathname | |
503 (if (file-directory-p name) | |
504 (concat (file-name-as-directory name) default-name) | |
505 (setq found name))) | |
506 | |
507 (ff-find-file pathname in-other-window t))) | |
508 | |
509 (t ;; don't create the file, just whinge | |
17548 | 510 (message "No file found for %s" fname)))) |
10491 | 511 |
512 (t ;; matching file found | |
513 nil)))) | |
514 | |
515 found)) ;; return buffer-name or filename | |
516 | |
42097
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
517 (defun ff-other-file-name () |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
518 "Return name of the header or source file corresponding to the current file. |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
519 Being on a `#include' line pulls in that file, but see the help on |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
520 the `ff-ignore-include' variable." |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
521 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
522 (let (match ;; matching regexp for this file |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
523 suffixes ;; set of replacing regexps for the matching regexp |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
524 action ;; function to generate the names of the other files |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
525 fname ;; basename of this file |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
526 pos ;; where we start matching filenames |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
527 stub ;; name of the file without extension |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
528 alist ;; working copy of the list of file extensions |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
529 pathname ;; the pathname of the file or the #include line |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
530 default-name ;; file we should create if none found |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
531 format ;; what we have to match |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
532 found ;; name of the file or buffer found - nil if none |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
533 dirs ;; local value of ff-search-directories |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
534 no-match) ;; whether we know about this kind of file |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
535 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
536 (message "Working...") |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
537 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
538 (setq dirs |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
539 (if (symbolp ff-search-directories) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
540 (ff-list-replace-env-vars (symbol-value ff-search-directories)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
541 (ff-list-replace-env-vars ff-search-directories))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
542 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
543 (save-excursion |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
544 (beginning-of-line 1) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
545 (setq fname (ff-treat-as-special))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
546 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
547 (cond |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
548 ((and (not ff-ignore-include) fname) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
549 (setq default-name fname) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
550 (setq found (ff-get-file-name dirs fname nil))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
551 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
552 ;; let's just get the corresponding file |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
553 (t |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
554 (setq alist (if (symbolp ff-other-file-alist) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
555 (symbol-value ff-other-file-alist) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
556 ff-other-file-alist) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
557 pathname (if (buffer-file-name) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
558 (buffer-file-name) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
559 "/none.none")) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
560 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
561 (setq fname (file-name-nondirectory pathname) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
562 no-match nil |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
563 match (car alist)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
564 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
565 ;; find the table entry corresponding to this file |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
566 (setq pos (ff-string-match (car match) fname)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
567 (while (and match (if (and pos (>= pos 0)) nil (not pos))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
568 (setq alist (cdr alist)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
569 (setq match (car alist)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
570 (setq pos (ff-string-match (car match) fname))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
571 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
572 ;; no point going on if we haven't found anything |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
573 (if (not match) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
574 (setq no-match t) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
575 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
576 ;; otherwise, suffixes contains what we need |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
577 (setq suffixes (car (cdr match)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
578 action (car (cdr match)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
579 found nil) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
580 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
581 ;; if we have a function to generate new names, |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
582 ;; invoke it with the name of the current file |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
583 (if (and (atom action) (fboundp action)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
584 (progn |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
585 (setq suffixes (funcall action (buffer-file-name)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
586 match (cons (car match) (list suffixes)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
587 stub nil |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
588 default-name (car suffixes))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
589 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
590 ;; otherwise build our filename stub |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
591 (cond |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
592 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
593 ;; get around the problem that 0 and nil both mean false! |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
594 ((= pos 0) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
595 (setq format "") |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
596 (setq stub "") |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
597 ) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
598 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
599 (t |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
600 (setq format (concat "\\(.+\\)" (car match))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
601 (string-match format fname) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
602 (setq stub (substring fname (match-beginning 1) (match-end 1))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
603 )) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
604 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
605 ;; if we find nothing, we should try to get a file like this one |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
606 (setq default-name |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
607 (concat stub (car (car (cdr match)))))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
608 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
609 ;; do the real work - find the file |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
610 (setq found |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
611 (ff-get-file-name dirs stub suffixes))))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
612 found)) ;; return buffer-name or filename |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
613 |
30816 | 614 (defun ff-get-file (search-dirs filename &optional suffix-list other-window) |
615 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub). | |
616 If (optional) SUFFIX-LIST is nil, search for fname, otherwise search | |
617 for fname with each of the given suffixes. Get the file or the buffer | |
618 corresponding to the name of the first file found, or nil." | |
619 (let ((filename (ff-get-file-name search-dirs filename suffix-list))) | |
10491 | 620 |
30816 | 621 (cond |
10491 | 622 ((not filename) |
623 nil) | |
624 | |
13896 | 625 ((bufferp (get-file-buffer filename)) |
626 (ff-switch-to-buffer (get-file-buffer filename) other-window) | |
10491 | 627 filename) |
628 | |
629 ((file-exists-p filename) | |
630 (ff-find-file filename other-window nil) | |
631 filename) | |
632 | |
633 (t | |
634 nil)))) | |
635 | |
636 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list) | |
30816 | 637 "Find a file in SEARCH-DIRS with the given name (or stub) FNAME-STUB. |
638 If (optional) SUFFIX-LIST is nil, search for FNAME-STUB, otherwise | |
639 search for FNAME-STUB with each of the given suffixes. Return the | |
640 name of the first file found." | |
10491 | 641 (let* (dirs ;; working copy of dirs to search |
642 dir ;; the current dir considered | |
643 file ;; filename being looked for | |
644 rest ;; pathname after first /* | |
645 this-suffix ;; the suffix we are currently considering | |
646 suffixes ;; working copy of suffix-list | |
647 filename ;; built filename | |
648 blist ;; list of live buffers | |
649 buf ;; current buffer in blist | |
650 found) ;; whether we have found anything | |
651 | |
652 (setq suffixes suffix-list) | |
653 | |
654 ;; suffixes is nil => fname-stub is the file we are looking for | |
655 ;; otherwise fname-stub is a stub, and we append a suffix | |
656 (if suffixes | |
657 (setq this-suffix (car suffixes)) | |
658 (setq this-suffix "") | |
659 (setq suffixes (list ""))) | |
660 | |
661 ;; find whether the file is in a buffer first | |
662 (while (and suffixes (not found)) | |
663 (setq filename (concat fname-stub this-suffix)) | |
664 | |
665 (if (not ff-quiet-mode) | |
17548 | 666 (message "Finding buffer %s..." filename)) |
10491 | 667 |
17548 | 668 (if (bufferp (get-file-buffer filename)) |
669 (setq found (buffer-file-name (get-file-buffer filename)))) | |
10491 | 670 |
671 (setq blist (buffer-list)) | |
672 (setq buf (buffer-name (car blist))) | |
673 (while (and blist (not found)) | |
674 | |
675 (if (string-match (concat filename "<[0-9]+>") buf) | |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
676 (setq found (buffer-file-name (car blist)))) |
10491 | 677 |
678 (setq blist (cdr blist)) | |
679 (setq buf (buffer-name (car blist)))) | |
680 | |
681 (setq suffixes (cdr suffixes)) | |
682 (setq this-suffix (car suffixes))) | |
683 | |
684 ;; now look for the real file | |
685 (setq dirs search-dirs) | |
686 (setq dir (car dirs)) | |
687 (while (and (not found) dirs) | |
688 | |
689 (setq suffixes suffix-list) | |
690 | |
691 ;; if dir does not contain '/*', look for the file | |
692 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir))) | |
30816 | 693 (progn |
10491 | 694 |
695 ;; suffixes is nil => fname-stub is the file we are looking for | |
696 ;; otherwise fname-stub is a stub, and we append a suffix | |
697 (if suffixes | |
698 (setq this-suffix (car suffixes)) | |
699 (setq this-suffix "") | |
700 (setq suffixes (list ""))) | |
701 | |
702 (while (and suffixes (not found)) | |
703 | |
704 (setq filename (concat fname-stub this-suffix)) | |
705 (setq file (concat dir "/" filename)) | |
706 | |
707 (if (not ff-quiet-mode) | |
17548 | 708 (message "Finding %s..." file)) |
10491 | 709 |
710 (if (file-exists-p file) | |
711 (setq found file)) | |
712 | |
713 (setq suffixes (cdr suffixes)) | |
714 (setq this-suffix (car suffixes)))) | |
715 | |
716 ;; otherwise dir matches the '/*', so search each dir separately | |
717 (progn | |
718 (if (match-beginning 2) | |
719 (setq rest (substring dir (match-beginning 2) (match-end 2))) | |
720 (setq rest "") | |
721 ) | |
722 (setq dir (substring dir (match-beginning 1) (match-end 1))) | |
723 | |
724 (let ((dirlist (ff-all-dirs-under dir '(".."))) | |
725 this-dir compl-dirs) | |
726 | |
727 (setq this-dir (car dirlist)) | |
728 (while dirlist | |
729 (setq compl-dirs | |
730 (append | |
731 compl-dirs | |
732 (list (concat this-dir rest)) | |
733 )) | |
734 (setq dirlist (cdr dirlist)) | |
735 (setq this-dir (car dirlist))) | |
736 | |
737 (if compl-dirs | |
738 (setq found (ff-get-file-name compl-dirs | |
739 fname-stub | |
740 suffix-list)))))) | |
741 (setq dirs (cdr dirs)) | |
742 (setq dir (car dirs))) | |
743 | |
744 (if found | |
745 (message "%s found" found)) | |
746 | |
747 found)) | |
748 | |
749 (defun ff-string-match (regexp string &optional start) | |
13896 | 750 "Like `string-match', but set `case-fold-search' temporarily. |
11272 | 751 The value used comes from `ff-case-fold-search'." |
752 (let ((case-fold-search ff-case-fold-search)) | |
10491 | 753 (if regexp |
11272 | 754 (string-match regexp string start)))) |
10491 | 755 |
756 (defun ff-list-replace-env-vars (search-list) | |
757 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST." | |
758 (let (list | |
759 (var (car search-list))) | |
760 (while search-list | |
761 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var) | |
762 (setq var | |
763 (concat | |
764 (substring var (match-beginning 1) (match-end 1)) | |
765 (getenv (substring var (match-beginning 2) (match-end 2))) | |
766 (substring var (match-beginning 3) (match-end 3))))) | |
767 (setq search-list (cdr search-list)) | |
768 (setq list (cons var list)) | |
769 (setq var (car search-list))) | |
770 (setq search-list (reverse list)))) | |
771 | |
772 (defun ff-treat-as-special () | |
30816 | 773 "Return the file to look for if the construct was special, else nil. |
13896 | 774 The construct is defined in the variable `ff-special-constructs'." |
10491 | 775 (let* (fname |
776 (list ff-special-constructs) | |
777 (elem (car list)) | |
778 (regexp (car elem)) | |
779 (match (cdr elem))) | |
780 (while (and list (not fname)) | |
781 (if (and (looking-at regexp) match) | |
782 (setq fname (funcall match))) | |
783 (setq list (cdr list)) | |
784 (setq elem (car list)) | |
785 (setq regexp (car elem)) | |
786 (setq match (cdr elem))) | |
787 fname)) | |
788 | |
789 (defun ff-basename (string) | |
30816 | 790 "Return the basename of pathname STRING." |
10491 | 791 (setq string (concat "/" string)) |
792 (string-match ".*/\\([^/]+\\)$" string) | |
793 (setq string (substring string (match-beginning 1) (match-end 1)))) | |
794 | |
795 (defun ff-all-dirs-under (here &optional exclude) | |
11272 | 796 "Get all the directory files under directory HERE. |
10491 | 797 Exclude all files in the optional EXCLUDE list." |
798 (if (file-directory-p here) | |
799 (condition-case nil | |
800 (progn | |
801 (let ((files (directory-files here t)) | |
802 (dirlist (list)) | |
803 file) | |
804 (while files | |
805 (setq file (car files)) | |
806 (if (and | |
807 (file-directory-p file) | |
808 (not (member (ff-basename file) exclude))) | |
809 (setq dirlist (cons file dirlist))) | |
810 (setq files (cdr files))) | |
811 (setq dirlist (reverse dirlist)))) | |
812 (error nil)) | |
813 nil)) | |
814 | |
815 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file) | |
11272 | 816 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW. |
817 In addition, this runs various hooks. | |
10491 | 818 |
11272 | 819 Either F1 or F2 receives FILE as the sole argument. |
820 The decision of which one to call is based on IN-OTHER-WINDOW | |
821 and on the global variable `ff-always-in-other-window'. | |
10491 | 822 |
11272 | 823 F1 and F2 are typically `find-file' / `find-file-other-window' |
824 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs. | |
825 | |
30816 | 826 If optional NEW-FILE is t, then a special hook (`ff-file-created-hooks') is |
11272 | 827 called before `ff-post-load-hooks'." |
10491 | 828 (if ff-pre-load-hooks |
829 (run-hooks 'ff-pre-load-hooks)) | |
830 (if (or | |
831 (and in-other-window (not ff-always-in-other-window)) | |
832 (and (not in-other-window) ff-always-in-other-window)) | |
833 (funcall f2 file) | |
834 (funcall f1 file)) | |
835 (if new-file | |
836 (if ff-file-created-hooks | |
837 (run-hooks 'ff-file-created-hooks))) | |
838 (if ff-post-load-hooks | |
839 (run-hooks 'ff-post-load-hooks))) | |
840 | |
841 (defun ff-find-file (file &optional in-other-window new-file) | |
13896 | 842 "Like `find-file', but may show the file in another window." |
30816 | 843 (ff-switch-file 'find-file |
844 'find-file-other-window | |
10491 | 845 file in-other-window new-file)) |
846 | |
13896 | 847 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window) |
848 "Like `switch-to-buffer', but may show the buffer in another window." | |
10491 | 849 |
30816 | 850 (ff-switch-file 'switch-to-buffer |
851 'switch-to-buffer-other-window | |
13896 | 852 buffer-or-name in-other-window nil)) |
10491 | 853 |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
854 ;;;###autoload |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
855 (defun ff-mouse-find-other-file (event) |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
856 "Visit the file you click on." |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
857 (interactive "e") |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
858 (save-excursion |
30816 | 859 (mouse-set-point event) |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
860 (ff-find-other-file nil))) |
10491 | 861 |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
862 ;;;###autoload |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
863 (defun ff-mouse-find-other-file-other-window (event) |
30816 | 864 "Visit the file you click on in another window." |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
865 (interactive "e") |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
866 (save-excursion |
30816 | 867 (mouse-set-point event) |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
868 (ff-find-other-file t))) |
10491 | 869 |
870 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
871 ;; This section offers an example of user defined function to select files | |
872 | |
11272 | 873 (defun ff-upcase-p (string &optional start end) |
30816 | 874 "Return t if STRING is all uppercase. |
11272 | 875 Given START and/or END, checks between these characters." |
10491 | 876 (let (match str) |
877 (if (not start) | |
878 (setq start 0)) | |
879 (if (not end) | |
880 (setq end (length string))) | |
881 (if (= start end) | |
882 (setq end (1+ end))) | |
883 (setq str (substring string start end)) | |
30816 | 884 (if (and |
10491 | 885 (ff-string-match "[A-Z]+" str) |
886 (setq match (match-data)) | |
887 (= (car match) 0) | |
888 (= (car (cdr match)) (length str))) | |
889 t | |
890 nil))) | |
891 | |
892 (defun ff-cc-hh-converter (arg) | |
11272 | 893 "Discriminate file extensions. |
894 Build up a new file list based possibly on part of the directory name | |
895 and the name of the file passed in." | |
10491 | 896 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg) |
30816 | 897 (let ((path (if (match-beginning 1) |
10491 | 898 (substring arg (match-beginning 1) (match-end 1)) nil)) |
30816 | 899 (dire (if (match-beginning 2) |
10491 | 900 (substring arg (match-beginning 2) (match-end 2)) nil)) |
30816 | 901 (file (if (match-beginning 3) |
10491 | 902 (substring arg (match-beginning 3) (match-end 3)) nil)) |
30816 | 903 (extn (if (match-beginning 4) |
10491 | 904 (substring arg (match-beginning 4) (match-end 4)) nil)) |
905 return-list) | |
906 (cond | |
907 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h} | |
30816 | 908 ((and (string= extn "cc") |
10491 | 909 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file)) |
910 (let ((stub (substring file (match-beginning 2) (match-end 2)))) | |
911 (setq dire (upcase (substring file (match-beginning 1) (match-end 1)))) | |
912 (setq return-list (list (concat stub ".hh") | |
913 (concat stub ".h") | |
914 (concat file ".hh") | |
915 (concat file ".h"))) | |
916 )) | |
917 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C} | |
11272 | 918 ((and (string= extn "hh") (ff-upcase-p dire) file) |
10491 | 919 (let ((stub (concat (downcase dire) file))) |
30816 | 920 (setq return-list (list (concat stub ".cc") |
10491 | 921 (concat stub ".C") |
922 (concat file ".cc") | |
923 (concat file ".C"))) | |
924 )) | |
925 ;; zap.cc => zap.hh or zap.h | |
926 ((string= extn "cc") | |
927 (let ((stub file)) | |
928 (setq return-list (list (concat stub ".hh") | |
929 (concat stub ".h"))) | |
930 )) | |
931 ;; zap.hh => zap.cc or zap.C | |
932 ((string= extn "hh") | |
933 (let ((stub file)) | |
934 (setq return-list (list (concat stub ".cc") | |
935 (concat stub ".C"))) | |
936 )) | |
30816 | 937 (t |
10491 | 938 nil)) |
939 | |
940 return-list)) | |
941 | |
942 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
943 ;; This section offers an example of user defined function to place point. | |
944 ;; The regexps are Ada specific. | |
945 | |
946 (defvar ff-function-name nil "Name of the function we are in.") | |
947 | |
18119
8805be62c8f7
(ada-other-file-alist): Variable definition moved to ada-mode.el.
Richard M. Stallman <rms@gnu.org>
parents:
17548
diff
changeset
|
948 (eval-when-compile (require 'ada-mode)) |
10491 | 949 |
950 ;; bind with (setq ff-pre-load-hooks 'ff-which-function-are-we-in) | |
951 ;; | |
952 (defun ff-which-function-are-we-in () | |
11272 | 953 "Return the name of the function whose definition/declaration point is in. |
954 Also remember that name in `ff-function-name'." | |
10491 | 955 |
956 (setq ff-function-name nil) | |
957 | |
958 (save-excursion | |
959 (if (re-search-backward ada-procedure-start-regexp nil t) | |
960 (setq ff-function-name (buffer-substring (match-beginning 0) | |
961 (match-end 0))) | |
962 ; we didn't find a procedure start, perhaps there is a package | |
963 (if (re-search-backward ada-package-start-regexp nil t) | |
964 (setq ff-function-name (buffer-substring (match-beginning 0) | |
965 (match-end 0))) | |
966 )))) | |
967 | |
968 ;; bind with (setq ff-post-load-hooks 'ff-set-point-accordingly) | |
969 ;; | |
970 (defun ff-set-point-accordingly () | |
11272 | 971 "Find the function specified in `ff-function-name'. |
12567
7318536fb256
(ff-set-point-accordingly): Doc fix.
Karl Heuer <kwzh@gnu.org>
parents:
11272
diff
changeset
|
972 That name was previously determined by `ff-which-function-are-we-in'." |
10491 | 973 (if ff-function-name |
974 (progn | |
975 (goto-char (point-min)) | |
976 (search-forward ff-function-name nil t)))) | |
977 | |
30816 | 978 (provide 'find-file) |
10491 | 979 |
30816 | 980 ;;; find-file.el ends here |