(defvar two-mode-lmatch "<+")
(defvar two-mode-rmatch "+>")
(defvar default-mode (list 'sgml-mode "SGML"))
(defvar second-mode (list 'tcl-mode "TCL"))
(defun two-mode-mode-setup ()
(make-local-hook 'post-command-hook)
(add-hook 'post-command-hook 'two-mode-mode-update-mode nil t)
(make-local-variable 'minor-mode-alist)
(or (assq 'two-mode-mode minor-mode-alist)
(setq minor-mode-alist
(cons '(two-mode-mode " two-mode") minor-mode-alist))))
(defun two-mode-change-mode (to-mode)
(if (string= to-mode mode-name)
t
(progn
(if (string= to-mode (cadr second-mode))
(funcall (car second-mode))
(funcall (car default-mode)))
(two-mode-mode-setup)
(if (eq font-lock-mode t)
(font-lock-fontify-buffer)))))
(defun two-mode-mode-update-mode ()
(let ((lm -1)
(rm -1))
(save-excursion
(if (search-backward two-mode-lmatch nil t)
(setq lm (point))
(setq lm -1)))
(save-excursion
(if (search-backward two-mode-rmatch nil t)
(setq rm (point))
(setq rm -1)))
(if (and (= lm -1) (= rm -1))
(two-mode-change-mode (cadr default-mode))
(if (>= lm rm)
(two-mode-change-mode (cadr second-mode))
(two-mode-change-mode (cadr default-mode))))))
(defun two-mode-mode ()
(interactive)
(funcall (car default-mode))
(setq two-mode-mode t)
(two-mode-mode-setup))
(provide 'two-mode-mode)