Thursday, February 7, 2008

Javish source code formatting for the php-mode

If you tried the php-mode on Emacs, you probably meet the situation that it follows the c-mode rules in the code formatting. And if then you're more OOP apologist you probably would prefer some more javish-looking source code. Good news is that you actually can get so. Emacs is a totally configurable thing.

Put the following code into your emacs config.

(defun my-phpstuff ()
(c-set-style "awk")
(c-set-offset 'arglist-close 0)
(c-set-offset 'string 'c-lineup-dont-change)
(c-set-offset 'arglist-cont-nonempty '+)
(c-set-offset 'case-label '+)
(c-set-offset 'comment-intro 0)
(c-set-offset 'defun-close 0)
(c-set-offset 'cpp-macro 0)
(c-set-offset 'knr-argdecl 'c-lineup-dont-change)

(setq tab-width 4
c-basic-offset 4
c-hanging-semi&comma-criteria nil
indent-tabs-mode nil
c-cleanup-list '(scope-operator)
c-comment-only-line-offset '(0 . 0)
c-backslash-column 48

c-hanging-colons-alist nil
c-hanging-comment-starter-p nil
c-hanging-comment-ender-p nil
c-indent-comments-syntactically-p nil
c-tab-always-indent t
c-comment-continuation-stars "* "
c-label-minimum-indentation 1
defun-prompt-regexp nil
)
)
(add-hook 'php-mode-hook 'my-phpstuff)


This will provide you a correct 4-spaces indentation for all of your constructions. If you prefer tabs instead of spaces, change indent-tabs-mode nil to indent-tabs-mode t and you'll have it.

No comments: