;;; -*-Emacs-Lisp-*-
;;; scala.el - Major mode for editing Scala code.
;;; $Id: scala-mode.el 15795 2008-08-15 08:32:12Z nielsen $


(require 'easymenu)
(require 'cl)
(require 'regexp-opt)
(require 'tempo)

(defconst scala-mode-version "0.5 ($Revision: 15795 $)")
(defconst scala-bug-e-mail "scala@listes.epfl.ch")
(defconst scala-web-url "http://scala-lang.org/")

;; XEmacs compatibility
;; (note that XEmacs is not supported currently, the function below
;; only works around a single incompatibility).

;;(defun scala-regexp-opt-charset (cset)
;;  (regexp-opt-charset (if (integerp ?a)
;;                          cset
;;                        (mapcar #'char-to-string cset))))
(defun scala-regexp-opt-charset (chars)
  ;;
  ;; Return a regexp to match a character in CHARS.
  ;;
  ;; The basic idea is to find character ranges.  Also we take care in the
  ;; position of character set meta characters in the character set regexp.
  ;;
  (let* ((charmap (make-char-table 'case-table))
     (start -1) (end -2)
     (charset "")
     (bracket "") (dash "") (caret ""))
    ;;
    ;; Make a character map but extract character set meta characters.
    (dolist (char chars)
      (case char
    (?\]
     (setq bracket "]"))
    (?^
     (setq caret "^"))
    (?-
     (setq dash "-"))
    (otherwise
     (aset charmap char t))))
    ;;
    ;; Make a character set from the map using ranges where applicable.
    (map-char-table
     (lambda (c v)
       (when v
        (if (listp c) (setq start (car c) end (cdr c)) 
          (if (= (1- c) end) (setq end c)
            (if (> end (+ start 2))
                (setq charset (format "%s%c-%c" charset start end))
              (while (>= end start)
                (setq charset (format "%s%c" charset start))
                (incf start)))
            (setq start c end c)))))
     charmap)
    (when (>= end start)
      (if (> end (+ start 2))
      (setq charset (format "%s%c-%c" charset start end))
    (while (>= end start)
      (setq charset (format "%s%c" charset start))
      (incf start))))
    ;;
    ;; Make sure a caret is not first and a dash is first or last.
    (if (and (string-equal charset "") (string-equal bracket ""))
    (concat "[" dash caret "]")
      (concat "[" bracket charset caret dash "]"))))

;;; Customization
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defgroup scala
  nil
  "Mode to edit Scala code."
  :group 'languages)

(defcustom scala-indent-step 2
  "Indentation step."
  :type 'integer
  :group 'scala)


(defconst scala-number-re
  "[[:digit:]]+\\(\\.[[:digit:]]+\\)?\\([eE][+-]?[[:digit:]]+\\)?[fl]?"
  "Regular expression matching a Scala number (integer or float).")

(defconst scala-rawstring-re
  "\"\"\"[^\"\"\"]*\"\"\""
  "Regular expression matching a Scala raw string literal.")

(defconst scala-string-re
  "\"\\([^\"\\\\]\\|\\\\\.\\)*\""
  "Regular expression matching a Scala string literal.")

(defconst scala-char-re
  "'\\([^\\\\]\\|\\(\\\\[^']\\)\\)'"
  "Regular expression matching a Scala character literal.")

(defconst scala-literal-re
  (concat "\\(" "\\(" scala-number-re "\\)"
          "\\|" "\\(" scala-rawstring-re "\\)"
          "\\|" "\\(" scala-string-re "\\)"
          "\\|" "\\(" scala-char-re "\\)" "\\)")
  "Regular expression matching any Scala literal.")

(defconst scala-most-special-chars (mapcar 'identity "<>+-*/|@#%&!?$^`~")
  "List of almost all Scala special characters.
Not included in this list are the special characters which are
reserved keywords when used alone.")

(defconst scala-all-special-chars (append (mapcar 'identity ":;,=")
                                          scala-most-special-chars)
  "List of all Scala special characters.")

(defconst scala-most-special-char-re
  (scala-regexp-opt-charset scala-most-special-chars)
  "Regular expression matching a single Scala special character")

(defconst scala-all-special-char-re
  (scala-regexp-opt-charset scala-all-special-chars)
  "Regular expression matching a single Scala special character")

(defconst scala-keywords-re
  (regexp-opt '("abstract" "case" "class" "catch" "def" "do" "else" "extends"
                "final" "finally" "for" "forSome" "if" "implicit" "import" "lazy"
                "new" "match" "mixin" "object" "override" "package" "private"
                "protected" "requires" "return" "sealed" "super" "this" "throw"
                "trait" "try" "type" "val" "var" "with" "while" "yield")
	      'words))

(defconst scala-constants-re
  (regexp-opt '("true" "false" "null") 'words))

(defconst scala-special-ident-re
  (concat "\\(" scala-all-special-char-re "\\{2,\\}"
          "\\|" scala-most-special-char-re "+"
          "\\)"))

(defconst scala-ident-re
  (let* ((varid-re "[[:alnum:]]+")
         (id-re (concat "\\(" varid-re "\\|" scala-special-ident-re "\\)")))
    (concat id-re
            "\\(" "_+" "\\(" id-re "\\)?" "\\)*"))
  "Regular expression matching a Scala identifier.")

(defconst scala-var-ident-re
  (concat "[[:lower:]][[:alnum:]]*" "\\(_" scala-ident-re "\\)*")
  "Relgular expression matching a Scala 'variable' identifier.")

(defconst scala-qual-ident-re
  (concat scala-ident-re "\\(" "\\." scala-ident-re "\\)*"))

(defconst scala-capitalized-ident-re
  (concat "\\(\\)\\([[:upper:]]" scala-ident-re "\\)"))

(defconst scala-expr-start-re
  (concat
   (regexp-opt '("if" "else" "for" "do" "yield") 'words) "\\|"
   (regexp-opt '("=" "=>") t)))

(defconst scala-expr-starter
  (mapcar (lambda (pair) (cons (car pair) (concat "\\<" (cdr pair) "\\>")))
          '(("else" . "if")
            ("yield" . "for")
            ("do" . "for")
            ("extends" . "class")
            ("with" . "class")
            ("=>" . "case"))))

(defconst scala-expr-middle-re
  (regexp-opt (mapcar #'car scala-expr-starter) 'words))

(defconst scala-compound-expr-re
  "\\<else\\s +if\\>")

(defun scala-special-char-p (char)
  (and char
       (string-match scala-all-special-char-re (string char))))

(defun scala-looking-at-special-identifier (regexp)
  (and (not (scala-special-char-p (char-before)))
       (looking-at regexp)
       (not (scala-special-char-p (char-after (match-end 0))))))

(defconst scala-comment-begin-or-end-re
  (concat "\\(" "^/\\*.*" "\\|" "^//.*" "\\|" ".*\\*/$" "\\)"))

(defun scala-search-special-identifier-forward (limit)
  (ignore-errors
    (while (and (search-forward-regexp scala-special-ident-re limit)
                (save-match-data
                  (string-match scala-comment-begin-or-end-re
                                (match-string-no-properties 0)))))
    t))

;;; Movement
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun scala-when-looking-at* (regexp &optional thunk)
  (let ((saved-match-data (match-data)))
    (if (looking-at regexp)
        (progn (goto-char (match-end 0))
               (set-match-data saved-match-data)
               (or (not thunk) (funcall thunk)))
      (set-match-data saved-match-data)
      nil)))

(defmacro scala-when-looking-at (regexp &rest body)
  (if body
      `(scala-when-looking-at* ,regexp (lambda () ,@body))
    `(scala-when-looking-at* ,regexp)))

(defun scala-forward-spaces (&optional limit)
  (if limit
      (save-restriction
        (narrow-to-region (point) limit)
        (forward-comment 100000))
    (forward-comment 100000)))

(defun scala-backward-spaces ()
  (forward-comment -100000))

(defun scala-looking-at-backward (re)
  (save-excursion
    (when (= 0 (skip-syntax-backward "w_")) (backward-char))
    (looking-at re)))

(defmacro scala-point-after (&rest body)
  `(save-excursion
     ,@body
     (point)))

(defmacro scala-move-if (&rest body)
  (let ((pt-sym (make-symbol "point"))
	(res-sym (make-symbol "result")))
    `(let ((,pt-sym (point))
	   (,res-sym ,(cons 'progn body)))
       (unless ,res-sym (goto-char ,pt-sym))
       ,res-sym)))

(defun scala-forward-ident ()
  ;; Move forward over an identifier.
  (scala-forward-spaces)
  (if (looking-at scala-ident-re)
      (goto-char (match-end 0))
    (forward-char))
  t)

(defun scala-backward-ident ()
  ;; Move backward over an identifier.
  (scala-backward-spaces)
  (if (scala-looking-at-backward scala-ident-re)
      (goto-char (match-beginning 0))
    (backward-char))
  t)

(defun scala-forward-qual-ident ()
  ;; Move forward over a qualifier identifier.
  (scala-forward-spaces)
  (if (looking-at scala-qual-ident-re)
      (goto-char (match-end 0))
    (forward-char))
  t)

(defun scala-backward-qual-ident ()
  ;; Move backward over a qualifier identifier.
  (scala-backward-spaces)
  (if (scala-looking-at-backward scala-qual-ident-re)
      (goto-char (match-beginning 0))
    (backward-char))
  t)

(defun scala-forward-simple-type ()
  ;; Move forward over a simple type (as defined by the grammar).
  ;; Works only when point is at the beginning of a simple type
  ;; (modulo initial spaces/comments).
  (cond ((eobp) nil)
        ((= (char-after) ?\()
         ;; Parenthesized type
         (forward-sexp)
         t)
        (t
         ;; Type designator
         (scala-forward-qual-ident)
         (scala-forward-spaces)
         (cond ((eobp) nil)
               ((= (char-after) ?\[)
                ;; Type arguments
                (forward-sexp))
               ((= (char-after) ?\#)
                ;; Type selection
                (forward-char)
                (scala-forward-ident)))
         t)))

(defun scala-forward-type1 ()
  ;; Move forward over a type1 (as defined by the grammar).
  ;; Works only when point is at the beginning of a type (modulo
  ;; initial spaces/comments).
  (scala-forward-spaces)
  (scala-when-looking-at "\\<class\\>"
                         (forward-word 1) (scala-forward-spaces))
  (scala-forward-simple-type)
  (while (scala-when-looking-at "\\s *\\<with\\>\\s *")
    (if (and (not (eobp)) (= (char-after) ?\{))
        (forward-sexp)                       ;skip refinement
      (scala-forward-simple-type)))
  t)

(defun scala-forward-type ()
  ;; Move forward over a type.
  (cond ((eobp) nil)
        ((= (char-after) ?\()
         ;; Function type (several arguments)
         (forward-sexp)
         (scala-when-looking-at "\\s *=>\\s *" (scala-forward-type))
         t)
        (t
         ;; Type1 or function type with one argument
         (scala-forward-type1)
         (scala-when-looking-at "\\s *=>\\s *" (scala-forward-type))
         t)))

(defun scala-forward-type-param ()
  ;; Move over a type parameter
  ;; variance
  (scala-when-looking-at "\\s *[-+]\\s *")
  (scala-forward-ident)
  ;; bounds
  (while (scala-when-looking-at "\\s *[<>][:%]\\s *")
    (scala-forward-type))
  t)

(defun scala-forward-literal ()
  ;; Move forward over an integer, float, character or string literal.
  (scala-forward-spaces)
  (scala-when-looking-at scala-literal-re)
  t)

;;; Indentation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun scala-parse-partial-sexp ()
  (parse-partial-sexp (point-min) (point)))

(defun scala-in-comment-p ()
  "Return t iff the point is inside a comment."
  ;; The two branches of the "if" below do not have the same behaviour
  ;; when the point is on the comment beginning/ending character(s).
  (if font-lock-mode
      (eq (get-text-property (point) 'face) 'font-lock-comment-face)
    (save-excursion (comment-beginning))))

(defun scala-in-string-p ()
  "Return t iff the point is inside a string."
  (if font-lock-mode
      (eq (get-text-property (point) 'face) 'font-lock-string-face)
    (let ((limit (point)))
      (beginning-of-line)
      (loop while (search-forward-regexp "\\(^\\|[^\\\\]\\)\"" limit 'move)
            count (not (scala-in-comment-p)) into quotes
            finally return (oddp quotes)))))

(defun scala-indentation ()
  "Return the suggested indentation for the current line."
  (save-excursion
    (beginning-of-line)
    (or (and (scala-in-comment-p)
             (not (= (char-after) ?\/))
             (scala-comment-indentation))
        (scala-indentation-from-following)
        (scala-indentation-from-preceding)
        (scala-indentation-from-block)
        0)))

(defun scala-comment-indentation ()
  ;; Return suggested indentation inside of a comment.
  (forward-line -1)
  (beginning-of-line)
  (skip-syntax-forward " ")
  (if (looking-at "/\\*")
      (+ 1 (current-column))
    (current-column)))

(defun scala-block-indentation ()
  (let ((block-start-eol (scala-point-after (end-of-line)))
        (block-after-spc (scala-point-after (scala-forward-spaces))))
    (if (> block-after-spc block-start-eol)
        (+ (current-indentation) scala-indent-step)
      (current-column))))

(defun scala-indentation-from-following ()
  ;; Return suggested indentation based on the following part of the
  ;; current expression. Return nil if indentation cannot be guessed.
  (save-excursion
    (scala-forward-spaces (scala-point-after (end-of-line)))
    (cond
     ((eobp) nil)
     ((= (char-syntax (char-after)) ?\))
      (let ((parse-sexp-ignore-comments t))
        (goto-char (1+ (scan-sexps (1+ (point)) -1))))
      (- (scala-block-indentation) scala-indent-step))
     ((looking-at scala-expr-middle-re)
      ;; [...] this is a somewhat of a hack.
      (let ((matching-kw (cdr (assoc (match-string-no-properties 0)
                                     scala-expr-starter))))
        (while (and (search-backward-regexp matching-kw nil t)
                    (or (scala-in-comment-p) (scala-in-string-p)))))
      (scala-move-if (backward-word 1)
                     (looking-at scala-compound-expr-re))
      (current-column)))))

(defun scala-indentation-from-preceding ()
  ;; Return suggested indentation based on the preceding part of the
  ;; current expression. Return nil if indentation cannot be guessed.
  (save-excursion
    (scala-backward-spaces)
    (when (and (not (bobp))
               (or (eq (char-syntax (char-before)) ?\()
                   (progn
                     (when (eq (char-before) ?\))
                       (backward-sexp)
                       (scala-backward-spaces))
                     (scala-looking-at-backward scala-expr-start-re))))
      (+ (current-indentation) scala-indent-step))))

(defun scala-indentation-from-block ()
  ;; Return suggested indentation based on the current block.
  (save-excursion
    (let* ((state (scala-parse-partial-sexp))
           (block-start (nth 1 state)))
      (if (not block-start)
          0
        (goto-char (1+ block-start))
        (scala-block-indentation)))))

(defun scala-indent-line-to (column)
  "Indent current line to COLUMN and perhaps move point.
The point is moved iff it is currently in the indentation, in which
case it is brought to the end of that indentation. Otherwise it does
not move."
  (if (<= (current-column) (current-indentation))
      (indent-line-to column)
    (save-excursion (indent-line-to column))))

(defun scala-indent-line ()
  "Indent current line as smartly as possible.
When called repeatedly, indent each time one stop further on the right."
  (interactive)
  (if (or (eq last-command this-command)
          (eq last-command 'scala-undent-line))
      (scala-indent-line-to (+ (current-indentation) scala-indent-step))
    (let ((indentation (scala-indentation)))
      (scala-indent-line-to indentation))))

(defun scala-undent-line ()
  "Indent line to previous tab stop."
  (interactive)
  (scala-indent-line-to (max 0 (- (current-indentation) scala-indent-step))))

(defun scala-electric-brace ()
  "Insert a brace, and if alone on a non-comment line, reindent."
  (interactive)
  (let ((on-empty-line-p (save-excursion
                           (beginning-of-line)
                           (looking-at "^\\s *$"))))
    ;; Calling self-insert-command will blink to the matching open-brace
    ;; (if blink-matching-paren is enabled); we first indent, then
    ;; call self-insert-command, so that the close-brace is correctly
    ;; positioned during the blink.
    (when on-empty-line-p
      (insert "}")
      (scala-indent-line)
      (delete-backward-char 1))
    (call-interactively 'self-insert-command)))

;;; Syntax highlighting
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun scala-mark-borders (funs)
  (loop for (fun . flag) in funs
        if flag collect (point-marker)
        while (funcall fun)
        if flag collect (point-marker)))

(defun scala-make-match (funs)
  (let ((start-mark (point-marker))
        (markers (scala-mark-borders funs))
        (end-mark (point-marker)))
    (cons start-mark (cons end-mark markers))))

(defconst scala-binding-end-re
  (regexp-opt '(":" "=" "=>" ";" "<-")))

(defun scala-match-and-skip-binding (limit)
  (skip-chars-forward " ()")
  (and (not (or (looking-at "\\<\\(extends\\|with\\)\\>\\|{")
                (scala-looking-at-special-identifier scala-binding-end-re)))
       (ignore-errors
         (save-restriction
           (narrow-to-region (point-min) limit)
           (let ((matches (scala-make-match
                           '((scala-forward-ident . t)
                             ((lambda ()
                                (scala-forward-spaces)
                                (when (scala-looking-at-special-identifier ":")
                                  (forward-char)
                                  (scala-forward-spaces)
                                  t)) . nil)
                             ((lambda ()
                                (scala-forward-type)
                                (scala-when-looking-at "\\s *\\*")
                                t) . t)))))
             (scala-when-looking-at "\\s *,")
             (set-match-data matches)))
         t)))

(defun scala-match-and-skip-ident (limit)
  (scala-forward-spaces)
  (when (and (not (looking-at scala-keywords-re))
             (looking-at scala-qual-ident-re))
    (goto-char (match-end 0))
    t))

(defun scala-match-and-skip-type-param (limit)
  (scala-when-looking-at "\\s *[[,]\\s *"
    (let ((matches (scala-make-match '((scala-forward-type-param . t)))))
      (scala-when-looking-at "\\s *\\]")
      (set-match-data matches)
      t)))

(defun scala-match-and-skip-result-type (limit)
  (scala-when-looking-at "\\s *:\\s *"
    (set-match-data (list (point-marker)
                          (progn (scala-forward-type) (point-marker))))
    t))

(defconst scala-pattern-end-re
  (regexp-opt '("if" "case" "class") 'words))

(defconst scala-pattern-end-special-re
  (regexp-opt '( "=>" "=" "<-") t))

(defun scala-match-and-skip-pattern (limit)
  (while (progn
           (skip-chars-forward "()[], ")
           (and (not (or (looking-at scala-pattern-end-re)
                         (scala-looking-at-special-identifier
                          scala-pattern-end-special-re)))
                (looking-at scala-literal-re)))
    (goto-char (match-end 0)))
  (and (not (or (looking-at scala-pattern-end-re)
                (scala-looking-at-special-identifier scala-pattern-end-special-re)))
       (let ((case-fold-search nil))
         (cond ((looking-at scala-capitalized-ident-re)
                (goto-char (match-end 0)))
               ((scala-match-and-skip-binding limit) t)))))

(defvar scala-font-lock-keywords
  `(;; keywords
    (,scala-keywords-re
     0 font-lock-keyword-face nil)

    ;; constants
    (,scala-constants-re
     0 ,(if (boundp 'font-lock-constant-face)
	    'font-lock-constant-face
	  'font-lock-keyword-face)
     nil)

    ;; modules
    (,(concat "\\<\\(module\\|object\\)\\>\\s *\\(" scala-ident-re "\\)")
     (2 font-lock-variable-name-face nil))

    ;; type definitions
    (,(concat "\\<type\\>\\s *\\(" scala-ident-re "\\)")
     (1 font-lock-type-face nil))

    ;; variables
    ("\\<var\\>"
     (scala-match-and-skip-binding (goto-char (match-end 0))
                                   nil
                                   (1 font-lock-variable-name-face nil)
                                   (2 font-lock-type-face nil t)))

    ;; functions
    (,(concat "\\(^\\|[^(,]\\)\\s *\\<def\\>" "\\s *" "\\(" scala-ident-re "\\)\\s *")
     (2 font-lock-function-name-face nil)
     (scala-match-and-skip-type-param (goto-char (match-end 0)) nil
                                      (1 font-lock-type-face nil t))
     (scala-match-and-skip-binding nil nil
                                   (1 font-lock-variable-name-face nil)
                                   (2 font-lock-type-face nil t))
     (scala-match-and-skip-result-type nil nil
                                       (0 font-lock-type-face nil)))

    ;; class definitions
    ("\\<\\(class\\|trait\\)\\>"
     (scala-match-and-skip-ident (goto-char (match-end 0)) nil
                                 (1 font-lock-type-face nil))
     (scala-match-and-skip-type-param nil nil
                                      (1 font-lock-type-face nil t))
     (scala-match-and-skip-binding nil nil
                                   (1 font-lock-variable-name-face nil)
                                   (2 font-lock-type-face nil t)))

    ;; "extends" and "with" clauses
    ("\\<\\(extends\\|with\\)\\>\\s *[^{]"
     (scala-match-and-skip-ident (goto-char (1- (match-end 0))) nil
                                 (0 font-lock-type-face nil))
     (scala-match-and-skip-type-param nil nil
                                      (1 font-lock-type-face nil t)))

    ;; patterns
    ("\\<\\(case\\|val\\)\\>\\s *"
     (scala-match-and-skip-pattern (goto-char (match-end 0)) nil
                                   (1 font-lock-variable-name-face nil)
                                   (2 font-lock-type-face nil t)))))


(defvar scala-font-lock-syntactic-keywords
  `((,scala-char-re (0 "\"" t nil))
    (scala-search-special-identifier-forward (0 "w" nil nil))))


; define scala-mode-hook
(defvar scala-mode-hook nil
  "Hook to run after installing scala mode")



;;; Customization
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun scala-customize ()
  "Customize Scala mode using the Customize package."
  (interactive)
  (customize-group 'scala))


(defun scala-interpreter-running-p ()
  "True iff a Scala interpreter is currently running in a buffer."
  ;; The following makes sure that we do not autoload
  ;; inferior-scala-mode just to check if the interpreter is running.
  (and (fboundp 'inferior-scala-mode)
       (let ((ism-def (symbol-function 'inferior-scala-mode)))
         (not (and (consp ism-def) (eq (car ism-def) 'autoload))))
       (scala-interpreter-running-p-1)))


(defun scala-browse-web-site ()
  "Browse the Scala home-page"
  (interactive)
  (require 'browse-url)
  (browse-url scala-web-url))


;; Print version in minibuffer
(defun scala-version ()
  "Report the current version of the scala emacs mode in the minibuffer."
  (interactive)
  (message "Using scala mode version %s" scala-mode-version))


;; Bug reporting
(defun scala-report-bug ()
  "Report a bug to the author of the Scala mode via e-mail.
The package used to edit and send the e-mail is the one selected
through `mail-user-agent'."
  (interactive)
  (require 'reporter)
  (let ((reporter-prompt-for-summary-p t))
    (reporter-submit-bug-report
     scala-bug-e-mail
     (concat "Emacs Scala mode v" scala-mode-version)
     '(scala-indent-step))))


;;; Tempo Templetes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun scala-tmpl-helper-name (qst)
  ""
  (let
      (tmpl-name (read-string qst))
    (if (string= tmpl-name "") 
	"NONAME"
      tmpl-name)))


(defun scala-tmpl-helper-extend ()
  ""
  (let
      (tmpl-name (read-string "Extend: "))
    (progn
      (if (string= tmpl-name "") 
	  (setq tmpl-name "")
	(setq tmpl-name (concat " extends " tmpl-name)))
      tmpl-name )))


(defun scala-tmpl-helper-with () 
  ""
  (let
      ((tmpl-accum "")(tmpl-name (read-string "With: ")))
    (progn
      (while (not (string= tmpl-name ""))
	(setq tmpl-accum (concat tmpl-accum " with " tmpl-name))
	(setq tmpl-name (read-string (concat "(" tmpl-accum " ) With: "))))
      tmpl-accum)))


(defun scala-tmpl-helper-find-abstract-class-name ()
  "Helper function for finding the name of the abstract class above point"
  (save-excursion
    (let 
	((tmpl-name ""))
      (if (re-search-backward "^abstract\\([ \t]*\\)class\\([ \t]*\\)\\(\\w+\\)\\([ \t]*\\)" nil t)
	  (setq tmpl-name (match-string 3))
	(progn
	  (message "No abstract class found! Using class Object.")
	  (setq tmpl-name "Object")))
      tmpl-name)))


(setq tempo-interactive t)
(defvar scala-tempo-tags nil "Tempo tags for Scala mode")

;; application template
(tempo-define-template "scala-object-main"
		       '(> "object App {" > n >
			 > "def main(args : Array[String]) : Unit = {" > n >
			 > r n >
			 > "}" > n >
			 "}" > n >
			 )
		       "application"
		       "Insert a new object with main method"
		       'scala-tempo-tags)

;; simple templates for trait, object, class, abs class
(tempo-define-template "scala-trait-s"
		       '(> (p "trait name: " traitname 'noinsert) "trait " (s traitname) " {" > n > r n "}" > n > )
		       "strait"
		       "Insert a new trait (simple)"
		       'scala-tempo-tags)


(tempo-define-template "scala-object-s"
		       '(> (p "object name: " objname 'noinsert) "object " (s objname) " {" > n > r n "}" > n > )
		       "sobject"
		       "Insert a new object (simple)"
		       'scala-tempo-tags)


(tempo-define-template "scala-class-s"
		       '(> (p "class name: " classname 'noinsert) "class " (s classname) " {" > n > r n "}" > n > )
		       "sclass"
		       "Insert a new class (simple)"
		       'scala-tempo-tags)


(tempo-define-template "scala-abs-class-s"
		       '(> (p "abstract class name: " classname 'noinsert) "abstract class " (s classname) " {" > n > r > n "}" > n > )
		       "sabs-class"
		       "Insert a new abstract class (simple)"
		       'scala-tempo-tags)

;; Case classes (both abstract and case)

(tempo-define-template "scala-abs-case-class-s"
		       '(> (p "abstract case class name: " classname 'noinsert) "abstract class " (s classname) > n > r > )
		       "abs-case-class"
		       "Insert a new abstract class (simple)"
		       'scala-tempo-tags)


(tempo-define-template "scala-case-class-s"
		       '(> (p "case class name: " cclassname 'noinsert) "case class " (s cclassname) "(" r ") extends " (scala-tmpl-helper-find-abstract-class-name) > n > )
		       "case-class"
		       "Insert a new case class (simple)"
		       'scala-tempo-tags)


;; extended templates for trait, object, class and abs class
(tempo-define-template "scala-trait-e"
		       '(> "trait " (scala-tmpl-helper-name "trait name: ") (scala-tmpl-helper-extend) (scala-tmpl-helper-with) " {" > n r > n "}" > n > )
		       "trait"
		       "Insert a new trait"
		       'scala-tempo-tags)


(tempo-define-template "scala-object-e"
		       '(> "object " (scala-tmpl-helper-name "object name: ") (scala-tmpl-helper-extend) (scala-tmpl-helper-with) " {" > n > r n "}" > n > )
		       "object"
		       "Insert a new object"
		       'scala-tempo-tags)


(tempo-define-template "scala-class-e"
		       '(> "class " (scala-tmpl-helper-name "class name: ") (scala-tmpl-helper-extend) (scala-tmpl-helper-with) " {" > n > r n "}" > n > )
		       "class"
		       "Insert a new class"
		       'scala-tempo-tags)


(tempo-define-template "scala-abs-class-e"
		       '(> "abstract class " (scala-tmpl-helper-name "class name: ") (scala-tmpl-helper-extend) (scala-tmpl-helper-with) " {" > n > r n "}" > n > )
		       "abs-class"
		       "Insert a new abstract class"
		       'scala-tempo-tags)


;; expressions statements

(tempo-define-template "scala-stmt-if"
		       '(> "if (" (p "if clause: ") ") " r > n > )
		       "if-statement"
		       "Insert a simple one-line if statement"
		       'scala-tempo-tags)
 

(tempo-define-template "scala-stmt-else"
		       '(> "else " r > n > )
		       "else-statement"
		       "Insert a simple one-line else statement"
		       'scala-tempo-tags)


(tempo-define-template "scala-stmt-ifelse"
		       '(> "if (" (p "if clause: ") ") {" > n > r n > "} else {" > n > n > "}"> n > )
		       "if-else-statement"
		       "Insert a if statement with else clause"
		       'scala-tempo-tags)


(tempo-define-template "scala-stmt-match"
		       '(> "match { " > n > r > n > "}" > n > )
		       "match-statement"
		       "Insert a match statement"
		       'scala-tempo-tags)


;(tempo-define-template "scala-stmt-case"
; 		       '(> "case " (p "case class: ") "(" r ") => " > n >)
; 		       "case-statement"
; 		       "Insert a case statement"
; 		       'scala-tempo-tags)

(tempo-define-template "scala-stmt-case"
		       '(> "case " (p "case pattern: ") r " => " > n >)
		       "case-statement"
		       "Insert a case statement"
		       'scala-tempo-tags)

(tempo-define-template "scala-stmt-case-guard"
		       '(> "case " (p "case pattern: ") r " if " (p "case guard: ") " => " > n >)
		       "case-guard-statement"
		       "Insert a case guard statement"
		       'scala-tempo-tags)

(tempo-define-template "scala-stmt-while"
		       '(> "while (" (p "while clause: ") ") { " > n > r > n > "}" > n > )
		       "while-statement"
		       "Insert a while statement"
		       'scala-tempo-tags)

(tempo-define-template "scala-stmt-do-while"
		       '(> "do { " > n > r > n > "} while (" (p "do-while clause: ") ")" > n > )
		       "do-while-statement"
		       "Insert a do-while statement"
		       'scala-tempo-tags)

(tempo-define-template "scala-stmt-for"
		       '(> "for (" (p "for comprehension: ") ") { " > n > r > n > "}" > n > )
		       "for-statement"
		       "Insert a while comprehension statement"
		       'scala-tempo-tags)

(tempo-define-template "scala-stmt-try-catch"
		       '(> "try {" > n > r n > "} catch { " > n > n > "}" > n > )
		       "try-catch-statement"
		       "Insert a try/catch statement"
		       'scala-tempo-tags)


;; Functions



;;; Mode
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;###autoload
(define-derived-mode scala-mode fundamental-mode "Scala"
  "Major mode for editing Scala code.

When started, run `scala-mode-hook'.

\\{scala-mode-map}"
  ;; Font lock
  (make-local-variable 'font-lock-defaults)
  (setq font-lock-defaults
        `(scala-font-lock-keywords
          nil
          nil
          ((?\_ . "w"))
          nil
          (font-lock-syntactic-keywords . ,scala-font-lock-syntactic-keywords)
          (parse-sexp-lookup-properties . t)))

  ;; Paragraph separation
  (make-local-variable 'paragraph-start)
  (setq paragraph-start (concat "^\\s *$\\|" page-delimiter))
  (make-local-variable 'paragraph-separate)
  (setq paragraph-separate paragraph-start)
  (make-local-variable 'paragraph-ignore-fill-prefix)
  (setq paragraph-ignore-fill-prefix t)

  ;; Comment handling
  (make-local-variable 'comment-start)
  (setq comment-start "// ")
  (make-local-variable 'comment-end)
  (setq comment-end "")
  (make-local-variable 'comment-multi-line)
  (setq comment-multi-line nil)
  (make-local-variable 'comment-start-skip)
  (setq comment-start-skip "/\\*+ *\\|//+ *")
  (make-local-variable 'comment-end-skip)
  (setq comment-end-skip " *\\*+/\\| *")

  ;; Misc
  (make-local-variable 'indent-line-function)
  (setq indent-line-function #'scala-indent-line)
  (make-local-variable 'require-final-newline)
  (setq require-final-newline t)
  
  ;; Tempo Templetes
  (tempo-use-tag-list 'scala-tempo-tags))


;; Keymap
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define-key scala-mode-map [tab] 'scala-indent-line)
(define-key scala-mode-map [(control tab)] 'scala-undent-line)
(define-key scala-mode-map [backspace] 'backward-delete-char-untabify)

(define-key scala-mode-map [(control c)(control l)] 'scala-load-file)
(define-key scala-mode-map [(control c)(control r)] 'scala-eval-region)
(define-key scala-mode-map [(control c)(control b)] 'scala-eval-buffer)

(define-key scala-mode-map [(control c)(control c)] 'comment-region)

(define-key scala-mode-map "}" 'scala-electric-brace)

;; Tempo templates
(define-key scala-mode-map [(control c)(f)] 'tempo-complete-tag)

(define-key scala-mode-map [(control c)(t)(o)] 'tempo-template-scala-object-s)
(define-key scala-mode-map [(control c)(t)(t)] 'tempo-template-scala-trait-s)
(define-key scala-mode-map [(control c)(t)(c)] 'tempo-template-scala-class-s)
(define-key scala-mode-map [(control c)(t)(a)] 'tempo-template-scala-abs-class-s)


(define-key scala-mode-map [(control c)(a)(a)] 'tempo-template-scala-abs-case-class-s)
(define-key scala-mode-map [(control c)(a)(c)] 'tempo-template-scala-case-class-s)


(define-key scala-mode-map [(control c)(t)(shift t)] 'tempo-template-scala-trait-e)
(define-key scala-mode-map [(control c)(t)(shift c)] 'tempo-template-scala-class-e)
(define-key scala-mode-map [(control c)(t)(shift a)] 'tempo-template-scala-abs-class-e)
(define-key scala-mode-map [(control c)(t)(shift o)] 'tempo-template-scala-object-e)
(define-key scala-mode-map [(control c)(t)(shift s)] 'tempo-template-scala-case-class-e)


(define-key scala-mode-map [(control c)(t)(m)] 'tempo-template-scala-object-main)


(define-key scala-mode-map [(control c)(s)(i)]       'tempo-template-scala-stmt-if)
(define-key scala-mode-map [(control c)(s)(e)]       'tempo-template-scala-stmt-else)
(define-key scala-mode-map [(control c)(s)(shift i)] 'tempo-template-scala-stmt-ifelse)
(define-key scala-mode-map [(control c)(s)(m)]       'tempo-template-scala-stmt-match)
(define-key scala-mode-map [(control c)(s)(c)]       'tempo-template-scala-stmt-case)
(define-key scala-mode-map [(control c)(s)(shift c)] 'tempo-template-scala-stmt-case-guard)
(define-key scala-mode-map [(control c)(s)(w)]       'tempo-template-scala-stmt-while)
(define-key scala-mode-map [(control c)(s)(shift w)] 'tempo-template-scala-stmt-do-while)
(define-key scala-mode-map [(control c)(s)(f)]       'tempo-template-scala-stmt-for)
(define-key scala-mode-map [(control c)(s)(t)]       'tempo-template-scala-stmt-try-catch)


;(define-key scala-mode-map [(control c)(d)(f)]       'tempo-template-scala-scaladoc)

;(define-key scala-mode-map [(control c)(f)(f)]       'tempo-template-scala-func)

;; Emacs memu entry
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(easy-menu-define scala-menu-bar scala-mode-map "Scala menu"
  '("Scala"
    ("Classes and Objects"
     ["application"             tempo-template-scala-object-main]
     "---"
     ["object (simple)"         tempo-template-scala-object-s]
     ["trait (simple)"          tempo-template-scala-trait-s]
     ["class (simple)"          tempo-template-scala-class-s]
     ["abstract class (simple)" tempo-template-scala-abs-class-s]
     "---"
     ["abstract case class"     tempo-template-scala-abs-case-class-s]
     ["case class"              tempo-template-scala-case-class-s]
     "---"
     ["trait"                   tempo-template-scala-trait-e]
     ["object"                  tempo-template-scala-object-e]
     ["class"                   tempo-template-scala-class-e]
     ["abstract class"          tempo-template-scala-abs-class-e]
     )
    ("Expressions"
     ["if statement (one line)"   tempo-template-scala-stmt-if t]
     ["else statement (one line)" tempo-template-scala-stmt-else t]
     ["if-else statement"         tempo-template-scala-stmt-ifelse t]
     ["while statement"           tempo-template-scala-stmt-while t]
     ["do-while statement"        tempo-template-scala-stmt-do-while t]
     ["for statement"             tempo-template-scala-stmt-for t]
     ["match statement"           tempo-template-scala-stmt-match t]
     ["case statement"            tempo-template-scala-stmt-case t]
     ["case-guard statement"      tempo-template-scala-stmt-case-guard t]
     ["try/catch statement"       tempo-template-scala-stmt-try-catch t]
     )
;    ("Functions"
;     ["simple"   ignore]
;     )
;    ("Documentation"
;     ["single line comment" ignore]
;     ["multi line comment"  ignore]
;     ["file comment"        ignore]
;     ["function comment"    ignore]
;     "---"
;     ["Run scaladoc"        ignore]
;     )
    "---"
    ["Run interpreter..."          run-scala (not (scala-interpreter-running-p))]
    ["Quit interpreter"            scala-quit-interpreter (scala-interpreter-running-p)]
    ["Load file in interpreter..." scala-load-file (scala-interpreter-running-p)]
    ["Switch to interpreter"       scala-switch-to-interpreter (scala-interpreter-running-p)]
    ["Evaluate region"             scala-eval-region (and (scala-interpreter-running-p) mark-active)]
    ["Evaluate buffer"             scala-eval-buffer (scala-interpreter-running-p)]
    "---"
    ("Options"
     ["Toggle Scala Electric Mode"   scala-electric-mode 
      :style toggle
      :help "Toggle on/off the electric insert mode for Scala"
      :selected (scala-electric-active-p)
      :active t] 
     )
    ["Browse Scala Web site..." scala-browse-web-site t]
    ["Customize..."             scala-customize t]
    ["Report bug..."            scala-report-bug t]
    ["Version"                  scala-version t]
    ))

;; Syntax tables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; strings and character literals
(modify-syntax-entry ?\" "\"" scala-mode-syntax-table)
(modify-syntax-entry ?\\ "\\" scala-mode-syntax-table)

;; different kinds of "parenthesis"
(modify-syntax-entry ?\( "()" scala-mode-syntax-table)
(modify-syntax-entry ?\[ "(]" scala-mode-syntax-table)
(modify-syntax-entry ?\{ "(}" scala-mode-syntax-table)
(modify-syntax-entry ?\) ")(" scala-mode-syntax-table)
(modify-syntax-entry ?\] ")[" scala-mode-syntax-table)
(modify-syntax-entry ?\} "){" scala-mode-syntax-table)

;; special characters
(modify-syntax-entry ?\_ "_" scala-mode-syntax-table)

(dolist (char scala-all-special-chars)
  (modify-syntax-entry char "." scala-mode-syntax-table))
(modify-syntax-entry ?\. "." scala-mode-syntax-table)

;; comments
(modify-syntax-entry ?\/  ". 124b" scala-mode-syntax-table)
(modify-syntax-entry ?\*  ". 23"   scala-mode-syntax-table)
(modify-syntax-entry ?\n "> b" scala-mode-syntax-table)
(modify-syntax-entry ?\r "> b" scala-mode-syntax-table)

; run hooks
(run-hooks 'scala-mode-hook)

(provide 'scala-mode)
