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