From 738ce16686f30db9dd14754efb006c34b0e05dcd Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Thu, 1 Mar 2018 23:06:50 +0900 Subject: [PATCH 1/6] Add php-project-coding-style variable This variable is introduced to locally override the customization variable php-mode-coding-style on a project basis. It is set by the .dir-locals.el located in the directory, not the user's own .emacs file. --- php-project.el | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 From 9911d8ceb589558be33859bbfbc11674c2bfc93a Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Fri, 2 Mar 2018 22:10:47 +0900 Subject: [PATCH 2/6] Add custom variable php-mode-enable-project-coding-style This variable controls enabling of style sets with directory/file local variables. --- php-mode.el | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/php-mode.el b/php-mode.el index a06d9ac7..79ae8e6e 100644 --- a/php-mode.el +++ b/php-mode.el @@ -383,6 +383,13 @@ This variable can take one of the following symbol values: (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." (interactive) From 3df45eeb4b608302430e53d1cd4912ae2a52657c Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Fri, 2 Mar 2018 22:55:28 +0900 Subject: [PATCH 3/6] Set style by php-project-coding-style instead of custom variable. Try to set it immediately, the local variable has not been set yet. I use hook to delay processing to avoid this problem. --- php-mode.el | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/php-mode.el b/php-mode.el index 79ae8e6e..8afe16ab 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) @@ -1131,6 +1132,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) @@ -1186,7 +1194,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) From 5edaae64af9f213692a12d48ad31d2f29c529c03 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Fri, 2 Mar 2018 23:00:16 +0900 Subject: [PATCH 4/6] Remove php-mode-custom-coding-style-set called when custom variable is set Since styles are set explicitly at the time of initialization of major mode, hacking of custom variables is no longer necessary. --- php-mode.el | 6 ------ 1 file changed, 6 deletions(-) diff --git a/php-mode.el b/php-mode.el index 8afe16ab..b95241b6 100644 --- a/php-mode.el +++ b/php-mode.el @@ -375,14 +375,8 @@ 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. From be1a488b178a90ae3738db42a9d2c63ad9d99f9d Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Fri, 2 Mar 2018 23:26:36 +0900 Subject: [PATCH 5/6] Add about personal and directory-local settings in Japanese --- README.ja.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) 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))) +``` + 実験的および作業中の機能 ------------------------------------- From db22b97408acba5e61908249e4c34ce1cc7d7452 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Fri, 2 Mar 2018 23:37:50 +0900 Subject: [PATCH 6/6] Fix test, use php-set-style instead php-mode-custom-coding-style-set --- php-mode-test.el | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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)))))