;;
;; $Id: apex-dat.el,v 1.2 1996/03/21 19:46:07 qjb Exp $
;; $Source: /home/qjb/elisp/q/RCS/apex-dat.el,v $
;; $Author: qjb $
;;
;; Major mode for editing apex dat files
;;

(defvar apex-dat-mode-map ()
  "Keymap used for editing dat files.")
(if apex-dat-mode-map
    nil
  (setq apex-dat-mode-map (make-sparse-keymap))
;  (define-key apex-dat-mode-map "\C-c\C-e" 'apex-dat-equation-edit)
  )

(defvar apex-dat-mode-syntax-table nil
  "Syntax table in use in dat buffers.")

(if apex-dat-mode-syntax-table
    ()
  (setq apex-dat-mode-syntax-table (make-syntax-table))
  )

(defconst apex-dat-font-lock-keywords
  '(("^\\$BD\\$\\([.\n]+\n\\)\\$ED\\$$"
     (1 font-lock-string-face t)
     )
    ("\\(\\(\\$[BE]D\\$\\)\\|\\(\\~[A-Z]+\\)\\|\\(\\^[A-Z][0-9]\\^\\)\\)"
     (1 font-lock-keyword-face t)
     )
    ("\\(\\$\\$[^\\$]+\\$\\$\\)"
     (1 font-lock-keyword-face t)
     )
    ("\\(\\${\\)\\([^$]+\\)\\(\\$}\\)"
     (1 font-lock-keyword-face t)
     (2 font-lock-string-face t)
     (3 font-lock-keyword-face t)
     )
    )
  "Additional keywords for font-lock in Apex dat mode")

(defun apex-dat-fontify-displayed-equations (beg end loudly)
  (require 'font-lock)
  (if (not (fboundp 'font-lock-default-fontify-region))
      (error "%s%s"
	     "No function font-lock-default-fontify-region in "
	     "this version of font-lock")
    )
  (font-lock-default-fontify-region beg end loudly)

  (let ((modified (buffer-modified-p))
	(buffer-undo-list t) (inhibit-read-only t))
    (if loudly
	(message "Fontifying displayed equations in %s..." (buffer-name)))
    (save-restriction
      (narrow-to-region beg end)
      (save-excursion
	(goto-char (point-min))
	(while (re-search-forward "^\\$BD\\$$" (point-max) t)
	  (let ((beg-eq (point))
		(end-eq (if (re-search-forward "^\\$ED\\$$" (point-max) t)
			    (match-beginning 0)
			  (point-max)))
		)
	    (put-text-property beg-eq end-eq 'face font-lock-string-face)
	    (goto-char end-eq)
	    )
	  )
	)
      )
    (if loudly (message "done"))
    (set-buffer-modified-p modified)
    )
  )

(defun apex-dat-mark-enclosing-equation ()
  (let ((beg (point-min))
	(end (point-max))
	(old-point (point))
	)
    (if (re-search-backward "^\\$BD\\$$" (point-min) t)
	(progn
	  (setq beg (match-beginning 0))
	  (goto-char beg)
	  (if (re-search-forward "^\\$ED\\$$" (point-max) t)
	      (progn
		(setq end (match-beginning 0))
		(if (> old-point end)
		    (setq end old-point))
		)
	    )
	  )
      )
    (goto-char beg)
    (push-mark end)
    )
  )

(defun apex-dat-mode ()
  "Major mode for editing apex dat files."
  (interactive)
  (use-local-map apex-dat-mode-map)
  (set-syntax-table apex-dat-mode-syntax-table)
  (setq major-mode 'apex-dat-mode)
  (setq mode-name "Apex dat")
  (make-local-variable 'require-final-newline)
  (setq require-final-newline t)
  (auto-fill-mode 0)
  (make-local-variable 'font-lock-defaults)
  (setq font-lock-defaults
	'(apex-dat-font-lock-keywords
	  t nil nil nil
	  (font-lock-fontify-region-function 
	   . apex-dat-fontify-displayed-equations)
	  (font-lock-mark-block-function . apex-dat-mark-enclosing-equation)
	  )
	)
  (run-hooks 'apex-dat-mode-hook))
