Skip to content

Commit d8b2479

Browse files
authored
Merge pull request #429 from zonuexe/feature/php-project-coding-style
Add php-project-coding-style variable
2 parents 11abbfb + db22b97 commit d8b2479

File tree

4 files changed

+74
-13
lines changed

4 files changed

+74
-13
lines changed

README.ja.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,37 @@ GNU Emacs 24以降では、[package][]機能を使って[MELPA][]からPHPモー
3737

3838
報告の際には `php-mode-version` コマンドを実行して、その出力をバグレポートに含めてください。問題を再現するための手がかりになります。
3939

40+
Settings
41+
--------
42+
43+
### 個人設定
44+
45+
.emacsファイル(`~/.emacs.d/init.el`)にPHPモードでの設定を記述できます。
46+
47+
```lisp
48+
(defun my-php-mode-init ()
49+
(setq-local show-trailing-whitespace t)
50+
(setq-local ac-disable-faces '(font-lock-comment-face font-lock-string-face))
51+
(setq-local page-delimiter "\\_<\\(class\\|function\\|namespace\\)\\_>.+$")
52+
53+
;; If you feel phumped and phpcs annoying, invalidate them.
54+
(when (boundp 'flycheck-disabled-checkers)
55+
(add-to-list 'flycheck-disabled-checkers 'php-phpmd)
56+
(add-to-list 'flycheck-disabled-checkers 'php-phpcs)))
57+
58+
(add-hook 'php-mode-hook #'my-php-mode-init)
59+
```
60+
61+
### プロジェクトローカル設定
62+
63+
プロジェクトのトップディレクトリに`.dir-locals.el`を記述すると、プロジェクト単位の設定を追加することができます。このファイルはユーザー自身のEmacsにインストールされたパッケージに依存するため、バージョン管理の対象に含めないことを推奨します。
64+
65+
```lisp
66+
((nil
67+
(php-project-root . git)
68+
(php-project-coding-style . psr2)))
69+
```
70+
4071
実験的および作業中の機能
4172
-------------------------------------
4273

php-mode-test.el

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,14 +275,14 @@ style from Drupal."
275275
;; the file written to has no significance, only the buffer
276276
(let ((tmp-filename (concat (make-temp-name temporary-file-directory) ".php")))
277277
(dolist (mode '(pear wordpress symfony2))
278-
(php-mode-custom-coding-style-set 'php-mode-coding-style 'drupal)
279-
(php-mode-custom-coding-style-set 'php-mode-coding-style mode)
278+
(php-set-style "drupal")
279+
(php-set-style (symbol-name mode))
280280
(should-not show-trailing-whitespace)
281-
(php-mode-custom-coding-style-set 'php-mode-coding-style 'psr2)
282-
(php-mode-custom-coding-style-set 'php-mode-coding-style mode)
281+
(php-set-style "psr2")
282+
(php-set-style (symbol-name mode))
283283
(should-not show-trailing-whitespace)
284284

285-
(php-mode-custom-coding-style-set 'php-mode-coding-style 'drupal)
285+
(php-set-style "drupal")
286286
(write-file tmp-filename)
287287
(should (looking-at-p "$"))))))
288288

@@ -871,7 +871,7 @@ style from Drupal."
871871
(ert-deftest php-mode-test-issue-200 ()
872872
"Test highlighting and elimination of extraneous whitespace in PSR-2 mode"
873873
(with-php-mode-test ("issue-200.php")
874-
(php-mode-custom-coding-style-set 'php-mode-coding-style 'psr2)
874+
(php-set-style "psr2")
875875
(should show-trailing-whitespace)
876876
(should (and (listp before-save-hook) (member 'delete-trailing-whitespace before-save-hook)))))
877877

php-mode.el

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787

8888
(require 'cl-lib)
8989
(require 'mode-local)
90+
(require 'php-project)
9091

9192
(eval-when-compile
9293
(require 'regexp-opt)
@@ -374,14 +375,15 @@ This variable can take one of the following symbol values:
374375
(const :tag "WordPress" wordpress)
375376
(const :tag "Symfony2" symfony2)
376377
(const :tag "PSR-2" psr2))
377-
:set 'php-mode-custom-coding-style-set
378378
:initialize 'custom-initialize-default)
379379

380-
(defun php-mode-custom-coding-style-set (sym value)
381-
(when (eq major-mode 'php-mode)
382-
(set sym value)
383-
(set-default sym value)
384-
(php-set-style (symbol-name value))))
380+
381+
(defcustom php-mode-enable-project-coding-style t
382+
"When set to true override php-mode-coding-style by php-project-coding-style.
383+
384+
If you want to suppress styles from being overwritten by directory / file
385+
local variables, set NIL."
386+
:type 'boolean)
385387

386388
(defun php-mode-version ()
387389
"Display string describing the version of PHP Mode."
@@ -1124,6 +1126,13 @@ After setting the stylevars run hooks according to STYLENAME
11241126

11251127
(put 'php-set-style 'interactive-form (interactive-form 'c-set-style))
11261128

1129+
(defun php-mode-set-style-delay ()
1130+
"Set the current `php-mode' buffer to use the style by custom or local variables."
1131+
(let ((coding-style (or (and (boundp 'php-project-coding-style) php-project-coding-style)
1132+
php-mode-coding-style)))
1133+
(prog1 (php-set-style (symbol-name coding-style))
1134+
(remove-hook 'hack-local-variables-hook #'php-mode-set-style-delay))))
1135+
11271136
(defun php-mode-debug ()
11281137
"Display informations useful for debugging PHP Mode."
11291138
(interactive)
@@ -1179,7 +1188,12 @@ After setting the stylevars run hooks according to STYLENAME
11791188
;; PHP vars are case-sensitive
11801189
(setq case-fold-search t)
11811190

1182-
(php-set-style (symbol-name php-mode-coding-style))
1191+
;; When php-mode-enable-project-coding-style is set, it is delayed by hook.
1192+
;; Since it depends on the timing at which the file local variable is set.
1193+
;; File local variables are set after initialization of major mode except `run-hook' is complete.
1194+
(if php-mode-enable-project-coding-style
1195+
(add-hook 'hack-local-variables-hook #'php-mode-set-style-delay t t)
1196+
(php-set-style (symbol-name php-mode-coding-style)))
11831197

11841198
(when (or php-mode-force-pear
11851199
(and (stringp buffer-file-name)

php-project.el

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
;; Return root directory of current buffer file. The root directory is
3434
;; determined by several marker file or directory.
3535
;;
36+
;; ## `.dir-locals.el' support
37+
;;
38+
;; - `php-project-coding-style'
39+
;; - Symbol value of the coding style. (ex. `pear', `psr2')
40+
;;
41+
;;
3642

3743
;;; Code:
3844
(require 'cl-lib)
@@ -64,6 +70,16 @@ SYMBOL
6470
(make-variable-buffer-local 'php-project-root)
6571
(put 'php-project-root 'safe-local-variable
6672
#'(lambda (v) (assq v php-project-available-root-files))))
73+
74+
;;;###autoload
75+
(progn
76+
(defvar php-project-coding-style nil
77+
"Symbol value of the coding style of the project that PHP major mode refers to.
78+
79+
Typically it is `pear', `drupal', `wordpress', `symfony2' and `psr2'.")
80+
(make-variable-buffer-local 'php-project-coding-style)
81+
(put 'php-project-coding-style 'safe-local-variable #'symbolp))
82+
6783

6884
;; Functions
6985

0 commit comments

Comments
 (0)