forked from rasensuihei/mcf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcf-mode.el
168 lines (143 loc) · 5.3 KB
/
mcf-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
;;; mcf-mode.el --- Major mode for editing Minecraft mcfunction -*- lexical-binding: t -*-
;; Copyright (C) 2019 rasensuihei
;; Author: rasensuihei <[email protected]>
;; URL: https://github.com/rasensuihei/mcf-mode
;; Version: 0.2.2
;; Keywords: languages
;; Package-Requires: ((emacs "24.3"))
;; 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; The main features of this mode are Minecraft mcfunction syntax
;; highlighting.
;; Settings example
;; (require 'mcf-mode)
;; (setq mcf-rcon-password "PASSWORD")
;; Default keybindings:
;; C-c C-c mcf-execute
;; C-c C-e mcf-execute-at-point
;; Usage
;; `M-x mcf-rcon` to connect to Minecraft RCON server.
;; `M-x mcf-rcon-disconnect` to disconnect from Minecraft RCON server.
;; See also:
;; https://github.com/rasensuihei/mcf-mode
;;; Code:
(require 'font-lock)
(require 'mcf-rcon)
(defgroup mcf nil
"Major mode for editing minecraft mcfunction."
:group 'languages)
(defface mcf-syntax-warning
'((((class color) (background light)) (:background "IndianRed1"))
(((class color) (background dark)) (:background "firebrick4")))
"Illegal space face"
:group 'mcf)
(defvar mcf-syntax-warning 'mcf-syntax-warning)
;; See https://www.emacswiki.org/emacs/FontLockKeywords for how to define
(defvar mcf--font-lock-keywords)
(setq mcf--font-lock-keywords
(list
;; Execute
'("\\<\\(execute\\)\\>"
(1 font-lock-keyword-face))
;; Command
'("\\(^\\|run \\)\\([a-z]+\\)\\>"
(1 font-lock-keyword-face)
(2 font-lock-builtin-face))
'("\\(function\\) \\([a-z0-9_.+-:]+\\)\\>"
(1 font-lock-keyword-face)
(2 font-lock-variable-name-face))
;; Syntax warning
'("\\( \s+\\|^\s+\\|\s+$\\|@[aeprs]\s+\\[\\)"
(1 mcf-syntax-warning))
;; Selector variable
'("\\(@[aeprs]\\)"
(1 font-lock-variable-name-face))
'("\\(@[aeprs]\\)\\(\\[.*?\\]\\)\\s-"
(1 font-lock-variable-name-face t)
(2 font-lock-type-face t))
;; Selector arguments
'("\\([a-zA-Z][a-zA-Z0-9_.+-]+\\)\s*="
(1 font-lock-builtin-face t))
'("\\([,=:]\\)"
(1 font-lock-builtin-face t))
;; Numbers
'("\\<\\([0-9]+\\(\\.[0-9]+\\)?\\)[bsilfd]?\\>"
(1 font-lock-constant-face t))
'("\\([\\^\\~]\\)\\([0-9]\\|\\s-\\)" (1 font-lock-builtin-face t))
'("\\.\\." (0 font-lock-builtin-face t))
;; Negation char
'("=\\(!\\)"
(1 font-lock-negation-char-face t))
;; String
'("\"\\(\\\\.\\|[^\"]\\)*\""
(0 font-lock-string-face t))
;; Line comment
'("^\\(#.*\\)$"
(1 font-lock-comment-face t))
))
(defvar mcf-mode-prefix-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c" 'mcf-execute)
(define-key map "\C-e" 'mcf-execute-at-point)
(define-key map "\C-r" 'mcf-reload)
map))
(defvar mcf-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c" mcf-mode-prefix-map)
map))
(defvar mcf-mode-hook nil
"This hook is run when mcf-mode starts.")
(defvar mcf-mode-syntax-table
(let ((table (make-syntax-table)))
;; (modify-syntax-entry ?# "<" table)
;; (modify-syntax-entry ?\r ">" table)
;; (modify-syntax-entry ?\n ">" table)
table))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.mcfunction\\'" . mcf-mode))
;;;###autoload
(define-derived-mode mcf-mode prog-mode "Minecraft-Function"
"Set major mode for editing Minecraft mcfunction file."
:group 'mcf
(setq-local font-lock-defaults
(list mcf--font-lock-keywords nil nil nil nil))
(setq-local comment-start "#")
(setq-local comment-end ""))
(defun mcf-execute (str)
"Execute Minecraft command STR and display the result in the minibuffer."
(interactive "MCommand: ")
;; (mcf-rcon-execute str handler)
(mcf-rcon-execute str (lambda (payload)
(if (eq payload "")
(message "Minecraft: <empty>")
(message "Minecraft: %s" payload)))))
(defun mcf-execute-at-point ()
"Execute a command at point."
(interactive)
(let ((line (thing-at-point 'line t)))
(when (string-match "^[# ]*\\(.+\\)$" line)
(mcf-execute (match-string 1 line)))))
(defun mcf-reload ()
"Execute a reload command."
(interactive)
(mcf-execute "reload"))
(defmacro mcf-eval (command &rest args)
"(mcf-eval COMMAND ARGS...)
Evaluate minecraft command with RCON server. A First argument must be payload variable name. (mcf-eval \"COMMAND\" (PAYLOAD) BODY...)"
(declare (indent defun) (debug args))
(if args
`(mcf-rcon-execute ,command (lambda (x) (let ((,(caar args) x)) ,@(cdr args))))
`(mcf-rcon-execute ,command)))
(provide 'mcf-mode)
;;; mcf-mode.el ends here