find-func.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. ;;; find-func.el --- find the definition of the Emacs Lisp function near point -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1997, 1999, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: Jens Petersen <petersen@kurims.kyoto-u.ac.jp>
  4. ;; Maintainer: petersen@kurims.kyoto-u.ac.jp
  5. ;; Keywords: emacs-lisp, functions, variables
  6. ;; Created: 97/07/25
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;
  20. ;; The funniest thing about this is that I can't imagine why a package
  21. ;; so obviously useful as this hasn't been written before!!
  22. ;; ;;; find-func
  23. ;; (find-function-setup-keys)
  24. ;;
  25. ;; or just:
  26. ;;
  27. ;; (load "find-func")
  28. ;;
  29. ;; if you don't like the given keybindings and away you go! It does
  30. ;; pretty much what you would expect, putting the cursor at the
  31. ;; definition of the function or variable at point.
  32. ;;
  33. ;; The code started out from `describe-function', `describe-key'
  34. ;; ("help.el") and `fff-find-loaded-emacs-lisp-function' (Noah Friedman's
  35. ;; "fff.el").
  36. ;;; Code:
  37. ;;; User variables:
  38. (defgroup find-function nil
  39. "Finds the definition of the Emacs Lisp symbol near point."
  40. ;; :prefix "find-function"
  41. :group 'lisp)
  42. (defconst find-function-space-re "\\(?:\\s-\\|\n\\|;.*\n\\)+")
  43. (defcustom find-function-regexp
  44. ;; Match things like (defun foo ...), (defmacro foo ...),
  45. ;; (define-skeleton foo ...), (define-generic-mode 'foo ...),
  46. ;; (define-derived-mode foo ...), (define-minor-mode foo)
  47. (concat
  48. "^\\s-*(\\(def\\(ine-skeleton\\|ine-generic-mode\\|ine-derived-mode\\|\
  49. ine\\(?:-global\\)?-minor-mode\\|ine-compilation-mode\\|un-cvs-mode\\|\
  50. foo\\|\\(?:[^icfgv]\\|g[^r]\\)\\(\\w\\|\\s_\\)+\\*?\\)\\|easy-mmode-define-[a-z-]+\\|easy-menu-define\\|\
  51. menu-bar-make-toggle\\)"
  52. find-function-space-re
  53. "\\('\\|\(quote \\)?%s\\(\\s-\\|$\\|\(\\|\)\\)")
  54. "The regexp used by `find-function' to search for a function definition.
  55. Note it must contain a `%s' at the place where `format'
  56. should insert the function name. The default value avoids `defconst',
  57. `defgroup', `defvar', `defface'.
  58. Please send improvements and fixes to the maintainer."
  59. :type 'regexp
  60. :group 'find-function
  61. :version "21.1")
  62. (defcustom find-variable-regexp
  63. (concat
  64. "^\\s-*(\\(def[^fumag]\\(\\w\\|\\s_\\)+\\*?\\|\
  65. easy-mmode-def\\(map\\|syntax\\)\\|easy-menu-define\\)"
  66. find-function-space-re
  67. "%s\\(\\s-\\|$\\)")
  68. "The regexp used by `find-variable' to search for a variable definition.
  69. Note it must contain a `%s' at the place where `format'
  70. should insert the variable name. The default value
  71. avoids `defun', `defmacro', `defalias', `defadvice', `defgroup', `defface'.
  72. Please send improvements and fixes to the maintainer."
  73. :type 'regexp
  74. :group 'find-function
  75. :version "21.1")
  76. (defcustom find-face-regexp
  77. (concat"^\\s-*(defface" find-function-space-re "%s\\(\\s-\\|$\\)")
  78. "The regexp used by `find-face' to search for a face definition.
  79. Note it must contain a `%s' at the place where `format'
  80. should insert the face name.
  81. Please send improvements and fixes to the maintainer."
  82. :type 'regexp
  83. :group 'find-function
  84. :version "22.1")
  85. (defcustom find-feature-regexp
  86. (concat ";;; Code:")
  87. "The regexp used by `xref-find-definitions' when searching for a feature definition.
  88. Note it must contain a `%s' at the place where `format'
  89. should insert the feature name."
  90. ;; We search for ";;; Code" rather than (feature '%s) because the
  91. ;; former is near the start of the code, and the latter is very
  92. ;; uninteresting. If the regexp is not found, just goes to
  93. ;; (point-min), which is acceptable in this case.
  94. :type 'regexp
  95. :group 'xref
  96. :version "25.0")
  97. (defcustom find-alias-regexp
  98. "(defalias +'%s"
  99. "The regexp used by `xref-find-definitions' to search for an alias definition.
  100. Note it must contain a `%s' at the place where `format'
  101. should insert the feature name."
  102. :type 'regexp
  103. :group 'xref
  104. :version "25.0")
  105. (defvar find-function-regexp-alist
  106. '((nil . find-function-regexp)
  107. (defvar . find-variable-regexp)
  108. (defface . find-face-regexp)
  109. (feature . find-feature-regexp)
  110. (defalias . find-alias-regexp))
  111. "Alist mapping definition types into regexp variables.
  112. Each regexp variable's value should actually be a format string
  113. to be used to substitute the desired symbol name into the regexp.
  114. Instead of regexp variable, types can be mapped to functions as well,
  115. in which case the function is called with one argument (the object
  116. we're looking for) and it should search for it.")
  117. (put 'find-function-regexp-alist 'risky-local-variable t)
  118. (defcustom find-function-source-path nil
  119. "The default list of directories where `find-function' searches.
  120. If this variable is nil then `find-function' searches `load-path' by
  121. default."
  122. :type '(repeat directory)
  123. :group 'find-function)
  124. (defcustom find-function-recenter-line 1
  125. "The window line-number from which to start displaying a symbol definition.
  126. A value of nil implies center the beginning of the definition.
  127. See `find-function' and `find-variable'."
  128. :type '(choice (const :tag "Center" nil)
  129. integer)
  130. :group 'find-function
  131. :version "20.3")
  132. (defcustom find-function-after-hook nil
  133. "Hook run after finding symbol definition.
  134. See the functions `find-function' and `find-variable'."
  135. :type 'hook
  136. :group 'find-function
  137. :version "20.3")
  138. ;;; Functions:
  139. (defun find-library-suffixes ()
  140. (let ((suffixes nil))
  141. (dolist (suffix (get-load-suffixes) (nreverse suffixes))
  142. (unless (string-match "elc" suffix) (push suffix suffixes)))))
  143. (defun find-library--load-name (library)
  144. (let ((name library))
  145. (dolist (dir load-path)
  146. (let ((rel (file-relative-name library dir)))
  147. (if (and (not (string-match "\\`\\.\\./" rel))
  148. (< (length rel) (length name)))
  149. (setq name rel))))
  150. (unless (equal name library) name)))
  151. (defun find-library-name (library)
  152. "Return the absolute file name of the Emacs Lisp source of LIBRARY.
  153. LIBRARY should be a string (the name of the library)."
  154. ;; If the library is byte-compiled, try to find a source library by
  155. ;; the same name.
  156. (if (string-match "\\.el\\(c\\(\\..*\\)?\\)\\'" library)
  157. (setq library (replace-match "" t t library)))
  158. (or
  159. (locate-file library
  160. (or find-function-source-path load-path)
  161. (find-library-suffixes))
  162. (locate-file library
  163. (or find-function-source-path load-path)
  164. load-file-rep-suffixes)
  165. (when (file-name-absolute-p library)
  166. (let ((rel (find-library--load-name library)))
  167. (when rel
  168. (or
  169. (locate-file rel
  170. (or find-function-source-path load-path)
  171. (find-library-suffixes))
  172. (locate-file rel
  173. (or find-function-source-path load-path)
  174. load-file-rep-suffixes)))))
  175. (error "Can't find library %s" library)))
  176. (defvar find-function-C-source-directory
  177. (let ((dir (expand-file-name "src" source-directory)))
  178. (if (file-accessible-directory-p dir) dir))
  179. "Directory where the C source files of Emacs can be found.
  180. If nil, do not try to find the source code of functions and variables
  181. defined in C.")
  182. (declare-function ad-get-advice-info "advice" (function))
  183. (defun find-function-advised-original (func)
  184. "Return the original function definition of an advised function FUNC.
  185. If FUNC is not a symbol, return it. Else, if it's not advised,
  186. return the symbol's function definition."
  187. (or (and (symbolp func)
  188. (featurep 'nadvice)
  189. (let ((ofunc (advice--symbol-function func)))
  190. (if (advice--p ofunc)
  191. (advice--cd*r ofunc)
  192. ofunc)))
  193. func))
  194. (defun find-function-C-source (fun-or-var file type)
  195. "Find the source location where FUN-OR-VAR is defined in FILE.
  196. TYPE should be nil to find a function, or `defvar' to find a variable."
  197. (let ((dir (or find-function-C-source-directory
  198. (read-directory-name "Emacs C source dir: " nil nil t))))
  199. (setq file (expand-file-name file dir))
  200. (if (file-readable-p file)
  201. (if (null find-function-C-source-directory)
  202. (setq find-function-C-source-directory dir))
  203. (error "The C source file %s is not available"
  204. (file-name-nondirectory file))))
  205. (unless type
  206. ;; Either or both an alias and its target might be advised.
  207. (setq fun-or-var (find-function-advised-original
  208. (indirect-function
  209. (find-function-advised-original fun-or-var)))))
  210. (with-current-buffer (find-file-noselect file)
  211. (goto-char (point-min))
  212. (unless (re-search-forward
  213. (if type
  214. (concat "DEFVAR[A-Z_]*[ \t\n]*([ \t\n]*\""
  215. (regexp-quote (symbol-name fun-or-var))
  216. "\"")
  217. (concat "DEFUN[ \t\n]*([ \t\n]*\""
  218. (regexp-quote (subr-name (advice--cd*r fun-or-var)))
  219. "\""))
  220. nil t)
  221. (error "Can't find source for %s" fun-or-var))
  222. (cons (current-buffer) (match-beginning 0))))
  223. ;;;###autoload
  224. (defun find-library (library)
  225. "Find the Emacs Lisp source of LIBRARY.
  226. LIBRARY should be a string (the name of the library)."
  227. (interactive
  228. (let* ((dirs (or find-function-source-path load-path))
  229. (suffixes (find-library-suffixes))
  230. (table (apply-partially 'locate-file-completion-table
  231. dirs suffixes))
  232. (def (if (eq (function-called-at-point) 'require)
  233. ;; `function-called-at-point' may return 'require
  234. ;; with `point' anywhere on this line. So wrap the
  235. ;; `save-excursion' below in a `condition-case' to
  236. ;; avoid reporting a scan-error here.
  237. (condition-case nil
  238. (save-excursion
  239. (backward-up-list)
  240. (forward-char)
  241. (forward-sexp 2)
  242. (thing-at-point 'symbol))
  243. (error nil))
  244. (thing-at-point 'symbol))))
  245. (when (and def (not (test-completion def table)))
  246. (setq def nil))
  247. (list
  248. (completing-read (if def (format "Library name (default %s): " def)
  249. "Library name: ")
  250. table nil nil nil nil def))))
  251. (let ((buf (find-file-noselect (find-library-name library))))
  252. (condition-case nil (switch-to-buffer buf) (error (pop-to-buffer buf)))))
  253. ;;;###autoload
  254. (defun find-function-search-for-symbol (symbol type library)
  255. "Search for SYMBOL's definition of type TYPE in LIBRARY.
  256. Visit the library in a buffer, and return a cons cell (BUFFER . POSITION),
  257. or just (BUFFER . nil) if the definition can't be found in the file.
  258. If TYPE is nil, look for a function definition.
  259. Otherwise, TYPE specifies the kind of definition,
  260. and it is interpreted via `find-function-regexp-alist'.
  261. The search is done in the source for library LIBRARY."
  262. (if (null library)
  263. (error "Don't know where ‘%s’ is defined" symbol))
  264. ;; Some functions are defined as part of the construct
  265. ;; that defines something else.
  266. (while (and (symbolp symbol) (get symbol 'definition-name))
  267. (setq symbol (get symbol 'definition-name)))
  268. (if (string-match "\\`src/\\(.*\\.\\(c\\|m\\)\\)\\'" library)
  269. (find-function-C-source symbol (match-string 1 library) type)
  270. (when (string-match "\\.el\\(c\\)\\'" library)
  271. (setq library (substring library 0 (match-beginning 1))))
  272. ;; Strip extension from .emacs.el to make sure symbol is searched in
  273. ;; .emacs too.
  274. (when (string-match "\\.emacs\\(.el\\)" library)
  275. (setq library (substring library 0 (match-beginning 1))))
  276. (let* ((filename (find-library-name library))
  277. (regexp-symbol (cdr (assq type find-function-regexp-alist))))
  278. (with-current-buffer (find-file-noselect filename)
  279. (let ((regexp (if (functionp regexp-symbol) regexp-symbol
  280. (format (symbol-value regexp-symbol)
  281. ;; Entry for ` (backquote) macro in loaddefs.el,
  282. ;; (defalias (quote \`)..., has a \ but
  283. ;; (symbol-name symbol) doesn't. Add an
  284. ;; optional \ to catch this.
  285. (concat "\\\\?"
  286. (regexp-quote (symbol-name symbol))))))
  287. (case-fold-search))
  288. (with-syntax-table emacs-lisp-mode-syntax-table
  289. (goto-char (point-min))
  290. (if (if (functionp regexp)
  291. (funcall regexp symbol)
  292. (or (re-search-forward regexp nil t)
  293. ;; `regexp' matches definitions using known forms like
  294. ;; `defun', or `defvar'. But some functions/variables
  295. ;; are defined using special macros (or functions), so
  296. ;; if `regexp' can't find the definition, we look for
  297. ;; something of the form "(SOMETHING <symbol> ...)".
  298. ;; This fails to distinguish function definitions from
  299. ;; variable declarations (or even uses thereof), but is
  300. ;; a good pragmatic fallback.
  301. (re-search-forward
  302. (concat "^([^ ]+" find-function-space-re "['(]?"
  303. (regexp-quote (symbol-name symbol))
  304. "\\_>")
  305. nil t)))
  306. (progn
  307. (beginning-of-line)
  308. (cons (current-buffer) (point)))
  309. (cons (current-buffer) nil))))))))
  310. (defun find-function-library (function &optional lisp-only verbose)
  311. "Return the pair (ORIG-FUNCTION . LIBRARY) for FUNCTION.
  312. ORIG-FUNCTION is the original name, after removing all advice and
  313. resolving aliases. LIBRARY is an absolute file name, a relative
  314. file name inside the C sources directory, or a name of an
  315. autoloaded feature.
  316. If ORIG-FUNCTION is a built-in function and LISP-ONLY is non-nil,
  317. signal an error.
  318. If VERBOSE is non-nil, and FUNCTION is an alias, display a
  319. message about the whole chain of aliases."
  320. (let ((def (if (symbolp function)
  321. (find-function-advised-original function)))
  322. aliases)
  323. ;; FIXME for completeness, it might be nice to print something like:
  324. ;; foo (which is advised), which is an alias for bar (which is advised).
  325. (while (and def (symbolp def))
  326. (or (eq def function)
  327. (not verbose)
  328. (setq aliases (if aliases
  329. (concat aliases
  330. (format-message
  331. ", which is an alias for ‘%s’"
  332. (symbol-name def)))
  333. (format-message "‘%s’ is an alias for ‘%s’"
  334. function (symbol-name def)))))
  335. (setq function (find-function-advised-original function)
  336. def (find-function-advised-original function)))
  337. (if aliases
  338. (message "%s" aliases))
  339. (cons function
  340. (cond
  341. ((autoloadp def) (nth 1 def))
  342. ((subrp def)
  343. (if lisp-only
  344. (error "%s is a built-in function" function))
  345. (help-C-file-name def 'subr))
  346. ((symbol-file function 'defun))))))
  347. ;;;###autoload
  348. (defun find-function-noselect (function &optional lisp-only)
  349. "Return a pair (BUFFER . POINT) pointing to the definition of FUNCTION.
  350. Finds the source file containing the definition of FUNCTION
  351. in a buffer and the point of the definition. The buffer is
  352. not selected. If the function definition can't be found in
  353. the buffer, returns (BUFFER).
  354. If FUNCTION is a built-in function, this function normally
  355. attempts to find it in the Emacs C sources; however, if LISP-ONLY
  356. is non-nil, signal an error instead.
  357. If the file where FUNCTION is defined is not known, then it is
  358. searched for in `find-function-source-path' if non-nil, otherwise
  359. in `load-path'."
  360. (if (not function)
  361. (error "You didn't specify a function"))
  362. (let ((func-lib (find-function-library function lisp-only t)))
  363. (find-function-search-for-symbol (car func-lib) nil (cdr func-lib))))
  364. (defun find-function-read (&optional type)
  365. "Read and return an interned symbol, defaulting to the one near point.
  366. If TYPE is nil, insist on a symbol with a function definition.
  367. Otherwise TYPE should be `defvar' or `defface'.
  368. If TYPE is nil, defaults using `function-called-at-point',
  369. otherwise uses `variable-at-point'."
  370. (let* ((symb1 (cond ((null type) (function-called-at-point))
  371. ((eq type 'defvar) (variable-at-point))
  372. (t (variable-at-point t))))
  373. (symb (unless (eq symb1 0) symb1))
  374. (predicate (cdr (assq type '((nil . fboundp)
  375. (defvar . boundp)
  376. (defface . facep)))))
  377. (prompt-type (cdr (assq type '((nil . "function")
  378. (defvar . "variable")
  379. (defface . "face")))))
  380. (prompt (concat "Find " prompt-type
  381. (and symb (format " (default %s)" symb))
  382. ": "))
  383. (enable-recursive-minibuffers t))
  384. (list (intern (completing-read
  385. prompt obarray predicate
  386. t nil nil (and symb (symbol-name symb)))))))
  387. (defun find-function-do-it (symbol type switch-fn)
  388. "Find Emacs Lisp SYMBOL in a buffer and display it.
  389. TYPE is nil to search for a function definition,
  390. or else `defvar' or `defface'.
  391. The variable `find-function-recenter-line' controls how
  392. to recenter the display. SWITCH-FN is the function to call
  393. to display and select the buffer.
  394. See also `find-function-after-hook'.
  395. Set mark before moving, if the buffer already existed."
  396. (let* ((orig-point (point))
  397. (orig-buffers (buffer-list))
  398. (buffer-point (save-excursion
  399. (find-definition-noselect symbol type)))
  400. (new-buf (car buffer-point))
  401. (new-point (cdr buffer-point)))
  402. (when buffer-point
  403. (when (memq new-buf orig-buffers)
  404. (push-mark orig-point))
  405. (funcall switch-fn new-buf)
  406. (when new-point (goto-char new-point))
  407. (recenter find-function-recenter-line)
  408. (run-hooks 'find-function-after-hook))))
  409. ;;;###autoload
  410. (defun find-function (function)
  411. "Find the definition of the FUNCTION near point.
  412. Finds the source file containing the definition of the function
  413. near point (selected by `function-called-at-point') in a buffer and
  414. places point before the definition.
  415. Set mark before moving, if the buffer already existed.
  416. The library where FUNCTION is defined is searched for in
  417. `find-function-source-path', if non-nil, otherwise in `load-path'.
  418. See also `find-function-recenter-line' and `find-function-after-hook'."
  419. (interactive (find-function-read))
  420. (find-function-do-it function nil 'switch-to-buffer))
  421. ;;;###autoload
  422. (defun find-function-other-window (function)
  423. "Find, in another window, the definition of FUNCTION near point.
  424. See `find-function' for more details."
  425. (interactive (find-function-read))
  426. (find-function-do-it function nil 'switch-to-buffer-other-window))
  427. ;;;###autoload
  428. (defun find-function-other-frame (function)
  429. "Find, in another frame, the definition of FUNCTION near point.
  430. See `find-function' for more details."
  431. (interactive (find-function-read))
  432. (find-function-do-it function nil 'switch-to-buffer-other-frame))
  433. ;;;###autoload
  434. (defun find-variable-noselect (variable &optional file)
  435. "Return a pair `(BUFFER . POINT)' pointing to the definition of VARIABLE.
  436. Finds the library containing the definition of VARIABLE in a buffer and
  437. the point of the definition. The buffer is not selected.
  438. If the variable's definition can't be found in the buffer, return (BUFFER).
  439. The library where VARIABLE is defined is searched for in FILE or
  440. `find-function-source-path', if non-nil, otherwise in `load-path'."
  441. (if (not variable)
  442. (error "You didn't specify a variable")
  443. (let ((library (or file
  444. (symbol-file variable 'defvar)
  445. (help-C-file-name variable 'var))))
  446. (find-function-search-for-symbol variable 'defvar library))))
  447. ;;;###autoload
  448. (defun find-variable (variable)
  449. "Find the definition of the VARIABLE at or before point.
  450. Finds the library containing the definition of the variable
  451. near point (selected by `variable-at-point') in a buffer and
  452. places point before the definition.
  453. Set mark before moving, if the buffer already existed.
  454. The library where VARIABLE is defined is searched for in
  455. `find-function-source-path', if non-nil, otherwise in `load-path'.
  456. See also `find-function-recenter-line' and `find-function-after-hook'."
  457. (interactive (find-function-read 'defvar))
  458. (find-function-do-it variable 'defvar 'switch-to-buffer))
  459. ;;;###autoload
  460. (defun find-variable-other-window (variable)
  461. "Find, in another window, the definition of VARIABLE near point.
  462. See `find-variable' for more details."
  463. (interactive (find-function-read 'defvar))
  464. (find-function-do-it variable 'defvar 'switch-to-buffer-other-window))
  465. ;;;###autoload
  466. (defun find-variable-other-frame (variable)
  467. "Find, in another frame, the definition of VARIABLE near point.
  468. See `find-variable' for more details."
  469. (interactive (find-function-read 'defvar))
  470. (find-function-do-it variable 'defvar 'switch-to-buffer-other-frame))
  471. ;;;###autoload
  472. (defun find-definition-noselect (symbol type &optional file)
  473. "Return a pair `(BUFFER . POINT)' pointing to the definition of SYMBOL.
  474. If the definition can't be found in the buffer, return (BUFFER).
  475. TYPE says what type of definition: nil for a function, `defvar' for a
  476. variable, `defface' for a face. This function does not switch to the
  477. buffer nor display it.
  478. The library where SYMBOL is defined is searched for in FILE or
  479. `find-function-source-path', if non-nil, otherwise in `load-path'."
  480. (cond
  481. ((not symbol)
  482. (error "You didn't specify a symbol"))
  483. ((null type)
  484. (find-function-noselect symbol))
  485. ((eq type 'defvar)
  486. (find-variable-noselect symbol file))
  487. (t
  488. (let ((library (or file (symbol-file symbol type))))
  489. (find-function-search-for-symbol symbol type library)))))
  490. ;; For symmetry, this should be called find-face; but some programs
  491. ;; assume that, if that name is defined, it means something else.
  492. ;;;###autoload
  493. (defun find-face-definition (face)
  494. "Find the definition of FACE. FACE defaults to the name near point.
  495. Finds the Emacs Lisp library containing the definition of the face
  496. near point (selected by `variable-at-point') in a buffer and
  497. places point before the definition.
  498. Set mark before moving, if the buffer already existed.
  499. The library where FACE is defined is searched for in
  500. `find-function-source-path', if non-nil, otherwise in `load-path'.
  501. See also `find-function-recenter-line' and `find-function-after-hook'."
  502. (interactive (find-function-read 'defface))
  503. (find-function-do-it face 'defface 'switch-to-buffer))
  504. (defun find-function-on-key-do-it (key find-fn)
  505. "Find the function that KEY invokes. KEY is a string.
  506. Set mark before moving, if the buffer already existed.
  507. FIND-FN is the function to call to navigate to the function."
  508. (let (defn)
  509. (save-excursion
  510. (let* ((event (and (eventp key) (aref key 0))) ; Null event OK below.
  511. (start (event-start event))
  512. (modifiers (event-modifiers event))
  513. (window (and (or (memq 'click modifiers) (memq 'down modifiers)
  514. (memq 'drag modifiers))
  515. (posn-window start))))
  516. ;; For a mouse button event, go to the button it applies to
  517. ;; to get the right key bindings. And go to the right place
  518. ;; in case the keymap depends on where you clicked.
  519. (when (windowp window)
  520. (set-buffer (window-buffer window))
  521. (goto-char (posn-point start)))
  522. (setq defn (key-binding key))))
  523. (let ((key-desc (key-description key)))
  524. (if (or (null defn) (integerp defn))
  525. (message "%s is unbound" key-desc)
  526. (if (consp defn)
  527. (message "%s runs %s" key-desc (prin1-to-string defn))
  528. (funcall find-fn defn))))))
  529. ;;;###autoload
  530. (defun find-function-on-key (key)
  531. "Find the function that KEY invokes. KEY is a string.
  532. Set mark before moving, if the buffer already existed."
  533. (interactive "kFind function on key: ")
  534. (find-function-on-key-do-it key #'find-function))
  535. ;;;###autoload
  536. (defun find-function-on-key-other-window (key)
  537. "Find, in the other window, the function that KEY invokes.
  538. See `find-function-on-key'."
  539. (interactive "kFind function on key: ")
  540. (find-function-on-key-do-it key #'find-function-other-window))
  541. ;;;###autoload
  542. (defun find-function-on-key-other-frame (key)
  543. "Find, in the other frame, the function that KEY invokes.
  544. See `find-function-on-key'."
  545. (interactive "kFind function on key: ")
  546. (find-function-on-key-do-it key #'find-function-other-frame))
  547. ;;;###autoload
  548. (defun find-function-at-point ()
  549. "Find directly the function at point in the other window."
  550. (interactive)
  551. (let ((symb (function-called-at-point)))
  552. (when symb
  553. (find-function-other-window symb))))
  554. ;;;###autoload
  555. (defun find-variable-at-point ()
  556. "Find directly the variable at point in the other window."
  557. (interactive)
  558. (let ((symb (variable-at-point)))
  559. (when (and symb (not (equal symb 0)))
  560. (find-variable-other-window symb))))
  561. ;;;###autoload
  562. (defun find-function-setup-keys ()
  563. "Define some key bindings for the find-function family of functions."
  564. (define-key ctl-x-map "F" 'find-function)
  565. (define-key ctl-x-4-map "F" 'find-function-other-window)
  566. (define-key ctl-x-5-map "F" 'find-function-other-frame)
  567. (define-key ctl-x-map "K" 'find-function-on-key)
  568. (define-key ctl-x-4-map "K" 'find-function-on-key-other-window)
  569. (define-key ctl-x-5-map "K" 'find-function-on-key-other-frame)
  570. (define-key ctl-x-map "V" 'find-variable)
  571. (define-key ctl-x-4-map "V" 'find-variable-other-window)
  572. (define-key ctl-x-5-map "V" 'find-variable-other-frame))
  573. (provide 'find-func)
  574. ;;; find-func.el ends here