comparison src/elisp/urweb-mode.el @ 386:ef43ed6cd1de

Fix a nasty indent bug, found while working on listFun
author Adam Chlipala <adamc@hcoop.net>
date Sun, 19 Oct 2008 16:49:09 -0400
parents d3de57ce4bca
children bc5a56ed0cd6
comparison
equal deleted inserted replaced
385:1195f6e4d208 386:ef43ed6cd1de
623 (if (urweb-dangling-sym) 623 (if (urweb-dangling-sym)
624 (urweb-indent-default 'noindent) 624 (urweb-indent-default 'noindent)
625 (current-column)))) 625 (current-column))))
626 626
627 (and (or (looking-at "FROM") (looking-at urweb-sql-starters-re)) 627 (and (or (looking-at "FROM") (looking-at urweb-sql-starters-re))
628
628 (save-excursion 629 (save-excursion
629 (and (re-search-backward urweb-sql-starters-re nil t) 630 (and (re-search-backward urweb-sql-starters-re nil t)
630 (if (looking-at urweb-sql-main-starters-re) 631 (if (looking-at urweb-sql-main-starters-re)
631 (current-column) 632 (current-column)
632 (current-indentation))))) 633 (current-indentation)))))
756 (urweb-forward-sym) (urweb-backward-sexp nil) 757 (urweb-forward-sym) (urweb-backward-sexp nil)
757 (setq head-sym 758 (setq head-sym
758 (if (second delegate) 759 (if (second delegate)
759 (save-excursion (urweb-forward-sym)) 760 (save-excursion (urweb-forward-sym))
760 (urweb-backward-spaces) (urweb-backward-sym)))) 761 (urweb-backward-spaces) (urweb-backward-sym))))
761
762 (let ((idata (assoc head-sym urweb-indent-rule))) 762 (let ((idata (assoc head-sym urweb-indent-rule)))
763 (when idata 763 (when idata
764 ;;(if (or style (not delegate)) 764 ;;(if (or style (not delegate))
765 ;; normal indentation 765 ;; normal indentation
766 (let ((indent (urweb-get-indent (cdr idata) sym))) 766 (let ((indent (urweb-get-indent (cdr idata) sym)))
776 ;; (urweb-get-indent (cdr indent-data) 0) 776 ;; (urweb-get-indent (cdr indent-data) 0)
777 ;; (urweb-get-indent (cdr parent-indent) 0)))))))) 777 ;; (urweb-get-indent (cdr parent-indent) 0))))))))
778 ))))) 778 )))))
779 779
780 (defun urweb-indent-default (&optional noindent) 780 (defun urweb-indent-default (&optional noindent)
781 (let* ((sym-after (save-excursion (urweb-forward-sym))) 781 (condition-case nil
782 (_ (urweb-backward-spaces)) 782 (progn
783 (sym-before (urweb-backward-sym)) 783 (let* ((sym-after (save-excursion (urweb-forward-sym)))
784 (sym-indent (and sym-before (urweb-get-sym-indent sym-before))) 784 (_ (urweb-backward-spaces))
785 (indent-after (or (cdr (assoc sym-after urweb-symbol-indent)) 0))) 785 (sym-before (urweb-backward-sym))
786 (when (equal sym-before "end") 786 (sym-indent (and sym-before (urweb-get-sym-indent sym-before)))
787 ;; I don't understand what's really happening here, but when 787 (indent-after (or (cdr (assoc sym-after urweb-symbol-indent)) 0)))
788 ;; it's `end' clearly, we need to do something special. 788 (when (equal sym-before "end")
789 (forward-word 1) 789 ;; I don't understand what's really happening here, but when
790 (setq sym-before nil sym-indent nil)) 790 ;; it's `end' clearly, we need to do something special.
791 (cond 791 (forward-word 1)
792 (sym-indent 792 (setq sym-before nil sym-indent nil))
793 ;; the previous sym is an indentation introducer: follow the rule 793 (cond
794 (if noindent 794 (sym-indent
795 ;;(current-column) 795 ;; the previous sym is an indentation introducer: follow the rule
796 sym-indent 796 (if noindent
797 (+ sym-indent indent-after))) 797 ;;(current-column)
798 ;; If we're just after a hanging open paren. 798 sym-indent
799 ((and (eq (char-syntax (preceding-char)) ?\() 799 (+ sym-indent indent-after)))
800 (save-excursion (backward-char) (urweb-dangling-sym))) 800 ;; If we're just after a hanging open paren.
801 (backward-char) 801 ((and (eq (char-syntax (preceding-char)) ?\()
802 (urweb-indent-default)) 802 (save-excursion (backward-char) (urweb-dangling-sym)))
803 (t 803 (backward-char)
804 ;; default-default 804 (urweb-indent-default))
805 (let* ((prec-after (urweb-op-prec sym-after 'back)) 805 (t
806 (prec (or (urweb-op-prec sym-before 'back) prec-after 100))) 806 ;; default-default
807 ;; go back until you hit a symbol that has a lower prec than the 807 (let* ((prec-after (urweb-op-prec sym-after 'back))
808 ;; "current one", or until you backed over a sym that has the same prec 808 (prec (or (urweb-op-prec sym-before 'back) prec-after 100)))
809 ;; but is at the beginning of a line. 809 ;; go back until you hit a symbol that has a lower prec than the
810 (while (and (not (urweb-bolp)) 810 ;; "current one", or until you backed over a sym that has the same prec
811 (while (urweb-move-if (urweb-backward-sexp (1- prec)))) 811 ;; but is at the beginning of a line.
812 (not (urweb-bolp))) 812 (while (and (not (urweb-bolp))
813 (while (urweb-move-if (urweb-backward-sexp prec)))) 813 (while (urweb-move-if (urweb-backward-sexp (1- prec))))
814 (if noindent 814 (not (urweb-bolp)))
815 ;; the `noindent' case does back over an introductory symbol 815 (while (urweb-move-if (urweb-backward-sexp prec))))
816 ;; such as `fun', ... 816 (if noindent
817 (progn 817 ;; the `noindent' case does back over an introductory symbol
818 (urweb-move-if 818 ;; such as `fun', ...
819 (urweb-backward-spaces) 819 (progn
820 (member (urweb-backward-sym) urweb-starters-syms)) 820 (urweb-move-if
821 (current-column)) 821 (urweb-backward-spaces)
822 ;; Use `indent-after' for cases such as when , or ; should be 822 (member (urweb-backward-sym) urweb-starters-syms))
823 ;; outdented so that their following terms are aligned. 823 (current-column))
824 (+ (if (progn 824 ;; Use `indent-after' for cases such as when , or ; should be
825 (if (equal sym-after ";") 825 ;; outdented so that their following terms are aligned.
826 (urweb-move-if 826 (+ (if (progn
827 (urweb-backward-spaces) 827 (if (equal sym-after ";")
828 (member (urweb-backward-sym) urweb-starters-syms))) 828 (urweb-move-if
829 (and sym-after (not (looking-at sym-after)))) 829 (urweb-backward-spaces)
830 indent-after 0) 830 (member (urweb-backward-sym) urweb-starters-syms)))
831 (current-column)))))))) 831 (and sym-after (not (looking-at sym-after))))
832 indent-after 0)
833 (current-column))))))))
834 (error 0)))
832 835
833 836
834 ;; maybe `|' should be set to word-syntax in our temp syntax table ? 837 ;; maybe `|' should be set to word-syntax in our temp syntax table ?
835 (defun urweb-current-indentation () 838 (defun urweb-current-indentation ()
836 (save-excursion 839 (save-excursion