]> code.delx.au - gnu-emacs/blob - lisp/cedet/semantic/html.el
Minor comment tweak.
[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
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
101 (let ((semantic--progress-reporter
102 (make-progress-reporter
103 (format "Parsing %s..."
104 (file-name-nondirectory buffer-file-name))
105 (point-min) (point-max))))
106 (while (re-search-forward semantic-html-super-regex nil t)
107 (setq pass1 (cons (match-beginning 0) pass1))
108 (progress-reporter-update semantic--progress-reporter (point)))
109 (progress-reporter-done semantic--progress-reporter)))
110
111 (setq pass1 (nreverse pass1))
112 ;; Now, make some tags while creating a set of children.
113 (car (semantic-html-recursive-combobulate-list pass1 0))
114 ))
115
116 (defun semantic-html-set-endpoint (metataglist pnt)
117 "Set the end point of the first section tag in METATAGLIST to PNT.
118 METATAGLIST is a list of tags in the intermediate tag format used by the
119 html parser. PNT is the new point to set."
120 (let ((metatag nil))
121 (while (and metataglist
122 (not (eq (semantic-tag-class (car metataglist)) 'section)))
123 (setq metataglist (cdr metataglist)))
124 (setq metatag (car metataglist))
125 (when metatag
126 (setcar (nthcdr (1- (length metatag)) metatag) pnt)
127 metatag)))
128
129 (defsubst semantic-html-new-section-tag (name members level start end)
130 "Create a semantic tag of class section.
131 NAME is the name of this section.
132 MEMBERS is a list of semantic tags representing the elements that make
133 up this section.
134 LEVEL is the levelling level.
135 START and END define the location of data described by the tag."
136 (let ((anchorp (eq level 11)))
137 (append (semantic-tag name
138 (cond (anchorp 'anchor)
139 (t 'section))
140 :members members)
141 (list start (if anchorp (point) end)) )))
142
143 (defun semantic-html-extract-section-name ()
144 "Extract a section name from the current buffer and point.
145 Assume the cursor is in the tag representing the section we
146 need the name from."
147 (save-excursion
148 ; Skip over the HTML tag.
149 (forward-sexp -1)
150 (forward-char -1)
151 (forward-sexp 1)
152 (skip-chars-forward "\n\t ")
153 (while (looking-at "<")
154 (forward-sexp 1)
155 (skip-chars-forward "\n\t ")
156 )
157 (let ((start (point))
158 (end nil))
159 (if (re-search-forward "</" nil t)
160 (progn
161 (goto-char (match-beginning 0))
162 (skip-chars-backward " \n\t")
163 (setq end (point))
164 (buffer-substring-no-properties start end))
165 ""))
166 ))
167
168 (defun semantic-html-recursive-combobulate-list (sectionlist level)
169 "Rearrange SECTIONLIST to be a hierarchical tag list starting at LEVEL.
170 Return the rearranged new list, with all remaining tags from
171 SECTIONLIST starting at ELT 2. Sections not are not dealt with as soon as a
172 tag with greater section value than LEVEL is found."
173 (let ((newl nil)
174 (oldl sectionlist)
175 (case-fold-search t)
176 tag
177 )
178 (save-excursion
179 (catch 'level-jump
180 (while oldl
181 (goto-char (car oldl))
182 (if (looking-at "<\\(\\w+\\)")
183 (let* ((word (match-string 1))
184 (levelmatch (assoc-string
185 word semantic-html-section-list t))
186 text begin tmp
187 )
188 (when (not levelmatch)
189 (error "Tag %s matched in regexp but is not in list"
190 word))
191 ;; Set begin to the right location
192 (setq begin (point))
193 ;; Get out of here if there if we made it that far.
194 (if (and levelmatch (<= (car (cdr levelmatch)) level))
195 (progn
196 (when newl
197 (semantic-html-set-endpoint newl begin))
198 (throw 'level-jump t)))
199 ;; When there is a match, the descriptive text
200 ;; consists of the rest of the line.
201 (goto-char (match-end 1))
202 (skip-chars-forward " \t")
203 (setq text (semantic-html-extract-section-name))
204 ;; Next, recurse into the body to find the end.
205 (setq tmp (semantic-html-recursive-combobulate-list
206 (cdr oldl) (car (cdr levelmatch))))
207 ;; Build a tag
208 (setq tag (semantic-html-new-section-tag
209 text (car tmp) (car (cdr levelmatch)) begin (point-max)))
210 ;; Before appending the newtag, update the previous tag
211 ;; if it is a section tag.
212 (when newl
213 (semantic-html-set-endpoint newl begin))
214 ;; Append new tag to our master list.
215 (setq newl (cons tag newl))
216 ;; continue
217 (setq oldl (cdr tmp))
218 )
219 (error "Problem finding section in semantic/html parser"))
220 ;; (setq oldl (cdr oldl))
221 )))
222 ;; Return the list
223 (cons (nreverse newl) oldl)))
224
225 (define-mode-local-override semantic-sb-tag-children-to-expand
226 html-mode (tag)
227 "The children TAG expands to."
228 (semantic-html-components tag))
229
230 ;;;###autoload
231 (defun semantic-default-html-setup ()
232 "Set up a buffer for parsing of HTML files."
233 ;; This will use our parser.
234 (setq semantic-parser-name "HTML"
235 semantic--parse-table t
236 imenu-create-index-function 'semantic-create-imenu-index
237 semantic-command-separation-character ">"
238 semantic-type-relation-separator-character '(":")
239 semantic-symbol->name-assoc-list '((section . "Section")
240
241 )
242 semantic-imenu-expandable-tag-classes '(section)
243 semantic-imenu-bucketize-file nil
244 semantic-imenu-bucketize-type-members nil
245 senator-step-at-start-end-tag-classes '(section)
246 semantic-stickyfunc-sticky-classes '(section)
247 )
248 (semantic-install-function-overrides
249 '((tag-components . semantic-html-components)
250 )
251 t)
252 )
253
254 (define-child-mode html-helper-mode html-mode
255 "`html-helper-mode' needs the same semantic support as `html-mode'.")
256
257 (provide 'semantic/html)
258
259 ;; Local variables:
260 ;; generated-autoload-file: "loaddefs.el"
261 ;; generated-autoload-feature: semantic/loaddefs
262 ;; generated-autoload-load-name: "semantic/html"
263 ;; End:
264
265 ;;; semantic/html.el ends here