├── .gitignore
├── LICENSE
├── README.md
├── moonscript-repl.el
└── moonscript.el
/.gitignore:
--------------------------------------------------------------------------------
1 | *.elc
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2013 K-2052
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # moonscript-mode
2 |
3 | ## Overview
4 |
5 | A basic major mode for editing [MoonScript](http://moonscript.org/), a
6 | preprocessed language for [Lua](https://www.lua.org/) which shares
7 | many similarities with [CoffeeScript](http://coffeescript.org/).
8 |
9 | Also includes a very basic major mode for the experimental
10 | [MoonScript REPL](https://github.com/leafo/moonscript/wiki/Moonscriptrepl).
11 |
12 | ## History
13 |
14 | This mode started out as a modification of stuff found in
15 | [@GriffinSchneider's Emacs configs](https://github.com/GriffinSchneider/emacs-config).
16 |
17 | ## Installation
18 |
19 | ### From MELPA
20 |
21 | The easiest way to get the mode is to install the `moonscript` package from the [MELPA](https://melpa.org/) package archive.
22 |
23 | ### Manual
24 |
25 | $ cd ~/.emacs.d/vendor
26 | $ git clone https://github.com/k2052/moonscript-mode.git
27 |
28 | And add following to your .emacs file:
29 |
30 | (add-to-list 'load-path "~/.emacs.d/vendor/moonscript-mode")
31 | (require 'moonscript-mode)
32 | (require 'moonscriptrepl-mode)
33 |
34 | ## Usage
35 |
36 | ### With a REPL
37 |
38 | If you load up a moonscript REPL (see https://github.com/leafo/moonscript/wiki/Moonscriptrepl) you can
39 | then hit:
40 |
41 | M-X `moonscriptrepl-mode`
42 |
43 | to activate.
44 |
45 | Improvements and more docs will come as we need them.
46 |
47 | ### Support
48 |
49 | If you found this repo useful please consider supporting me on [Gittip](https://www.gittip.com/k2052) or sending me some
50 | bitcoin `1csGsaDCFLRPPqugYjX93PEzaStuqXVMu`
51 |
--------------------------------------------------------------------------------
/moonscript-repl.el:
--------------------------------------------------------------------------------
1 | ;;; moonscript-repl.el --- Major mode to interact with MoonScript REPL
2 | ;;
3 | ;; Author: @GriffinSchneider, @k2052, @EmacsFodder
4 | ;; Version: 20140803-0.1.0
5 | ;;
6 | ;;; Commentary:
7 | ;;
8 | ;; A basic major mode for MoonScript REPL
9 | ;;
10 | ;;; License: MIT Licence
11 | ;;
12 | ;;; Code:
13 |
14 | (require 'moonscript)
15 |
16 | (define-derived-mode moonscript-repl-mode comint-mode "MoonScript REPL"
17 | "Major mode to interact with a MoonScript REPL.
18 |
19 | See https://github.com/leafo/moonscript/wiki/Moonscriptrepl"
20 | (set-syntax-table moonscript-mode-syntax-table)
21 | (setq font-lock-defaults '(moonscript-font-lock-defaults)))
22 |
23 | (provide 'moonscript-repl)
24 |
25 | ;;; moonscript-repl.el ends here
26 |
--------------------------------------------------------------------------------
/moonscript.el:
--------------------------------------------------------------------------------
1 | ;;; moonscript.el --- Major mode for editing MoonScript code
2 | ;;;
3 | ;; Author: @GriffinSchneider, @k2052, @EmacsFodder
4 | ;; Version: 20140803-0.1.0
5 | ;; Package-Requires: ((cl-lib "0.5") (emacs "24"))
6 | ;;; Commentary:
7 | ;;
8 | ;; A basic major mode for editing MoonScript, a preprocessed language
9 | ;; for Lua which shares many similarities with CoffeeScript.
10 | ;;
11 | ;;; License: MIT Licence
12 | ;;
13 | ;;; Code:
14 |
15 | (require 'cl-lib)
16 |
17 | (defgroup moonscript nil
18 | "MoonScript (for Lua) language support for Emacs."
19 | :tag "MoonScript"
20 | :group 'languages)
21 |
22 | (defcustom moonscript-indent-offset 2
23 | "How many spaces to indent MoonScript code per level of nesting."
24 | :group 'moonscript
25 | :type 'integer
26 | :safe 'integerp)
27 |
28 | (defcustom moonscript-comment-start "-- "
29 | "Default value of `comment-start'."
30 | :group 'moonscript
31 | :type 'string
32 | :safe 'stringp)
33 |
34 | (defvar moonscript-statement
35 | '("return" "break" "continue"))
36 |
37 | (defvar moonscript-repeat
38 | '("for" "while"))
39 |
40 | (defvar moonscript-conditional
41 | '("if" "else" "elseif" "then" "switch" "when" "unless"))
42 |
43 | (defvar moonscript-keyword
44 | '("export" "local" "import" "from" "with" "in" "and" "or" "not"
45 | "class" "extends" "super" "using" "do"))
46 |
47 | (defvar moonscript-keywords
48 | (append moonscript-statement moonscript-repeat moonscript-conditional moonscript-keyword))
49 |
50 | (defvar moonscript-constants
51 | '("nil" "true" "false" "self"))
52 |
53 | (defvar moonscript-keywords-regex (regexp-opt moonscript-keywords 'symbols))
54 |
55 | (defvar moonscript-constants-regex (regexp-opt moonscript-constants 'symbols))
56 |
57 | (defvar moonscript-class-name-regex "\\<[A-Z]\\w*\\>")
58 |
59 | (defvar moonscript-function-keywords
60 | '("->" "=>" "(" ")" "[" "]" "{" "}"))
61 | (defvar moonscript-function-regex (regexp-opt moonscript-function-keywords))
62 |
63 | (defvar moonscript-octal-number-regex
64 | "\\_<0x[[:xdigit:]]+\\_>")
65 |
66 | (defvar moonscript-table-key-regex
67 | "\\_<\\w+:")
68 |
69 | (defvar moonscript-ivar-regex
70 | "@\\_<\\w+\\_>")
71 |
72 | (defvar moonscript-assignment-regex
73 | "\\([-+/*%]\\|\\.\\.\\)?=")
74 |
75 | (defvar moonscript-number-regex
76 | (mapconcat 'identity '("[0-9]+\\.[0-9]*" "[0-9]*\\.[0-9]+" "[0-9]+") "\\|"))
77 |
78 | (defvar moonscript-assignment-var-regex
79 | (concat "\\(\\_<\\w+\\) = "))
80 |
81 | (defvar moonscript-font-lock-defaults
82 | `((,moonscript-class-name-regex . font-lock-type-face)
83 | (,moonscript-function-regex . font-lock-function-name-face)
84 | (,moonscript-assignment-regex . font-lock-preprocessor-face)
85 | (,moonscript-constants-regex . font-lock-constant-face)
86 | (,moonscript-keywords-regex . font-lock-keyword-face)
87 | (,moonscript-ivar-regex . font-lock-variable-name-face)
88 | (,moonscript-assignment-var-regex . (1 font-lock-variable-name-face))
89 | (,moonscript-octal-number-regex . font-lock-constant-face)
90 | (,moonscript-number-regex . font-lock-constant-face)
91 | (,moonscript-table-key-regex . font-lock-variable-name-face)
92 | ("!" . font-lock-warning-face)))
93 |
94 | (defun moonscript-indent-level (&optional blankval)
95 | "Return nesting depth of current line.
96 |
97 | If BLANKVAL is non-nil, return that instead if the line is blank.
98 | Upon return, regexp match data is set to the leading whitespace."
99 | (cl-assert (= (point) (point-at-bol)))
100 | (looking-at "^[ \t]*")
101 | (if (and blankval (= (match-end 0) (point-at-eol)))
102 | blankval
103 | (floor (/ (- (match-end 0) (match-beginning 0))
104 | moonscript-indent-offset))))
105 |
106 | (defun moonscript-indent-line ()
107 | "Cycle indentation levels for the current line of MoonScript code.
108 |
109 | Looks at how deeply the previous non-blank line is nested. The
110 | maximum indentation level for the current line is that level plus
111 | one.
112 |
113 | When computing indentation depth, one tab is currently considered
114 | equal to one space. Tabs are currently replaced with spaces when
115 | re-indenting a line."
116 | (goto-char (point-at-bol))
117 | (let ((curlinestart (point))
118 | (prevlineindent -1))
119 | ;; Find indent level of previous non-blank line.
120 | (while (and (< prevlineindent 0) (> (point) (point-min)))
121 | (goto-char (1- (point)))
122 | (goto-char (point-at-bol))
123 | (setq prevlineindent (moonscript-indent-level -1)))
124 | ;; Re-indent current line based on what we know.
125 | (goto-char curlinestart)
126 | (let* ((oldindent (moonscript-indent-level))
127 | (newindent (if (= oldindent 0) (1+ prevlineindent)
128 | (1- oldindent))))
129 | (replace-match (make-string (* newindent moonscript-indent-offset)
130 | ? )))))
131 |
132 | ;;;###autoload
133 | (define-derived-mode moonscript-mode prog-mode "MoonScript"
134 | "Major mode for editing MoonScript code."
135 | (setq font-lock-defaults '(moonscript-font-lock-defaults))
136 | (set (make-local-variable 'indent-line-function) 'moonscript-indent-line)
137 | (set (make-local-variable 'electric-indent-inhibit) t)
138 | (set (make-local-variable 'comment-start) moonscript-comment-start)
139 | (modify-syntax-entry ?\- ". 12b" moonscript-mode-syntax-table)
140 | (modify-syntax-entry ?\n "> b" moonscript-mode-syntax-table)
141 | (modify-syntax-entry ?\_ "w" moonscript-mode-syntax-table))
142 |
143 | ;;;###autoload
144 | (add-to-list 'auto-mode-alist '("\\.moon\\'" . moonscript-mode))
145 |
146 | (provide 'moonscript)
147 |
148 | ;;; moonscript.el ends here
149 |
--------------------------------------------------------------------------------