Emacs and PHP: There’s more than one way to do it, of course

The PhpMode page on EmacsWiki lists five options for developing PHP in Emacs. I’m currently using the php-mode maintained by Aaron Hawley, and I’m quite happy with it. I’m curious about the php-mode maintained by Ahmet Usal, though, as it has extensive templating support and electric behavior. It seems to have abbreviations and argument lists for every PHP function, and I might borrow the code and convert it to a yasnippet file if I don’t end up using this. I’ll switch to Ahmet Usal’s php-mode today, see what it feels like, and post my notes. Has anyone else tried both?

  • http://blog.nethazard.net Gabriel Saldana

    I’ve been working with Ahmet Usal’s php-mode. The electric behavior is nice sometimes, but most times it gets in your way and annoys you. Even though its a lot fancier than the one mantained by Aaron Hawley. I tried reaching Ahmet Usal for some feedback and patches but had no response. Probably I’ll just publish my patches on my blog someday, as soon as I get around most annoyances.

    • http://sachachua.com Sacha Chua

      You could also fork it and start maintaining your own version, I guess. =)

  • Chris.

    I also found it annoying for the same reason that Gabriel gave. I also used the Smarty mode and had the same problem.

    If it had a way to customize its behavior (short of modifying the source), I couldn’t find it, and I’d still be using it if it did.

    On a related topic, have you tried the php mode that comes with nxml? It contains a modified version of Aaron’s php mode. The problem I have with it is how it (in)validates PHP.

    It considers the PHP invalid if it’s missing ‘?>’ at the end of the file. Also, it won’t go into php-mode for a file that has ‘<?’ instead of ‘<?php’. Supposedly, this was fixed, but even with the latest version it still does it.

    • http://sachachua.com Sacha Chua

      Okay, the second php-mode (Vincent Debout’s) officially drove me crazy. stutter-mode totally didn’t work for me. I’m a Lisp girl who thinks nothing of having nested function calls–thus the occasional foo(bar()) and so on)–and stutter-mode was converting my )) into ]. The indenting didn’t honor the C offsets I’d already set up, and looking at php-indent-line, I feel it would require too much tweaking to get it to behave the way I want to. I didn’t use the autocompletions at all, but that was probably because I used only a couple of core PHP functions and quick access to the PHP documentation was enough for the one or two times I forgot the argument list. The electric behavior was nice but not important. It’d probably work for other people, but I guess the php-mode from php-mode.sourceforge.net has warped my brain. ;)

      I avoid short tags (< ? instead of , so I’m probably going to stay away from the mode that comes with nxml. =(

  • http://ourcomments.org/Emacs/EmacsW32.html LeoB

    Hi Sacha and Chris,

    There are only minor differences between the php-mode.el that comes with nXhtml and Aaron’s php mode. (They are just for information about how to turning on multi major modes. If there are more differences then either me or Aaron has missed to update something.)

    This has nothing to with validation of the XHTML code. That is a feature that comes from nxml-mode (and is inheritied to nxhtml-mode).

    What nXhtml offer you over plain php-mode is the possibility for validation and completion in the XHTML part of a php file (and a lot of other things of course).

    It is not very simple to do that and I guess there are still some bugs, but I am not sure what problems you are referring to, Chris. Could you please try with version nXhtml ver 1.35 (just released) and see if the problems are still there?

    And Sacha, why would you stay away from the php-mode.el that comes with nXhtml? Is there any problem you have seen?

    If you have any questions or suggestions about using nXhtml for php you are very welcome. At the moment I am moving the development of nXhtml because some people using ruby were interested. If you want to take part it would be nice.

    I would have hoped to get mumamo.el, which handles multi major modes and is one of the core modules of nXhtml, into Emacs by now, but there are unfortunately still some problems that should be solved. Those mostly have to do with parsing.

    • http://ourcomments.org/Emacs/EmacsW32.html LeoB

      Sacha, isn’t it a bit hard to distinguish the paragraphs with you current CSS style?

      • http://sachachua.com Sacha Chua

        It is, isn’t it? Let me go and make that better… =) Thanks!

        • http://ourcomments.org/Emacs/EmacsW32.html LeoB

          Thanks, looks much better. Now I hope you are able to read what I wrote ;-)

    • Chris.

      LeoB,

      I’ll give it a try, and let you know.

      This is timely because I just downloaded the latest version of Carbon Emacs, and decided to get it working (nXhtml, that is). Not successful yet, but I’m trying to do that and my regular work at the same time.

      Ideally, I would like to have HTML, XML (and other structured documents) validated, and PHP not. By “PHP”, I mean any file ending in “.php”. If there’s a straightforward way of doing this, I’d like to know. Prior to using nXhtml, it was enough to have (“\\.php$” . php-mode) appended to auto-mode-alist to handle the PHP part, but now it gives a vacuous schema message, and then shows the red lines throughout the code.

      Also, I noticed that js2-mode is included now, but I missed this in the changes file. I was confused for a while, trying to figure out why Emacs wouldn’t go into js2-mode for files that ended in “.js”. After looking around, I realized that the included js2 was overriding my local copy, and the included version was not byte-compiled (which js2 requires to work).

      • http://ourcomments.org/Emacs/EmacsW32.html LeoB

        Hi Chris,

        The goal is to get nXhtml to validate any XHTML template language and avoid things like PHP in the template page. What you may have to do is turn on what I call “fictive validation headers” which will tell the validator from nxml-mode how the dtd looks.

        Please tell me if this works for you.

        I have included a slightly patched version of js2.el that works rather well with javascript chunks in nXhtml. I am waiting for Steve Yegge to get it to work without any changes. (And of course to get that nice completion in javascript he has promised ;-)

        And yes, it was not bytecompiled. I do not feel very comfortable about distributing .elc files. Maybe I should give a un easy way to install the files then.

    • Chris.

      LeoB,

      Just to let you know. I tried 1.35, and the first PHP file that I open works fine, but the second file shows the red lines.

      I appreciate all of the effort you have put into nXhtml, it’s a daunting task getting such diverse modes to work together. I’ll make a list of suggestions. :)

      • http://ourcomments.org/Emacs/EmacsW32.html LeoB

        Hi Chris,

        The red lines are supposed to help you, not annoy you ;-)

        If you can send me a copy of the second file I can give better comments about what to do and maybe I can make nXhtml a bit better. Can you do that? If you do, please tell me if I can put it in my distribution as test or example.

        • Chris.

          They would help if I needed them, but… :)

          Because it’s work code I can’t send it, but I do know that adding the ending ‘?>’ causes them to go away.

        • http://ourcomments.org/Emacs/EmacsW32.html LeoB

          Is it possible that you can send me a file (with nonsense code) that shows the problem?

        • Chris.

          Rather than clutter the comments here, is there an email address on the nXhtml site that I can send it to?

        • http://ourcomments.org/Emacs/EmacsW32.html LeoB

          Yes, my mail address is in the source files, for example mumamo.el.

      • http://ourcomments.org/Emacs/EmacsW32.html LeoB

        Hi Chris,

        I have just uploaded version 1.36 of nXhtml which I hope solves your problem. Could you please try it?

  • http://www.disruptech.com Paul Gaskin

    I just upgraded to nXhtml v1.35 and it is rid of the one bug I found most troublesome. I developed a habit long ago of doing something manually which Textmate (prorietary Apple code editor) and Yasnippet seem to enable automatically. I always write the closing syntax at the same time I write the opening syntax of whatever I’m doing from parenthesis, curly braces, angle brackets, quote marks, etc.

    There was this bug which would hang emacs when I’d type “” and then in between the angle brackets I’d add “??” with the intention of putting php inside of those question marks and then filling out my php chunk. It would hang with the first question mark I added, but now that’s fixed.

    The way I was going about it probably wasn’t the optimal way, but I’m not a very experienced programmer or Emacs user, so I’m just trying to figure out how to use Emacs to become a more versatile and productive programmer.

    The nXhtml package is much appreciated since I really like syntax highlighting and the automatic switching of modes as the mark moves through chunks of code of various languages.

    I’m excited about yasnippet and I like the idea of nXhtml and yasnippet functionality merged into one mode. I’m not sure if they’re already inherently compatible because I’m an Emacs newb, but I like the idea of automatic closure of opened syntax and automatic population of functions with appropriate argument place-holders.

    I develop PHP in Drupal, so making bits of Drupal code easily accessible would be nice as well. My friend who uses Textmate is pretty happy with the way he’s added snippets of Drupal-specific PHP code he often uses, and I’d like that kind of simple code snippet utility.

    I bet that’s very easy to do for those who know how, but I’m not much of an Emacs hacker and more of a user, so all I can do for now is to appreciate that which more capable programmers make available for everyone else. Thanks!

  • http://ourcomments.org/Emacs/EmacsW32.html LeoB

    Hi Paul,

    The problem with the hang was something I did not notice, but which I got through a bug report. Otherwise I could not have solved it (and it was still a bit hard ;-)

    There is no cooperation between me and the yasnippet author, but I would like yasnippet to work with nXhtml/MuMaMo of course. If you seen any trouble then please let me know. And maybe you have suggestions for how to use yasnippets with nXhtml. That could be useful to other people as well.

    I see there is a discussion group for yasnippet. Have you seen that?

    • http://www.disruptech.com Paul Gaskin

      Well, I’m really happy you squashed that bug because I use nXhtml/MuMaMo every day at work. I’m also using emacs-goodies for color themes (with a .emacs configuration to keep the color theme between sessions) and now the yasnippet.el.

      Overall, Emacs is a bit rough around the edges, not being a proprietary product, but it’s so much more liberating.

  • http://mewde.blogspot.com Ahmet Usal

    I have big update for mewde php-mode; check this here : http://mewde.blogspot.com/2008/10/new-update-for-emacs-php-mode.html
    and some big updates for mewde; I’m working on it.

    I noticed lately this post; last two months I used my updating php-mode.

    I’m using mostly with drupal too…:) I loved your module update functions; and I’m big fan this blog and your Emacs tips&tricks; nice thing to writing here.

    This mode needs to handling elisp hackers; I’m not that one; I hope you can take a look and doing some good things for that matter.

  • http://gongzine.com Kusnadhi

    Hi Sacha Chua

    Wow, nice article and I am a new begginer with Emacs. I found this was helped me alot. Thanks.

  • haheng?

    Don’t like Emacs32 don’t like nXhtml it’s not the Emacs way!

  • http://ourcomments.org/cgi-bin/emacsw32-dl-latest.pl LeoB

    @haheng:

    Hm, interesting. So what is it that u don’t like?

  • http://www.gongzine.com Kusnadhi

    @haheng:

    If you do not like Emacs, why do not give a try on Xemacs?
    http://www.xemacs.org/