Last active
February 11, 2016 10:59
-
-
Save aaronbieber/f76e5a20a1a5c81e6c23 to your computer and use it in GitHub Desktop.
Emacs jump to PHP function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; This is an attempt to mimic what the "CtrlP Funky" plug-in | |
;; for Vim does, allowing a "fuzzy search" of functions within | |
;; the current buffer. | |
;; | |
;; Because this uses completing-read (in my config., this is | |
;; handled by helm, which is awesome), it isn't actually | |
;; "fuzzy," but it gets the job done. | |
;; | |
;; Note: This is my first real attempt at writing elisp so it | |
;; might not be "proper." | |
(defun find-php-functions-in-current-buffer () | |
"Find lines that appear to be PHP functions in the buffer. | |
This function performs a regexp forward search from the top | |
(point-min) of the buffer to the end, looking for lines that | |
appear to be PHP function declarations. | |
The return value of this function is a list of cons in which | |
the car of each cons is the bare function name and the cdr | |
is the buffer location at which the function was found." | |
(save-excursion | |
(goto-char (point-min)) | |
(let (res) | |
(save-match-data | |
(while (re-search-forward "^ *\\(public \\|private \\|protected \\|static \\)*?function \\([^{]+\\)" nil t) | |
(let* ((fn-name (save-match-data (match-string-no-properties 2))) | |
(fn-location (save-match-data (match-beginning 0)))) | |
(setq res | |
(append res | |
(list `(,fn-name . ,fn-location))))))) | |
res))) | |
(defun jump-to-php-function () | |
"Jump to the selected function in the current buffer. | |
This function will display a list with completing read | |
of the PHP function declarations in the current buffer, | |
found by find-php-functions-in-current-buffer. Selecting | |
a function name from the list will set point to the line | |
where that function is defined." | |
(interactive) | |
(let* ((search-results (find-php-functions-in-current-buffer)) | |
(function-names (mapcar #'(lambda (search-result) (car search-result)) search-results)) | |
(selected-function (if (> (length function-names) 0) | |
(completing-read "Function: " function-names) | |
nil))) | |
(if selected-function | |
(goto-char (car (delq nil (mapcar #'(lambda (search-result) | |
(if (equal selected-function (car search-result)) | |
(cdr search-result) | |
nil) ) | |
search-results)))) | |
(message "%s" (propertize "No functions found." 'face '(:foreground "red")))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment