diff --git a/AUTHORS.md b/AUTHORS.md index e873d9b5..6a9e4335 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -74,6 +74,7 @@ Names Sorted Alphabetically: - phil-s - takeokunn - tangxinfa +- tetsujin - tijsmallaerts - zapad - 顾伟刚 diff --git a/Makefile b/Makefile index 5dc3f21e..682675df 100644 --- a/Makefile +++ b/Makefile @@ -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) @@ -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) \ diff --git a/php-align.el b/php-align.el new file mode 100644 index 00000000..a4e8754d --- /dev/null +++ b/php-align.el @@ -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) +;; Maintainer: USAMI Kenta +;; 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 . + +;;; 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-*\\)\\(&&\\|||\\|\\\\|\\\\)") + (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 diff --git a/php-align/README.md b/php-align/README.md new file mode 100644 index 00000000..0e8000be --- /dev/null +++ b/php-align/README.md @@ -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 +); +``` diff --git a/php-project.el b/php-project.el index 99c3a898..f82b4e87 100644 --- a/php-project.el +++ b/php-project.el @@ -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)