Started work on handler API, minor logic fixes.

- Added a handle module which must be imported by handler files
(preferably with *). This provides a decorator ``@handles(hook)`` which
registers the associated function as a handler of the specified hook.
- Improved line processing logic to sort hooks by line number so that
single-line hooks are handled before multi-line ones.