-
Notifications
You must be signed in to change notification settings - Fork 5
/
php-doc.el
281 lines (247 loc) · 9.66 KB
/
php-doc.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
;;; php-doc.el --- Php document helper
;; Copyright (C) 2009 Free Software Foundation, Inc.
;;
;; Author: Ye Wenbin <[email protected]>
;; Maintainer: Ye Wenbin <[email protected]>
;; Created: 01 Jan 2009
;; Version: 0.01
;; Keywords: languages, convenience
;; This program 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 2, or (at your option)
;; any later version.
;;
;; This program 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 program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Dependency:
;; 1. tree-mode.el - http://www.emacswiki.org/cgi-bin/emacs/tree-mode.el
;; 2. windata.el - http://www.emacswiki.org/cgi-bin/emacs/windata.el
;; 3. help-dwim.el - http://www.emacswiki.org/cgi-bin/emacs/help-dwim.el
;;
;; If you use perl, install Emacs::PDE instead, it provide more features.
;;; Installation:
;; 1. Download php_manaul_en.tar.gz to local
;; 2. extract to certain directory
;; 3. add this to .emacs
;; (require 'php-doc nil t)
;; (setq php-doc-directory "/path/to/php_manual/html")
;; (add-hook 'php-mode-hook
;; (lambda ()
;; (local-set-key "\t" 'php-doc-complete-function)
;; (local-set-key (kbd "\C-c h") 'php-doc)
;; (set (make-local-variable 'eldoc-documentation-function)
;; 'php-doc-eldoc-function)
;; (eldoc-mode 1)))
;; Put this file into your load-path and the following into your ~/.emacs:
;; (require 'php-doc)
;;; Code:
(eval-when-compile
(require 'cl))
(require 'complete)
(require 'tree-mode)
(require 'windata)
(defgroup php-doc nil
"View Php manual in emacs"
:group 'tools)
(defcustom php-doc-directory "/Users/manuel/code/web/php-chunked-xhtml"
"*Directory of php manual (multiple page html version)"
:type 'directory
:group 'php-doc)
(defcustom php-doc-cachefile "~/.emacs.d/php-doc"
"*File to save php function symbol"
:type 'file
:group 'php-doc)
(defcustom php-doc-tree-windata '(frame left 0.3 delete)
"*Arguments to set the window buffer display.
See `windata-display-buffer' for setup the arguments."
:type 'sexp
:group 'php-doc)
(defcustom php-doc-tree-theme "default"
"*Theme of tree-widget."
:type 'string
:group 'php-doc)
(defcustom php-doc-tree-buffer "*PHP-doc*"
"*Buffer name for `php-doc-tree'"
:type 'string
:group 'php-doc)
(defcustom php-doc-browser-function
(if (featurep 'w3m-load)
'php-doc-w3m
browse-url-browser-function)
"*Function to browse html file"
:type 'function
:group 'php-doc)
(defvar php-doc-tree nil)
(defvar php-doc-obarray nil
"All php functions")
(defsubst php-doc-function-file (sym)
(expand-file-name (format "function.%s.html" (replace-regexp-in-string "_" "-" (symbol-name sym)))
php-doc-directory))
(defun php-doc-build-tree (&optional no-cache)
(interactive "P")
(let (functions function tree path)
(if (and (not no-cache)
(file-readable-p php-doc-cachefile))
(with-temp-buffer
(insert-file-contents php-doc-cachefile)
(setq functions (read (current-buffer))))
(let ((files (directory-files php-doc-directory nil "\\.html$")))
(dolist (file files)
(when (not (string-match "^index" file))
(setq path (nbutlast (split-string file "\\."))
function nil)
(when (string= (car path) "function")
(setq function (replace-regexp-in-string "-" "_" (cadr path)))
(if (string-match "-" (cadr path))
(setq path (nconc (list (car path) (substring (cadr path) 0 (match-beginning 0)))
(cdr path)))))
(push (cons path function) functions)))
;; save to cache file
(when (file-writable-p php-doc-cachefile)
(with-temp-buffer
(prin1 functions (current-buffer))
(write-file php-doc-cachefile)))))
(setq php-doc-obarray (make-vector 1519 nil))
(dolist (function functions)
(when (cdr function)
(intern (cdr function) php-doc-obarray))
(php-doc-add-to-tree 'tree (car function)))
(setq php-doc-tree tree)))
(defun php-doc-add-to-tree (sym list)
(if list
(let ((val (assoc (car list) (symbol-value sym)))
(subtree (gensym))
restree)
(unless val
(setq val (cons (car list) nil))
(set sym (cons val (symbol-value sym))))
(set subtree (cdr val))
(setq restree (php-doc-add-to-tree subtree (cdr list)))
(if restree
(setcdr val restree))
(symbol-value sym))))
(define-derived-mode php-doc-mode tree-mode "PHPDoc"
"List perl module using tree-widget.
\\{perldoc-mode-map}"
(tree-widget-set-theme php-doc-tree-theme))
(defun php-doc (sym)
"Display document of php function"
(interactive
(progn
(or php-doc-obarray (php-doc-build-tree))
(let ((def (current-word)))
(list (intern (completing-read (if def
(format "PHP Function(default %s): " def)
"PHP Function: ")
php-doc-obarray nil t nil nil def) php-doc-obarray)))))
(let ((browse-url-browser-function php-doc-browser-function)
(file (php-doc-function-file sym)))
(if (file-exists-p file)
(browse-url (concat "file://" file)))))
(defun php-doc-tree ()
(interactive)
(unless php-doc-tree
(php-doc-build-tree))
(unless (get-buffer php-doc-tree-buffer)
(with-current-buffer (get-buffer-create php-doc-tree-buffer)
(php-doc-mode)
(widget-apply-action (widget-create (php-doc-tree-widget (cons "PHP-Doc" php-doc-tree))))
(widget-setup)
(goto-char (point-min))))
(select-window (apply 'windata-display-buffer
(get-buffer php-doc-tree-buffer)
php-doc-tree-windata)))
(defun php-doc-tree-widget (list)
`(tree-widget
:node (push-button :button-face dired-directory
:notify php-doc-view-or-expand
:tag ,(car list)
:format "%[%t%]\n")
,@(mapcar
(lambda (elem)
(if (cdr elem)
(php-doc-tree-widget elem)
`(push-button :notify php-doc-view-or-expand
:tag ,(car elem)
:format "%[%t%]\n")))
(cdr list))))
(defun php-doc-view-or-expand (node &rest ignore)
(let ((browse-url-browser-function php-doc-browser-function)
(me node)
path)
(while (widget-get node :parent)
(push (tree-mode-node-tag node) path)
(setq node (widget-get node :parent)))
(if (and (string= (car path) "function")
(= (length path) 3))
(setq path (cons (car path) (cddr path))))
(setq file (expand-file-name (concat (mapconcat 'identity path ".") ".html")
php-doc-directory))
(if (file-exists-p file)
(browse-url (concat "file://" file))
(tree-mode-toggle-expand))))
(defun w3m-browse-url-other-window (url &optional newwin)
(let ((w3m-pop-up-windows t))
(if (one-window-p) (split-window))
(other-window 1)
(w3m-browse-url url newwin)))
(defun php-doc-w3m (url &rest ignore)
(let ((win (next-window))
buf)
(save-excursion
(w3m-browse-url url))))
(if (featurep 'help-dwim)
(help-dwim-register
(cons 'php-doc ["a-z_" php-doc-obarray nil php-doc]) t))
(defun php-doc-complete-function ()
(interactive)
(unless php-doc-tree
(php-doc-build-tree))
(let* ((end (point))
(beg (save-excursion
(with-syntax-table c-mode-syntax-table
(backward-sexp 1)
(while (= (char-syntax (following-char)) ?\')
(forward-char 1))
(point))))
(minibuffer-completion-table php-doc-obarray)
(minibuffer-completion-predicate 'identity)
(PC-not-minibuffer t))
(if (equal last-command 'PC-lisp-complete-symbol)
(PC-do-completion nil beg PC-lisp-complete-end t)
(if PC-lisp-complete-end
(move-marker PC-lisp-complete-end end)
(setq PC-lisp-complete-end (copy-marker end t)))
(PC-do-completion nil beg end t))))
(defun php-doc-eldoc-function ()
(let (string symbol done)
(save-excursion
(while (not (or done symbol))
(or (and (setq string (thing-at-point 'symbol))
(setq symbol (intern-soft string php-doc-obarray)))
(setq done (null (re-search-backward "\\>\\s-*(" (line-beginning-position) t))))))
(when symbol
(php-doc-function-synopsis symbol))))
(defun php-doc-function-synopsis (sym)
(or (get sym 'method-synopsis)
(let ((file (php-doc-function-file sym))
begin synopsis)
(when (file-exists-p file)
(with-temp-buffer
(insert-file-contents file)
(re-search-forward "<div class=\"methodsynopsis dc-description\">")
(setq begin (point))
(re-search-forward "</div>")
(setq synopsis
(replace-regexp-in-string "[ \t\n]+" " "
(replace-regexp-in-string "<[^<]+>" "" (buffer-substring begin (point)))))
(put sym 'method-synopsis synopsis)
synopsis)))))
(provide 'php-doc)
;;; php-doc.el ends here