-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathphp-align.el
126 lines (110 loc) · 4.14 KB
/
php-align.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
;;; php-align.el --- Alignment configuration for PHP -*- lexical-binding: t; -*-
;; Copyright (C) 2011 tetsujin (Yusuke Segawa)
;; Copyright (C) 2023 Friends of Emacs-PHP development
;; Author: tetsujin (Yusuke Segawa) <tetsujin85 (at) gmail.com>
;; Maintainer: USAMI Kenta <[email protected]>
;; Keywords: php languages convenience align
;; Homepage: https://github.com/emacs-php/php-mode
;; Version: 1.26.1
;; License: GPL-3.0-or-later
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This extension provides alignment for PHP.
;; Note that you must have Font Lock mode enabled.
;;
;; Put this file into your load-path.and the following code into your ~/.emacs
;;
;; (add-hook 'php-mode-hook #'php-align-setup)
;;; TODO:
;; - Add test codes using el-expectations.
;;; Code:
(require 'align)
(require 'regexp-opt)
(require 'php-project)
(defvar php-align-rules-list
`((php-comma-delimiter
(regexp . ",\\(\\s-*\\)[^/ \t\n]")
(repeat . t)
(modes . '(php-mode))
(run-if . ,(function (lambda () current-prefix-arg))))
(php-assignment
(regexp . ,(concat "[^=!^&*-+<>/.| \t\n]\\(\\s-*[=!^&%*-+<>/.|]*\\)=>?"
"\\(\\s-*\\)\\([^= \t\n]\\|$\\)"))
(group . (1 2))
(modes . '(php-mode))
(justify . t)
(tab-stop . nil))
(php-comment
(regexp . "\\(\\s-*\\)\\(//.*\\|/\\*.*\\*/\\s-*\\)$")
(modes . (php-mode))
(column . comment-column)
(valid . ,(function
(lambda ()
(save-excursion
(goto-char (match-beginning 1))
(not (bolp)))))))
(php-chain-logic
(regexp . "\\(\\s-*\\)\\(&&\\|||\\|\\<and\\>\\|\\<or\\>\\)")
(modes . (php-mode))
(valid . ,(function
(lambda ()
(save-excursion
(goto-char (match-end 2))
(looking-at "\\s-*\\(/[*/]\\|$\\)"))))))))
(defvar php-align-region-separate
(eval-when-compile
(concat
;; blank line
"\\(?:" "^\\s-*$" "\\)"
"\\|"
;; comment start or end line
"\\(?:" "^\\s-*\\(?:/[/*]\\|\\*/\\)" "\\)"
"\\|"
;; end of line are '[', '(', '{', '}', '/*'
"\\(?:" "\\(?:[[({}]\\|/\\*+\\)\\s-*$" "\\)"
"\\|"
;; beginning of line are ')', '}', ']' and trailing character are ',', ';'
"\\(?:" "^\\s-*[)}]][ \t,;]?\\s-*$" "\\)"
"\\|"
;; beginning of line are some PHP keywrods
"\\(?:"
"^\\s-*"
(regexp-opt
'("for" "foreach" "while" "if" "else" "switch" "case" "break" "continue"
"try" "catch" "declare" "do" "return" "namespace" "use"))
"[ ;]"
"\\)"
"\\|"
;; function or method call
"\\(?:" "^\\s-*" "\\(?:" "\\w\\|[->\\: \t]" "\\)+" "(" "\\)"))
"Regexp of a section of PHP for alignment.")
;;;###autoload
(defun php-align-setup ()
"Setup alignment configuration for PHP code."
(when php-project-align-lines
(php-align-mode 1)))
(defvar php-align-mode-lighter " PHP-Align")
;;;###autoload
(define-minor-mode php-align-mode
"Alignment lines for PHP script."
:lighter php-align-mode-lighter
(add-to-list 'align-open-comment-modes 'php-mode)
(add-to-list 'align-dq-string-modes 'php-mode)
(add-to-list 'align-sq-string-modes 'php-mode)
(if php-align-mode
(progn
(setq-local align-mode-rules-list php-align-rules-list)
(setq-local align-region-separate php-align-region-separate))
(setq-local align-mode-rules-list nil)
(setq-local align-region-separate nil)))
(provide 'php-align)
;;; php-align.el ends here