Skip to content

Commit 6cd78b8

Browse files
committed
Add 'php-align/' from commit '1761ede04060acc723c9c297794846c1213d241c'
git-subtree-dir: php-align git-subtree-mainline: 423c054 git-subtree-split: 1761ede
2 parents 423c054 + 1761ede commit 6cd78b8

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed

php-align/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# About #
2+
CAUTION!! this is still experimental.
3+
4+
Support alignment (e.g. align, align-current) for PHP.
5+
6+
Put this file into your load-path.and the following code into your ~/.emacs
7+
8+
<code>
9+
(add-hook 'php-mode-hook
10+
(lambda ()
11+
(require 'php-align)
12+
(php-align-setup)))
13+
</code>
14+
15+
# Examples #
16+
17+
## 1. ##
18+
19+
### before ###
20+
21+
$foo = "string"; // M-x arign-current
22+
$looooooooong = 1; //
23+
24+
### after ###
25+
26+
$foo = "string"; // M-x arign-current
27+
$looooooooong = 1; //
28+
29+
## 2. ##
30+
31+
### before ###
32+
33+
"$foo = 1";
34+
$foo = "string"; // M-x arign-current
35+
$looooooooong = 1; //
36+
37+
$bar = 2; //
38+
39+
### after ###
40+
41+
"$foo = 1";
42+
$foo = "string"; // M-x arign-current
43+
$looooooooong = 1; //
44+
45+
$bar = 2; //
46+
47+
## 3. ##
48+
49+
### before ###
50+
$variable = 1;
51+
$vars = array(); // M-x align-current
52+
if ($variable == $vars) {
53+
54+
}
55+
56+
### after ###
57+
$variable = 1;
58+
$vars = array(); // M-x align-current
59+
if ($variable == $vars) {
60+
61+
}
62+
63+
## 4. ##
64+
65+
### before ###
66+
$vars = array(
67+
1, 2, 3,
68+
4, 5, 6,
69+
7, 8, 9,
70+
10, 11, 12, // C-u M-x align-current
71+
);
72+
73+
### after ###
74+
$vars = array(
75+
1, 2, 3,
76+
4, 5, 6,
77+
7, 8, 9,
78+
10, 11, 12, // C-u M-x align-current
79+
);

php-align/php-align.el

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

0 commit comments

Comments
 (0)