/
io-mode.el
389 lines (309 loc) · 11.2 KB
/
io-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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
;;; io-mode.el --- Major mode to edit Io language files in Emacs
;; Copyright (C) 2010 Sergei Lebedev
;; Version: 20100405
;; Keywords: languages, io
;; Author: Sergei Lebedev <superbobry@gmail.com>
;; URL: http://bitbucket.com/bobry/io-mode
;; This file is not part of GNU Emacs.
;; 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 2, 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, write to the Free Software
;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Commentary
;; No documentation is availible at the moment, but a nice and clean
;; README is soon to come.
;;; Installation
;; In your shell:
;; $ cd ~/.emacs.d/packges
;; $ git clone git://github.com/superbobry/io-mode.git
;; In your emacs config:
;; (add-to-list 'load-path "~/.emacs.d/packages/io-mode")
;; (require 'io-mode)
;;; Thanks
;; Major thanks to defunkt's coffee-mode, which helped me to get over most
;; of the tought parts of writing a major mode.
;;
;; Sources:
;; http://renormalist.net/Renormalist/EmacsLanguageModeCreationTutorial
;;
;;; Code:
(require 'comint)
(require 'cl)
(require 'font-lock)
(require 'hideshow)
(require 'newcomment)
;;
;; Customizable Variables
;;
(defconst io-mode-version "20100405"
"The version of this `io-mode'.")
(defgroup io nil
"A major mode for editing Io language."
:group 'languages)
(defcustom io-debug-mode nil
"Whether to run in debug mode or not. Logs to `*Messages*'."
:type 'boolean
:group 'io)
(defcustom io-cleanup-whitespace t
"Should we `delete-trailing-whitespace' on save? Probably."
:type 'boolean
:group 'io)
(defcustom io-tab-width tab-width
"The tab width to use when indenting."
:type 'integer
:group 'io)
(defcustom io-command "io"
"The Io command used for evaluating code. Must be in your path."
:type 'string
:group 'io)
(defvar io-mode-hook nil
"A hook for you to run your own code when the mode is loaded.")
(defvar io-mode-map (make-keymap)
"Keymap for Io major mode.")
;;
;; Macros
;;
(defun io-debug (string &rest args)
"Print a message when in debug mode."
(when io-debug-mode
(apply 'message (append (list string) args))))
(defmacro io-line-as-string ()
"Returns the current line as a string."
`(buffer-substring (point-at-bol) (point-at-eol)))
;;
;; REPL
;;
(defun io-normalize-sexp (str)
"Normalize a given Io code string, removing all newline characters."
;; Oddly enough, Io interpreter doesn't allow newlines anywhere,
;; including multiline strings and method calls, we need to make
;; a flat string from a code block, before it's passed to the
;; interpreter. Obviously, this isn't a good solution, since
;; a := """Cool multiline
;; string!"""
;; would become
;; a := """Cool multiline string!"""
;; ...
(replace-regexp-in-string
;; ... and finally strip the remaining newlines.
"[\r\n]+" "; " (replace-regexp-in-string
;; ... then strip multiple whitespaces ...
"\s+" " "
(replace-regexp-in-string
;; ... then remove all newline characters near brackets
;; and comas ...
"\\([(,]\\)[\n\r\s]+\\|[\n\r\s]+\\()\\)" "\\1\\2"
;; This should really be read bottom-up, start by removing
;; all comments ...
(replace-regexp-in-string io-comments-re "" str)))))
(defun io-repl ()
"Launch an Io REPL using `io-command' as an inferior mode."
(interactive)
(let ((io-repl-buffer (get-buffer "*Io*")))
(unless (comint-check-proc io-repl-buffer)
(setq io-repl-buffer
(apply 'make-comint "Io" io-command nil)))
(pop-to-buffer io-repl-buffer)))
(defun io-repl-sexp (str)
"Send the expression to an Io REPL."
(interactive "sExpression: ")
(let ((io-repl-buffer (io-repl)))
(save-current-buffer
(set-buffer io-repl-buffer)
(comint-goto-process-mark)
(insert (io-normalize-sexp str))
;; Probably ARTIFICIAL value should be made an option,
;; like `io-repl-display-sent'.
(comint-send-input))))
(defun io-repl-sregion (beg end)
"Send the region to an Io REPL."
(interactive "r")
(io-repl-sexp (buffer-substring beg end)))
(defun io-repl-sbuffer ()
"Send the content of the buffer to an Io REPL."
(interactive)
(io-repl-sregion (point-min) (point-max)))
;;
;; Define Language Syntax
;;
;; Special? Self and call objects are :)
(defvar io-special-re "\\<self\\|thisContext\\|call\\>")
;; Operators (not all of them are present in the Io core,
;; but you can still define them, if you need it)
;; a) normal
(defvar io-operators-re
(regexp-opt
'("*" "/" "%" "^" "+" "-" ">>" "++" "--"
"<<" ">" "<" "<=" ">=" "==" "!=" "&"
"^" ".." "|" "&&" "||" "!=" "+=" "-="
"*=" "/=" "<<=" ">>=" "&=" "|=" "%="
"=" ":=" "<-" "<->" "->")))
;; b) special
(defvar io-operators-special-re "@\\{1,2\\}\\|?")
;; Booleans
(defvar io-boolean-re "\\b\\(true\\|false\\|nil\\)\\b")
;; Prototypes
(defvar io-prototypes-re "\\b[A-Z]+\\w*\\b")
;; Messages
(defvar io-messages-re
(regexp-opt
'("activate" "activeCoroCount" "and" "asString"
"block" "break" "catch" "clone" "collectGarbage"
"compileString" "continue" "do" "doFile" "doMessage"
"doString" "else" "elseif" "exit" "for" "foreach"
"foreachReversed" "forward" "getSlot" "getEnvironmentVariable"
"hasSlot" "if" "ifFalse" "ifNil" "ifTrue"
"isActive" "isNil" "isResumable" "list"
"message" "method" "or" "parent" "pass" "pause"
"perform" "performOn" "performWithArgList" "print"
"println" "proto" "raise" "raiseResumable" "removeSlot"
"resend" "resume" "return" "schedulerSleepSeconds"
"sender" "setSchedulerSleepSeconds" "setSlot"
"shallowCopy" "slotNames" "super" "system"
"then" "thisBlock" "thisMessage" "try" "type"
"uniqueId" "updateSlot" "wait" "while" "write"
"writeln" "yield")
'words))
;; Comments
(defvar io-comments-re "\\(\\(#\\|//\\).*$\\|/\\*\\(.\\|[\r\n]\\)*?\\*/\\)")
;; Create the list for font-lock. Each class of keyword is given a
;; particular face.
(defvar io-font-lock-keywords
;; Note: order here matters!
`((,io-special-re . font-lock-variable-name-face)
(,io-operators-re . font-lock-builtin-face)
(,io-operators-special-re . font-lock-warning-face)
(,io-boolean-re . font-lock-constant-face)
(,io-prototypes-re . font-lock-type-face)
(,io-messages-re . font-lock-keyword-face)
(,io-comments-re . font-lock-comment-face)))
;;
;; Helper Functions
;;
(defun io-before-save ()
"Hook run before file is saved. Deletes whitespace if `io-cleanup-whitespace' is non-nil."
(when io-cleanup-whitespace
(delete-trailing-whitespace)))
;;
;; Indentation
;;
(defun io-indent-line ()
"Indent current line as Io source."
(interactive)
(if (= (point) (point-at-bol))
(insert-tab)
(save-excursion
(let ((prev-indent 0) (cur-indent 0))
;; Figure out the indentation of the previous
;; and current lines.
(setq prev-indent (io-previous-indent)
cur-indent (current-indentation))
;; Shift one column to the left.
(beginning-of-line)
(insert-tab)
(when (= (point-at-bol) (point))
(forward-char io-tab-width))
;; We're too far, remove all indentation.
(when (> (- (current-indentation) prev-indent) io-tab-width)
(backward-to-indentation 0)
(delete-region (point-at-bol) (point)))))))
(defun io-previous-indent ()
"Returns the indentation level of the previous non-blank line."
(save-excursion
(forward-line -1)
(if (bobp)
0
(progn
(while (io-line-empty-p) (forward-line -1))
(current-indentation)))))
(defun io-line-empty-p ()
"Is this line empty? Returns non-nil if so, nil if not."
(or (bobp)
(string-match "^\\ s*$" (io-line-as-string))))
(defun io-newline-and-indent ()
"Inserts a newline and indents it to the same level as the previous line."
(interactive)
;; Remember the current line indentation level,
;; insert a newline, and indent the newline to the same
;; level as the previous line.
(let ((prev-indent (current-indentation)) (indent-next nil))
(newline)
(insert-tab (/ prev-indent io-tab-width)))
;; Last line was a comment so this one should probably be,
;; too. Makes it easy to write multi-line comments (like the
;; one I'm writing right now).
(when (io-previous-line-is-comment)
;; Using `match-string' is probably not obvious, but current
;; implementation of `io-previous-is-comment' is using `looking-at',
;; which modifies match-data variables.
(insert (match-string 0))))
;;
;; Comments
;;
(defun io-previous-line-is-comment ()
"Returns `t' if previous line is a comment."
(save-excursion
(forward-line -1)
(io-line-is-comment)))
(defun io-line-is-comment ()
"Returns `t' if current line is a comment."
(save-excursion
(backward-to-indentation 0)
;; No support for multi line comments yet.
(looking-at "\\(#\\|//\\)+\s*")))
;;
;; Define Major Mode
;;
;;;###autoload
(define-derived-mode io-mode fundamental-mode
"Io"
"Major mode for editing Io language..."
(define-key io-mode-map (kbd "C-m") 'io-newline-and-indent)
(define-key io-mode-map (kbd "C-c <SPC>") 'io-repl)
(define-key io-mode-map (kbd "C-c C-c") 'io-repl-sbuffer)
(define-key io-mode-map (kbd "C-c C-r") 'io-repl-sregion)
(define-key io-mode-map (kbd "C-c C-e") 'io-repl-sexp)
(define-key io-mode-map (kbd "C-c ;") 'comment-dwim)
;; code for syntax highlighting
(set (make-local-variable 'font-lock-defaults) '((io-font-lock-keywords)))
;; comments
;; a) python style
(modify-syntax-entry ?# "< b" io-mode-syntax-table)
(modify-syntax-entry ?\n "> b" io-mode-syntax-table)
;; b) c style
(modify-syntax-entry ?/ ". 124b" io-mode-syntax-table)
(modify-syntax-entry ?* ". 23" io-mode-syntax-table)
(modify-syntax-entry ?\n "> b" io-mode-syntax-table)
(setq comment-start "# "
comment-start-skip "# *"
comment-end ""
comment-column 40
comment-style 'indent)
;; strings
(modify-syntax-entry ?\' "\"" io-mode-syntax-table)
(modify-syntax-entry ?\" "\"" io-mode-syntax-table)
;; indentation
(make-local-variable 'indent-line-function)
(setq indent-line-function 'io-indent-line
io-tab-width tab-width ;; just in case...
indent-tabs-mode nil) ;; tabs are evil..
;; hideshow
(unless (assq 'io-mode hs-special-modes-alist)
(add-to-list 'hs-special-modes-alist
'(io-mode "(" ")" "\\(?:#\\|/[*/]\\)")))
;; hooks
(set (make-local-variable 'before-save-hook) 'io-before-save))
;;
;; On Load
;;
;; Run io-mode for files ending in .io.
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.io$" . io-mode))
(provide 'io-mode)