|
47 | 47 |
|
48 | 48 | ;;; Code:
|
49 | 49 | (require 'php)
|
| 50 | +(require 'etags) |
50 | 51 |
|
51 | 52 | (defconst php-local-manual-documentation-types
|
52 | 53 | '("function" "control-structures" "class" "book")
|
@@ -124,6 +125,93 @@ With a prefix argument, prompt (with completion) for a word to search for."
|
124 | 125 |
|
125 | 126 | ;;;###autoload
|
126 | 127 | (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))) |
127 | 215 |
|
128 | 216 | (provide 'php-local-manual)
|
129 | 217 | ;;; php-local-manual.el ends here
|
0 commit comments