1 ;;; hm--html-indentation.el
3 ;;; Copyright (C) 1997 Heiko Muenkel
4 ;;; email: muenkel@tnt.uni-hannover.de
6 ;;; This program is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 1, or (at your option)
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with this program; if not, write to the Free Software
18 ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 ;;; Defines functions for the indentation.
27 ;;; Put this file in one of your load path directories.
30 (defun hm--html-point-between-strings-p (string-1
33 "Returns non nil, if the current point is between STRING-1 and STRING-2."
34 (when (and (re-search-backward (concat "\\("
35 (regexp-quote string-1)
37 (regexp-quote string-2)
44 (defun hm--html-in-comment-p ()
45 "Checks if the current point is in a comment block.
46 If this is the case, then the start point of the comment is returned.
47 Otherwise nil is returned."
49 (hm--html-point-between-strings-p comment-start comment-end)))
51 (defun hm--html-previous-line-start ()
52 "Returns the start of the previous non blank line."
55 (skip-chars-backward " \t\n")
59 (defun hm--html-look-at-comment-end-p ()
60 "T, if the current line starts with the comment end."
61 (looking-at (regexp-quote comment-end)))
63 (defun hm--html-column-of-previous-regexp (regexp)
64 "Returns the column of the start of the previous REGEXP.
65 It searches backward until the REGEXP is found. If no
66 REGEXP is found, then it returns 0."
68 (if (re-search-backward regexp nil t)
72 (defun hm--html-look-at-end-tag-p ()
73 "Returns the end tag name if the point is at the start of an end tag.
74 nil is returned otherwise."
75 (when (looking-at "\\(<[ \t\n]*/[ \t\n]*\\)\\([^ \t\n>]+\\)")
79 (defun hm--html-previous-line-indentation ()
80 "Returns the indentation of the previous non blank line."
83 (skip-chars-backward " \t\n")
87 (defun hm--html-in-tag-p ()
88 "Checks if the current point is in a tag.
89 If this is the case, then the start point of the tag is returned.
90 Otherwise nil is returned."
92 (let ((start (re-search-backward "\\(<\\)\\|\\(>\\)" nil t)))
93 (when (match-string 1)
96 (defun hm--html-return-beginning-of-line ()
97 "Returns the beginning of the current line."
102 (defun hm--html-return-end-of-line ()
103 "Returns the end of the current line."
108 (defun hm--html-paramter-column-in-line-after-point (point)
109 "Returns the column where the second non blank text after POINT starts.
110 This point must be in the line with POINT otherwise it returns nil."
113 (when (re-search-forward "<[ \t]*[^ \t]+[ \t]"
114 (hm--html-return-end-of-line)
116 (when (looking-at "[^\n]")
119 (defun hm--html-column-of-point (point)
120 "Returns the column of the POINT."
125 (defun hm--html-search-previous-tag-in-current-line ()
126 "Searches tags from the `(point)' to the beginning of the line.
127 It returns nil, if there is no tag and the tag name, if there is
128 a tag. The tag name contains a leading /, if it is an end tag."
129 (when (re-search-backward ">" (hm--html-return-beginning-of-line) t)
130 (when (re-search-backward
131 "\\(<[ \t\n]*\\(/?\\)\\([ \t\n]*[^> \t\n]+\\)[^>]*\\)"
134 (concat (match-string 2) (match-string 3)))))
136 (defun hm--html-search-start-tag (tag-name until)
137 "Searches start tag backwards from the current point until the point UNTIL.
138 The name of the tag is TAG-NAME. After this function the point is at UNTIL
139 (then it returns nil) or at the start of the tag, then it returns t."
140 (if (re-search-backward (concat "\\(<[ \t\n]*\\)\\(/?\\)\\("
142 "\\)\\([^>]*>\\)") until t)
143 (if (string= "/" (match-string 2))
145 (hm--html-search-start-tag tag-name until)
146 (hm--html-search-start-tag tag-name until))
151 (defun hm--html-is-one-element-tag-p (tag-name)
152 "Returns t, if the tag with the tag-name is a one element tag."
153 (assoc ':hm--html-one-element-tag
154 (cdr (assoc* (downcase tag-name)
155 hm--html-tag-name-alist
158 (defun hm--html-calculate-indent-according-to-previous-tags ()
159 "Calculate the indent according to the previous tags in this line.
160 If no tags are found, then nil is returned."
162 (let ((tag (hm--html-search-previous-tag-in-current-line)))
163 (cond ((not tag) nil)
165 ((eq ?/ (elt tag 0)) ; end tag found
166 (if (hm--html-search-start-tag
169 (or (hm--html-calculate-indent-according-to-previous-tags)
171 (backward-to-indentation 0)
173 0)) ; it may be that the current indentation is better here
175 ((hm--html-is-one-element-tag-p tag) ; one element tag
176 (or (hm--html-calculate-indent-according-to-previous-tags)
178 (backward-to-indentation 0)
182 (+ (current-column) hm--html-inter-tag-indent))))))
185 (defun hm--html-calculate-indent ()
186 "Calculate the indentation of the current line."
191 (back-to-indentation)
192 (cond ((eq (count-lines (point-min) (point)) 0) 0) ; Filestart
194 ((setq match-point (hm--html-in-comment-p)) ; in a comment
195 (if (>= match-point (hm--html-previous-line-start)) ; 1. line
196 (if (hm--html-look-at-comment-end-p)
197 (hm--html-column-of-previous-regexp
198 (regexp-quote comment-start))
199 (+ (hm--html-column-of-previous-regexp
200 (regexp-quote comment-start))
201 hm--html-comment-indent))
202 (if (hm--html-look-at-comment-end-p)
203 (- (hm--html-previous-line-indentation)
204 hm--html-comment-indent)
205 (hm--html-previous-line-indentation))))
207 ((setq tag (hm--html-look-at-end-tag-p)) ; look at end tag
208 (hm--html-search-start-tag tag (point-min))
212 (hm--html-column-of-previous-regexp "<"))
214 ((setq match-point (hm--html-in-tag-p))
215 (if (>= match-point (hm--html-previous-line-start)) ; 1. line
216 (or (hm--html-paramter-column-in-line-after-point match-point)
217 (+ (hm--html-column-of-point match-point)
218 hm--html-intra-tag-indent))
219 (hm--html-previous-line-indentation)))
221 (t (or (save-excursion ; check previous line
222 (skip-chars-backward " \t\n")
223 (hm--html-calculate-indent-according-to-previous-tags))
224 (hm--html-previous-line-indentation)))
228 ;;; Indentation commands
230 (defun hm--html-indent-line ()
231 "Indent the current line line."
233 (unless hm--html-disable-indentation
234 (let ((pos (- (point-max) (point))))
235 (indent-line-to (max 0 (hm--html-calculate-indent)))
236 (when (> (- (point-max) pos) (point))
237 (goto-char (- (point-max) pos))))))
239 ;(defun hm--html-indent-region (begin end)
240 ; "Indents the region between BEGIN and END according to the major mode."
241 ; (interactive "d\nm")
242 ; (when (< end begin)
249 ; (while (and (<= (point) end)
250 ; (not (eq (point) old-point)))
251 ; (setq old-point (point))
252 ; (indent-according-to-mode)
256 (defun hm--html-indent-region (begin end)
257 "Indents the region between BEGIN and END according to the major mode."
263 (let ((lines (count-lines begin end)))
267 do (indent-according-to-mode)
271 (provide 'hm--html-indentation)