Skip to content

Commit 5bdd2f9

Browse files
authored
Merge pull request #667 from emacs-php/refactor/php-completion-move-to-local-manual
Move php-completion functions to local manual
2 parents 7954afb + e151159 commit 5bdd2f9

File tree

3 files changed

+112
-101
lines changed

3 files changed

+112
-101
lines changed

Diff for: lisp/php-local-manual.el

+88
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
;;; Code:
4949
(require 'php)
50+
(require 'etags)
5051

5152
(defconst php-local-manual-documentation-types
5253
'("function" "control-structures" "class" "book")
@@ -124,6 +125,93 @@ With a prefix argument, prompt (with completion) for a word to search for."
124125

125126
;;;###autoload
126127
(define-obsolete-function-alias 'php-search-local-documentation #'php-local-manual-search "2.0.0")
128+
129+
;; Define function name completion function
130+
(defvar php-local-manual--completion-table nil
131+
"Obarray of tag names defined in current tags table and functions known to PHP.")
132+
133+
(defun php-local-manual-complete-function ()
134+
"Perform function completion on the text around point.
135+
Completes to the set of names listed in the current tags table
136+
and the standard php functions.
137+
The string to complete is chosen in the same way as the default
138+
for \\[find-tag] (which see)."
139+
(interactive)
140+
(let ((pattern (php-get-pattern))
141+
beg
142+
completion
143+
(php-functions (php-local-manual-completion-table)))
144+
(if (not pattern) (message "Nothing to complete")
145+
(if (not (search-backward pattern nil t))
146+
(message "Can't complete here")
147+
(setq beg (point))
148+
(forward-char (length pattern))
149+
(setq completion (try-completion pattern php-functions nil))
150+
(cond ((eq completion t))
151+
((null completion)
152+
(message "Can't find completion for \"%s\"" pattern)
153+
(ding))
154+
((not (string= pattern completion))
155+
(delete-region beg (point))
156+
(insert completion))
157+
(t
158+
(let ((selected (completing-read
159+
"Select completion: "
160+
(all-completions pattern php-functions)
161+
nil t pattern)))
162+
(delete-region beg (point))
163+
(insert selected))))))))
164+
165+
(defun php-local-manual-completion-table ()
166+
"Build variable `php-local-manual--completion-table' on demand.
167+
The table includes the PHP functions and the tags from the
168+
current `tags-file-name'."
169+
(or (and tags-file-name
170+
(save-excursion (tags-verify-table tags-file-name))
171+
php-local-manual--completion-table)
172+
(let ((tags-table
173+
(when tags-file-name
174+
(with-current-buffer (get-file-buffer tags-file-name)
175+
(etags-tags-completion-table))))
176+
(php-table
177+
(cond ((and (not (string= "" php-completion-file))
178+
(file-readable-p php-completion-file))
179+
(php-local-manual-build-table-from-file php-completion-file))
180+
((and (not (string= "" php-manual-path))
181+
(file-directory-p php-manual-path))
182+
(php-local-manual-build-table-from-path php-manual-path))
183+
(t nil))))
184+
(unless (or php-table tags-table)
185+
(user-error
186+
(concat "No TAGS file active nor are "
187+
"`php-completion-file' or `php-manual-path' set")))
188+
(when tags-table
189+
;; Combine the tables.
190+
(if (obarrayp tags-table)
191+
(mapatoms (lambda (sym) (intern (symbol-name sym) php-table))
192+
tags-table)
193+
(setq php-table (append tags-table php-table))))
194+
(setq php-local-manual--completion-table php-table))))
195+
196+
(defun php-local-manual-build-table-from-file (filename)
197+
(let ((table (make-vector 1022 0))
198+
(buf (find-file-noselect filename)))
199+
(with-current-buffer buf
200+
(goto-char (point-min))
201+
(while (re-search-forward
202+
"^\\([-a-zA-Z0-9_.]+\\)\n"
203+
nil t)
204+
(intern (buffer-substring (match-beginning 1) (match-end 1))
205+
table)))
206+
(kill-buffer buf)
207+
table))
208+
209+
(defun php-local-manual-build-table-from-path (path)
210+
"Return list of PHP function name from `PATH' directory."
211+
(cl-loop for file in (directory-files path nil "^function\\..+\\.html$")
212+
if (string-match "\\.\\([-a-zA-Z_0-9]+\\)\\.html$" file)
213+
collect (replace-regexp-in-string
214+
"-" "_" (substring file (match-beginning 1) (match-end 1)) t)))
127215

128216
(provide 'php-local-manual)
129217
;;; php-local-manual.el ends here

Diff for: lisp/php-mode.el

+2-101
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969

7070
(require 'font-lock)
7171
(require 'custom)
72-
(require 'etags)
7372
(require 'speedbar)
7473
(require 'imenu)
7574
(require 'package)
@@ -1267,10 +1266,7 @@ After setting the stylevars run hooks according to STYLENAME
12671266
#'semantic-create-imenu-index)
12681267
"Imenu index function for PHP.")
12691268

1270-
1271-
;; Define function name completion function
1272-
(defvar php-completion-table nil
1273-
"Obarray of tag names defined in current tags table and functions known to PHP.")
1269+
(autoload 'php-local-manual-complete-function "php-local-manual")
12741270

12751271
(defun php-complete-function ()
12761272
"Perform function completion on the text around point.
@@ -1279,102 +1275,7 @@ and the standard php functions.
12791275
The string to complete is chosen in the same way as the default
12801276
for \\[find-tag] (which see)."
12811277
(interactive)
1282-
(let ((pattern (php-get-pattern))
1283-
beg
1284-
completion
1285-
(php-functions (php-completion-table)))
1286-
(if (not pattern) (message "Nothing to complete")
1287-
(if (not (search-backward pattern nil t))
1288-
(message "Can't complete here")
1289-
(setq beg (point))
1290-
(forward-char (length pattern))
1291-
(setq completion (try-completion pattern php-functions nil))
1292-
(cond ((eq completion t))
1293-
((null completion)
1294-
(message "Can't find completion for \"%s\"" pattern)
1295-
(ding))
1296-
((not (string= pattern completion))
1297-
(delete-region beg (point))
1298-
(insert completion))
1299-
(t
1300-
(let ((selected (completing-read
1301-
"Select completion: "
1302-
(all-completions pattern php-functions)
1303-
nil t pattern)))
1304-
(delete-region beg (point))
1305-
(insert selected))))))))
1306-
1307-
(defun php-completion-table ()
1308-
"Build variable `php-completion-table' on demand.
1309-
The table includes the PHP functions and the tags from the
1310-
current `tags-file-name'."
1311-
(or (and tags-file-name
1312-
(save-excursion (tags-verify-table tags-file-name))
1313-
php-completion-table)
1314-
(let ((tags-table
1315-
(when tags-file-name
1316-
(with-current-buffer (get-file-buffer tags-file-name)
1317-
(etags-tags-completion-table))))
1318-
(php-table
1319-
(cond ((and (not (string= "" php-completion-file))
1320-
(file-readable-p php-completion-file))
1321-
(php-build-table-from-file php-completion-file))
1322-
((and (not (string= "" php-manual-path))
1323-
(file-directory-p php-manual-path))
1324-
(php-build-table-from-path php-manual-path))
1325-
(t nil))))
1326-
(unless (or php-table tags-table)
1327-
(error
1328-
(concat "No TAGS file active nor are "
1329-
"`php-completion-file' or `php-manual-path' set")))
1330-
(when tags-table
1331-
;; Combine the tables.
1332-
(if (obarrayp tags-table)
1333-
(mapatoms (lambda (sym) (intern (symbol-name sym) php-table))
1334-
tags-table)
1335-
(setq php-table (append tags-table php-table))))
1336-
(setq php-completion-table php-table))))
1337-
1338-
(defun php-build-table-from-file (filename)
1339-
(let ((table (make-vector 1022 0))
1340-
(buf (find-file-noselect filename)))
1341-
(with-current-buffer buf
1342-
(goto-char (point-min))
1343-
(while (re-search-forward
1344-
"^\\([-a-zA-Z0-9_.]+\\)\n"
1345-
nil t)
1346-
(intern (buffer-substring (match-beginning 1) (match-end 1))
1347-
table)))
1348-
(kill-buffer buf)
1349-
table))
1350-
1351-
(defun php-build-table-from-path (path)
1352-
"Return list of PHP function name from `PATH' directory."
1353-
(cl-loop for file in (directory-files path nil "^function\\..+\\.html$")
1354-
if (string-match "\\.\\([-a-zA-Z_0-9]+\\)\\.html$" file)
1355-
collect (replace-regexp-in-string
1356-
"-" "_" (substring file (match-beginning 1) (match-end 1)) t)))
1357-
1358-
;; Find the pattern we want to complete
1359-
;; find-tag-default from GNU Emacs etags.el
1360-
(defun php-get-pattern ()
1361-
(save-excursion
1362-
(while (looking-at "\\sw\\|\\s_")
1363-
(forward-char 1))
1364-
(if (or (re-search-backward "\\sw\\|\\s_"
1365-
(save-excursion (beginning-of-line) (point))
1366-
t)
1367-
(re-search-forward "\\(\\sw\\|\\s_\\)+"
1368-
(save-excursion (end-of-line) (point))
1369-
t))
1370-
(progn (goto-char (match-end 0))
1371-
(buffer-substring-no-properties
1372-
(point)
1373-
(progn (forward-sexp -1)
1374-
(while (looking-at "\\s'")
1375-
(forward-char 1))
1376-
(point))))
1377-
nil)))
1278+
(php-local-manual-complete-function))
13781279

13791280
(defun php-show-arglist ()
13801281
"Show function arguments at cursor position."

Diff for: lisp/php.el

+22
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,28 @@ can be used to match against definitions for that classlike."
419419
(when (re-search-backward re-pattern nil t)
420420
(match-string-no-properties 1)))))
421421

422+
(defun php-get-pattern ()
423+
"Find the pattern we want to complete.
424+
`find-tag-default' from GNU Emacs etags.el"
425+
(save-excursion
426+
(save-match-data
427+
(while (looking-at "\\sw\\|\\s_")
428+
(forward-char 1))
429+
(when (or (re-search-backward "\\sw\\|\\s_"
430+
(save-excursion (beginning-of-line) (point))
431+
t)
432+
(re-search-forward "\\(\\sw\\|\\s_\\)+"
433+
(save-excursion (end-of-line) (point))
434+
t))
435+
(goto-char (match-end 0))
436+
(buffer-substring-no-properties
437+
(point)
438+
(progn
439+
(forward-sexp -1)
440+
(while (looking-at "\\s'")
441+
(forward-char 1))
442+
(point)))))))
443+
422444
;;; Provide support for Flymake so that users can see warnings and
423445
;;; errors in real-time as they write code.
424446
(defun php-flymake-php-init ()

0 commit comments

Comments
 (0)