 ;; define major mode hook
 ;;; Code:
 (defvar sparkling-mode-hook nil)
+
+;; Local variables
+(defgroup sparkling nil
+  "Major mode for editing Sparkling code."
+  :prefix "sparkling-"
+  :group 'languages)
+
+(defcustom sparkling-indent-offset 4
+  "*Indentation offset for `sparkling-mode'."
+  :type 'integer
+  :group 'sparkling
+  :safe #'integerp)
+
+(defcustom sparkling-mode-hook nil
+  "Hooks called on Sparkling mode."
+  :type 'hook
+  :group 'sparkling)
+
+(eval-when-compile
+  ;; Silence compilation warning about `compilation-error-regexp-alist' defined
+  ;; in compile.el.
+  (require 'compile))
+
+(eval-and-compile
+  (if (fboundp 'setq-local)
+      (defalias 'sparkling--setq-local 'setq-local)
+    (defmacro sparkling--setq-local (var val)
+      `(set (make-local-variable (quote ,var)) ,val)))
+
+  ;; Backward compatibility for Emacsen < 24.1
+  (unless (fboundp 'prog-mode)
+    (define-derived-mode prog-mode fundamental-mode "Prog")))
 
 (defun sparkling-eval-replace-expr (start end)
   "Evaluate the contents of the selected region (or the region between START and END) as an expression and replace it with the result."

  "Keymap for Sparkling major mode"
)

;; automatically turn it on
(add-to-list 'auto-mode-alist '("\\.spn\\'" . sparkling-mode))

(defvar sparkling-font-lock-keywords
  (list
   '("\\<\\(and\\|break\\|continue\\|do\\|e\\(lse\\|xtern\\)\\|f\\(n\\|or\\)\\|if\\|let\\|not\\|or\\|return\\|sizeof\\|while\\)\\>" . font-lock-keyword-face)


;; Syntax table
(defvar sparkling-mode-syntax-table
  (with-syntax-table (copy-syntax-table)
	;; This is added so entity names with underscores can be more easily parsed
    (modify-syntax-entry ?_ "w")
	;; C-style comments
    (modify-syntax-entry ?/ ". 124b")
    (modify-syntax-entry ?# "< b")
    (modify-syntax-entry ?* ". 23")
    (modify-syntax-entry ?\n "> b")
    (syntax-table))
  "`sparkling-mode' Syntax table")

(define-derived-mode sparkling-mode prog-mode "Sparkling"
  "Major mode for Sparkling source"
  :syntax-table sparkling-mode-syntax-table
  :group 'sparkling
  (sparkling--setq-local font-lock-defaults '(sparkling-font-lock-keywords
                                              nil ;; keywords-only
                                              nil ;; case-fold
                                              nil ;; syntax-alist
                                              nil ;; syntax-begin
                                              ))
  (sparkling--setq-local indent-line-function 'sparkling-indent-line)
  (sparkling--setq-local comment-start "//")
  (sparkling--setq-local comment-use-syntax t)
)

;; automatically turn it on
(add-to-list 'auto-mode-alist '("\\.spn\\'" . sparkling-mode))
;; (add-to-list 'interpreter-mode-alist '("spn" . sparkling-mode))

(provide 'sparkling-mode)
;;; sparkling-mode.el ends here
