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.

452 lines
17 KiB

5 years ago
  1. ;;; php.el --- PHP support for friends -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2020 Friends of Emacs-PHP development
  3. ;; Author: USAMI Kenta <tadsan@zonu.me>
  4. ;; Created: 5 Dec 2018
  5. ;; Version: 1.24.0
  6. ;; Keywords: languages, php
  7. ;; Homepage: https://github.com/emacs-php/php-mode
  8. ;; License: GPL-3.0-or-later
  9. ;; This program is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; This file provides common variable and functions for PHP packages.
  21. ;;; Code:
  22. (require 'cl-lib)
  23. (require 'flymake)
  24. (require 'php-project)
  25. (require 'rx)
  26. ;;;###autoload
  27. (defgroup php nil
  28. "Language support for PHP."
  29. :tag "PHP"
  30. :group 'languages
  31. :link '(url-link :tag "Official Site" "https://github.com/emacs-php/php-mode")
  32. :link '(url-link :tag "PHP Mode Wiki" "https://github.com/emacs-php/php-mode/wiki"))
  33. (defcustom php-executable (or (executable-find "php") "/usr/bin/php")
  34. "The location of the PHP executable."
  35. :group 'php
  36. :tag "PHP Executable"
  37. :type 'string)
  38. (defcustom php-site-url "https://php.net/"
  39. "Default PHP.net site URL.
  40. The URL to use open PHP manual and search word."
  41. :group 'php
  42. :tag "PHP Site URL"
  43. :type 'string)
  44. (defcustom php-manual-url 'en
  45. "URL at which to find PHP manual.
  46. You can replace \"en\" with your ISO language code."
  47. :group 'php
  48. :tag "PHP Manual URL"
  49. :type '(choice (const :tag "English" 'en)
  50. (const :tag "Brazilian Portuguese" 'pt_BR)
  51. (const :tag "Chinese (Simplified)" 'zh)
  52. (const :tag "French" 'fr)
  53. (const :tag "German" 'de)
  54. (const :tag "Japanese" 'ja)
  55. (const :tag "Romanian" 'ro)
  56. (const :tag "Russian" 'ru)
  57. (const :tag "Spanish" 'es)
  58. (const :tag "Turkish" 'tr)
  59. (string :tag "PHP manual URL")))
  60. (defcustom php-search-url nil
  61. "URL at which to search for documentation on a word."
  62. :group 'php
  63. :tag "PHP Search URL"
  64. :type '(choice (string :tag "URL to search PHP documentation")
  65. (const :tag "Use `php-site-url' variable" nil)))
  66. (defcustom php-class-suffix-when-insert "::"
  67. "Suffix for inserted class."
  68. :group 'php
  69. :type 'string)
  70. (defcustom php-namespace-suffix-when-insert "\\"
  71. "Suffix for inserted namespace."
  72. :group 'php
  73. :type 'string)
  74. (defcustom php-default-major-mode 'php-mode
  75. "Major mode for editing PHP script."
  76. :group 'php
  77. :tag "PHP Default Major Mode"
  78. :type 'function)
  79. (defcustom php-html-template-major-mode 'web-mode
  80. "Major mode for editing PHP-HTML template."
  81. :group 'php
  82. :tag "PHP-HTML Template Major Mode"
  83. :type 'function)
  84. (defcustom php-blade-template-major-mode 'web-mode
  85. "Major mode for editing Blade template."
  86. :group 'php
  87. :tag "PHP Blade Template Major Mode"
  88. :type 'function)
  89. (defcustom php-template-mode-alist
  90. `(("\\.blade" . ,php-blade-template-major-mode)
  91. ("\\.phpt\\'" . ,(if (fboundp 'phpt-mode) 'phpt-mode php-default-major-mode))
  92. ("\\.phtml\\'" . ,php-html-template-major-mode))
  93. "Automatically use another MAJOR-MODE when open template file."
  94. :group 'php
  95. :tag "PHP Template Mode Alist"
  96. :type '(alist :key-type regexp :value-type function)
  97. :link '(url-link :tag "web-mode" "http://web-mode.org/")
  98. :link '(url-link :tag "phpt-mode" "https://github.com/emacs-php/phpt-mode"))
  99. (defcustom php-mode-maybe-hook nil
  100. "List of functions to be executed on entry to `php-mode-maybe'."
  101. :group 'php
  102. :tag "PHP Mode Maybe Hook"
  103. :type 'hook)
  104. (defcustom php-default-builtin-web-server-port 3939
  105. "Port number of PHP Built-in HTTP server (php -S)."
  106. :group 'php
  107. :tag "PHP Default Built-in Web Server Port"
  108. :type 'integer
  109. :link '(url-link :tag "Built-in web server"
  110. "https://www.php.net/manual/features.commandline.webserver.php"))
  111. ;;; PHP Keywords
  112. (defconst php-magical-constants
  113. (list "__LINE__" "__FILE__" "__FUNCTION__" "__CLASS__" "__TRAIT__" "__METHOD__" "__NAMESPACE__")
  114. "Magical keyword that is expanded at compile time.
  115. These are different from \"constants\" in strict terms.
  116. see https://www.php.net/manual/language.constants.predefined.php")
  117. ;;; Utillity for locate language construction
  118. (defsubst php-in-string-p ()
  119. "Return non-nil if inside a string.
  120. it is the character that will terminate the string, or t if the string should be terminated by a generic string delimiter."
  121. (nth 3 (syntax-ppss)))
  122. (defsubst php-in-comment-p ()
  123. "Return nil if outside a comment, t if inside a non-nestable comment, else an integer (the current comment nesting)."
  124. (nth 4 (syntax-ppss)))
  125. (defsubst php-in-string-or-comment-p ()
  126. "Return character address of start of comment or string; nil if not in one."
  127. (nth 8 (syntax-ppss)))
  128. (defsubst php-in-poly-php-html-mode ()
  129. "Return T if current buffer is in `poly-html-mode'."
  130. (and (boundp 'poly-php-html-mode)
  131. (symbol-value 'poly-php-html-mode)))
  132. (defconst php-beginning-of-defun-regexp
  133. "^\\s-*\\(?:\\(?:abstract\\|final\\|private\\|protected\\|public\\|static\\)\\s-+\\)*function\\s-+&?\\(\\(\\sw\\|\\s_\\)+\\)\\s-*("
  134. "Regular expression for a PHP function.")
  135. (eval-when-compile
  136. (defun php-create-regexp-for-method (&optional visibility)
  137. "Make a regular expression for methods with the given VISIBILITY.
  138. VISIBILITY must be a string that names the visibility for a PHP
  139. method, e.g. 'public'. The parameter VISIBILITY can itself also
  140. be a regular expression.
  141. The regular expression this function returns will check for other
  142. keywords that can appear in method signatures, e.g. 'final' and
  143. 'static'. The regular expression will have one capture group
  144. which will be the name of the method."
  145. (when (stringp visibility)
  146. (setq visibility (list visibility)))
  147. (rx-to-string `(: line-start
  148. (* (syntax whitespace))
  149. ,@(if visibility
  150. `((* (or "abstract" "final" "static")
  151. (+ (syntax whitespace)))
  152. (or ,@visibility)
  153. (+ (syntax whitespace))
  154. (* (or "abstract" "final" "static")
  155. (+ (syntax whitespace))))
  156. '((* (* (or "abstract" "final" "static"
  157. "private" "protected" "public")
  158. (+ (syntax whitespace))))))
  159. "function"
  160. (+ (syntax whitespace))
  161. (? "&" (* (syntax whitespace)))
  162. (group (+ (or (syntax word) (syntax symbol))))
  163. (* (syntax whitespace))
  164. "(")))
  165. (defun php-create-regexp-for-classlike (type)
  166. "Accepts a `TYPE' of a 'classlike' object as a string, such as
  167. 'class' or 'interface', and returns a regexp as a string which
  168. can be used to match against definitions for that classlike."
  169. (concat
  170. ;; First see if 'abstract' or 'final' appear, although really these
  171. ;; are not valid for all values of `type' that the function
  172. ;; accepts.
  173. "^\\s-*\\(?:\\(?:abstract\\|final\\)\\s-+\\)?"
  174. ;; The classlike type
  175. type
  176. ;; Its name, which is the first captured group in the regexp. We
  177. ;; allow backslashes in the name to handle namespaces, but again
  178. ;; this is not necessarily correct for all values of `type'.
  179. "\\s-+\\(\\(?:\\sw\\|\\\\\\|\\s_\\)+\\)")))
  180. (defconst php-imenu-generic-expression
  181. (eval-when-compile
  182. `(("Namespaces"
  183. ,(php-create-regexp-for-classlike "namespace") 1)
  184. ("Classes"
  185. ,(php-create-regexp-for-classlike "class") 1)
  186. ("Interfaces"
  187. ,(php-create-regexp-for-classlike "interface") 1)
  188. ("Traits"
  189. ,(php-create-regexp-for-classlike "trait") 1)
  190. ("All Methods"
  191. ,(php-create-regexp-for-method) 1)
  192. ("Private Methods"
  193. ,(php-create-regexp-for-method '("private")) 1)
  194. ("Protected Methods"
  195. ,(php-create-regexp-for-method '("protected")) 1)
  196. ("Public Methods"
  197. ,(php-create-regexp-for-method '("public")) 1)
  198. ("Anonymous Functions"
  199. "\\<\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*=\\s-*f\\(unctio\\)?n\\s-*(" 1)
  200. ("Named Functions"
  201. "^\\s-*function\\s-+\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*(" 1)))
  202. "Imenu generic expression for PHP Mode. See `imenu-generic-expression'.")
  203. (defconst php--re-namespace-pattern
  204. (eval-when-compile
  205. (php-create-regexp-for-classlike "namespace")))
  206. (defconst php--re-classlike-pattern
  207. (eval-when-compile
  208. (php-create-regexp-for-classlike (regexp-opt '("class" "interface" "trait")))))
  209. (defun php-get-current-element (re-pattern)
  210. "Return backward matched element by RE-PATTERN."
  211. (save-excursion
  212. (save-match-data
  213. (when (re-search-backward re-pattern nil t)
  214. (match-string-no-properties 1)))))
  215. ;;; Provide support for Flymake so that users can see warnings and
  216. ;;; errors in real-time as they write code.
  217. (defun php-flymake-php-init ()
  218. "PHP specific init-cleanup routines.
  219. This is an alternative function of `flymake-php-init'.
  220. Look at the `php-executable' variable instead of the constant \"php\" command."
  221. (let* ((init (funcall (eval-when-compile
  222. (if (fboundp 'flymake-proc-php-init)
  223. 'flymake-proc-php-init
  224. 'flymake-php-init)))))
  225. (list php-executable (cdr init))))
  226. (defconst php-re-detect-html-tag-aggressive
  227. (eval-when-compile
  228. (rx (or (: string-start (* (in space))
  229. "<!"
  230. (or "DOCTYPE" "doctype")
  231. (+ (in space))
  232. (or "HTML" "html"))
  233. (: (or line-start
  234. (: "<" (? "/")
  235. (* (in space)) (+ (in alpha "-")) (* (in space)) ">"))
  236. (: "<" (* (in space)) (+ (in alpha "-")) (* (in space)) ">"))))))
  237. (defconst php-re-detect-html-tag-default
  238. (eval-when-compile
  239. (rx (or (: string-start (* (in space))
  240. "<!"
  241. (or "DOCTYPE" "doctype")
  242. (+ (in space))
  243. (or "HTML" "html"))
  244. (: line-start
  245. (: "<" (* (in space)) (+ (in alpha "-")) (* (in space)) ">"))))))
  246. (defcustom php-re-detect-html-tag 'php-re-detect-html-tag-default
  247. "Regexp pattern variable-name of HTML detection."
  248. :group 'php
  249. :tag "PHP Re Detect HTML Tag"
  250. :type '(choice (const :tag "Default pattern" 'php-re-detect-html-tag-default)
  251. (const :tag "Aggressive pattern" 'php-re-detect-html-tag-aggressive)
  252. (variable :tag "Variable name of RegExp pattern")))
  253. (defsubst php-re-detect-html-tag ()
  254. "Return RegExp pattern for HTML detection."
  255. (if (symbolp php-re-detect-html-tag)
  256. (symbol-value php-re-detect-html-tag)
  257. php-re-detect-html-tag))
  258. (defun php-buffer-has-html-tag ()
  259. "Return position of HTML tag or NIL in current buffer."
  260. (save-excursion
  261. (save-restriction
  262. (widen)
  263. (goto-char (point-min))
  264. (save-match-data
  265. (re-search-forward (php-re-detect-html-tag) nil t)))))
  266. (defun php-derivation-major-mode ()
  267. "Return major mode for PHP file by file-name and its content."
  268. (let ((mode (assoc-default buffer-file-name
  269. php-template-mode-alist
  270. #'string-match-p))
  271. type)
  272. (when (and (null mode) buffer-file-name
  273. php-project-php-file-as-template)
  274. (setq type (php-project-get-file-html-template-type buffer-file-name))
  275. (cond
  276. ((eq t type) (setq mode php-html-template-major-mode))
  277. ((eq 'auto type)
  278. (when (php-buffer-has-html-tag)
  279. (setq mode php-html-template-major-mode)))))
  280. (when (and mode (not (fboundp mode)))
  281. (if (string-match-p "\\.blade\\." buffer-file-name)
  282. (warn "php-mode is NOT support blade template. %s"
  283. "Please install `web-mode' package")
  284. (setq mode nil)))
  285. (or mode php-default-major-mode)))
  286. ;;;###autoload
  287. (defun php-mode-maybe ()
  288. "Select PHP mode or other major mode."
  289. (interactive)
  290. (run-hooks php-mode-maybe-hook)
  291. (funcall (php-derivation-major-mode)))
  292. ;;;###autoload
  293. (defun php-current-class ()
  294. "Insert current class name if cursor in class context."
  295. (interactive)
  296. (let ((matched (php-get-current-element php--re-classlike-pattern)))
  297. (when matched
  298. (insert (concat matched php-class-suffix-when-insert)))))
  299. ;;;###autoload
  300. (defun php-current-namespace ()
  301. "Insert current namespace if cursor in namespace context."
  302. (interactive)
  303. (let ((matched (php-get-current-element php--re-namespace-pattern)))
  304. (when matched
  305. (insert (concat matched php-namespace-suffix-when-insert)))))
  306. ;;;###autoload
  307. (defun php-copyit-fqsen ()
  308. "Copy/kill class/method FQSEN."
  309. (interactive)
  310. (let ((namespace (or (php-get-current-element php--re-namespace-pattern) ""))
  311. (class (or (php-get-current-element php--re-classlike-pattern) ""))
  312. (namedfunc (php-get-current-element php-beginning-of-defun-regexp)))
  313. (kill-new (concat (if (string= namespace "") "" namespace)
  314. (if (string= class "") "" (concat "\\" class "::"))
  315. (if (string= namedfunc "") "" (concat namedfunc "()"))))))
  316. ;;;###autoload
  317. (defun php-run-builtin-web-server (router-or-dir hostname port &optional document-root)
  318. "Run PHP Built-in web server.
  319. `ROUTER-OR-DIR': Path to router PHP script or Document root.
  320. `HOSTNAME': Hostname or IP address of Built-in web server.
  321. `PORT': Port number of Built-in web server.
  322. `DOCUMENT-ROOT': Path to Document root.
  323. When `DOCUMENT-ROOT' is NIL, the document root is obtained from `ROUTER-OR-DIR'."
  324. (interactive
  325. (let ((insert-default-directory t)
  326. (d-o-r (read-file-name "Document root or Script: " default-directory)))
  327. (list
  328. (expand-file-name d-o-r)
  329. (read-string "Hostname: " "0.0.0.0")
  330. (read-number "Port: " php-default-builtin-web-server-port)
  331. (if (file-directory-p d-o-r)
  332. nil
  333. (let ((root-input (read-file-name "Document root: " (directory-file-name d-o-r))))
  334. (file-name-directory
  335. (if (file-directory-p root-input)
  336. root-input
  337. (directory-file-name root-input))))))))
  338. (let* ((default-directory
  339. (or document-root
  340. (if (file-directory-p router-or-dir)
  341. router-or-dir
  342. (directory-file-name router-or-dir))))
  343. (short-dirname (abbreviate-file-name default-directory))
  344. (short-filename (abbreviate-file-name router-or-dir))
  345. (buf-name (format "php -S %s:%s -t %s %s"
  346. hostname
  347. port
  348. short-dirname
  349. (if document-root short-filename "")))
  350. (args (cl-remove-if
  351. #'null
  352. (list "-S"
  353. (format "%s:%d" hostname port)
  354. "-t"
  355. default-directory
  356. (when document-root router-or-dir)))))
  357. (message "Run PHP built-in server: %s" buf-name)
  358. (apply #'make-comint buf-name php-executable nil args)
  359. (funcall
  360. (if (called-interactively-p 'interactive) #'display-buffer #'get-buffer)
  361. (format "*%s*" buf-name))))
  362. (defun php-ini ()
  363. "Get `php --ini' output buffer."
  364. (interactive)
  365. (let ((buffer (get-buffer-create " *php --ini*")))
  366. (with-current-buffer buffer
  367. (view-mode -1)
  368. (read-only-mode -1)
  369. (erase-buffer)
  370. (shell-command (concat php-executable " --ini") buffer)
  371. (view-mode +1))
  372. (if (called-interactively-p 'interactive)
  373. (pop-to-buffer buffer)
  374. buffer)))
  375. ;;;###autoload
  376. (defun php-find-system-php-ini-file (&optional file)
  377. "Find php.ini FILE by `php --ini'."
  378. (interactive
  379. (list
  380. (let* ((default-directory (expand-file-name "~"))
  381. (buffer (php-ini))
  382. (path (with-current-buffer buffer
  383. (goto-char (point-min))
  384. (save-match-data
  385. (when (re-search-forward ": \\(.+?\\)$" nil nil)
  386. (match-string 1))))))
  387. (when (or (null path) (not (file-directory-p path)))
  388. (when (called-interactively-p 'interactive)
  389. (pop-to-buffer buffer))
  390. (user-error "Failed get path to PHP ini files directory"))
  391. (read-file-name "Find php.ini file: "
  392. (concat (expand-file-name path) "/")
  393. nil nil nil
  394. #'file-exists-p))))
  395. (find-file file))
  396. (provide 'php)
  397. ;;; php.el ends here