forked from arnested/php-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-extras-gen-eldoc.el
134 lines (106 loc) · 5.47 KB
/
php-extras-gen-eldoc.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
;;; php-extras-gen-eldoc.el --- Extra features for `php-mode'
;; Copyright (C) 2012, 2013 Arne Jørgensen
;; Author: Arne Jørgensen <[email protected]>
;; 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:
;; Download and parse PHP manual from php.net and build a new
;; `php-extras-function-arguments' hash table of PHP functions and
;; their arguments.
;; Please note that build a new `php-extras-function-arguments' is a
;; slow process and might be error prone.
;; The code in this file can must likely be heavily improved.
;;; Code:
(require 'php-extras)
(defvar php-extras-gen-eldoc-temp-methodname nil)
(defvar php-extras-php-manual-url "http://dk1.php.net/distributions/manual/php_manual_en.tar.gz"
"URL of the downloadable .tar.gz version of the PHP manual.")
(defun php-extras-gen-eldoc-elem-string (elem)
"Helper function for regenerating PHP function argument hash."
;; Element is a string it self just return it.
(if (stringp elem)
elem
;; If element is a list of exactly 3 elements - we continue with
;; the 3rd element.
(if (and (listp elem)
(= (safe-length elem) 3))
(progn
(ignore-errors
(when (string= (cdr (car (car (cdr elem)))) "methodname")
(setq php-extras-gen-eldoc-temp-methodname (nth 2 (nth 2 elem)))))
(php-extras-gen-eldoc-elem-string (nth 2 elem)))
;; If the element is a list with anything but 3 elements iterate
;; over them.
(if (listp elem)
(let ((result ""))
(dolist (elem2 (cdr (cdr elem)) result)
(setq result (concat result (php-extras-gen-eldoc-elem-string elem2)))))))))
;;;###autoload
(defun php-extras-generate-eldoc ()
"Regenerate PHP function argument hash table from php.net. This is slow!"
(interactive)
(when (yes-or-no-p "Regenerate PHP function argument hash table from php.net. This is slow! ")
(php-extras-generate-eldoc-1 t)))
(defun php-extras-generate-eldoc-1 (&optional byte-compile)
"Regenerate PHP function argument hash table from php.net. This is slow!"
(save-excursion
(let ((gzip (executable-find "gzip"))
(php-extras-function-arguments (make-hash-table
:size 4400
:rehash-threshold 1.0
:rehash-size 100
:test 'equal))
(tar-buffer nil))
(with-temp-buffer (url-insert-file-contents php-extras-php-manual-url)
(call-process-region (point-min) (point-max) gzip t t nil "-c" "-d")
(tar-mode)
(setq tar-buffer (current-buffer))
(goto-char (point-min))
(while (re-search-forward "/function\.\\(.+\\)\.html" nil t)
(message "Parsing %s..." (match-string-no-properties 1))
(let ((buf (tar-extract)))
(with-current-buffer buf
(goto-char (point-min))
(let ((min nil)
(max nil)
(help-string ""))
(when (search-forward "<div class=\"methodsynopsis dc-description\">" nil t)
(setq min (point))
(search-forward "</div>" nil t)
(setq max (point))
(dolist (elem (cdr (cdr (nth 2 (libxml-parse-html-region min max)))))
(setq help-string (concat help-string (php-extras-gen-eldoc-elem-string elem))))
(setq help-string (replace-regexp-in-string "[ \n]+" " " help-string))
(when (and php-extras-gen-eldoc-temp-methodname help-string)
(puthash php-extras-gen-eldoc-temp-methodname help-string php-extras-function-arguments)))))
(kill-buffer buf))
(set-buffer tar-buffer)))
(let* ((file (concat php-extras-eldoc-functions-file ".el"))
(buf (find-file file)))
(with-current-buffer buf
(widen)
(kill-region (point-min) (point-max))
(insert (format
";;; %s.el -- file auto generated by `php-extras-generate-eldoc'
(require 'php-extras)
(setq php-extras-function-arguments %s)
(provide 'php-extras-eldoc-functions)
;;; %s.el ends here
"
(file-name-nondirectory php-extras-eldoc-functions-file)
(prin1-to-string php-extras-function-arguments)
(file-name-nondirectory php-extras-eldoc-functions-file)))
(save-buffer)
(when byte-compile
(byte-compile-file file t)))))))
(provide 'php-extras-gen-eldoc)
;;; php-extras-gen-eldoc.el ends here