Remove non-free old and crusty clearcase pkg
[packages] / xemacs-packages / semantic / semantic-html.el
1 ;;; semantic-html.el --- Semantic details for html files
2
3 ;;; Copyright (C) 2004, 2005, 2007 Eric M. Ludlam
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; X-RCS: $Id: semantic-html.el,v 1.1 2007-11-26 15:10:38 michaels Exp $
7
8 ;; This file is not part of GNU Emacs.
9
10 ;; This is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This software is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26 ;;
27 ;; Parse HTML files and organize them in a nice way.
28 ;; Pay attention to anchors, including them in the tag list.
29 ;;
30 ;; Copied from the original semantic-texi.el.
31 ;;
32 ;; ToDo: Find <script> tags, and parse the contents in other
33 ;; parsers, such as javascript, php, shtml, or others.
34
35 (require 'semantic)
36 (require 'semantic-format)
37 (require 'sgml-mode) ;; html-mode is in here.
38
39 ;;; Code:
40 (eval-when-compile
41   (require 'semantic-ctxt)
42   (require 'semantic-imenu)
43   (require 'senator))
44
45 (defvar semantic-html-super-regex
46   "<\\(h[1-9]\\|title\\|script\\|body\\|a +href\\)\\>"
47   "Regular expression used to find special sections in an HTML file.")
48
49 (defvar semantic-html-section-list
50   '(("title" 1)
51     ("script" 1)
52     ("body" 1)
53     ("a" 11)
54     ("h1" 2)
55     ("h2" 3)
56     ("h3" 4)
57     ("h4" 5)
58     ("h5" 6)
59     ("h6" 7)
60     ("h7" 8)
61     ("h8" 9)
62     ("h9" 10)
63     )
64   "Alist of sectioning commands and their relative level.")
65
66 (define-mode-local-override semantic-parse-region
67   html-mode (&rest ignore)
68   "Parse the current html buffer for semantic tags.
69 INGNORE any arguments.  Always parse the whole buffer.
70 Each tag returned is of the form:
71  (\"NAME\" section (:members CHILDREN))
72 or
73  (\"NAME\" anchor)"
74   (mapcar 'semantic-html-expand-tag
75           (semantic-html-parse-headings)))
76
77 (define-mode-local-override semantic-parse-changes
78   html-mode ()
79   "We can't parse changes for HTML mode right now."
80   (semantic-parse-tree-set-needs-rebuild))
81
82 (defun semantic-html-expand-tag (tag)
83   "Expand the HTML tag TAG."
84   (let ((chil (semantic-html-components tag)))
85     (if chil
86         (semantic-tag-put-attribute
87          tag :members (mapcar 'semantic-html-expand-tag chil)))
88     (car (semantic--tag-expand tag))))
89
90 (defun semantic-html-components (tag)
91   "Return components belonging to TAG."
92   (semantic-tag-get-attribute tag :members))
93
94 (defun semantic-html-parse-headings ()
95   "Parse the current html buffer for all semantic tags."
96   (let ((pass1 nil))
97     ;; First search and snarf.
98     (save-excursion
99       (goto-char (point-min))
100       (working-status-forms (file-name-nondirectory buffer-file-name) "done"
101         (while (re-search-forward semantic-html-super-regex nil t)
102           (setq pass1 (cons (match-beginning 0) pass1))
103           (working-status)
104           )
105         (working-status t)))
106     (setq pass1 (nreverse pass1))
107     ;; Now, make some tags while creating a set of children.
108     (car (semantic-html-recursive-combobulate-list pass1 0))
109     ))
110
111 (defun semantic-html-set-endpoint (metataglist pnt)
112   "Set the end point of the first section tag in METATAGLIST to PNT.
113 METATAGLIST is a list of tags in the intermediate tag format used by the
114 html parser.  PNT is the new point to set."
115   (let ((metatag nil))
116     (while (and metataglist
117                 (not (eq (semantic-tag-class (car metataglist)) 'section)))
118       (setq metataglist (cdr metataglist)))
119     (setq metatag (car metataglist))
120     (when metatag
121       (setcar (nthcdr (1- (length metatag)) metatag) pnt)
122       metatag)))
123
124 (defsubst semantic-html-new-section-tag (name members start end)
125   "Create a semantic tag of class section.
126 NAME is the name of this section.
127 MEMBERS is a list of semantic tags representing the elements that make
128 up this section.
129 START and END define the location of data described by the tag."
130   (append (semantic-tag name 'section :members members)
131           (list start end)))
132
133 (defun semantic-html-extract-section-name ()
134   "Extract a section name from the current buffer and point.
135 Assume the cursor is in the tag representing the section we
136 need the name from."
137   (save-excursion
138     ; Skip over the HTML tag.
139     (forward-sexp -1)
140     (forward-char -1)
141     (forward-sexp 1)
142     (skip-chars-forward "\n\t ")
143     (while (looking-at "<")
144       (forward-sexp 1)
145       (skip-chars-forward "\n\t ")
146       )
147     (let ((start (point))
148           (end nil))
149       (if (re-search-forward "</" nil t)
150           (progn
151             (goto-char (match-beginning 0))
152             (skip-chars-backward " \n\t")
153             (setq end (point))
154             (buffer-substring-no-properties start end))
155         ""))
156     ))
157
158 (defun semantic-html-recursive-combobulate-list (sectionlist level)
159   "Rearrange SECTIONLIST to be a hierarchical tag list starting at LEVEL.
160 Return the rearranged new list, with all remaining tags from
161 SECTIONLIST starting at ELT 2.  Sections not are not dealt with as soon as a
162 tag with greater section value than LEVEL is found."
163   (let ((newl nil)
164         (oldl sectionlist)
165         (case-fold-search t)
166         tag
167         )
168     (save-excursion
169       (catch 'level-jump
170         (while oldl
171           (goto-char (car oldl))
172           (if (looking-at "<\\(\\w+\\)")
173               (let* ((word (match-string 1))
174                      (levelmatch (assoc-ignore-case
175                                   word semantic-html-section-list))
176                      text begin tmp
177                      )
178                 (when (not levelmatch)
179                   (error "Tag %s matched in regexp but is not in list"
180                          word))
181                 ;; Set begin to the right location
182                 (setq begin (point))
183                 ;; Get out of here if there if we made it that far.
184                 (if (and levelmatch (<= (car (cdr levelmatch)) level))
185                     (progn
186                       (when newl
187                         (semantic-html-set-endpoint newl begin))
188                       (throw 'level-jump t)))
189                 ;; When there is a match, the descriptive text
190                 ;; consists of the rest of the line.
191                 (goto-char (match-end 1))
192                 (skip-chars-forward " \t")
193                 (setq text (semantic-html-extract-section-name))
194                 ;; Next, recurse into the body to find the end.
195                 (setq tmp (semantic-html-recursive-combobulate-list
196                            (cdr oldl) (car (cdr levelmatch))))
197                 ;; Build a tag
198                 (setq tag (semantic-html-new-section-tag
199                            text (car tmp) begin (point)))
200                 ;; Before appending the newtag, update the previous tag
201                 ;; if it is a section tag.
202                 (when newl
203                   (semantic-html-set-endpoint newl begin))
204                 ;; Append new tag to our master list.
205                 (setq newl (cons tag newl))
206                 ;; continue
207                 (setq oldl (cdr tmp))
208                 )
209             (error "Problem finding section in semantic/html parser"))
210           ;; (setq oldl (cdr oldl))
211           )))
212     (cons (nreverse newl) oldl)))
213
214 (define-mode-local-override semantic-sb-tag-children-to-expand
215   html-mode (tag)
216   "The children TAG expands to."
217   (semantic-html-components tag))
218
219 ;;;###autoload
220 (defun semantic-default-html-setup ()
221   "Set up a buffer for parsing of HTML files."
222   ;; This will use our parser.
223   (setq semantic-parser-name "HTML"
224         semantic--parse-table t
225         imenu-create-index-function 'semantic-create-imenu-index
226         semantic-command-separation-character ">"
227         semantic-type-relation-separator-character '(":")
228         semantic-symbol->name-assoc-list '((section . "Section")
229                                            
230                                            )
231         semantic-imenu-expandable-tag-classes 'section
232         semantic-imenu-bucketize-file nil
233         semantic-imenu-bucketize-type-members nil
234         senator-step-at-start-end-tag-classes '(section)
235         semantic-stickyfunc-sticky-classes '(section)
236         )
237   (semantic-install-function-overrides
238    '((tag-components . semantic-html-components)
239      )
240    t)
241   )
242
243 ;;;###autoload
244 (add-hook 'html-mode-hook 'semantic-default-html-setup)
245
246 (define-child-mode html-helper-mode html-mode
247   "`html-helper-mode' needs the same semantic support as `html-mode'.")
248
249 (provide 'semantic-html)
250
251 ;;; semantic-html.el ends here