Initial Commit
[packages] / xemacs-packages / speedbar / sb-html.el
1 ;;; sb-html.el --- provide hierarchical speedbar menu's for HTML files
2
3 ;; Copyright (c) 2001, 2002 Richard Y. Kim
4
5 ;; Author: Richard Y. Kim, <ryk@dspwiz.com>
6 ;; Maintainer: Richard Y. Kim, <ryk@dspwiz.com>
7 ;; Created: Mon Apr 09 09:44:06 2001
8 ;; Version: $Id: sb-html.el,v 1.4 2005/09/30 20:25:56 zappo Exp $
9 ;; Keywords: speedbar, html
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2 of
14 ;; the License, or (at your option) any later version.
15
16 ;; This program is distributed in the hope that it will be
17 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
18 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 ;; PURPOSE.  See the GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public
22 ;; License along with this program; if not, write to the Free
23 ;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24 ;; MA 02110-1301 USA
25
26 ;;; Commentary:
27 ;;
28 ;; This small add-on to speedbar provides an alternate way to view HTML
29 ;; documents which shows the natural hierarchy of the document based
30 ;; on H1, H2, ... H6 tags.
31 ;;
32 ;; This code is a simple modification of sb-texinfo.el which comes
33 ;; with speedbar.
34
35 ;; Installation procedure:
36 ;;
37 ;;   Install speedbar 0.12 or later.
38 ;;   Add the following to your ~/.emacs file:
39 ;;   (eval-after-load "speedbar" '(load-library "sb-html"))
40 ;;
41 ;; Known Problems:
42 ;;
43 ;;   Perhaps <HEAD> and <BODY> should be treated as H0 tag?
44
45 ;;; Change Log:
46 ;;;
47 ;;; 1.1 - modified regexp based on Eric's suggestion.
48 ;;;
49 ;;; 1.0 - sent to Eric Ludlam.
50
51 ;;; Code:
52
53 (require 'speedbar)
54 (require 'sb-texinfo)   ; for speedbar-format-texinfo-list
55
56 ;; Attach these new functions to handle texinfo-mode.
57 (add-to-list 'speedbar-dynamic-tags-function-list
58              '(speedbar-fetch-dynamic-html . speedbar-insert-html-list))
59
60 ;; This returns t if the major mode of the current buffer is not
61 ;; 'html-helper-mode. If it is 'html-helper-mode, then this returns a
62 ;; list where each element is (LEVEL NAME . MARKER). LEVEL is 0, 1, 2,
63 ;; 3, 4, or 5 corresponding to H1, H2, H3, H4, H5 an H6
64 ;; tags. respectively. NAME is the name of the section. MARKER is
65 ;; emacs marker that points to the beginning of the section. The
66 ;; elements in the list returned are in ascending order of the
67 ;; MARKER. This function along with it's parter,
68 ;; speedbar-insert-html-list, are designed to be added to the
69 ;; speedbar-dynamic-tags-function-list list.
70 ;;
71 ;; This function is based on `speedbar-fetch-dynamic-texinfo'.
72 (defun speedbar-fetch-dynamic-html ( filename )
73   (set-buffer (find-file-noselect filename))
74   (if (not (or (eq major-mode 'html-helper-mode)
75                (eq major-mode 'html-mode)))
76       t
77     (condition-case nil
78         (save-excursion
79
80           ;; Set speedbar-tag-hierarchy-method to nil so that
81           ;; speedbar-create-tag-hierarchy won't reorder the list.
82           ;; Make it buffer local so that the global value is not touched.
83           (make-local-variable 'speedbar-tag-hierarchy-method)
84           (setq speedbar-tag-hierarchy-method nil)
85
86           (set (make-local-variable
87                 'speedbar-generic-list-group-expand-button-type)
88                'expandtag)
89           (set (make-local-variable
90                 'speedbar-generic-list-tag-button-type)
91                'statictag)
92
93           (let ((case-fold-search t)    ; HTML tags are case insensitive
94                 pos-beg title level alist beg)
95             (goto-char (point-min))
96             ;; regexp below will not work if `>' appears within an attribute
97             ;; value, e.g., <h1 foo="I'm a weird value >"> ... </h1>
98             (while (re-search-forward "<h\\([1-6]\\)\\b[^>]*>\\s-*" nil t)
99               (setq beg (match-end 0))
100               (goto-char (match-beginning 0))
101               (setq pos-beg (point-marker))
102               (setq level (1- (read (match-string 1))))
103               (re-search-forward (concat "\\s-*</h" (match-string 1) "\\b"))
104               (setq title (buffer-substring beg (match-beginning 0)))
105               (setq alist (cons (cons level (cons title pos-beg)) alist)))
106             (nreverse alist)))
107       (error t))))
108
109 (fset 'speedbar-format-html-list 'speedbar-format-texinfo-list)
110
111 (defun speedbar-insert-html-list (indent lst)
112   (speedbar-insert-generic-list indent (speedbar-format-html-list lst 0)
113                                 'speedbar-tag-expand
114                                 'speedbar-tag-find))
115
116 (provide 'sb-html)
117
118 ;;; sb-html.el ends here