| | 171 | |
| | 172 | (defvar phpcmp-get-functions-async-buffer-name " *php-completion functions*") |
| | 173 | (lexical-let (functions) |
| | 174 | (defun phpcmp-get-functions () |
| | 175 | (or functions |
| | 176 | (prog1 phpcmp-functions |
| | 177 | (phpcmp-async-set-functions)))) |
| | 178 | |
| | 179 | (defun phpcmp-clear-functions () |
| | 180 | (setq functions nil)) |
| | 181 | |
| | 182 | (defun phpcmp-async-set-functions () |
| | 183 | (or functions |
| | 184 | (let* ((buf-name phpcmp-get-functions-async-buffer-name) |
| | 185 | (process-running? (eq 'run |
| | 186 | (let ((proc (get-buffer-process buf-name))) |
| | 187 | (when (processp proc) |
| | 188 | (process-status proc))))) |
| | 189 | (php-command (executable-find "php"))) |
| | 190 | (when (and (not (phpcmp-tramp-p)) |
| | 191 | (not process-running?) |
| | 192 | php-command) |
| | 193 | (phpcmp-async-do |
| | 194 | :buffer-name buf-name |
| | 195 | :command php-command |
| | 196 | :args '("-r" |
| | 197 | "foreach (get_defined_functions() as $vars) { foreach ($vars as $var) {echo \"$var\n\";}}") |
| | 198 | :callback (lambda () |
| | 199 | (setq functions (phpcmp-collect-matches "[[:print:]]+"))))))))) |
| | 200 | |
| | 201 | (defun phpcmp-tramp-p () |
| | 202 | (when (and (featurep 'tramp) |
| | 203 | (fboundp 'tramp-tramp-file-p)) |
| | 204 | (tramp-tramp-file-p (phpcmp-get-current-directory)))) |
| | 205 | |
| | 206 | (defun phpcmp-get-current-directory () |
| | 207 | (file-name-directory |
| | 208 | (expand-file-name |
| | 209 | (or (buffer-file-name) |
| | 210 | default-directory)))) |
| | 211 | |
| | 212 | (defun* phpcmp-collect-matches |
| | 213 | (re &optional (count 0) (match-string-fn 'match-string) |
| | 214 | (point-min (point-min)) (point-max (point-max))) |
| | 215 | (save-excursion |
| | 216 | (loop initially (goto-char point-min) |
| | 217 | while (re-search-forward re point-max t) |
| | 218 | collect (funcall match-string-fn count)))) |
| | 219 | |
| | 220 | (defun* phpcmp-async-do |
| | 221 | (&key command args buffer-name |
| | 222 | (callback 'identity) |
| | 223 | (errorback (lambda() (message (buffer-string))))) |
| | 224 | (lexical-let ((buf (get-buffer-create buffer-name)) |
| | 225 | (callback callback) |
| | 226 | (errorback errorback)) |
| | 227 | (lexical-let |
| | 228 | ((sentinel (lambda (proc event) |
| | 229 | (cond ((and (string= event "finished\n") |
| | 230 | (= (process-exit-status proc) 0)) |
| | 231 | (with-current-buffer buf |
| | 232 | (funcall callback))) |
| | 233 | ((and (string= event "finished\n") |
| | 234 | (/= (process-exit-status proc) 0)) |
| | 235 | (with-current-buffer buf |
| | 236 | (funcall errorback))))))) |
| | 237 | (set-process-sentinel (apply 'start-process command buf command args) sentinel)))) |
| | 238 | |