Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.2 KiB

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