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.

126 lines
4.1 KiB

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