Mercurial > emacs
annotate lisp/find-file.el @ 89765:b06a8c2162e5
(SJIS_TO_JIS2, JIS_TO_SJIS2): New macros.
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Tue, 27 Jan 2004 12:12:16 +0000 |
parents | 375f2633d815 |
children | 68c22ea6027c |
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 | |
44880
e772dea9fb2e
Remove Ada-specific example code.
Gerd Moellmann <gerd@gnu.org>
parents:
42097
diff
changeset
|
7 ;; Copyright (C) 1994, 1995, 2002 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")))) | |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
73 ;; |
10491 | 74 ;; ff-cc-hh-converter is included at the end of this file as a reference. |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
75 ;; |
10491 | 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 ;; | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
104 ;; - ff-pre-find-hook - called before the search for the other file starts |
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
105 ;; - ff-not-found-hook - called when the other file could not be found |
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
106 ;; - ff-pre-load-hook - called just before the other file is 'loaded' |
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
107 ;; - ff-file-created-hook - called when the other file is created |
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
108 ;; - ff-post-load-hook - called just after the other file is 'loaded' |
10491 | 109 ;; |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
110 ;; The *load-hook 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 |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
133 (defcustom ff-pre-find-hook nil |
21087 | 134 "*List of functions to be called before the search for the file starts." |
135 :type 'hook | |
136 :group 'ff) | |
10491 | 137 |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
138 (defcustom ff-pre-load-hook nil |
21087 | 139 "*List of functions to be called before the other file is loaded." |
140 :type 'hook | |
141 :group 'ff) | |
10491 | 142 |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
143 (defcustom ff-post-load-hook nil |
21087 | 144 "*List of functions to be called after the other file is loaded." |
145 :type 'hook | |
146 :group 'ff) | |
10491 | 147 |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
148 (defcustom ff-not-found-hook nil |
21087 | 149 "*List of functions to be called if the other file could not be found." |
150 :type 'hook | |
151 :group 'ff) | |
10491 | 152 |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
153 (defcustom ff-file-created-hook nil |
21087 | 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 ) | |
30816 | 192 "*A list of regular expressions for `ff-find-file'. |
193 Specifies how to recognise special constructs such as include files | |
194 etc. and an associated method for extracting the filename from that | |
195 construct.") | |
10491 | 196 |
47860
621b10466c9a
(ff-related-file-alist): New alias.
Richard M. Stallman <rms@gnu.org>
parents:
47354
diff
changeset
|
197 (defvaralias 'ff-related-file-alist 'ff-other-file-alist) |
21087 | 198 (defcustom ff-other-file-alist 'cc-other-file-alist |
10491 | 199 "*Alist of extensions to find given the current file's extension. |
200 | |
201 This list should contain the most used extensions before the others, | |
202 since the search algorithm searches sequentially through each | |
11272 | 203 directory specified in `ff-search-directories'. If a file is not found, |
204 a new one is created with the first matching extension (`.cc' yields `.hh'). | |
21087 | 205 This alist should be set by the major mode." |
206 :type '(choice (repeat (list regexp (choice (repeat string) function))) | |
207 symbol) | |
208 :group 'ff) | |
10491 | 209 |
21087 | 210 (defcustom ff-search-directories 'cc-search-directories |
10491 | 211 "*List of directories to search for a specific file. |
212 | |
11272 | 213 Set by default to `cc-search-directories', expanded at run-time. |
10491 | 214 |
215 This list is searched through with each extension specified in | |
11272 | 216 `ff-other-file-alist' that matches this file's extension. So the |
10491 | 217 longer the list, the longer it'll take to realise that a file |
218 may not exist. | |
219 | |
30816 | 220 A typical format is |
10491 | 221 |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
222 '(\".\" \"/usr/include\" \"$PROJECT/*/include\") |
10491 | 223 |
11272 | 224 Environment variables can be inserted between slashes (`/'). |
10491 | 225 They will be replaced by their definition. If a variable does |
11272 | 226 not exist, it is replaced (silently) with an empty string. |
10491 | 227 |
11272 | 228 The stars are *not* wildcards: they are searched for together with |
229 the preceding slash. The star represents all the subdirectories except | |
21087 | 230 `..', and each of these subdirectories will be searched in turn." |
231 :type '(choice (repeat directory) symbol) | |
232 :group 'ff) | |
10491 | 233 |
21087 | 234 (defcustom cc-search-directories |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
235 '("." "/usr/include" "/usr/local/include/*") |
21087 | 236 "*See the description of the `ff-search-directories' variable." |
237 :type '(repeat directory) | |
238 :group 'ff) | |
10491 | 239 |
21087 | 240 (defcustom cc-other-file-alist |
10491 | 241 '( |
242 ("\\.cc$" (".hh" ".h")) | |
243 ("\\.hh$" (".cc" ".C")) | |
244 | |
245 ("\\.c$" (".h")) | |
246 ("\\.h$" (".c" ".cc" ".C" ".CC" ".cxx" ".cpp")) | |
247 | |
248 ("\\.C$" (".H" ".hh" ".h")) | |
249 ("\\.H$" (".C" ".CC")) | |
250 | |
251 ("\\.CC$" (".HH" ".H" ".hh" ".h")) | |
252 ("\\.HH$" (".CC")) | |
253 | |
254 ("\\.cxx$" (".hh" ".h")) | |
255 ("\\.cpp$" (".hh" ".h")) | |
256 ) | |
257 "*Alist of extensions to find given the current file's extension. | |
258 | |
259 This list should contain the most used extensions before the others, | |
260 since the search algorithm searches sequentially through each directory | |
11272 | 261 specified in `ff-search-directories'. If a file is not found, a new one |
21087 | 262 is created with the first matching extension (`.cc' yields `.hh')." |
263 :type '(repeat (list regexp (choice (repeat string) function))) | |
264 :group 'ff) | |
10491 | 265 |
21087 | 266 (defcustom modula2-other-file-alist |
10491 | 267 '( |
268 ("\\.mi$" (".md")) ;; Modula-2 module definition | |
269 ("\\.md$" (".mi")) ;; and implementation. | |
270 ) | |
21087 | 271 "*See the description for the `ff-search-directories' variable." |
272 :type '(repeat (list regexp (choice (repeat string) function))) | |
273 :group 'ff) | |
274 | |
10491 | 275 |
276 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
277 ;; No user definable variables beyond this point! | |
278 ;; ============================================== | |
279 | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
280 (make-variable-buffer-local 'ff-pre-find-hook) |
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
281 (make-variable-buffer-local 'ff-pre-load-hook) |
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
282 (make-variable-buffer-local 'ff-post-load-hook) |
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
283 (make-variable-buffer-local 'ff-not-found-hook) |
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
284 (make-variable-buffer-local 'ff-file-created-hook) |
10491 | 285 (make-variable-buffer-local 'ff-case-fold-search) |
286 (make-variable-buffer-local 'ff-always-in-other-window) | |
287 (make-variable-buffer-local 'ff-ignore-include) | |
288 (make-variable-buffer-local 'ff-quiet-mode) | |
289 (make-variable-buffer-local 'ff-other-file-alist) | |
290 (make-variable-buffer-local 'ff-search-directories) | |
291 | |
292 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
293 ;; User entry points | |
294 | |
295 ;;;###autoload | |
296 (defun ff-get-other-file (&optional in-other-window) | |
11272 | 297 "Find the header or source file corresponding to this file. |
30816 | 298 See also the documentation for `ff-find-other-file'. |
10491 | 299 |
11272 | 300 If optional IN-OTHER-WINDOW is non-nil, find the file in another window." |
10491 | 301 (interactive "P") |
302 (let ((ignore ff-ignore-include)) | |
303 (setq ff-ignore-include t) | |
304 (ff-find-the-other-file in-other-window) | |
305 (setq ff-ignore-include ignore))) | |
306 | |
48009
8b3977d58df6
(ff-find-related-file): Fix autoload cookie.
Richard M. Stallman <rms@gnu.org>
parents:
47860
diff
changeset
|
307 ;;;###autoload |
47860
621b10466c9a
(ff-related-file-alist): New alias.
Richard M. Stallman <rms@gnu.org>
parents:
47354
diff
changeset
|
308 (defalias 'ff-find-related-file 'ff-find-other-file) |
621b10466c9a
(ff-related-file-alist): New alias.
Richard M. Stallman <rms@gnu.org>
parents:
47354
diff
changeset
|
309 |
10491 | 310 ;;;###autoload |
311 (defun ff-find-other-file (&optional in-other-window ignore-include) | |
11272 | 312 "Find the header or source file corresponding to this file. |
313 Being on a `#include' line pulls in that file. | |
10491 | 314 |
11272 | 315 If optional IN-OTHER-WINDOW is non-nil, find the file in the other window. |
316 If optional IGNORE-INCLUDE is non-nil, ignore being on `#include' lines. | |
10491 | 317 |
318 Variables of interest include: | |
319 | |
30816 | 320 - `ff-case-fold-search' |
321 Non-nil means ignore cases in matches (see `case-fold-search'). | |
10491 | 322 If you have extensions in different cases, you will want this to be nil. |
323 | |
30816 | 324 - `ff-always-in-other-window' |
10491 | 325 If non-nil, always open the other file in another window, unless an |
30816 | 326 argument is given to `ff-find-other-file'. |
10491 | 327 |
30816 | 328 - `ff-ignore-include' |
10491 | 329 If non-nil, ignores #include lines. |
330 | |
30816 | 331 - `ff-always-try-to-create' |
10491 | 332 If non-nil, always attempt to create the other file if it was not found. |
333 | |
30816 | 334 - `ff-quiet-mode' |
10491 | 335 If non-nil, traces which directories are being searched. |
336 | |
30816 | 337 - `ff-special-constructs' |
338 A list of regular expressions specifying how to recognise special | |
339 constructs such as include files etc, and an associated method for | |
10491 | 340 extracting the filename from that construct. |
341 | |
30816 | 342 - `ff-other-file-alist' |
10491 | 343 Alist of extensions to find given the current file's extension. |
344 | |
30816 | 345 - `ff-search-directories' |
10491 | 346 List of directories searched through with each extension specified in |
30816 | 347 `ff-other-file-alist' that matches this file's extension. |
10491 | 348 |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
349 - `ff-pre-find-hook' |
10491 | 350 List of functions to be called before the search for the file starts. |
351 | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
352 - `ff-pre-load-hook' |
10491 | 353 List of functions to be called before the other file is loaded. |
354 | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
355 - `ff-post-load-hook' |
10491 | 356 List of functions to be called after the other file is loaded. |
357 | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
358 - `ff-not-found-hook' |
10491 | 359 List of functions to be called if the other file could not be found. |
360 | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
361 - `ff-file-created-hook' |
10491 | 362 List of functions to be called if the other file has been created." |
363 (interactive "P") | |
364 (let ((ignore ff-ignore-include)) | |
365 (setq ff-ignore-include ignore-include) | |
366 (ff-find-the-other-file in-other-window) | |
367 (setq ff-ignore-include ignore))) | |
368 | |
369 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
370 ;; Support functions | |
371 | |
372 (defun ff-find-the-other-file (&optional in-other-window) | |
11272 | 373 "Find the header or source file corresponding to the current file. |
374 Being on a `#include' line pulls in that file, but see the help on | |
375 the `ff-ignore-include' variable. | |
10491 | 376 |
11272 | 377 If optional IN-OTHER-WINDOW is non-nil, find the file in another window." |
10491 | 378 |
379 (let (match ;; matching regexp for this file | |
380 suffixes ;; set of replacing regexps for the matching regexp | |
381 action ;; function to generate the names of the other files | |
382 fname ;; basename of this file | |
383 pos ;; where we start matching filenames | |
384 stub ;; name of the file without extension | |
385 alist ;; working copy of the list of file extensions | |
386 pathname ;; the pathname of the file or the #include line | |
387 default-name ;; file we should create if none found | |
30816 | 388 format ;; what we have to match |
389 found ;; name of the file or buffer found - nil if none | |
10491 | 390 dirs ;; local value of ff-search-directories |
391 no-match) ;; whether we know about this kind of file | |
392 | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
393 (run-hooks 'ff-pre-find-hook 'ff-pre-find-hooks) |
10491 | 394 |
395 (message "Working...") | |
396 | |
397 (setq dirs | |
398 (if (symbolp ff-search-directories) | |
399 (ff-list-replace-env-vars (symbol-value ff-search-directories)) | |
400 (ff-list-replace-env-vars ff-search-directories))) | |
401 | |
402 (save-excursion | |
403 (beginning-of-line 1) | |
404 (setq fname (ff-treat-as-special))) | |
405 | |
406 (cond | |
407 ((and (not ff-ignore-include) fname) | |
408 (setq default-name fname) | |
409 (setq found (ff-get-file dirs fname nil in-other-window))) | |
410 | |
411 ;; let's just get the corresponding file | |
412 (t | |
413 (setq alist (if (symbolp ff-other-file-alist) | |
414 (symbol-value ff-other-file-alist) | |
415 ff-other-file-alist) | |
416 pathname (if (buffer-file-name) | |
417 (buffer-file-name) | |
418 "/none.none")) | |
419 | |
38273
910cd30b1586
(ff-find-the-other-file): Use file-name-nondirectory
Eli Zaretskii <eliz@gnu.org>
parents:
30816
diff
changeset
|
420 (setq fname (file-name-nondirectory pathname) |
10491 | 421 no-match nil |
422 match (car alist)) | |
423 | |
424 ;; find the table entry corresponding to this file | |
425 (setq pos (ff-string-match (car match) fname)) | |
426 (while (and match (if (and pos (>= pos 0)) nil (not pos))) | |
427 (setq alist (cdr alist)) | |
428 (setq match (car alist)) | |
429 (setq pos (ff-string-match (car match) fname))) | |
430 | |
431 ;; no point going on if we haven't found anything | |
432 (if (not match) | |
433 (setq no-match t) | |
434 | |
435 ;; otherwise, suffixes contains what we need | |
436 (setq suffixes (car (cdr match)) | |
437 action (car (cdr match)) | |
438 found nil) | |
439 | |
30816 | 440 ;; if we have a function to generate new names, |
10491 | 441 ;; invoke it with the name of the current file |
442 (if (and (atom action) (fboundp action)) | |
443 (progn | |
444 (setq suffixes (funcall action (buffer-file-name)) | |
445 match (cons (car match) (list suffixes)) | |
446 stub nil | |
447 default-name (car suffixes))) | |
448 | |
449 ;; otherwise build our filename stub | |
30816 | 450 (cond |
10491 | 451 |
452 ;; get around the problem that 0 and nil both mean false! | |
453 ((= pos 0) | |
454 (setq format "") | |
455 (setq stub "") | |
456 ) | |
457 | |
458 (t | |
459 (setq format (concat "\\(.+\\)" (car match))) | |
460 (string-match format fname) | |
461 (setq stub (substring fname (match-beginning 1) (match-end 1))) | |
462 )) | |
463 | |
464 ;; if we find nothing, we should try to get a file like this one | |
465 (setq default-name | |
466 (concat stub (car (car (cdr match)))))) | |
467 | |
468 ;; do the real work - find the file | |
30816 | 469 (setq found |
10491 | 470 (ff-get-file dirs |
471 stub | |
30816 | 472 suffixes |
10491 | 473 in-other-window))))) |
474 | |
475 (cond | |
476 (no-match ;; could not even determine the other file | |
477 (message "")) | |
478 | |
30816 | 479 (t |
10491 | 480 (cond |
481 | |
482 ((not found) ;; could not find the other file | |
483 | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
484 (run-hooks 'ff-not-found-hook 'ff-not-found-hooks) |
10491 | 485 |
30816 | 486 (cond |
10491 | 487 (ff-always-try-to-create ;; try to create the file |
488 (let (name pathname) | |
489 | |
490 (setq name | |
491 (expand-file-name | |
492 (read-file-name | |
493 (format "Find or create %s in: " default-name) | |
494 default-directory default-name nil))) | |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
495 |
10491 | 496 (setq pathname |
497 (if (file-directory-p name) | |
498 (concat (file-name-as-directory name) default-name) | |
499 (setq found name))) | |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
500 |
10491 | 501 (ff-find-file pathname in-other-window t))) |
502 | |
503 (t ;; don't create the file, just whinge | |
17548 | 504 (message "No file found for %s" fname)))) |
10491 | 505 |
506 (t ;; matching file found | |
507 nil)))) | |
508 | |
509 found)) ;; return buffer-name or filename | |
510 | |
42097
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
511 (defun ff-other-file-name () |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
512 "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
|
513 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
|
514 the `ff-ignore-include' variable." |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
515 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
516 (let (match ;; matching regexp for this file |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
517 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
|
518 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
|
519 fname ;; basename of this file |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
520 pos ;; where we start matching filenames |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
521 stub ;; name of the file without extension |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
522 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
|
523 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
|
524 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
|
525 format ;; what we have to match |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
526 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
|
527 dirs ;; local value of ff-search-directories |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
528 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
|
529 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
530 (message "Working...") |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
531 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
532 (setq dirs |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
533 (if (symbolp ff-search-directories) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
534 (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
|
535 (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
|
536 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
537 (save-excursion |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
538 (beginning-of-line 1) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
539 (setq fname (ff-treat-as-special))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
540 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
541 (cond |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
542 ((and (not ff-ignore-include) fname) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
543 (setq default-name fname) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
544 (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
|
545 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
546 ;; let's just get the corresponding file |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
547 (t |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
548 (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
|
549 (symbol-value ff-other-file-alist) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
550 ff-other-file-alist) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
551 pathname (if (buffer-file-name) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
552 (buffer-file-name) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
553 "/none.none")) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
554 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
555 (setq fname (file-name-nondirectory pathname) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
556 no-match nil |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
557 match (car alist)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
558 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
559 ;; 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
|
560 (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
|
561 (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
|
562 (setq alist (cdr alist)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
563 (setq match (car alist)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
564 (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
|
565 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
566 ;; 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
|
567 (if (not match) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
568 (setq no-match t) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
569 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
570 ;; otherwise, suffixes contains what we need |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
571 (setq suffixes (car (cdr match)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
572 action (car (cdr match)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
573 found nil) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
574 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
575 ;; 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
|
576 ;; 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
|
577 (if (and (atom action) (fboundp action)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
578 (progn |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
579 (setq suffixes (funcall action (buffer-file-name)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
580 match (cons (car match) (list suffixes)) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
581 stub nil |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
582 default-name (car suffixes))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
583 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
584 ;; otherwise build our filename stub |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
585 (cond |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
586 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
587 ;; 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
|
588 ((= pos 0) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
589 (setq format "") |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
590 (setq stub "") |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
591 ) |
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 (t |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
594 (setq format (concat "\\(.+\\)" (car match))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
595 (string-match format fname) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
596 (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
|
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 ;; 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
|
600 (setq default-name |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
601 (concat stub (car (car (cdr match)))))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
602 |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
603 ;; do the real work - find the file |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
604 (setq found |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
605 (ff-get-file-name dirs stub suffixes))))) |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
606 found)) ;; return buffer-name or filename |
e12d545c9a48
(ff-other-file-name): New function.
Richard M. Stallman <rms@gnu.org>
parents:
38273
diff
changeset
|
607 |
30816 | 608 (defun ff-get-file (search-dirs filename &optional suffix-list other-window) |
609 "Find a file in the SEARCH-DIRS with the given FILENAME (or filename stub). | |
610 If (optional) SUFFIX-LIST is nil, search for fname, otherwise search | |
611 for fname with each of the given suffixes. Get the file or the buffer | |
612 corresponding to the name of the first file found, or nil." | |
613 (let ((filename (ff-get-file-name search-dirs filename suffix-list))) | |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
614 |
30816 | 615 (cond |
10491 | 616 ((not filename) |
617 nil) | |
618 | |
13896 | 619 ((bufferp (get-file-buffer filename)) |
620 (ff-switch-to-buffer (get-file-buffer filename) other-window) | |
10491 | 621 filename) |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
622 |
10491 | 623 ((file-exists-p filename) |
624 (ff-find-file filename other-window nil) | |
625 filename) | |
626 | |
627 (t | |
628 nil)))) | |
629 | |
630 (defun ff-get-file-name (search-dirs fname-stub &optional suffix-list) | |
30816 | 631 "Find a file in SEARCH-DIRS with the given name (or stub) FNAME-STUB. |
632 If (optional) SUFFIX-LIST is nil, search for FNAME-STUB, otherwise | |
633 search for FNAME-STUB with each of the given suffixes. Return the | |
634 name of the first file found." | |
10491 | 635 (let* (dirs ;; working copy of dirs to search |
636 dir ;; the current dir considered | |
637 file ;; filename being looked for | |
638 rest ;; pathname after first /* | |
639 this-suffix ;; the suffix we are currently considering | |
640 suffixes ;; working copy of suffix-list | |
641 filename ;; built filename | |
642 blist ;; list of live buffers | |
643 buf ;; current buffer in blist | |
644 found) ;; whether we have found anything | |
645 | |
646 (setq suffixes suffix-list) | |
647 | |
648 ;; suffixes is nil => fname-stub is the file we are looking for | |
649 ;; otherwise fname-stub is a stub, and we append a suffix | |
650 (if suffixes | |
651 (setq this-suffix (car suffixes)) | |
652 (setq this-suffix "") | |
653 (setq suffixes (list ""))) | |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
654 |
10491 | 655 ;; find whether the file is in a buffer first |
656 (while (and suffixes (not found)) | |
657 (setq filename (concat fname-stub this-suffix)) | |
658 | |
659 (if (not ff-quiet-mode) | |
17548 | 660 (message "Finding buffer %s..." filename)) |
10491 | 661 |
17548 | 662 (if (bufferp (get-file-buffer filename)) |
663 (setq found (buffer-file-name (get-file-buffer filename)))) | |
10491 | 664 |
665 (setq blist (buffer-list)) | |
666 (setq buf (buffer-name (car blist))) | |
667 (while (and blist (not found)) | |
668 | |
669 (if (string-match (concat filename "<[0-9]+>") buf) | |
16510
e619a826afdb
Enabled commentary for Finder.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
670 (setq found (buffer-file-name (car blist)))) |
10491 | 671 |
672 (setq blist (cdr blist)) | |
673 (setq buf (buffer-name (car blist)))) | |
674 | |
675 (setq suffixes (cdr suffixes)) | |
676 (setq this-suffix (car suffixes))) | |
677 | |
678 ;; now look for the real file | |
679 (setq dirs search-dirs) | |
680 (setq dir (car dirs)) | |
681 (while (and (not found) dirs) | |
682 | |
683 (setq suffixes suffix-list) | |
684 | |
685 ;; if dir does not contain '/*', look for the file | |
686 (if (and dir (not (string-match "\\([^*]*\\)/\\\*\\(/.*\\)*" dir))) | |
30816 | 687 (progn |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
688 |
10491 | 689 ;; suffixes is nil => fname-stub is the file we are looking for |
690 ;; otherwise fname-stub is a stub, and we append a suffix | |
691 (if suffixes | |
692 (setq this-suffix (car suffixes)) | |
693 (setq this-suffix "") | |
694 (setq suffixes (list ""))) | |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
695 |
10491 | 696 (while (and suffixes (not found)) |
697 | |
698 (setq filename (concat fname-stub this-suffix)) | |
699 (setq file (concat dir "/" filename)) | |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
700 |
10491 | 701 (if (not ff-quiet-mode) |
17548 | 702 (message "Finding %s..." file)) |
10491 | 703 |
704 (if (file-exists-p file) | |
705 (setq found file)) | |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
706 |
10491 | 707 (setq suffixes (cdr suffixes)) |
708 (setq this-suffix (car suffixes)))) | |
709 | |
710 ;; otherwise dir matches the '/*', so search each dir separately | |
711 (progn | |
712 (if (match-beginning 2) | |
713 (setq rest (substring dir (match-beginning 2) (match-end 2))) | |
714 (setq rest "") | |
715 ) | |
716 (setq dir (substring dir (match-beginning 1) (match-end 1))) | |
717 | |
718 (let ((dirlist (ff-all-dirs-under dir '(".."))) | |
719 this-dir compl-dirs) | |
720 | |
721 (setq this-dir (car dirlist)) | |
722 (while dirlist | |
723 (setq compl-dirs | |
724 (append | |
725 compl-dirs | |
726 (list (concat this-dir rest)) | |
727 )) | |
728 (setq dirlist (cdr dirlist)) | |
729 (setq this-dir (car dirlist))) | |
730 | |
731 (if compl-dirs | |
732 (setq found (ff-get-file-name compl-dirs | |
733 fname-stub | |
734 suffix-list)))))) | |
735 (setq dirs (cdr dirs)) | |
736 (setq dir (car dirs))) | |
737 | |
738 (if found | |
739 (message "%s found" found)) | |
740 | |
741 found)) | |
742 | |
743 (defun ff-string-match (regexp string &optional start) | |
13896 | 744 "Like `string-match', but set `case-fold-search' temporarily. |
11272 | 745 The value used comes from `ff-case-fold-search'." |
746 (let ((case-fold-search ff-case-fold-search)) | |
10491 | 747 (if regexp |
11272 | 748 (string-match regexp string start)))) |
10491 | 749 |
750 (defun ff-list-replace-env-vars (search-list) | |
751 "Replace environment variables (of the form $VARIABLE) in SEARCH-LIST." | |
752 (let (list | |
753 (var (car search-list))) | |
754 (while search-list | |
755 (if (string-match "\\(.*\\)\\$[({]*\\([a-zA-Z0-9_]+\\)[)}]*\\(.*\\)" var) | |
756 (setq var | |
757 (concat | |
758 (substring var (match-beginning 1) (match-end 1)) | |
759 (getenv (substring var (match-beginning 2) (match-end 2))) | |
760 (substring var (match-beginning 3) (match-end 3))))) | |
761 (setq search-list (cdr search-list)) | |
762 (setq list (cons var list)) | |
763 (setq var (car search-list))) | |
764 (setq search-list (reverse list)))) | |
765 | |
766 (defun ff-treat-as-special () | |
30816 | 767 "Return the file to look for if the construct was special, else nil. |
13896 | 768 The construct is defined in the variable `ff-special-constructs'." |
10491 | 769 (let* (fname |
770 (list ff-special-constructs) | |
771 (elem (car list)) | |
772 (regexp (car elem)) | |
773 (match (cdr elem))) | |
774 (while (and list (not fname)) | |
775 (if (and (looking-at regexp) match) | |
776 (setq fname (funcall match))) | |
777 (setq list (cdr list)) | |
778 (setq elem (car list)) | |
779 (setq regexp (car elem)) | |
780 (setq match (cdr elem))) | |
781 fname)) | |
782 | |
783 (defun ff-basename (string) | |
30816 | 784 "Return the basename of pathname STRING." |
10491 | 785 (setq string (concat "/" string)) |
786 (string-match ".*/\\([^/]+\\)$" string) | |
787 (setq string (substring string (match-beginning 1) (match-end 1)))) | |
788 | |
789 (defun ff-all-dirs-under (here &optional exclude) | |
11272 | 790 "Get all the directory files under directory HERE. |
10491 | 791 Exclude all files in the optional EXCLUDE list." |
792 (if (file-directory-p here) | |
793 (condition-case nil | |
794 (progn | |
795 (let ((files (directory-files here t)) | |
796 (dirlist (list)) | |
797 file) | |
798 (while files | |
799 (setq file (car files)) | |
800 (if (and | |
801 (file-directory-p file) | |
802 (not (member (ff-basename file) exclude))) | |
803 (setq dirlist (cons file dirlist))) | |
804 (setq files (cdr files))) | |
805 (setq dirlist (reverse dirlist)))) | |
806 (error nil)) | |
807 nil)) | |
808 | |
809 (defun ff-switch-file (f1 f2 file &optional in-other-window new-file) | |
11272 | 810 "Call F1 or F2 on FILE, according to IN-OTHER-WINDOW. |
811 In addition, this runs various hooks. | |
10491 | 812 |
11272 | 813 Either F1 or F2 receives FILE as the sole argument. |
814 The decision of which one to call is based on IN-OTHER-WINDOW | |
815 and on the global variable `ff-always-in-other-window'. | |
10491 | 816 |
11272 | 817 F1 and F2 are typically `find-file' / `find-file-other-window' |
818 or `switch-to-buffer' / `switch-to-buffer-other-window' function pairs. | |
819 | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
820 If optional NEW-FILE is t, then a special hook (`ff-file-created-hook') is |
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
821 called before `ff-post-load-hook'." |
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
822 (run-hooks 'ff-pre-load-hook 'ff-pre-load-hooks) |
10491 | 823 (if (or |
824 (and in-other-window (not ff-always-in-other-window)) | |
825 (and (not in-other-window) ff-always-in-other-window)) | |
826 (funcall f2 file) | |
827 (funcall f1 file)) | |
828 (if new-file | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
829 (run-hooks 'ff-file-created-hook 'ff-file-created-hooks)) |
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
830 (run-hooks 'ff-post-load-hook 'ff-post-load-hooks)) |
10491 | 831 |
832 (defun ff-find-file (file &optional in-other-window new-file) | |
13896 | 833 "Like `find-file', but may show the file in another window." |
30816 | 834 (ff-switch-file 'find-file |
835 'find-file-other-window | |
10491 | 836 file in-other-window new-file)) |
837 | |
13896 | 838 (defun ff-switch-to-buffer (buffer-or-name &optional in-other-window) |
839 "Like `switch-to-buffer', but may show the buffer in another window." | |
10491 | 840 |
30816 | 841 (ff-switch-file 'switch-to-buffer |
842 'switch-to-buffer-other-window | |
13896 | 843 buffer-or-name in-other-window nil)) |
10491 | 844 |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
845 ;;;###autoload |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
846 (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
|
847 "Visit the file you click on." |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
848 (interactive "e") |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
849 (save-excursion |
30816 | 850 (mouse-set-point event) |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
851 (ff-find-other-file nil))) |
10491 | 852 |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
853 ;;;###autoload |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
854 (defun ff-mouse-find-other-file-other-window (event) |
30816 | 855 "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
|
856 (interactive "e") |
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
857 (save-excursion |
30816 | 858 (mouse-set-point event) |
22192
bf83c23f3300
(ff-emacs-19, ff-xemacs): Functions deleted.
Richard M. Stallman <rms@gnu.org>
parents:
21087
diff
changeset
|
859 (ff-find-other-file t))) |
10491 | 860 |
861 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
862 ;; This section offers an example of user defined function to select files | |
863 | |
11272 | 864 (defun ff-upcase-p (string &optional start end) |
30816 | 865 "Return t if STRING is all uppercase. |
11272 | 866 Given START and/or END, checks between these characters." |
10491 | 867 (let (match str) |
868 (if (not start) | |
869 (setq start 0)) | |
870 (if (not end) | |
871 (setq end (length string))) | |
872 (if (= start end) | |
873 (setq end (1+ end))) | |
874 (setq str (substring string start end)) | |
30816 | 875 (if (and |
10491 | 876 (ff-string-match "[A-Z]+" str) |
877 (setq match (match-data)) | |
878 (= (car match) 0) | |
879 (= (car (cdr match)) (length str))) | |
880 t | |
881 nil))) | |
882 | |
883 (defun ff-cc-hh-converter (arg) | |
11272 | 884 "Discriminate file extensions. |
885 Build up a new file list based possibly on part of the directory name | |
886 and the name of the file passed in." | |
10491 | 887 (ff-string-match "\\(.*\\)/\\([^/]+\\)/\\([^.]+\\).\\([^/]+\\)$" arg) |
30816 | 888 (let ((path (if (match-beginning 1) |
10491 | 889 (substring arg (match-beginning 1) (match-end 1)) nil)) |
30816 | 890 (dire (if (match-beginning 2) |
10491 | 891 (substring arg (match-beginning 2) (match-end 2)) nil)) |
30816 | 892 (file (if (match-beginning 3) |
10491 | 893 (substring arg (match-beginning 3) (match-end 3)) nil)) |
30816 | 894 (extn (if (match-beginning 4) |
10491 | 895 (substring arg (match-beginning 4) (match-end 4)) nil)) |
896 return-list) | |
897 (cond | |
898 ;; fooZapJunk.cc => ZapJunk.{hh,h} or fooZapJunk.{hh,h} | |
30816 | 899 ((and (string= extn "cc") |
10491 | 900 (ff-string-match "^\\([a-z]+\\)\\([A-Z].+\\)$" file)) |
901 (let ((stub (substring file (match-beginning 2) (match-end 2)))) | |
902 (setq dire (upcase (substring file (match-beginning 1) (match-end 1)))) | |
903 (setq return-list (list (concat stub ".hh") | |
904 (concat stub ".h") | |
905 (concat file ".hh") | |
906 (concat file ".h"))) | |
907 )) | |
908 ;; FOO/ZapJunk.hh => fooZapJunk.{cc,C} or ZapJunk.{cc,C} | |
11272 | 909 ((and (string= extn "hh") (ff-upcase-p dire) file) |
10491 | 910 (let ((stub (concat (downcase dire) file))) |
30816 | 911 (setq return-list (list (concat stub ".cc") |
10491 | 912 (concat stub ".C") |
913 (concat file ".cc") | |
914 (concat file ".C"))) | |
915 )) | |
916 ;; zap.cc => zap.hh or zap.h | |
917 ((string= extn "cc") | |
918 (let ((stub file)) | |
919 (setq return-list (list (concat stub ".hh") | |
920 (concat stub ".h"))) | |
921 )) | |
922 ;; zap.hh => zap.cc or zap.C | |
923 ((string= extn "hh") | |
924 (let ((stub file)) | |
925 (setq return-list (list (concat stub ".cc") | |
926 (concat stub ".C"))) | |
927 )) | |
30816 | 928 (t |
10491 | 929 nil)) |
49588
37645a051842
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
48009
diff
changeset
|
930 |
10491 | 931 return-list)) |
932 | |
933 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
934 ;; This section offers an example of user defined function to place point. | |
935 ;; The regexps are Ada specific. | |
936 | |
937 (defvar ff-function-name nil "Name of the function we are in.") | |
938 | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
939 ;; bind with (setq ff-pre-load-hook 'ff-which-function-are-we-in) |
10491 | 940 ;; |
941 (defun ff-which-function-are-we-in () | |
11272 | 942 "Return the name of the function whose definition/declaration point is in. |
943 Also remember that name in `ff-function-name'." | |
10491 | 944 |
945 (setq ff-function-name nil) | |
946 | |
947 (save-excursion | |
948 (if (re-search-backward ada-procedure-start-regexp nil t) | |
949 (setq ff-function-name (buffer-substring (match-beginning 0) | |
950 (match-end 0))) | |
951 ; we didn't find a procedure start, perhaps there is a package | |
952 (if (re-search-backward ada-package-start-regexp nil t) | |
953 (setq ff-function-name (buffer-substring (match-beginning 0) | |
954 (match-end 0))) | |
955 )))) | |
956 | |
47354
27533450eb3b
(ff-pre-find-hook, ff-pre-load-hook, ff-post-load-hook)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44880
diff
changeset
|
957 ;; bind with (setq ff-post-load-hook 'ff-set-point-accordingly) |
10491 | 958 ;; |
959 (defun ff-set-point-accordingly () | |
11272 | 960 "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
|
961 That name was previously determined by `ff-which-function-are-we-in'." |
10491 | 962 (if ff-function-name |
963 (progn | |
964 (goto-char (point-min)) | |
965 (search-forward ff-function-name nil t)))) | |
966 | |
30816 | 967 (provide 'find-file) |
10491 | 968 |
30816 | 969 ;;; find-file.el ends here |