Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Names Sorted Alphabetically:
- phil-s
- takeokunn
- tangxinfa
- tetsujin
- tijsmallaerts
- zapad
- 顾伟刚
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
EMACS ?= emacs
ELS = php.el php-face.el php-project.el php-mode.el php-mode-debug.el
ELS = php.el php-align.el php-face.el php-project.el php-mode.el php-mode-debug.el
AUTOLOADS = php-mode-autoloads.el
ELCS = $(ELS:.el=.elc)

Expand All @@ -21,7 +21,7 @@ AUTHORS.md: AUTHORS.md.in

autoloads: $(AUTOLOADS)

$(AUTOLOADS): php.el php-face.el php-project.el php-mode-debug.el php-mode.el
$(AUTOLOADS): php.el php-align.el php-face.el php-project.el php-mode-debug.el php-mode.el
$(EMACS) -Q -batch -L . --eval \
"(progn \
(require 'package) \
Expand Down
127 changes: 127 additions & 0 deletions php-align.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
;;; php-align.el --- Alignment configuration for PHP -*- lexical-binding: t; -*-

;; Copyright (C) 2011 tetsujin (Yusuke Segawa)
;; Copyright (C) 2020 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.22.2
;; Package-Requires: ((emacs "24.3"))
;; 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
97 changes: 97 additions & 0 deletions php-align/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# php-align.el

CAUTION!! this is still experimental.

Support alignment (e.g. `align`, `align-current`) for PHP.

Put this file into your load-path.and the following code into your ~/.emacs

```el
(add-hook 'php-mode-hook #'php-align-setup)
```

## Examples

### 1.

#### before

```php
$foo = "string"; // M-x align-current
$looooooooong = 1; //
```

#### after

```php
$foo = "string"; // M-x align-current
$looooooooong = 1; //
```

### 2.

#### before

```php
"$foo = 1";
$foo = "string"; // M-x align-current
$looooooooong = 1; //

$bar = 2; //
```

#### after

```php
"$foo = 1";
$foo = "string"; // M-x align-current
$looooooooong = 1; //

$bar = 2; //
```

### 3.

#### before

```php
$variable = 1;
$vars = array(); // M-x align-current
if ($variable == $vars) {

}
```

#### after

```php
$variable = 1;
$vars = array(); // M-x align-current
if ($variable == $vars) {

}
```

### 4.

#### before

```php
$vars = array(
1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12, // C-u M-x align-current
);
```

#### after

```php
$vars = array(
1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12, // C-u M-x align-current
);
```
4 changes: 4 additions & 0 deletions php-project.el
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ defines constants, and sets the class loaders.")
Typically it is `pear', `drupal', `wordpress', `symfony2' and `psr2'.")
(put 'php-project-coding-style 'safe-local-variable #'symbolp)

(defvar-local php-project-align-lines t
"If T, automatically turn on `php-align-mode' by `php-align-setup'.")
(put 'php-project-align-lines 'safe-local-variable #'booleanp)

(defvar-local php-project-php-file-as-template 'auto
"
`auto' (default)
Expand Down