Mercurial > urweb
comparison src/elisp/urweb-mode.el @ 369:226c977faa9c
Crud indented properly, except for <xml>...</xml> outside parens and sig/struct
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 16 Oct 2008 14:40:28 -0400 |
parents | b6be16792584 |
children | 4f75cc2e1373 |
comparison
equal
deleted
inserted
replaced
368:b6be16792584 | 369:226c977faa9c |
---|---|
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-begun-xml () | 455 (defun urweb-empty-line () |
456 "Check if this is the first new line in a new <xml>...</xml> section" | 456 (save-excursion |
457 (save-excursion | 457 (beginning-of-line) |
458 (let ((start-pos (point))) | 458 (let ((start-pos (point))) |
459 (previous-line 1) | 459 (end-of-line) |
460 (search-forward "<xml>" start-pos t)))) | 460 (not (re-search-backward "[^\n \t]" start-pos t))))) |
461 | 461 |
462 (defun urweb-new-tags () | 462 (defun urweb-seek-back () |
463 "Decide if the previous line of XML introduced unclosed tags" | 463 (while (urweb-empty-line) (previous-line 1))) |
464 (save-excursion | 464 |
465 (let ((start-pos (point)) | 465 (defun urweb-skip-matching-braces () |
466 (depth 0) | |
467 (done nil)) | |
468 (previous-line 1) | |
469 (beginning-of-line) | |
470 (while (and (not done) (search-forward "<" start-pos t)) | |
471 (if (looking-at "/") | |
472 (if (search-forward ">" start-pos t) | |
473 (when (> depth 0) (decf depth)) | |
474 (setq done t)) | |
475 (if (search-forward ">" start-pos t) | |
476 (if (not (save-excursion (backward-char 2) (looking-at "/"))) | |
477 (incf depth)) | |
478 (setq done t)))) | |
479 (and (not done) (> depth 0))))) | |
480 | |
481 (defun skip-matching-braces () | |
482 "Skip backwards past matching brace pairs, to calculate XML indentation after quoted Ur code" | 466 "Skip backwards past matching brace pairs, to calculate XML indentation after quoted Ur code" |
483 (beginning-of-line) | 467 (beginning-of-line) |
484 (let ((start-pos (point)) | 468 (let ((start-pos (point)) |
485 (depth 0)) | 469 (depth 0)) |
486 (end-of-line) | 470 (end-of-line) |
495 ((looking-at "}") | 479 ((looking-at "}") |
496 (incf depth)) | 480 (incf depth)) |
497 ((looking-at "{") | 481 ((looking-at "{") |
498 (decf depth))))))) | 482 (decf depth))))))) |
499 | 483 |
484 (defun urweb-new-tags () | |
485 "Decide if the previous line of XML introduced unclosed tags" | |
486 (save-excursion | |
487 (let ((start-pos (point)) | |
488 (depth 0) | |
489 (done nil)) | |
490 (previous-line 1) | |
491 (urweb-seek-back) | |
492 (urweb-skip-matching-braces) | |
493 (urweb-seek-back) | |
494 (beginning-of-line) | |
495 (while (and (not done) (search-forward "<" start-pos t)) | |
496 (if (looking-at "/") | |
497 (if (search-forward ">" start-pos t) | |
498 (when (> depth 0) (decf depth)) | |
499 (setq done t)) | |
500 (if (search-forward ">" start-pos t) | |
501 (if (not (save-excursion (backward-char 2) (looking-at "/"))) | |
502 (incf depth)) | |
503 (setq done t)))) | |
504 (and (not done) (> depth 0))))) | |
505 | |
500 (defun urweb-tag-matching-indent () | 506 (defun urweb-tag-matching-indent () |
501 "Seek back to a matching opener tag and get its line's indent" | 507 "Seek back to a matching opener tag and get its line's indent" |
502 (save-excursion | 508 (save-excursion |
509 (end-of-line) | |
510 (search-backward "</" nil t) | |
503 (urweb-tag-matcher) | 511 (urweb-tag-matcher) |
504 (if (looking-at "<xml") | 512 (beginning-of-line) |
505 (+ (current-indentation) 2) | 513 (current-indentation))) |
506 (current-indentation)))) | |
507 | 514 |
508 (defun urweb-calculate-indentation () | 515 (defun urweb-calculate-indentation () |
509 (save-excursion | 516 (save-excursion |
510 (beginning-of-line) (skip-chars-forward "\t ") | 517 (beginning-of-line) (skip-chars-forward "\t ") |
511 (urweb-with-ist | 518 (urweb-with-ist |
525 (and (looking-at "\\*") (urweb-find-comment-indent)) | 532 (and (looking-at "\\*") (urweb-find-comment-indent)) |
526 | 533 |
527 (and (urweb-in-xml) | 534 (and (urweb-in-xml) |
528 (let ((prev-indent (save-excursion | 535 (let ((prev-indent (save-excursion |
529 (previous-line 1) | 536 (previous-line 1) |
530 (skip-matching-braces) | 537 (urweb-seek-back) |
531 (re-search-backward "^[^\n]" nil t) | 538 (urweb-skip-matching-braces) |
539 (urweb-seek-back) | |
532 (current-indentation)))) | 540 (current-indentation)))) |
533 (cond | 541 (cond |
534 ((looking-at "</") | 542 ((looking-at "</") |
535 (urweb-tag-matching-indent)) | 543 (urweb-tag-matching-indent)) |
536 ((urweb-begun-xml) | |
537 (+ prev-indent 4)) | |
538 ((urweb-new-tags) | 544 ((urweb-new-tags) |
539 (+ prev-indent 2)) | 545 (+ prev-indent 2)) |
540 (t | 546 (t |
541 prev-indent)))) | 547 prev-indent)))) |
542 | 548 |