]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/html.el
cedet/semantic/analyze.el, cedet/semantic/complete.el,
[gnu-emacs] / lisp / cedet / semantic / html.el
1 ;;; 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 (defvar semantic-html-super-regex
48 "<\\(h[1-9]\\|title\\|script\\|body\\|a +href\\)\\>"
49 "Regular expression used to find special sections in an HTML file.")
50
51 (defvar semantic-html-section-list
52 '(("title" 1)
53 ("script" 1)
54 ("body" 1)
55 ("a" 11)
56 ("h1" 2)
57 ("h2" 3)
58 ("h3" 4)
59 ("h4" 5)
60 ("h5" 6)
61 ("h6" 7)
62 ("h7" 8)
63 ("h8" 9)
64 ("h9" 10)
65 )
66 "Alist of sectioning commands and their relative level.")
67
68 (define-mode-local-override semantic-parse-region
69 html-mode (&rest ignore)
70 "Parse the current html buffer for semantic tags.
71 INGNORE any arguments. Always parse the whole buffer.
72 Each tag returned is of the form:
73 (\"NAME\" section (:members CHILDREN))
74 or
75 (\"NAME\" anchor)"
76 (mapcar 'semantic-html-expand-tag
77 (semantic-html-parse-headings)))
78
79 (define-mode-local-override semantic-parse-changes
80 html-mode ()
81 "We can't parse changes for HTML mode right now."
82 (semantic-parse-tree-set-needs-rebuild))
83
84 (defun semantic-html-expand-tag (tag)
85 "Expand the HTML tag TAG."
86 (let ((chil (semantic-html-components tag)))
87 (if chil
88 (semantic-tag-put-attribute
89 tag :members (mapcar 'semantic-html-expand-tag chil)))
90 (car (semantic--tag-expand tag))))
91
92 (defun semantic-html-components (tag)
93 "Return components belonging to TAG."
94 (semantic-tag-get-attribute tag :members))
95
96 (defun semantic-html-parse-headings ()
97 "Parse the current html buffer for all semantic tags."
98 (let ((pass1 nil))
99 ;; First search and snarf.
100 (save-excursion
101 (goto-char (point-min))
102
103 (let ((semantic--progress-reporter
104 (make-progress-reporter
105 (format "Parsing %s..."
106 (file-name-nondirectory buffer-file-name))
107 (point-min) (point-max))))
108 (while (re-search-forward semantic-html-super-regex nil t)
109 (setq pass1 (cons (match-beginning 0) pass1))
110 (progress-reporter-update semantic--progress-reporter (point)))
111 (progress-reporter-done semantic--progress-reporter)))
112
113 (setq pass1 (nreverse pass1))
114 ;; Now, make some tags while creating a set of children.
115 (car (semantic-html-recursive-combobulate-list pass1 0))
116 ))
117
118 (defun semantic-html-set-endpoint (metataglist pnt)
119 "Set the end point of the first section tag in METATAGLIST to PNT.
120 METATAGLIST is a list of tags in the intermediate tag format used by the
121 html parser. PNT is the new point to set."
122 (let ((metatag nil))
123 (while (and metataglist
124 (not (eq (semantic-tag-class (car metataglist)) 'section)))
125 (setq metataglist (cdr metataglist)))
126 (setq metatag (car metataglist))
127 (when metatag
128 (setcar (nthcdr (1- (length metatag)) metatag) pnt)
129 metatag)))
130
131 (defsubst semantic-html-new-section-tag (name members level start end)
132 "Create a semantic tag of class section.
133 NAME is the name of this section.
134 MEMBERS is a list of semantic tags representing the elements that make
135 up this section.
136 LEVEL is the levelling level.
137 START and END define the location of data described by the tag."
138 (let ((anchorp (eq level 11)))
139 (append (semantic-tag name
140 (cond (anchorp 'anchor)
141 (t 'section))
142 :members members)
143 (list start (if anchorp (point) end)) )))
144
145 (defun semantic-html-extract-section-name ()
146 "Extract a section name from the current buffer and point.
147 Assume the cursor is in the tag representing the section we
148 need the name from."
149 (save-excursion
150 ; Skip over the HTML tag.
151 (forward-sexp -1)
152 (forward-char -1)
153 (forward-sexp 1)
154 (skip-chars-forward "\n\t ")
155 (while (looking-at "<")
156 (forward-sexp 1)
157 (skip-chars-forward "\n\t ")
158 )
159 (let ((start (point))
160 (end nil))
161 (if (re-search-forward "</" nil t)
162 (progn
163 (goto-char (match-beginning 0))
164 (skip-chars-backward " \n\t")
165 (setq end (point))
166 (buffer-substring-no-properties start end))
167 ""))
168 ))
169
170 (defun semantic-html-recursive-combobulate-list (sectionlist level)
171 "Rearrange SECTIONLIST to be a hierarchical tag list starting at LEVEL.
172 Return the rearranged new list, with all remaining tags from
173 SECTIONLIST starting at ELT 2. Sections not are not dealt with as soon as a
174 tag with greater section value than LEVEL is found."
175 (let ((newl nil)
176 (oldl sectionlist)
177 (case-fold-search t)
178 tag
179 )
180 (save-excursion
181 (catch 'level-jump
182 (while oldl
183 (goto-char (car oldl))
184 (if (looking-at "<\\(\\w+\\)")
185 (let* ((word (match-string 1))
186 (levelmatch (assoc-ignore-case
187 word semantic-html-section-list))
188 text begin tmp
189 )
190 (when (not levelmatch)
191 (error "Tag %s matched in regexp but is not in list"
192 word))
193 ;; Set begin to the right location
194 (setq begin (point))
195 ;; Get out of here if there if we made it that far.
196 (if (and levelmatch (<= (car (cdr levelmatch)) level))
197 (progn
198 (when newl
199 (semantic-html-set-endpoint newl begin))
200 (throw 'level-jump t)))
201 ;; When there is a match, the descriptive text
202 ;; consists of the rest of the line.
203 (goto-char (match-end 1))
204 (skip-chars-forward " \t")
205 (setq text (semantic-html-extract-section-name))
206 ;; Next, recurse into the body to find the end.
207 (setq tmp (semantic-html-recursive-combobulate-list
208 (cdr oldl) (car (cdr levelmatch))))
209 ;; Build a tag
210 (setq tag (semantic-html-new-section-tag
211 text (car tmp) (car (cdr levelmatch)) begin (point-max)))
212 ;; Before appending the newtag, update the previous tag
213 ;; if it is a section tag.
214 (when newl
215 (semantic-html-set-endpoint newl begin))
216 ;; Append new tag to our master list.
217 (setq newl (cons tag newl))
218 ;; continue
219 (setq oldl (cdr tmp))
220 )
221 (error "Problem finding section in semantic/html parser"))
222 ;; (setq oldl (cdr oldl))
223 )))
224 ;; Return the list
225 (cons (nreverse newl) oldl)))
226
227 (define-mode-local-override semantic-sb-tag-children-to-expand
228 html-mode (tag)
229 "The children TAG expands to."
230 (semantic-html-components tag))
231
232 (defun semantic-default-html-setup ()
233 "Set up a buffer for parsing of HTML files."
234 ;; This will use our parser.
235 (setq semantic-parser-name "HTML"
236 semantic--parse-table t
237 imenu-create-index-function 'semantic-create-imenu-index
238 semantic-command-separation-character ">"
239 semantic-type-relation-separator-character '(":")
240 semantic-symbol->name-assoc-list '((section . "Section")
241
242 )
243 semantic-imenu-expandable-tag-classes '(section)
244 semantic-imenu-bucketize-file nil
245 semantic-imenu-bucketize-type-members nil
246 senator-step-at-start-end-tag-classes '(section)
247 semantic-stickyfunc-sticky-classes '(section)
248 )
249 (semantic-install-function-overrides
250 '((tag-components . semantic-html-components)
251 )
252 t)
253 )
254
255 (add-hook 'html-mode-hook 'semantic-default-html-setup)
256
257 (define-child-mode html-helper-mode html-mode
258 "`html-helper-mode' needs the same semantic support as `html-mode'.")
259
260 (provide 'semantic/html)
261
262 ;;; semantic-html.el ends here