;; DELAF.el -- mode for editing DELAF-dictionary files ;;

;; Copyright (C) 2004 Sebastian Nagel ;;

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; 
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

(provide 'DELAF)

(defvar DELAF-mode-hook nil)

(defconst DELAF-version "0.08" "Version of DELAF-mode")

(setq auto-mode-alist ; files for which DELAF mode will be invoked.
      (append '(("\\.d\\(?:lf\\|elaf?\\|ic\\)$" . DELAF-mode)
                ) auto-mode-alist))

(defun DELAF-mode ()
  "Major mode for editing dictionaries in DELAF format.
An entry in a DELAF dictionary has the form:
  <wordform>,<lemma>.<part-of-speech>[+<classificat.feature>]*[:<inflect.feature>]*
e.g.
  men,man.N+Hum:pm
"
  (interactive)
  (kill-all-local-variables)
  (setq major-mode 'DELAF-mode)
  (setq mode-name "DELAF")
  (setq parse-sexp-ignore-comments t)
  (set-syntax-table DELAF-mode-syntax-table)
  (make-local-variable 'comment-start)  
  (setq comment-start "//")
  (make-local-variable 'comment-end)
  (setq comment-end "")
  (make-local-variable 'comment-start-skip)
  (setq comment-start-skip "^// *\\|\\(\\(?:[^/\n\\\\]+\\|\\\\[^\n]\\)*\\) *")
  (make-local-variable 'completion-ignore-case)
  (setq completion-ignore-case nil)
  (DELAF-font)
  (run-hooks 'DELAF-mode-hook)
)
(defalias 'delaf-mode 'DELAF-mode) 

(defconst DELAF-font-lock-keywords
  (list
   (list "^//[^\n]*$"                  ;; lines commented out start with '//'
	 0 'font-lock-comment-face nil t)
   (list "[,.+:|;]"                    ;; meta signs (DELA: comma, dot, plus sign, colon,
                                       ;;             FLEX-code: semi-colon, bar)
 	 0 'font-lock-doc-face nil t)
   (list "^[^,\n]+$"                   ;; line without komma: invalid line
 	 0 'font-lock-warning-face nil t)
   (list
    (concat
     ;; dictionary entry
     "^"
     "\\(\\(?:[^,/\\\\\n]\\|\\\\[^\n]\\)+\\)"   ; 1: word form
     ","                                        ;    meta (comma)
     "\\(\\(?:[^\\./\\\\\n]\\|\\\\[^\n]\\)*\\)" ; 2: lemma
     "\\."                                      ;    meta (dot)
     "\\(\\(?:[^+:/{\\\\\n]\\|\\\\[^\n]\\)+\\)" ; 5: part-of-speech
     "\\(\\(?:\\+\\(?:[^+:/{\\\\\n]\\|\\\\[^\n]\\)+\\)*\\)"  ; 6: semantic info
     "\\(\\(?::\\(?:[^:/{\\\\\n]\\|\\\\[^\n]\\)+\\)*\\)"     ; 7: inflectional info
     "\\(?:\\({\\)\\([^}\n]+\\)\\(}\\)\\)?"     ; 8,9,10: non-DELAF extension: morphology
     "\\(/[^\n]*\\)?$" ; 11: end-of-line or comment
     )
    (list 1 'font-lock-variable-name-face t nil)
    (list 2 'font-lock-type-face t t)
    (list 3 'font-lock-function-name-face nil nil)
    (list 4 'font-lock-keyword-face 'append t)
    (list 5 'font-lock-constant-face 'append t)
    (list 6 'font-lock-doc-face nil t)
    (list 7 'font-lock-builtin-face 'append t)
    (list 8 'font-lock-doc-face nil t)
    (list 9 'font-lock-comment-face nil t))
   (list "^.+$"                     ;; error: invalid line
	 0 'font-lock-warning-face 'keep t))
  "Expressions to highlight in DELAF mode.")


(defun DELAF-font ()
  "Set font-lock variables for DELAF mode."
  (make-local-variable 'font-lock-keywords-case-fold-search) ; For GNU Emacs.
  (setq font-lock-keywords-case-fold-search nil)
  (put major-mode 'font-lock-keywords-case-fold-search nil) ; For XEmacs.
  (make-local-variable 'font-lock-defaults)
  (setq font-lock-defaults '(DELAF-font-lock-keywords nil nil nil beginning-of-line)))



(defvar DELAF-mode-syntax-table
  (let ((DELAF-mode-syntax-table (make-syntax-table)))
    ; comments are start with /
    (modify-syntax-entry ?/ "<" DELAF-mode-syntax-table)
    ; until end-of-line
    (modify-syntax-entry ?\n ">" DELAF-mode-syntax-table)
    ; escape-character is backslash
    (modify-syntax-entry ?\\ "\\" DELAF-mode-syntax-table)
    ; quotation mark is a word character
    (modify-syntax-entry ?\" "w" DELAF-mode-syntax-table)
    DELAF-mode-syntax-table)
  "Syntax table used in DELAF-mode.")


