find-file.el 35 KB

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