Wednesday, December 24, 2008

PHP development in VIM

Emacs is just as good for PHP. Why VIM? Why not.

Basic PHP development in VIM is obviously no problem: we have highlighting, we have indenting - but what are the options when we want to get serious?

Here's a list of plugins I found especially useful.

Syntax checking
checksyntax: provides syntax checking for PHP, (La)TeX and Ruby. Note that you'll need display_errors set to stderr in php.ini for this to work.
Folding
phpfolding: What it says, and it Just Works.
Documenting

This is a bit more complicated. First you need to decide on a documenting system. phpDocumentor and Doxygen seem to be the most widely used. phpDocumentor is obviously written specifically for PHP, but it feels sluggish - though that's just my opinion. Doxygen is originally written for C and C++, but PHP shares much of it's syntax with them, so that's a non-issue.

For phpDocumentor there are several plugins. I found PDV (PHP Documentor for Vim) the nicest. It lets you insert comment blocks with a shortcut, and extracts as much as possible from the definition (name, visibility, arguments).

Same goes for Doxygen. DoxygenToolkit is (like Doxygen itself) written for C/C++/Java, but it gets things right in PHP most of the time. Doxygen-via-Doxygen looks promising: it uses Doxygen to actually generate the comments. Elegant. I can't comment on quality though, haven't tried it myself (download an XSLT processor, set up the XSLT file, I'm tired for that now).

Highlight MySQL
Serious PHP usually means a database, and some kind of SQL at that - so why not syntax-highlight the SQL code? Yeah, I know, it looks funny inside strings, but I like it - YMMV. One: you need SQL highlighting in sql files (you most likely already have that). Two: stick the following line somewhere it gets executed (I put it in ~/.vim/ftplugin/php/custom.vim): let php_sql_query = 1

Additional suggestions are welcome. Not out of courtesy, I genuinely am interested.

Sunday, December 21, 2008

Learning Haskell

Partly because of XMonad, partly because I've been looking into functional languages for a while, I'm now learning Haskell. Looking for some introductory material I've found the following docs useful:

And just for kicks, here's a solution to the last exercise of the chapter "Language basics" of YAHT. The only difference between this and the one in the book is that here getNums is tail-recursive. Not that it really matters...

module Main where

import IO

main = do
  hSetBuffering stdin LineBuffering
  nums <- getNums []
  putStrLn ("The sum is " ++ (show (sum nums)))
  putStrLn ("The product is " ++ (show (product nums)))
  printFactorials nums

getNums xs = do
    putStr "Give me a number (or 0 to stop): "
    num <- getLine
    if num == "0"
       then return xs
       else getNums ( (read num :: Integer) : xs)

printFactorials []     = return ()
printFactorials (x:xs) = do
  putStrLn ((show x) ++ " factorial is " ++ (show (factorial x)))
  printFactorials xs

factorial n | n < 0     = 0
            | n > 1     = n * factorial (n-1)
            | otherwise = 1 -- 0 or 1

Note that this is a basic solution; it's possible to solve the problem in different, more elegant ways - see the comments at Reddit.

Sunday, December 7, 2008

WMII to XMonad

Update (2008/12/08): With the pipe-notification Pidgin plugin, the example script provided with it and a little modification to your status script, it's possible to have notification on new messages. See conky-bar for an example - the first command simply calls the pipe-notification example script.

Update 2 (2009/01/16): It can actually be even simpler than that. One of the plugins included with Pidgin, Message Notification, has an option for setting the urgent flag on windows with new messages. You can make XMonad highlight the names of workspaces with urgent windows (see my xmonad.hs). Tada - message notification.

Update 3 (2009/03/04): Actually, the above links were dead. You can now find all my dotfiles in a GitHub repo.

I gave up trying to bend WMII to work the way I wanted it, and gave XMonad a try. After the initial difficulties with Haskell, it feels like a grown-up WMII (XMonad is written in Haskell, a functional language). It first seemed strange to have.. well, anything useful written in a functional language (no offense, I just haven't encountered any such)... But it does work, and quite well at that.

If interested, you can check out the current state of my xmonad.hs at dotfiles.org GitHub. Once it's about done, I'll probably send it/upload it/whatever to the XMonad community configs. It started out as a modification on And1's config, but most of it has been replaced/rewritten.

Some details, in case you're interested: I use dzen2 bars for status information and the like, which means the limitations of the (only one) WMII status-bar are gone. I kept all nine workspaces, and use ones tagged www (with M-w), mail (M-e) and im (M-i) with rules for moving the appropriate windows there when opened. There are some key-bindings in there for controlling mpd through mpc (next song, previous song, toggle pause/play and a big dzen bar for the current song, shown for just a second).

<Subjective, no flames please>
XMonad feels a bit alien at first, but it's really a bit like the switch from Ubuntu to Arch Linux: you can do everything with either one, but one (Arch, and XMonad) is just made to be customized.
</Subjective, no flames please>

It also isn't the 'build everything yourself' kind of customization: there are numerous, complete configurations published you can use as a starting point. The documentation of the config system is very well written, and easy to use even without prior knowledge of Haskell (I speak from experience).

All in all, I like it.