Emacs configurations for Horde

Jan's configuration

(autoload 'po-mode "po-mode" "Major mode for translators to edit PO files" t)
(setq auto-mode-alist (cons '("\\.po\\'\\|\\.po\\." . po-mode) auto-mode-alist))                                   

(require 'php-mode)
(add-to-list 'auto-mode-alist '("\\.dist\\'" . php-mode))
(add-to-list 'auto-mode-alist '("\\.phpt\\'" . php-mode))

(autoload 'javascript-mode "javascript" nil t)
(add-to-list 'auto-mode-alist (cons  "\\.js\\'" 'javascript-mode))

(global-set-key "\C-l" 'goto-line)

(c-add-style "horde"
  '((c-basic-offset . 4)
    (c-tab-always-indent . t)
    (indent-tabs-mode . nil)
    (fill-column . 79)
    (c-offsets-alist . ((inline-open . 0)
                        (defun-block-intro . +)
                        (block-open . -)
                        (statement-cont . +)
                        (statement-block-intro . +)
                        (arglist-close . 0)))
    (c-comment-only-line-offset (0 . 0))
    (c-hanging-braces-alist . ((defun-open after)
                               (substatement-open after)))
    (c-cleanup-list . (brace-else-brace brace-elseif-brace))))
(add-hook 'php-mode-common-hook
   '(lambda () (c-set-style "horde")))

Gunnar's configuration

Gunnar keeps his Emacs configuration in a puppet configuration repository on github.com. It contains lisp functionality to run code checks, style checking, the GEBEN debugger and PHPUnit tests from within Emacs.

Emacs features

Checking the code for syntax errors

PHP allows you to check (lint) your code for syntax errors. It can be helpful to bind that functionality to a key combination within emacs. This way you can quickly check that your new code has no major flaws in it.

Emacs will present the result of the check in a compilation buffer that allows you to directly jump to any problematic line.


(defun php-lint ()
  "Performs a PHP lint check on the current file."
  (interactive)
  (let ((compilation-error-regexp-alist '(php))
        (compilation-error-regexp-alist-alist ()))
    (pushnew '(php "\\(syntax error.*\\) in \\(.*\\) on line \\([0-9]+\\)$" 2 3 nil nil 1)
             compilation-error-regexp-alist-alist)
    (compile (concat "php -l -f \"" (buffer-file-name) "\""))))

;; Check code                                                                                                                                                             
(define-key php-mode-map (kbd "<f3> <f10>") 'php-lint)

The last line binds the function to the key combination <f3> <f10> but it is of course completely up to you if you like that combination.

Checking the code style for problems

In order to adhere to the Horde coding standards you can run PHP_CodeSniffer over your code to detect problems. It might not be completely aware of the Horde code style but the PEAR code style is close enough to it in order to be useful.

Emacs will present the result of the check in a compilation buffer that allows you to directly jump to any problematic line.

CodeSniffer < 1.2.0

;; taken from http://atomized.org/2008/10/php-lint-and-style-checking-from-emacs/                                                                                         
(defun phpcs ()
  "Performs a PHPCS lint-check on the current file."
  (interactive)
  (let ((compilation-error-regexp-alist '(php))
        (compilation-error-regexp-alist-alist ()))
    (pushnew '(php "\"\\([^\"]+\\)\",\\([0-9]+\\),\\([0-9]+\\),\\(\\(warning\\)\\|\\(error\\)\\),\\(.*\\)" 1 2 3 (5 . 6) 4)
             compilation-error-regexp-alist-alist)
    (compile (concat "phpcs --standard=PEAR --report=csv \"" (buffer-file-name) "\""))))

;; Check code style                                                                                                                                                       
(define-key php-mode-map (kbd "<f3> <f11>") 'phpcs)

The last line binds the function to the key combination <f3> <f11> but it is of course completely up to you if you like that combination.

CodeSniffer >= 1.2.0

The newer code sniffer version can directly output an emacs compatible format. You have two options of using the output for compilation within emacs.

Binding the compile command for you PHP mode

;;taken from http://pear.php.net/manual/en/package.php.php-codesniffer.reporting.php
(defun my-php-hook-function ()
  (set (make-local-variable 'compile-command)
    (format "phpcs --report=emacs --standard=PEAR %s"
            (buffer-file-name))))
(add-hook 'php-mode-hook 'my-php-hook-function)

The style check will be run once you execute the "M-x compile" command.

Binding the style check to a command

(defun phpcs ()
  "Performs a PHP code sniffer check on the current file."
  (interactive)
  (let ((compilation-error-regexp-alist '(gnu)))
    (compile (format "phpcs --standard=PEAR --report=emacs \"%s\""
                     (buffer-file-name)))))

;; Check code style                                                                                                                                                       
(define-key php-mode-map (kbd "<f3> <f11>") 'phpcs)

The last line binds the function to the key combination <f3> <f11> but it is of course completely up to you if you like that combination.