Lines Matching +defs:turn +defs:on +defs:casl +defs:indent
9 ;; casl-indent.el --- indentation module for CASL Mode
12 (defgroup casl-indent nil
13 "casl indentation."
14 :group 'casl
15 :prefix "casl-indent-")
17 (defcustom casl-indent-offset 5
18 "*Indentation of casl statements with respect to containing block."
20 :group 'casl-indent)
22 (defcustom casl-indent-rhs-align-column 0
23 "*Column on which to align right-hand sides (use 0 for ad-hoc alignment)."
25 :group 'casl-indent)
27 (defsubst casl-indent-get-beg-of-line (&optional arg)
32 (defsubst casl-indent-get-end-of-line (&optional arg)
37 (defun casl-indent-point-to-col (apoint)
43 (defconst casl-indent-start-keywords-re
50 (defconst casl-indent-dont-indent-re
54 (defconst casl-running-xemacs
60 (if casl-running-xemacs
64 (defun casl-indent-mark-active ()
69 (defun casl-indent-mark-active ()
75 (defun casl-indent-push-col (col &optional name)
78 is not pushed twice in a row. Uses free var `indent-info'."
80 (if (and indent-info (equal tmp (car indent-info)))
81 indent-info
82 (push tmp indent-info))))
84 (defun casl-indent-push-pos (pos &optional name)
87 (casl-indent-push-col (casl-indent-point-to-col pos) name))
89 (defun casl-indent-push-pos-offset (pos &optional offset)
92 `casl-indent-offset')."
93 (casl-indent-push-col (+ (casl-indent-point-to-col pos)
94 (or offset casl-indent-offset))))
99 (defun casl-indent-bolp ()
104 (defun casl-indent-empty-line-p ()
111 (defun casl-indent-back-to-indentation ()
116 (defun casl-indent-current-indentation ()
121 (defun casl-indent-backward-to-indentation (n)
126 (defun casl-indent-forward-line (&optional n)
131 (defun casl-indent-line-to (n)
132 "`indent-line-to' function."
133 (indent-line-to n)
136 (defun casl-indent-skip-blanks-and-newlines-forward (end)
141 (defun casl-indent-skip-blanks-and-newlines-backward (start)
149 (defun casl-indent-start-of-def ()
159 (casl-indent-forward-line -1)
160 (while (and (not (casl-indent-empty-line-p))
162 (= 0 (casl-indent-forward-line -1))))
164 (if (casl-indent-empty-line-p)
165 (casl-indent-forward-line 1))
167 (while (and (casl-indent-in-comment start-code (point))
168 (= 0 (casl-indent-forward-line 1))))
169 (casl-indent-skip-blanks-and-newlines-forward save-point)
173 (defun casl-indent-open-structure (start end)
182 (defun casl-indent-in-comment (start end)
190 ((looking-at "%{\\|%%\\|%\\[\\|%\(") ; on the first char of a comment ?
192 ((and (= (preceding-char) ?%) ; on the second char ?
204 (casl-indent-get-beg-of-line) t))))
209 (defvar casl-indent-off-side-keywords-re
212 (defun casl-indent-type-at-point ()
215 ((casl-indent-empty-line-p) 'empty)
216 ((casl-indent-in-comment 1 (point)) 'comment)
218 ;; 'logic' sowie '%xxxx' anfaengt, wird auch als ein indent angewendet.
230 (defvar casl-indent-current-line-first-ident ""
231 "Global variable that keeps track of the first ident of the line to indent.")
233 (defun casl-indent-contour-line (start end)
241 (casl-indent-skip-blanks-and-newlines-backward start)
243 (casl-indent-back-to-indentation)
244 (and (not (member (casl-indent-type-at-point)
249 (setq fl (casl-indent-forward-line -1))
253 (defun casl-indent-next-symbol (end)
261 (casl-indent-skip-blanks-and-newlines-forward end))
264 (defun casl-indent-separate-valdef (start end)
273 (setq type (casl-indent-type-at-point))
284 (casl-indent-next-symbol end))
286 (setq type (casl-indent-type-at-point))
290 (casl-indent-next-symbol end))))
296 (setq type (casl-indent-type-at-point))
300 (casl-indent-next-symbol end))))
311 (defun casl-indent-guard (start end end-visible indent-info)
314 (let* ((sep (casl-indent-separate-valdef start end))
320 (casl-indent-push-pos guard)
322 (casl-indent-push-pos-offset rhs-sign 2) ; behind "::="
324 (casl-indent-push-pos-offset valname)
326 indent-info)
328 (defun casl-indent-rhs (start end end-visible indent-info)
331 (let* ((sep (casl-indent-separate-valdef start end))
337 (casl-indent-push-pos rhs-sign)
339 (casl-indent-push-pos-offset guard)
341 (casl-indent-push-pos-offset valname)
342 (casl-indent-push-pos-offset valname))))))
343 indent-info)
345 (defun casl-indent-comment (start end indent-info)
348 -- comments are aligned on their start
349 {- comments are aligned on the first non-blank char following the open {
351 indent at the same indentation as the previous line."
353 (let ((comment-start (casl-indent-in-comment start end)))
357 (casl-indent-push-pos comment-start)
360 (casl-indent-skip-blanks-and-newlines-forward end)
361 (casl-indent-push-pos (point)))
362 ;; no previous comment indent with previous line
363 (casl-indent-push-col (casl-indent-current-indentation)))))
364 indent-info)
366 (defconst casl-indent-decision-table
380 (defun casl-indent-find-case (test)
382 (if (string-match casl-indent-decision-table test)
387 (error "casl-indent-find-case: impossible case: %s" test)
390 (defun casl-indent-empty (start end end-visible indent-info)
394 ((sep (casl-indent-separate-valdef start end))
413 (string-match casl-indent-start-keywords-re valname-string))
415 (casl-indent-push-pos valname)
416 (casl-indent-push-pos-offset valname)
419 (string-match casl-indent-dont-indent-re valname-string))
420 (casl-indent-push-pos valname)
422 (casl-indent-find-case test)
424 (1 (casl-indent-push-pos valname)
425 (casl-indent-push-pos-offset rhs-sign 2)
426 (casl-indent-push-pos guard))
428 (2 (casl-indent-push-pos valname)
430 (casl-indent-push-pos aft-valname)
431 (casl-indent-push-pos valname valname-string)))
433 (3 (casl-indent-push-pos valname)
435 (casl-indent-push-pos aft-valname))
436 (casl-indent-push-pos-offset rhs-sign 2)
439 (4 (casl-indent-push-pos valname)
440 (casl-indent-push-pos-offset rhs-sign 2))
442 (5 (casl-indent-push-pos guard))
444 (6 (casl-indent-push-pos rhs-sign))
446 (7 (casl-indent-push-pos-offset rhs-sign 2))
448 (8 (if guard (casl-indent-push-pos guard))
449 (casl-indent-push-pos valname))
451 (t (error "casl-indent-empty: %s impossible case" test )
453 indent-info)
455 (defun casl-indent-ident (start end end-visible indent-info)
459 ((sep (casl-indent-separate-valdef start end))
477 (string-match casl-indent-start-keywords-re valname-string))
479 (casl-indent-push-pos valname)
481 casl-indent-start-keywords-re
482 casl-indent-current-line-first-ident))
483 (casl-indent-push-pos-offset valname))
486 (string-match casl-indent-dont-indent-re valname-string))
487 (casl-indent-push-pos valname)
489 (casl-indent-find-case test)
491 (1 (casl-indent-push-pos valname))
493 (2 (casl-indent-push-pos valname)
495 (casl-indent-push-pos aft-valname)
496 (casl-indent-push-pos valname valname-string)))
498 (3 (casl-indent-push-pos-offset rhs-sign 2))
500 (4 (casl-indent-push-pos valname))
502 (5 ;; (casl-indent-push-pos 0)
503 (casl-indent-push-pos guard))
505 (6 (casl-indent-push-pos rhs-sign))
508 (casl-indent-push-pos-offset rhs-sign 2))
510 (8 (casl-indent-push-pos valname))
512 (t (error "casl-indent-ident: %s impossible case" test )))))))
513 indent-info)
515 (defun casl-indent-other (start end end-visible indent-info)
520 ((sep (casl-indent-separate-valdef start end))
530 ; (string-match "where[ \t]*" casl-indent-current-line-first-ident))
540 (string-match casl-indent-start-keywords-re valname-string))
541 (casl-indent-push-pos-offset valname)
543 (casl-indent-find-case test)
545 (1 (casl-indent-push-pos valname))
547 (2 (casl-indent-push-pos-offset valname))
549 (3 (casl-indent-push-pos valname))
551 (4 (casl-indent-push-pos valname))
553 (5 (casl-indent-push-pos guard))
555 (6 (casl-indent-push-pos-offset rhs-sign 2))
559 (8 (casl-indent-push-pos valname))
561 (t (error "casl-indent-other: %s impossible case" test ))))))
562 indent-info)
564 (defun casl-indent-valdef-indentation (start end end-visible curr-line-type
565 indent-info)
569 ('empty (casl-indent-empty start end end-visible indent-info))
570 ('ident (casl-indent-ident start end end-visible indent-info))
571 ('guard (casl-indent-guard start end end-visible indent-info))
572 ('rhs (casl-indent-rhs start end end-visible indent-info))
573 ('comment (error "Comment indent should never happen"))
574 ('other (casl-indent-other start end end-visible indent-info)))
575 indent-info))
577 (defun casl-indent-line-indentation (line-start line-end end-visible
578 curr-line-type indent-info)
584 (setq start-comment (casl-indent-in-comment line-start line-end))
587 ;; loop on all parts separated by off-side-keywords
588 (while (re-search-forward casl-indent-off-side-keywords-re line-end t)
591 (if (casl-indent-in-comment line-start (point)) ; keyword in a {- comment
593 (setq indent-info
594 (casl-indent-valdef-indentation
597 end-visible curr-line-type indent-info))
603 (setq indent-info ; do not try to find indentation points
604 (casl-indent-valdef-indentation line-start beg-match
606 curr-line-type indent-info))
607 ;; but keep the start of the line if keyword alone on the line
609 (casl-indent-push-pos beg-match)))
612 (setq indent-info
613 (casl-indent-valdef-indentation line-start line-end end-visible
614 curr-line-type indent-info))))
615 indent-info)
617 (defun casl-indent-indentation-info ()
619 are then used by `casl-indent-cycle'."
620 (let ((start (casl-indent-start-of-def))
621 (end (progn (casl-indent-back-to-indentation) (point)))
622 indent-info open follow contour-line pt)
624 (if (setq open (casl-indent-open-structure start end))
627 (casl-indent-push-pos open); align a ) or punct with (
631 (casl-indent-skip-blanks-and-newlines-forward end)
634 (casl-indent-push-pos-offset open 1)
635 (casl-indent-push-pos follow))))
637 (if (casl-indent-in-comment start end)
640 (setq indent-info ; computing comment indentation
641 (casl-indent-comment (casl-indent-get-beg-of-line)
642 (point) indent-info)))
644 (setq contour-line (casl-indent-contour-line start end))
646 (let* ((curr-line-type (casl-indent-type-at-point))
652 (casl-indent-separate-valdef (point)
653 (casl-indent-get-end-of-line)))
657 (setq casl-indent-current-line-first-ident
659 (setq casl-indent-current-line-first-ident ""))))
663 (setq line-end (casl-indent-get-end-of-line))
668 (casl-indent-point-to-col (car contour-line)))
671 (if (and (not (casl-indent-open-structure start line-start))
672 (not (casl-indent-in-comment start line-start)))
673 (setq indent-info
674 (casl-indent-line-indentation line-start line-end
676 indent-info)))
679 (casl-indent-push-pos start)))
680 indent-info
683 (defun casl-indent-event-type (event)
692 (defun casl-indent-cycle ()
698 (let (il indent-list com indent-info cdrii marker)
699 (if (> (current-column) (casl-indent-current-indentation))
701 (setq il (setq indent-list (casl-indent-indentation-info)))
702 ;;(message "Indent-list:%s" indent-list) (read-event) ; uncomment for debug!!
703 (setq indent-info (car il))
704 (casl-indent-line-to (car indent-info)) ; insert indentation
705 (if (= (length indent-list) 1)
707 (message (format "Indent cycle (%d)..." (length indent-list)))
708 (while (equal (casl-indent-event-type (setq com (read-event))) 'tab)
710 (or il (setq il indent-list)) ; if at the end of insertion, restart
712 (setq indent-info (car il))
713 (casl-indent-line-to (car indent-info)) ; insert indentation
715 (if (not (equal (casl-indent-event-type com) 'return))
725 (defun casl-indent-shift-columns (dest-column region-stack)
750 ;;; turn-on-casl-indent to be used in conjunction with
751 ;;; the casl-mode of Graeme E Moss and Tommy Thorn
754 (defun turn-on-casl-indent ()
755 "Turn on ``intelligent'' casl indentation mode."
757 (make-local-variable 'indent-line-function)
758 (setq indent-line-function 'casl-indent-cycle)
759 (local-set-key "\t" 'casl-indent-cycle)
760 (setq casl-indent-mode t)
761 (run-hooks 'casl-indent-hook)
764 (defun turn-off-casl-indent ()
765 "Turn off ``intelligent'' casl indentation mode that deals with
766 the layout rule of casl."
768 (setq indent-line-function 'indent-to-left-margin)
770 (setq casl-indent-mode nil)
773 (defvar casl-indent-mode nil
774 "Indicates if the semi-intelligent casl indentation mode is in effect
776 (make-variable-buffer-local 'casl-indent-mode)
778 ;; Put this minor mode on the global minor-mode-alist.
779 (or (assq 'casl-indent-mode (default-value 'minor-mode-alist))
782 '((casl-indent-mode " Ind")))))
784 (defun casl-indent-mode (&optional arg)
785 "``intelligent'' casl indentation mode that deals with
786 the layout rule of casl.
787 Invokes `casl-indent-hook' if not nil."
789 (setq casl-indent-mode
790 (if (null arg) (not casl-indent-mode)
792 (if casl-indent-mode
793 (turn-on-casl-indent)
794 (turn-off-casl-indent)))
796 (provide 'casl-indent)