Today I received an email with some good questions about php-mode. So I am sharing the email here, along with my response, because it may be useful to other Emacs users writing PHP code.
Possible Auto Complete Problem
I’m just an Emacs newbie, starting using it for php programming.
And I’m trying to use php-mode for writting php code. So I have several questions about it:
when using php-mode with autocomplete-mode hitting key when ac-mode makes a suggestion just close the suggestion window and doesn’t complete; how can I fix it?
It has been a long time since I have used Auto Complete so what I am about to say may not be accurate anymore. But when I last used it Tab was not the default key for selecting a suggestion. I think it was Return. To expand a suggestion you had to use the command
ac-complete, which I think was Return by default, and Tab was
ac-expand instead. One way to check for that would be to use the command
C-h w ac-complete. The command
C-h w is short for ‘whereis’; it tells you where a command is on the keyboard, so if any keys are assigned to
ac-complete then ‘whereis’ will tell you what they are. It may be that you need to use that command instead of
ac-expand. But like I said, I am basing that off knowledge from about three years ago.
Do you only have this problem with php-mode? If so then that is a bug with php-mode itself. If Auto Complete only fails to work with php-mode then please create an issue describing the problem, and with this information:
Your version of php-mode:
C-h v php-mode-version-number. The command
C-h vwill tell you the current value of any variable in Emacs, so it can be useful to look at the value of settings in different Emacs modes.
The value of
C-h v php-mode-modified. This will be a date like ‘2013-02-15’. That variable tells you the last day we updated that version of php-mode.
Your version of Auto Complete. You will have to look at the top of the
auto-complete.elfile to find that number. It will be something like ‘Version: 1.4’.
So the problem may be that you need to use a different key/command to expand the suggestion. But if you are only having the problem when you use php-mode then it is possible Auto Complete and php-mode are trying to use the same key for two different things and they are conflicting. And that would be a bug I need to fix.
I’d like to have something like an omnicompletion (or intellisense) for php; maybe you can suggest something?
Auto Complete is one of the better Intellisense-ish modes out there. But there are other choices too. However, one thing Intellisense does very well is it provides context specific information. For example, if you type the name of an object and then begin to type a method Intellisense will only show you the methods that you can use with that object. Intellisense is able to do this because it has an understanding of the programming language you are using. So in order to do the same in Emacs we would need a package that actually understands PHP code and is able to process it so that it can determine exactly what methods are valid for an object, what variables make sense in a given function, and so on. As far as I know there is no such package for Emacs that does this for PHP.
You can get Intellisense-ish behavior for some programming languages in Emacs by using CEDET, but PHP is not a language it supports. Futhermore, CEDET is a complicated package to use and personally I have never had a lot of success with it.
Personally I use ‘dynamic abbreviations’, which comes built-in with GNU Emacs. By default you can press
M-/ to use ‘dabbrev’, as people call it. Or you can use the command
C-h k M-/ to see, because
C-h k will tell you what command is bound to a given key. You should see that
M-/ is set to run
dabbrev-expand. Dabbrev is kind of like Auto Complete except without the menu. The first time you press
M-/ Emacs will try to guess how to expand what you are typing based on the text closest to the point (i.e. the cursor). Then it will start to look through the rest of the buffer, and if it cannot find anything there it will start looking through all of the open buffers. So dabbrev does not actually understand PHP or any other language; it is not ‘smart’ like Intellisense and cannot restrict its suggestions only to valid method names or anything. But it works very well for me personally.
There is also a package similar to dabbrev called Hippie Expand which you can use together with dabbrev. And finally there is Company Mode, which you can configure to expand PHP’s standard library functions.
I like using dabbrev because it is simple and does what I want 99% of the time. Company Mode and CEDET are the most complex choices, but they have the potential to present much more refined and intelligent results than ‘dumber’ packages like dabrrev and Hippie Expand. My suggestion is to try them out and see what works best for you.
how can I achive something like eldoc (showing function arguments in minibuffer) for built in php functions?
That is a good question. Currently I am looking at ways to make php-mode work with Eldoc Mode, together with other php-mode developers. As an experiment to test one idea for Eldoc support I wrote a package that can expand standard PHP functions. But that package is still just an experiment.
Eldoc support is one of the big features we are working on for the next version of php-mode. However, it is a big task and will take time to solve and implement. So unfortunately the only thing I can say for now is to please wait patiently while we work on the feature, and hopefully the other developers and I can come up with a complete solution in the near future.