comparison src/elisp/urweb-mode.el @ 363:9d81597e03e8

Reasonable indenting for pure XML
author Adam Chlipala <adamc@hcoop.net>
date Thu, 16 Oct 2008 12:50:59 -0400
parents 24a31b35e08f
children 7c58dc323683
comparison
equal deleted inserted replaced
362:24a31b35e08f 363:9d81597e03e8
158 158
159 ;;; Font-lock settings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 159 ;;; Font-lock settings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
160 160
161 ;; The font lock regular expressions. 161 ;; The font lock regular expressions.
162 162
163 (defun inXml () 163 (defun urweb-in-xml ()
164 (save-excursion 164 (save-excursion
165 (let ( 165 (let (
166 (depth 0) 166 (depth 0)
167 (finished nil) 167 (finished nil)
168 (answer nil) 168 (answer nil)
169 ) 169 )
170 (while (and (not finished) (re-search-backward "[<>{}]" nil t)) 170 (while (and (not finished) (re-search-backward "[<>{}]" nil t))
171 (cond 171 (cond
172 ((looking-at "{") 172 ((looking-at "{")
173 (if (> depth 0) 173 (if (> depth 0)
174 (setq depth (- depth 1)) 174 (decf depth)
175 (setq finished t))) 175 (setq finished t)))
176 ((looking-at "}") 176 ((looking-at "}")
177 (setq depth (+ depth 1))) 177 (incf depth))
178 ((save-excursion (backward-char 1) (or (looking-at "=>") (looking-at "->"))) 178 ((save-excursion (backward-char 1) (or (looking-at "=>") (looking-at "->")))
179 nil) 179 nil)
180 ((looking-at "<") 180 ((looking-at "<")
181 (setq finished t)) 181 (setq finished t))
182 ((looking-at ">") 182 ((looking-at ">")
201 (1 font-lock-tag-face) 201 (1 font-lock-tag-face)
202 (3 font-lock-tag-face)) 202 (3 font-lock-tag-face))
203 ("\\(</\\sw+>\\)" 203 ("\\(</\\sw+>\\)"
204 (1 font-lock-tag-face)) 204 (1 font-lock-tag-face))
205 ("\\([^<>{}]+\\)" 205 ("\\([^<>{}]+\\)"
206 (1 (if (inXml) 206 (1 (if (urweb-in-xml)
207 font-lock-string-face 207 font-lock-string-face
208 nil))) 208 nil)))
209 209
210 ("\\<\\(fun\\|and\\)\\s-+\\(\\sw+\\)\\s-+[^ \t\n=]" 210 ("\\<\\(fun\\|and\\)\\s-+\\(\\sw+\\)\\s-+[^ \t\n=]"
211 (1 font-lock-keyword-face) 211 (1 font-lock-keyword-face)
450 (setq depth -1))) 450 (setq depth -1)))
451 (if (= depth 0) 451 (if (= depth 0)
452 (1+ (current-column)) 452 (1+ (current-column))
453 nil)))) 453 nil))))
454 454
455 (defun urweb-new-tags ()
456 "Decide if the previous line of XML introduced unclosed tags"
457 (save-excursion
458 (let ((start-pos (point))
459 (depth 0)
460 (done nil))
461 (previous-line 1)
462 (beginning-of-line)
463 (while (and (not done) (search-forward "<" start-pos t))
464 (if (looking-at "/")
465 (if (search-forward ">" start-pos t)
466 (when (> depth 0) (decf depth))
467 (setq done t))
468 (if (search-forward ">" start-pos t)
469 (if (not (save-excursion (backward-char 2) (looking-at "/")))
470 (incf depth))
471 (setq done t))))
472 (and (not done) (> depth 0)))))
473
474 (defun urweb-tag-matching-indent ()
475 "Seek back to a matching opener tag and get its line's indent"
476 (save-excursion
477 (let ((depth 0)
478 (done nil))
479 (while (and (not done) (search-backward ">" nil t))
480 (if (save-excursion (backward-char 1) (looking-at "/"))
481 (when (not (search-backward "<" nil t))
482 (setq done t))
483 (if (search-backward "<" nil t)
484 (if (looking-at "</")
485 (incf depth)
486 (if (= depth 0)
487 (setq done t)
488 (decf depth)))
489 (setq done t))))
490 (current-indentation))))
491
455 (defun urweb-calculate-indentation () 492 (defun urweb-calculate-indentation ()
456 (save-excursion 493 (save-excursion
457 (beginning-of-line) (skip-chars-forward "\t ") 494 (beginning-of-line) (skip-chars-forward "\t ")
458 (urweb-with-ist 495 (urweb-with-ist
459 ;; Indentation for comments alone on a line, matches the 496 ;; Indentation for comments alone on a line, matches the
468 (regexp-quote comment-end))) 505 (regexp-quote comment-end)))
469 (current-indentation)) 506 (current-indentation))
470 507
471 ;; Continued comment. 508 ;; Continued comment.
472 (and (looking-at "\\*") (urweb-find-comment-indent)) 509 (and (looking-at "\\*") (urweb-find-comment-indent))
510
511 (and (urweb-in-xml)
512 (let ((prev-indent (save-excursion
513 (previous-line 1)
514 (re-search-backward "^[^\n]" nil t)
515 (current-indentation))))
516 (cond
517 ((looking-at "</")
518 (urweb-tag-matching-indent))
519 ((urweb-new-tags)
520 (+ prev-indent 2))
521 (t
522 prev-indent))))
473 523
474 ;; Continued string ? (Added 890113 lbn) 524 ;; Continued string ? (Added 890113 lbn)
475 (and (looking-at "\\\\") 525 (and (looking-at "\\\\")
476 (save-excursion 526 (save-excursion
477 (if (save-excursion (previous-line 1) 527 (if (save-excursion (previous-line 1)