(require 'cl-lib)

(load-library "resource-db.el")
(require 'renpy-asset-store)

(defvar renpy-mode-hook nil)

(defun renpy-scene ()
  (interactive)
  (insert "scene "))

(defun renpy-show ()
  (interactive)
  (insert "show "))

(defun renpy-hide ()
  (interactive)
  (insert "hide "))

(defun renpy-add-indentation ()
  (interactive)
  (save-excursion
    (move-beginning-of-line nil)
    (insert "    ")))

(defun renpy-remove-indentation ()
  (interactive)
  (save-excursion
    (move-beginning-of-line nil)
    (if (char-equal (char-after (point)) ?\C-i)
	(delete-char 1)
      (dotimes (i 4)
	(if (char-equal (char-after (point)) ? )
	    (delete-char 1))))))

(defvar renpy-mode-map
  (let ((map (make-keymap)))
    (define-key map "\C-j" 'newline-and-indent)
    (define-key map (kbd "C-+") 'renpy-add-indentation)
    (define-key map (kbd "M-+") 'renpy-remove-indentation)
    ;; Goodies from the renpy-asset-store
    (define-key map (kbd "C-c i")   'renpy-asset-import-image-resource)
    (define-key map (kbd "C-c C-i") 'renpy-asset-import-image-resource)
    (define-key map (kbd "C-c l")   'renpy-asset-print-characters)
    (define-key map (kbd "C-c C-l") 'renpy-asset-print-characters)
    (define-key map (kbd "C-c r")   'renpy-asset-discover-image-assets)
    (define-key map (kbd "C-c C-r") 'renpy-asset-discover-image-assets)
    (define-key map (kbd "C-c a")   'renpy-asset-add-image)
    (define-key map (kbd "C-c C-a") 'renpy-asset-add-image)
    (define-key map (kbd "C-c e")   'renpy-asset-import-every-image)
    (define-key map (kbd "C-c C-e") 'renpy-asset-import-every-image)
    (define-key map (kbd "C-c f")   'renpy-asset-set-store)
    (define-key map (kbd "C-c C-f") 'renpy-asset-set-store)
    ;; Very useful features in general
    (define-key map (kbd "C-c p")   'find-file-at-point)
    (define-key map (kbd "C-c C-p") 'find-file-at-point)
    map)
  "Keymap for Renpy major mode")

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.rpy\\'" . renpy-mode))



;; Regex generated by Emacs' own optimising regex generator.
;; Original list of inputs were:
;; (regexp-opt '("at" "call" "expression" "hide" "image" "init" "jump" "label" "menu" "onlayer" "python" "scene" "set" "show" "with" "and" "assert" "break" "class" "continue" "def" "del" "elif" "else" "except" "exec" "finally" "for" "from" "global" "if" "import" "in" "is" "lambda" "not" "or" "pass" "print" "raise" "return" "try" "while" "yield" "\$" "play" "queue" "stop" "sound" "music" "fadeout" "fadein" "channel" "voice" "sustain" "nvl" "clear") t)
;; In case that's needed for later.
(defconst renpy-font-lock-keywords-1
  (list
   '("\\<\\(\\$\\|a\\(?:nd\\|\\(?:sser\\)?t\\)\\|break\\|c\\(?:all\\|hannel\\|l\\(?:ass\\|ear\\)\\|ontinue\\)\\|de[fl]\\|e\\(?:l\\(?:if\\|se\\)\\|x\\(?:cept\\|ec\\|pression\\)\\)\\|f\\(?:ade\\(?:in\\|out\\)\\|inally\\|or\\|rom\\)\\|global\\|hide\\|i\\(?:m\\(?:age\\|port\\)\\|nit\\|[fns]\\)\\|jump\\|la\\(?:bel\\|mbda\\)\\|m\\(?:enu\\|usic\\)\\|n\\(?:ot\\|vl\\)\\|o\\(?:\\(?:nlaye\\)?r\\)\\|p\\(?:ass\\|lay\\|rint\\|ython\\)\\|queue\\|r\\(?:aise\\|eturn\\)\\|s\\(?:cene\\|et\\|how\\|ound\\|top\\|ustain\\)\\|try\\|voice\\|w\\(?:hile\\|ith\\)\\|yield\\)\\>" . font-lock-builtin-face))
  "Minimal highlighting expressions for Renpy-mode")

(defvar renpy-font-lock-keywords renpy-font-lock-keywords-1
  "Default highlighting for renpy mode")

;; TODO: Create indentation rules

(defun renpy-indent-line ()
    "Does nothing right now. Will do something later")

(defvar renpy-mode-syntax-table
  (let ((st (make-syntax-table)))
    (modify-syntax-entry ?_ "w" st)
    (modify-syntax-entry ?# "<" st)
    (modify-syntax-entry ?\n ">" st)
    (modify-syntax-entry ?' "\"" st)
    st)
  "Syntax table for renpy-mode")

(define-derived-mode renpy-mode fundamental-mode "Ren'Py"
  "Major mode for editing renpy-code. Not ready for prime-time yet."
  (set (make-local-variable 'font-lock-defaults) '(renpy-font-lock-keywords))
  (set (make-local-variable 'indent-line-function) 'renpy-indent-line))

(provide 'renpy-mode)



