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