summaryrefslogtreecommitdiff
path: root/.emacs.d/lisp/php.el
diff options
context:
space:
mode:
Diffstat (limited to '.emacs.d/lisp/php.el')
-rw-r--r--.emacs.d/lisp/php.el207
1 files changed, 207 insertions, 0 deletions
diff --git a/.emacs.d/lisp/php.el b/.emacs.d/lisp/php.el
new file mode 100644
index 0000000..db9e46d
--- /dev/null
+++ b/.emacs.d/lisp/php.el
@@ -0,0 +1,207 @@
1;;; php.el --- PHP support for friends -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2019 Friends of Emacs-PHP development
4
5;; Author: USAMI Kenta <tadsan@zonu.me>
6;; Created: 5 Dec 2018
7;; Version: 1.21.2
8;; Keywords: languages, php
9;; Homepage: https://github.com/emacs-php/php-mode
10;; Package-Requires: ((emacs "24.3") (cl-lib "0.5"))
11;; License: GPL-3.0-or-later
12
13;; This program is free software; you can redistribute it and/or modify
14;; it under the terms of the GNU General Public License as published by
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
17
18;; This program is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
24;; along with this program. If not, see <https://www.gnu.org/licenses/>.
25
26;;; Commentary:
27
28;; This file provides common variable and functions for PHP packages.
29
30;;; Code:
31(require 'flymake)
32
33;;;###autoload
34(defgroup php nil
35 "Language support for PHP."
36 :tag "PHP"
37 :group 'languages
38 :link '(url-link :tag "Official Site" "https://github.com/emacs-php/php-mode")
39 :link '(url-link :tag "PHP Mode Wiki" "https://github.com/emacs-php/php-mode/wiki"))
40
41(defcustom php-executable (or (executable-find "php") "/usr/bin/php")
42 "The location of the PHP executable."
43 :type 'string)
44
45(defcustom php-site-url "https://php.net/"
46 "Default PHP.net site URL.
47
48The URL to use open PHP manual and search word."
49 :type 'string)
50
51(defcustom php-manual-url 'en
52 "URL at which to find PHP manual.
53You can replace \"en\" with your ISO language code."
54 :type '(choice (const :tag "English" 'en)
55 (const :tag "Brazilian Portuguese" 'pt_BR)
56 (const :tag "Chinese (Simplified)" 'zh)
57 (const :tag "French" 'fr)
58 (const :tag "German" 'de)
59 (const :tag "Japanese" 'ja)
60 (const :tag "Romanian" 'ro)
61 (const :tag "Russian" 'ru)
62 (const :tag "Spanish" 'es)
63 (const :tag "Turkish" 'tr)
64 (string :tag "PHP manual URL")))
65
66(defcustom php-search-url nil
67 "URL at which to search for documentation on a word."
68 :group 'php
69 :type '(choice (string :tag "URL to search PHP documentation")
70 (const :tag "Use `php-site-url' variable" nil)))
71
72(defcustom php-class-suffix-when-insert "::"
73 "Suffix for inserted class."
74 :group 'php
75 :type 'string)
76
77(defcustom php-namespace-suffix-when-insert "\\"
78 "Suffix for inserted namespace."
79 :group 'php
80 :type 'string)
81
82;;; PHP Keywords
83(defconst php-magical-constants
84 (list "__LINE__" "__FILE__" "__FUNCTION__" "__CLASS__" "__TRAIT__" "__METHOD__" "__NAMESPACE__")
85 "Magical keyword that is expanded at compile time.
86
87These are different from \"constants\" in strict terms.
88see https://www.php.net/manual/language.constants.predefined.php")
89
90;;; Utillity for locate language construction
91(defsubst php-in-string-p ()
92 "Return non-nil if inside a string.
93it is the character that will terminate the string, or t if the string should be terminated by a generic string delimiter."
94 (nth 3 (syntax-ppss)))
95
96(defsubst php-in-comment-p ()
97 "Return nil if outside a comment, t if inside a non-nestable comment, else an integer (the current comment nesting)."
98 (nth 4 (syntax-ppss)))
99
100(defsubst php-in-string-or-comment-p ()
101 "Return character address of start of comment or string; nil if not in one."
102 (nth 8 (syntax-ppss)))
103
104(defun php-create-regexp-for-method (visibility)
105 "Make a regular expression for methods with the given VISIBILITY.
106
107VISIBILITY must be a string that names the visibility for a PHP
108method, e.g. 'public'. The parameter VISIBILITY can itself also
109be a regular expression.
110
111The regular expression this function returns will check for other
112keywords that can appear in method signatures, e.g. 'final' and
113'static'. The regular expression will have one capture group
114which will be the name of the method."
115 (concat
116 ;; Initial space with possible 'abstract' or 'final' keywords
117 "^\\s-*\\(?:\\(?:abstract\\|final\\)\\s-+\\)?"
118 ;; 'static' keyword may come either before or after visibility
119 "\\(?:" visibility "\\(?:\\s-+static\\)?\\|\\(?:static\\s-+\\)?" visibility "\\)\\s-+"
120 ;; Make sure 'function' comes next with some space after
121 "function\\s-+"
122 ;; Capture the name as the first group and the regexp and make sure
123 ;; by the end we see the opening parenthesis for the parameters.
124 "\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*("))
125
126(defun php-create-regexp-for-classlike (type)
127 "Accepts a `TYPE' of a 'classlike' object as a string, such as
128'class' or 'interface', and returns a regexp as a string which
129can be used to match against definitions for that classlike."
130 (concat
131 ;; First see if 'abstract' or 'final' appear, although really these
132 ;; are not valid for all values of `type' that the function
133 ;; accepts.
134 "^\\s-*\\(?:\\(?:abstract\\|final\\)\\s-+\\)?"
135 ;; The classlike type
136 type
137 ;; Its name, which is the first captured group in the regexp. We
138 ;; allow backslashes in the name to handle namespaces, but again
139 ;; this is not necessarily correct for all values of `type'.
140 "\\s-+\\(\\(?:\\sw\\|\\\\\\|\\s_\\)+\\)"))
141
142(defvar php-imenu-generic-expression
143 `(("Namespaces"
144 ,(php-create-regexp-for-classlike "namespace") 1)
145 ("Classes"
146 ,(php-create-regexp-for-classlike "class") 1)
147 ("Interfaces"
148 ,(php-create-regexp-for-classlike "interface") 1)
149 ("Traits"
150 ,(php-create-regexp-for-classlike "trait") 1)
151 ("All Methods"
152 ,(php-create-regexp-for-method "\\(?:\\sw\\|\\s_\\)+") 1)
153 ("Private Methods"
154 ,(php-create-regexp-for-method "private") 1)
155 ("Protected Methods"
156 ,(php-create-regexp-for-method "protected") 1)
157 ("Public Methods"
158 ,(php-create-regexp-for-method "public") 1)
159 ("Anonymous Functions"
160 "\\<\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*=\\s-*function\\s-*(" 1)
161 ("Named Functions"
162 "^\\s-*function\\s-+\\(\\(?:\\sw\\|\\s_\\)+\\)\\s-*(" 1))
163 "Imenu generic expression for PHP Mode. See `imenu-generic-expression'.")
164
165(defvar php--re-namespace-pattern
166 (php-create-regexp-for-classlike "namespace"))
167
168(defvar php--re-classlike-pattern
169 (php-create-regexp-for-classlike (regexp-opt '("class" "interface" "trait"))))
170
171(defun php-get-current-element (re-pattern)
172 "Return backward matched element by RE-PATTERN."
173 (save-excursion
174 (when (re-search-backward re-pattern nil t)
175 (match-string-no-properties 1))))
176
177;;; Provide support for Flymake so that users can see warnings and
178;;; errors in real-time as they write code.
179(defun php-flymake-php-init ()
180 "PHP specific init-cleanup routines.
181
182This is an alternative function of `flymake-php-init'.
183Look at the `php-executable' variable instead of the constant \"php\" command."
184 (let* ((init (funcall (eval-when-compile
185 (if (fboundp 'flymake-proc-php-init)
186 'flymake-proc-php-init
187 'flymake-php-init)))))
188 (list php-executable (cdr init))))
189
190;;;###autoload
191(defun php-current-class ()
192 "Insert current class name if cursor in class context."
193 (interactive)
194 (let ((matched (php-get-current-element php--re-classlike-pattern)))
195 (when matched
196 (insert (concat matched php-class-suffix-when-insert)))))
197
198;;;###autoload
199(defun php-current-namespace ()
200 "Insert current namespace if cursor in namespace context."
201 (interactive)
202 (let ((matched (php-get-current-element php--re-namespace-pattern)))
203 (when matched
204 (insert (concat matched php-namespace-suffix-when-insert)))))
205
206(provide 'php)
207;;; php.el ends here