Configuring Emacs
Last modified: April 17, 2009 - 20:09
This short snippet will extend the php-mode in Emacs to follow Drupal coding style. Add this to your $HOME/.emacs:
(defun drupal-mode ()
"Drupal php-mode"
(interactive)
(php-mode)
(message "Drupal mode activated.")
(set 'c-basic-offset 2)
(set 'indent-tabs-mode nil)
(c-set-offset 'case-label '+)
(c-set offset 'arglist-intro '+) ; for FAPI arrays and DBTNG
(c-set-offset 'arglist-cont-nonempty 'c-lineup-math) ; for DBTNG fields and values
; More Drupal-specific customizations here
)
(defconst my-php-style
'((c-offsets-alist , (
(arglist-close , c-lineup-close-paren) ; correct arglist closing parenthesis
)))
"My PHP Programming style"
)
(c-add-style "my-php-style" my-php-style)
(defun my-php-mode ()
"my personal php-mode customizations,"
(c-set-style "my-php-style")
; More generic PHP customizations here
)
(defun setup-php ()
; PHP
(add-hook 'php-mode-hook 'my-php-mode)
; Drupal
(add-to-list 'auto-mode-alist '("\\.\\(module\\|test\\|install\\|theme\\)$" . drupal-mode))
(add-to-list 'auto-mode-alist '("/drupal.*\\.\\(php\\|inc\\)$ . drupal-mode))
(add-to-list 'auto-mode-alist '("\\.info" . conf-windows-mode))
)
(setup-php)This was just for simplicity, in my system however, I replace the last line: ((setup-php)) with:
(provide 'setup-php)
(provide 'my-php-mode)And instead of putting that in my ~/.emacs directly I put in a separate file e.g. ~/.emacs.d/setup-php.el. Then in my .emacs I have:
(add-to-list 'load-path "~/.emacs.d")
; My PHP setup
(require 'setup-php)
(setup-php)This will automatically set you in drupal-mode if you load a .php, .module or .inc file from beneath a drupal* directory. You may also manually select drupal mode by hitting M-x drupal-mode.

array indentation
Thanks for posting this!
AFAIK the correct Drupal indentation for arrays looks like this:
<?php$a = array(
'foo' => 'bar',
'gaz' => 'gazonk',
) ;
?>
but for me, this mode results in
<?php$a = array(
'foo' => 'bar',
'gaz' => 'gazonk',
) ;
?>
Any tips for resolving this?
Here is what works for me
I didn't want to edit the page unless someone confirms the following works for them too.
In your .emacs:
(defun drupal-mode ()
"Drupal php-mode."
(interactive)
(php-mode)
(message "Drupal mode activated.")
(set 'tab-width 2)
(set 'c-basic-offset 2)
(set 'indent-tabs-mode nil)
(c-set-offset 'case-label '+)
(c-set-offset 'arglist-intro '+) ; for FAPI arrays and DBTNG
(c-set-offset 'arglist-cont-nonempty 'c-lineup-math) ; for DBTNG fields and values
; More Drupal-specific customizations here
)
(defconst my-php-style
'((c-offsets-alist . (
(arglist-close . c-lineup-close-paren) ; correct arglist closing parenthesis
)))
"My PHP Programming style"
)
(c-add-style "my-php-style" my-php-style)
(defun my-php-mode ()
"My personal php-mode customizations."
(c-set-style "my-php-style")
; More generic PHP customizations here
)
(defun setup-php ()
; PHP
(add-hook 'php-mode-hook 'my-php-mode)
; Drupal
(add-to-list 'auto-mode-alist '("\\.\\(module\\|test\\|install\\|theme\\)$" . drupal-mode))
(add-to-list 'auto-mode-alist '("/drupal.*\\.\\(php\\|inc\\)$" . drupal-mode))
(add-to-list 'auto-mode-alist '("\\.info" . conf-windows-mode))
; More startup-setup for PHP customizations to work here
)
(setup-php)
This was just for simplicity, in my system however, I replace the last line: (
(setup-php)) with:(provide 'setup-php)(provide 'my-php-mode)
And instead of putting that in my
~/.emacsdirectly I put in a separate file e.g.~/.emacs.d/setup-php.el. Then in my.emacsI have:(add-to-list 'load-path "~/.emacs.d")
; My PHP setup
(require 'setup-php)
(setup-php)
-- Amr Mostafa
array indentation issue seems fixed with this - thanks
Cheers AMR, that seems to have cleared up the array indentation issue nicely for me. Haven't given it extensive testing yet but will do so today :)
Thanks again!
forms break out of comment mode
In Emacs PHP-mode, the following code evaluates so that Emacs thinks the comment has closed at the end of first array entry:
<?php
/*
$form['back'] = array(
'#type' => 'submit',
'#value' => 'Cancel',
'#id' => 'new-contact-cancel'
) ;
*/
?>
This is because Emacs detects the '#' in the second line as another comment opening, and believes that this has an implicit closing comment at the end of the line. From the line starting '#value', the code is treated as though it's outside the comment.
Do others see the same issue with Emacs and PHP-mode?
This isn't specifically related to the config posted above, rather it is an issue with Emacs PHP mode which shows up with commented forms code using multi-line comments.
One workaround is to comment out code blocks using M-x comment-region (and M-x uncomment-region) rather than multi-line comment blocks, but it would be cool to figure a fix for PHP mode's comment detection regex.
Indeed
I've had the same issue as well. I found this to be fixed in the latest version of php-mode though!
Now for the seasoned emacs-er this should have been enough instructions, for beginners like myself:
~/.emacs.ddirectory or wherever you stash your elisp ;)(add-to-list 'load-path "~/.emacs.d")at the top of your~/.emacsfile.Now that should do it! But If you want to make sure it was loaded and you are indeed running the latest and greatest:
M-:(that'sAlt-Shift-;) and evaluate the following lisp which will confirm which version of php-mode we are running:(condition-case nil php-mode-version-number ((error nil) php-version)). If you see 1.5.0 (which is the latest as of 4th of April, 2009) then you are all set! Anything lower means you are still running some old version. Perhaps the right thing to do in this case is to find the otherphp-mode.el, wherever it is, and replace it with the new one.Enjoy!
-- Amr Mostafa
Window version help?...
Need help setting this up on a Windows Vista. I just download emacs-23.1, but don't have a clue how to set this config up. Can anyone help me out in what I need to do?
Coding is like a box of chocolates!...
Cancel That! I found an
Cancel That! I found an excellent IDE in Netbeans. My style of an environment...
Coding is like a box of chocolates!...
Cancel That! I found an
Cancel That! I found an excellent IDE in Netbeans. My style of an environment...
Coding is like a box of chocolates!...