forked from arnested/php-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp-extras.el
169 lines (134 loc) · 6.16 KB
/
php-extras.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
;;; php-extras.el --- Extra features for `php-mode'
;; Copyright (C) 2012, 2013 Arne Jørgensen
;; Author: Arne Jørgensen <[email protected]>
;; URL: https://github.com/arnested/php-extras
;; Created: June 28, 2012
;; Version: 0.4.3
;; Package-Requires: ((php-mode "1.5.0"))
;; Keywords: programming, php
;; This software 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 software 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 software. If not, see
;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Extra features for `php-mode':
;; * `php-extras-insert-previous-variable'
;; * `php-extras-eldoc-documentation-function'
;; * Auto complete source for PHP functions based on
;; `php-extras-eldoc-documentation-function'
;;; Code:
(defvar php-extras-php-variable-regexp
(format "\\(\\$[a-zA-Z_%s-%s][a-zA-Z0-9_%s-%s]*\\(\\[.*\\]\\)*\\)"
(char-to-string 127) (char-to-string 255)
(char-to-string 127) (char-to-string 255))
"Regexp for a PHP variable.")
(defvar php-extras-eldoc-functions-file (concat (file-name-directory (or load-file-name buffer-file-name)) "php-extras-eldoc-functions")
"File holding `php-extras-function-arguments' hash table.")
(defvar php-extras-function-arguments 'not-loaded
"Hash table of PHP functions and their function arguments.
Generated by `php-extras-gen-eldoc-elem-string'.")
;;;###autoload
(defcustom php-extras-insert-previous-variable-key [(control c) (control $)]
"Key sequence for `php-extras-insert-previous-variable'."
:group 'php
:set #'(lambda (symbol value)
;; When `php-mode-map' is already loaded define it in the map.
(when (boundp 'php-mode-map)
;; First undefine the old key sequence if defined.
(when (eq (lookup-key php-mode-map php-extras-insert-previous-variable-key) 'php-extras-insert-previous-variable)
(define-key php-mode-map php-extras-insert-previous-variable-key 'undefined))
;; Then define the new key sequence.
(define-key php-mode-map value 'php-extras-insert-previous-variable))
;; Finally and always set the variable `php-extras-insert-previous-variable-key'.
(set-default symbol value))
:type 'key-sequence)
;;;###autoload
(defcustom php-extras-auto-complete-insert-parenthesis t
"Whether auto complete insert should add a pair of parenthesis."
:group 'php
:type 'boolean)
;;;###autoload
(defun php-extras-insert-previous-variable (arg)
"Insert previously used variable from buffer.
With prefix argument search that number of times backwards for
variable. If prefix argument is negative search forward."
(interactive "P")
(when (null arg)
(setq arg 1))
(save-excursion
(dotimes (var (abs arg))
(if (> arg 0)
(re-search-backward php-extras-php-variable-regexp nil t)
(re-search-forward php-extras-php-variable-regexp nil t))))
(if (match-string-no-properties 1)
(insert (match-string-no-properties 1))
(message "No variable to insert.")))
;;;###autoload
(defun php-extras-eldoc-documentation-function ()
"Get function arguments for core PHP function at point."
(when (eq php-extras-function-arguments 'not-loaded)
(require 'php-extras-eldoc-functions php-extras-eldoc-functions-file t))
(when (hash-table-p php-extras-function-arguments)
(gethash (php-get-pattern) php-extras-function-arguments)))
;;;###autoload
(add-hook 'php-mode-hook #'(lambda ()
(unless eldoc-documentation-function
(set (make-local-variable 'eldoc-documentation-function)
#'php-extras-eldoc-documentation-function))))
(defun php-extras-ac-insert-action ()
"Auto-complete insert action for PHP.
If `php-extras-auto-complete-insert-parenthesis' is t insert a
pair of parenthesis after the inserted auto complete selection;
place point in between the parenthesis and show the `eldoc'
documentation for the inserted selection."
(when php-extras-auto-complete-insert-parenthesis
(insert "()")
(backward-char)
(when (and (boundp 'eldoc-documentation-function)
(fboundp 'eldoc-message))
(eldoc-message (funcall eldoc-documentation-function)))))
(defvar ac-source-php-extras
'((candidates . php-extras-autocomplete-candidates)
(candidate-face . php-extras-autocomplete-candidate-face)
(selection-face . php-extras-autocomplete-selection-face)
(action . php-extras-ac-insert-action)
(cache))
"Auto complete source for PHP functions.")
(defface php-extras-autocomplete-candidate-face
'((t (:inherit ac-candidate-face)))
"Face for PHP auto complete candidate."
:group 'php)
(defface php-extras-autocomplete-selection-face
'((t (:inherit ac-selection-face)))
"Face for PHP auto complete selection."
:group 'php)
(defun php-extras-autocomplete-candidates ()
"Generate PHP auto complete candidates.
The candidates are generated from the
`php-extras-function-arguments' hash table."
(let (candidates)
(when (eq php-extras-function-arguments 'not-loaded)
(require 'php-extras-eldoc-functions php-extras-eldoc-functions-file t))
(when (hash-table-p php-extras-function-arguments)
(maphash (lambda (key value) (setq candidates (cons key candidates))) php-extras-function-arguments))
candidates))
;;;###autoload
(defun php-extras-autocomplete-setup ()
(eval-after-load 'auto-complete
'(add-to-list 'ac-sources 'ac-source-php-extras)))
;;;###autoload
(add-hook 'php-mode-hook #'php-extras-autocomplete-setup)
;;;###autoload
(eval-after-load 'php-mode
`(let ((map php-mode-map)
(key php-extras-insert-previous-variable-key))
(define-key map key 'php-extras-insert-previous-variable)))
(provide 'php-extras)
;;; php-extras.el ends here