07/28/2007
Editing PHP in Emacs
I've been trying to get good, reliable command name completion working in emacs; including something that helps me remember parameters. If you write PHP, you've certainly spent some time banging your head against a wall trying to remember if in_array is "needle, haystack" or "haystack, needle." And then again with strpos, which is the opposite. Or you've wondered why array_search has the array as the second parameter when every other array function has the array as the first parameter. (And yes I know why, but that doesn't mean I know why when I need to).
Anyways, I've got function completion working just fine. In case you don't have that working, here's how to set it up. First, I created a file with all the php functions listed in it, saved it to my .emacs.d/lisp directory and added this to my .emacs file:
(defun teds-php-code-thing () (setq php-completion-file "~/.emacs.d/lisp/php-completions.txt") (define-key php-mode-map [(control tab)] 'php-complete-function) ) (add-hook 'php-mode-user-hook 'teds-php-code-thing)
Now when I type "adds," I can type control-tab and it will expand to "addslashes." Which is great, but it's only half the battle; it still doesn't tell me if array_search is needle/haystack or haystack/needle.
I figured out that parameter suggestions can be done easily with etags. Since I'd never used etags/ctags before, I ran it on the codebase of my project and it worked like a charm. If I type out my_pizza_function(), I can type control-. and the message buffer will list the args: $cheese, $sausage, $morecheese. (Once I start typing again they disappear, but it's a great start).
You can set that up by doing this from your project directory:
find . -type f -name "*php" -exec ctags --php-kinds=-v -a -e -o ~/TAGS {} \;
This is all great. However, I still need to generate one for all of the php language functions. To do that, I just need a file with all the function definitions, including arguments, so that I can run ctags on that. I'll buy a beer for the first person to send me that.
Oh! P.S. the current version of ctags doesn't like php very much. So if you're going to try this at home, get the newest version from SVN or patch ctags-5.6 with this dirty hack that I made.
P.P.S. Let me know if this helps anyone out, I imagine there must be other php/emacs users.