diff --git a/README.ja.md b/README.ja.md index 45a432d4..0009a501 100644 --- a/README.ja.md +++ b/README.ja.md @@ -37,6 +37,37 @@ GNU Emacs 24以降では、[package][]機能を使って[MELPA][]からPHPモー 報告の際には `php-mode-version` コマンドを実行して、その出力をバグレポートに含めてください。問題を再現するための手がかりになります。 +Settings +-------- + +### 個人設定 + +.emacsファイル(`~/.emacs.d/init.el`)にPHPモードでの設定を記述できます。 + +```lisp +(defun my-php-mode-init () + (setq-local show-trailing-whitespace t) + (setq-local ac-disable-faces '(font-lock-comment-face font-lock-string-face)) + (setq-local page-delimiter "\\_<\\(class\\|function\\|namespace\\)\\_>.+$") + + ;; If you feel phumped and phpcs annoying, invalidate them. + (when (boundp 'flycheck-disabled-checkers) + (add-to-list 'flycheck-disabled-checkers 'php-phpmd) + (add-to-list 'flycheck-disabled-checkers 'php-phpcs))) + +(add-hook 'php-mode-hook #'my-php-mode-init) +``` + +### プロジェクトローカル設定 + +プロジェクトのトップディレクトリに`.dir-locals.el`を記述すると、プロジェクト単位の設定を追加することができます。このファイルはユーザー自身のEmacsにインストールされたパッケージに依存するため、バージョン管理の対象に含めないことを推奨します。 + +```lisp +((nil + (php-project-root . git) + (php-project-coding-style . psr2))) +``` + 実験的および作業中の機能 ------------------------------------- diff --git a/php-mode-test.el b/php-mode-test.el index bd05b5d2..ee2a17ef 100644 --- a/php-mode-test.el +++ b/php-mode-test.el @@ -275,14 +275,14 @@ style from Drupal." ;; the file written to has no significance, only the buffer (let ((tmp-filename (concat (make-temp-name temporary-file-directory) ".php"))) (dolist (mode '(pear wordpress symfony2)) - (php-mode-custom-coding-style-set 'php-mode-coding-style 'drupal) - (php-mode-custom-coding-style-set 'php-mode-coding-style mode) + (php-set-style "drupal") + (php-set-style (symbol-name mode)) (should-not show-trailing-whitespace) - (php-mode-custom-coding-style-set 'php-mode-coding-style 'psr2) - (php-mode-custom-coding-style-set 'php-mode-coding-style mode) + (php-set-style "psr2") + (php-set-style (symbol-name mode)) (should-not show-trailing-whitespace) - (php-mode-custom-coding-style-set 'php-mode-coding-style 'drupal) + (php-set-style "drupal") (write-file tmp-filename) (should (looking-at-p "$")))))) @@ -871,7 +871,7 @@ style from Drupal." (ert-deftest php-mode-test-issue-200 () "Test highlighting and elimination of extraneous whitespace in PSR-2 mode" (with-php-mode-test ("issue-200.php") - (php-mode-custom-coding-style-set 'php-mode-coding-style 'psr2) + (php-set-style "psr2") (should show-trailing-whitespace) (should (and (listp before-save-hook) (member 'delete-trailing-whitespace before-save-hook))))) diff --git a/php-mode.el b/php-mode.el index a06d9ac7..b95241b6 100644 --- a/php-mode.el +++ b/php-mode.el @@ -87,6 +87,7 @@ (require 'cl-lib) (require 'mode-local) +(require 'php-project) (eval-when-compile (require 'regexp-opt) @@ -374,14 +375,15 @@ This variable can take one of the following symbol values: (const :tag "WordPress" wordpress) (const :tag "Symfony2" symfony2) (const :tag "PSR-2" psr2)) - :set 'php-mode-custom-coding-style-set :initialize 'custom-initialize-default) -(defun php-mode-custom-coding-style-set (sym value) - (when (eq major-mode 'php-mode) - (set sym value) - (set-default sym value) - (php-set-style (symbol-name value)))) + +(defcustom php-mode-enable-project-coding-style t + "When set to true override php-mode-coding-style by php-project-coding-style. + +If you want to suppress styles from being overwritten by directory / file +local variables, set NIL." + :type 'boolean) (defun php-mode-version () "Display string describing the version of PHP Mode." @@ -1124,6 +1126,13 @@ After setting the stylevars run hooks according to STYLENAME (put 'php-set-style 'interactive-form (interactive-form 'c-set-style)) +(defun php-mode-set-style-delay () + "Set the current `php-mode' buffer to use the style by custom or local variables." + (let ((coding-style (or (and (boundp 'php-project-coding-style) php-project-coding-style) + php-mode-coding-style))) + (prog1 (php-set-style (symbol-name coding-style)) + (remove-hook 'hack-local-variables-hook #'php-mode-set-style-delay)))) + (defun php-mode-debug () "Display informations useful for debugging PHP Mode." (interactive) @@ -1179,7 +1188,12 @@ After setting the stylevars run hooks according to STYLENAME ;; PHP vars are case-sensitive (setq case-fold-search t) - (php-set-style (symbol-name php-mode-coding-style)) + ;; When php-mode-enable-project-coding-style is set, it is delayed by hook. + ;; Since it depends on the timing at which the file local variable is set. + ;; File local variables are set after initialization of major mode except `run-hook' is complete. + (if php-mode-enable-project-coding-style + (add-hook 'hack-local-variables-hook #'php-mode-set-style-delay t t) + (php-set-style (symbol-name php-mode-coding-style))) (when (or php-mode-force-pear (and (stringp buffer-file-name) diff --git a/php-project.el b/php-project.el index bd6ac99a..c2ab6675 100644 --- a/php-project.el +++ b/php-project.el @@ -33,6 +33,12 @@ ;; Return root directory of current buffer file. The root directory is ;; determined by several marker file or directory. ;; +;; ## `.dir-locals.el' support +;; +;; - `php-project-coding-style' +;; - Symbol value of the coding style. (ex. `pear', `psr2') +;; +;; ;;; Code: (require 'cl-lib) @@ -64,6 +70,16 @@ SYMBOL (make-variable-buffer-local 'php-project-root) (put 'php-project-root 'safe-local-variable #'(lambda (v) (assq v php-project-available-root-files)))) + +;;;###autoload +(progn + (defvar php-project-coding-style nil + "Symbol value of the coding style of the project that PHP major mode refers to. + +Typically it is `pear', `drupal', `wordpress', `symfony2' and `psr2'.") + (make-variable-buffer-local 'php-project-coding-style) + (put 'php-project-coding-style 'safe-local-variable #'symbolp)) + ;; Functions