blob: 634d008b4169c74387c0d04bfe3f8fc49de93db8 [file] [log] [blame]
Daniel Veillardca989762001-06-23 17:39:29 +00001;;; libxml-doc.el - look up libxml-symbols and start browser on documentation
Daniel Veillardc310d562000-06-23 18:32:15 +00002
Daniel Veillardca989762001-06-23 17:39:29 +00003;; Author: Felix Natter <fnatter@gmx.net>, Geert Kloosterman <geertk@ai.rug.nl>
Daniel Veillardc310d562000-06-23 18:32:15 +00004;; Created: Jun 21 2000
5;; Keywords: libxml documentation
6
7;; This program is free software; you can redistribute it and/or
8;; modify it under the terms of the GNU General Public License
9;; as published by the Free Software Foundation; either version 2
10;; of the License, or (at your option) any later version.
11;;
12;; This program is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16;;
17;; You should have received a copy of the GNU General Public License
18;; along with this program; if not, write to the Free Software
19;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 ;;; Commentary / README
22
23;; these functions allow you to browse the libxml documentation
24;; (using lynx within emacs by default;
25;; ----- Installing
26;; 1. add the following to ~/.emacs (adapt path and remove comments !)
Daniel Veillardca989762001-06-23 17:39:29 +000027;; (autoload 'libxmldoc-lookup-symbol "~/elisp/libxml-doc"
28;; "Look up libxml-symbols and start browser on documentation." t)
29;; or put this file in load-path and use this:
30;; (autoload 'libxmldoc-lookup-symbol "libxml-doc"
31;; "Look up libxml-symbols and start browser on documentation." t)
Daniel Veillardc310d562000-06-23 18:32:15 +000032;; 2. adapt libxmldoc-root:
33;; i.e. (setq libxmldoc-root "~/libxml2-2.0.0/doc/html/")
34;; 3. change the filter-regex: by default, cpp-defines, callbacks and
35;; html-functions are excluded (C-h v libxmldoc-filter-regexp)
36;; 4. consider customizing libxmldoc-browse-url (lynx by default);
37;; cannot use Emacs/W3 4.0pre46 because it has problems with the html
38;; ----- Using
Daniel Veillardca989762001-06-23 17:39:29 +000039;; call M-x libxmldoc-lookup-symbol: this will prompt with completion and
40;; then open the browser showing the documentation. If the word around the
41;; point matches a symbol, that is used instead. You can also call
42;; libxmldoc-lookup-symbol noninteractively and pass the symbol.
43
44;; Note:
45;; an alternative to libxml-doc is emacs tags:
46;; $ cd libxml2-2.3.8
47;; $ etags *.c *.h
48;; $ emacs
49;; M-. (M-x find-tag) ... or
50;; M-x tags-search ... RET M-, M-, ...
51;; (for more information: info emacs RET m Tags RET)
52
Daniel Veillardc310d562000-06-23 18:32:15 +000053
54 ;;; ChangeLog:
55;; Wed Jun 21 01:07:12 2000: initial release
56;; Wed Jun 21 01:45:29 2000: added libxmldoc-lookup-symbol-at-point
57;; Wed Jun 21 23:37:58 2000: libxmldoc-lookup-symbol now uses
58;; (thing-at-point 'word) if it matches a symbol
59;; Thu Jun 22 02:37:46 2000: filtering is only done for completion
60;; Thu Jun 22 21:03:41 2000: libxmldoc-browse-url can be customized
61
Daniel Veillard8a367d42001-06-07 14:01:34 +000062;; Thu May 31 2001 (Geert):
63;; - Changed the `gnome-xml-' html file prefix into `libxml-'.
64;; - Changed the 'word match from thing-at-point into 'symbol.
65;; With 'word, identifiers with an underscore (e.g. BAD_CAST)
66;; don't get matched.
67
Daniel Veillardca989762001-06-23 17:39:29 +000068;; Fri Jun 8 16:29:18 2001, Sat Jun 23 16:19:47 2001:
69;; complete rewrite of libxmldoc-lookup-symbol
70;; by Felix Natter <fnatter@gmx.net>, Geert Kloosterman <geertk@ai.rug.nl>:
Daniel Veillard8a367d42001-06-07 14:01:34 +000071;; - Now keeps the list of symbols between calls to speed things up.
Daniel Veillardca989762001-06-23 17:39:29 +000072;; - filtering is only used when no symbol is passed and
73;; thing-at-point does not match a symbol and "*" + thing-at-point
74;; does not match a symbol (this is used to catch callbacks) and
75;; libxmldoc-filter-regexp is non-nil.
76;; Sat Jun 23 16:20:34 2001: update the docstrings
77;; Sat Jun 23 16:22:54 2001 (Geert Kloosterman <geertk@ai.rug.nl>):
78;; update README: use autoload instead of load+c-mode-hook
Daniel Veillard8a367d42001-06-07 14:01:34 +000079
Daniel Veillardc310d562000-06-23 18:32:15 +000080;;; TODO:
Daniel Veillardca989762001-06-23 17:39:29 +000081;; - use command-execute for libxmldoc-browse-url
82;; - keep (match-string 1) in a variable (libxmldoc-get-list-of-symbols)
83;; - check the (require ..)-statements
Daniel Veillardc310d562000-06-23 18:32:15 +000084
85 ;;; Code:
86
Daniel Veillardca989762001-06-23 17:39:29 +000087(provide 'libxmldoc)
88
89(require 'browse-url)
90(require 'term)
91
92(defvar libxmldoc-root "~/src/libxml2-2.3.8/doc/html"
Daniel Veillardc310d562000-06-23 18:32:15 +000093 "The root-directory of the libxml2-documentation (~ will be expanded).")
Daniel Veillardca989762001-06-23 17:39:29 +000094(defvar libxmldoc-filter-regexp "^html\\|^\\*\\|^[A-Z_]+"
Daniel Veillardc310d562000-06-23 18:32:15 +000095 "Symbols that match this regular expression will be excluded when doing
Daniel Veillardca989762001-06-23 17:39:29 +000096completion and no symbol is specified.
Daniel Veillardc310d562000-06-23 18:32:15 +000097 For example:
98 callbacks: \"^\\\\*\"
99 cpp-defines: \"[A-Z_]+\"
100 xml-functions \"^xml\"
101 html-functions \"^html\"
102 sax-functions \".*SAX\"
Daniel Veillardca989762001-06-23 17:39:29 +0000103By default, callbacks, cpp-defines and html* are excluded.
104Set this to nil if you don't want filtering.")
Daniel Veillardc310d562000-06-23 18:32:15 +0000105(defvar libxmldoc-browse-url 'browse-url-lynx-emacs
106 "Browser used for browsing documentation. Emacs/W3 4.0pre46 cannot handle
Daniel Veillardca989762001-06-23 17:39:29 +0000107the html (and would be too slow), so lynx-emacs is used by default.")
Daniel Veillardc310d562000-06-23 18:32:15 +0000108(defvar libxmldoc-symbol-history nil
109 "History for looking up libxml-symbols.")
Daniel Veillardca989762001-06-23 17:39:29 +0000110(defvar libxmldoc-symbols nil
111 "The list of libxml-symbols.")
Daniel Veillard8a367d42001-06-07 14:01:34 +0000112
Daniel Veillardc310d562000-06-23 18:32:15 +0000113 ;;;; public functions
114
115(defun libxmldoc-lookup-symbol(&optional symbol)
116 "Look up xml-symbol." (interactive)
Daniel Veillardca989762001-06-23 17:39:29 +0000117 (let
118 ;; this is necessary so that filtering is done case-sensitively
119 ((case-fold-search nil))
Daniel Veillard8a367d42001-06-07 14:01:34 +0000120
Daniel Veillardca989762001-06-23 17:39:29 +0000121 ;; Build up a symbol list if neccesary
122 (if (null libxmldoc-symbols)
123 (setq libxmldoc-symbols (libxmldoc-get-list-of-symbols)))
124
125 (cond
126 ((not (null symbol)) ;; symbol is specified as argument
127 (if (not (assoc symbol libxmldoc-symbols))
128 (setq symbol nil)))
129 ((assoc (thing-at-point 'symbol) libxmldoc-symbols)
130 (setq symbol (thing-at-point 'symbol)))
131 ;; this is needed to catch callbacks
132 ;; note: this could be rewritten to use (thing-at-point 'word)
133 ((assoc (concat "*" (thing-at-point 'symbol)) libxmldoc-symbols)
134 (setq symbol (concat "*" (thing-at-point 'symbol))))
135 )
Daniel Veillard8a367d42001-06-07 14:01:34 +0000136
Daniel Veillardca989762001-06-23 17:39:29 +0000137 ;; omit "" t) from call to completing-read for the sake of xemacs
138 (setq symbol
139 (completing-read "Libxml: " (if (or symbol
140 (null libxmldoc-filter-regexp))
141 libxmldoc-symbols
142 (mapcar
143 '(lambda(key,value)
144 (if (null (string-match
145 libxmldoc-filter-regexp
146 (car key,value)))
147 key,value))
148 libxmldoc-symbols))
149 nil t symbol
150 'libxmldoc-symbol-history))
151
152
153 ;; start browser
154 (apply libxmldoc-browse-url
155 (list (cdr (assoc symbol libxmldoc-symbols))))))
Daniel Veillardc310d562000-06-23 18:32:15 +0000156
157;;;; internal
158
Daniel Veillardca989762001-06-23 17:39:29 +0000159(defun libxmldoc-get-list-of-symbols()
Daniel Veillardc310d562000-06-23 18:32:15 +0000160 "Get the list of html-links in the libxml-documentation."
Daniel Veillardca989762001-06-23 17:39:29 +0000161 (let ((files
162 (directory-files
163 libxmldoc-root t
164 (concat "^" (if (file-exists-p (concat libxmldoc-root
165 "/libxml-parser.html"))
166 "libxml-"
167 "gnome-xml-")
168 ".*\\.html$") t))
Daniel Veillard8a367d42001-06-07 14:01:34 +0000169 (symbols ())
170 (case-fold-search t)
Daniel Veillard8a367d42001-06-07 14:01:34 +0000171 (uri))
Daniel Veillardca989762001-06-23 17:39:29 +0000172 (message "collecting libxml-symbols...")
Daniel Veillard8a367d42001-06-07 14:01:34 +0000173 (while (car files)
Daniel Veillardca989762001-06-23 17:39:29 +0000174 (with-temp-buffer
175 (insert-file-contents (car files))
176 (goto-char (point-min))
177 (while (re-search-forward
178 "<a[^>]*href[ \t\n]*=[ \t\n]*\"\\([^=>]*\\)\"[^>]*>" nil t nil)
179 (setq uri (concat "file://" (expand-file-name libxmldoc-root) "/"
180 (match-string 1)))
181 (if (not (re-search-forward "\\([^<]*\\)<" nil t nil))
182 (error "regexp error while getting libxml-symbols.."))
183 ;; this needs add-to-list because i.e. xmlChar appears often
184 (if (not (string-equal "" (match-string 1)))
185 (add-to-list 'symbols (cons (match-string 1) uri))))
186 ;; (setq symbols (cons (cons (match-string 1) uri) symbols)))
187 )
188 (setq files (cdr files)))
189 symbols))
Daniel Veillardc310d562000-06-23 18:32:15 +0000190
191;;; libxml-doc.el ends here
Daniel Veillardca989762001-06-23 17:39:29 +0000192
Daniel Veillard8a367d42001-06-07 14:01:34 +0000193;;; Local Variables:
194;;; indent-tabs-mode: nil
195;;; End: