Initial Commit
[packages] / xemacs-packages / w3 / contrib / w3-imenu.el
1 ;;;w3-imenu.el,v 1.1.1.1 1998/12/01 22:11:57 wmperry Exp
2 ;;;Description: Build up navigation index for W3 documents:
3 (require 'cl)
4 (require 'imenu)
5 ;;{{{ Tags to index
6
7 (defvar w3-imenu-index-html-elements
8   (list 'h1 'h2 'h3)
9   "*List of HTML tags whose buffer positions in the W3 presentation
10 should appear in the index")
11
12 (make-variable-buffer-local 'w3-imenu-index-html-elements)
13 ;;}}}
14 ;;{{{ helpers 
15
16 (defsubst w3-html-stack () (get-text-property (point) 'html-stack))
17
18 (defsubst w3-html-stack-top-element (stack)
19   (first (first stack )))
20
21 ;;}}}
22 ;;{{{  Move to an element position
23
24 (defun w3-imenu-goto-next-element (element)
25   "Move forward in the W3 buffer to point where
26 the next occurrence of element element starts.
27 Return nil and leave point at end of buffer  if not found."
28   (let ((position nil)
29         (found nil)
30         (stack (w3-html-stack)))
31     (while  (and (not (eobp))
32                  (not found))
33       (setq found
34             (or (eq (w3-html-stack-top-element stack)  element)
35                 (and (eq (w3-html-stack-top-element stack) 'a)
36                      (eq (first (second stack)) element))))
37       (setq position  (point))
38       (goto-char
39        (next-single-property-change  (point)  'html-stack
40                                      (current-buffer) (point-max)))
41       (setq stack (w3-html-stack)))
42     (if found position nil)))
43
44 ;;}}}
45 ;;{{{  create an index 
46
47 (defun w3-imenu-create-index ()
48   "Returns an alist suitable for use by imenu"
49   (declare (special w3-imenu-index-html-elements))
50   (let ((index nil)
51         (position nil)
52         (marker nil))
53     (save-excursion
54       (loop for element in w3-imenu-index-html-elements
55             do 
56             (goto-char (point-min))
57             (while (setq position
58                          (w3-imenu-goto-next-element element))
59               (setq marker (make-marker))
60               (set-marker marker position)
61             (push
62              (cons
63               (buffer-substring-no-properties position (point))
64               marker)
65              index))))
66     index))
67
68 ;;}}}
69 ;;{{{ Tell W3 to start using it:
70 (declaim (special imenu-create-index-function))
71 (add-hook
72  'w3-mode-hook
73  (function
74   (lambda ()
75     (setq imenu-create-index-function 'w3-imenu-create-index)
76     (define-key w3-mode-map "j" 'imenu))))
77
78 ;;}}}
79 (provide 'w3-imenu)
80 ;;{{{ end of file 
81
82 ;;; local variables:
83 ;;; folded-file: t
84 ;;; end: 
85
86 ;;}}}