1 ;;; sb-html.el --- provide hierarchical speedbar menu's for HTML files
3 ;; Copyright (c) 2001, 2002 Richard Y. Kim
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
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.
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.
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,
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.
32 ;; This code is a simple modification of sb-texinfo.el which comes
35 ;; Installation procedure:
37 ;; Install speedbar 0.12 or later.
38 ;; Add the following to your ~/.emacs file:
39 ;; (eval-after-load "speedbar" '(load-library "sb-html"))
43 ;; Perhaps <HEAD> and <BODY> should be treated as H0 tag?
47 ;;; 1.1 - modified regexp based on Eric's suggestion.
49 ;;; 1.0 - sent to Eric Ludlam.
54 (require 'sb-texinfo) ; for speedbar-format-texinfo-list
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))
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.
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)))
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)
86 (set (make-local-variable
87 'speedbar-generic-list-group-expand-button-type)
89 (set (make-local-variable
90 'speedbar-generic-list-tag-button-type)
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)))
109 (fset 'speedbar-format-html-list 'speedbar-format-texinfo-list)
111 (defun speedbar-insert-html-list (indent lst)
112 (speedbar-insert-generic-list indent (speedbar-format-html-list lst 0)
118 ;;; sb-html.el ends here