-
Notifications
You must be signed in to change notification settings - Fork 2
/
blitzmax-mode.el
executable file
·760 lines (609 loc) · 30 KB
/
blitzmax-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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
;;; blitzmax-mode.el --- A major mode for editing BlitzMax source code -*- lexical-binding: t; -*-
;; Copyright (C) 2012-2020 Phil Newton
;; Version: 1.0.0
;; Keywords: languages blitzmax
;; Author: Phil Newton
;; URL: https://www.sodaware.net/dev/tools/blitzmax-mode/
;; Package-Requires: ((emacs "24.1"))
;; This file is NOT part of GNU Emacs.
;; This file 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 file 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 Emacs; see the file COPYING, or type `C-h C-c'. If not,
;; write to the Free Software Foundation at this address:
;; Free Software Foundation
;; 51 Franklin Street, Fifth Floor
;; Boston, MA 02110-1301
;; USA
;;; Commentary:
;; This is a major mode for editing BlitzMax files. It supports syntax
;; highlighting, keyword capitalization, and automatic indentation.
;; blitzmax-mode provides the following configuration options:
;; blitzmax-mode-indent
;; The number of spaces to indent by. By default blitzmax-mode indents by 4
;; spaces which is converted to a single tab.
;; blitzmax-mode-capitalize-keywords-p
;; Disable automatic capitalization of keywords by setting this to =nil=. =t=
;; by default.
;; blitzmax-mode-smart-indent-p
;; Disable smart indentation by setting this to =nil=. =t= by default.
;; blitzmax-mode-complete-pairs-p
;; Enable automatic insertion of matching keyword pairs - such as "End Method"
;; - by setting this to =t=. =nil= by default.
;; blitzmax-mode-compiler-pathname
;; Full pathname to the BlitzMax compiler bmk. "bmk" by default.
;; To enable quickrun integration, add the following code to your Emacs
;; initialization file.
;; (with-eval-after-load 'quickrun
;; (blitzmax-mode-quickrun-integration))
;;; Code:
;; --------------------------------------------------
;; -- Configuration
(defgroup blitzmax nil
"Major mode for editing BlitzMax source files."
:group 'languages
:prefix "blitzmax-")
(defcustom blitzmax-mode-indent 4
"Default indentation per nesting level."
:type 'integer
:group 'blitzmax)
(defcustom blitzmax-mode-smart-indent-p t
"Whether to use smart-indentation."
:type 'boolean
:group 'blitzmax)
(defcustom blitzmax-mode-complete-pairs-p nil
"Whether to auto-complete pairs like `Method...End Method'."
:type 'boolean
:group 'blitzmax)
(defcustom blitzmax-mode-capitalize-keywords-p t
"Whether to automatically capitalize keywords."
:type 'boolean
:group 'blitzmax)
(defcustom blitzmax-mode-compiler-pathname "bmk"
"The full pathname of the BlitzMax compiler (e.g. /usr/bin/bmk)."
:type 'string
:group 'blitzmax)
(defcustom blitzmax-mode-hook nil
"Hook run when entering blitzmax mode."
:type 'hook
:group 'hook)
;; --------------------------------------------------
;; -- Local Variables
(defvar blitzmax-mode-abbrev-table nil)
(defvar blitzmax-mode-font-lock-keywords)
;; --------------------------------------------------
;; -- Regex for indentation & completion
(defconst blitzmax-mode-type-start-regexp
(concat
"^[ \t]*[Tt]ype"
"[ \t]+_?\\(\\w+\\)[ \t]*?"))
(defconst blitzmax-mode-type-end-regexp
"^[ \t]*[Ee]nd[ ]*[Tt]ype")
(defconst blitzmax-mode-defun-start-regexp
(concat
"^[\t ]*\\([Mm]ethod\\|[Ff]unction\\)"
"[ \t ]+_?\\(\\w+\\)[ \t ]*(?"))
(defconst blitzmax-mode-defun-end-regexp
"^[ \t]*[Ee]nd[ \t]*\\([Mm]ethod\\|[Ff]unction\\)")
(defconst blitzmax-mode-function-start-regexp
(concat
"^[\t ]*[Ff]unction"
"[ \t ]+_?\\(\\w+\\)[ \t ]*(?"))
(defconst blitzmax-mode-function-end-regexp
"^[ \t]*[Ee]nd[ \t]*[Ff]unction")
(defconst blitzmax-mode-method-start-regexp
(concat
"^[\t ]*[Mm]ethod"
"[ \t ]+_?\\(\\w+\\)[ \t ]*(?"))
(defconst blitzmax-mode-method-end-regexp
"^[ \t]*[Ee]nd[ \t]*[Mm]ethod")
(defconst blitzmax-mode-abstract-defun-regexp
(concat
"^[\t ]*\\([Mm]ethod\\|[Ff]unction\\)"
"[ \t ]+\\(\\w+\\)[ \t ]*(?"
".*[ \t]+[Aa]bstract"))
(defconst blitzmax-mode-extern-start-regexp
"^[\t ]*[Ee]xtern")
(defconst blitzmax-mode-extern-end-regexp
"^[ \t]*[Ee]nd[ ]*[Ee]xtern")
(defconst blitzmax-mode-if-regexp "^[ \t]*[Ii]f")
(defconst blitzmax-mode-if-oneline-regexp "^[ \t]*[Ii]f.*[Tt]hen[\\ t:]*[[:alnum:]]+[^']+?")
(defconst blitzmax-mode-else-regexp "^[ \t]*[Ee]lse\\([Ii]f\\)?")
(defconst blitzmax-mode-endif-regexp "[ \t]*[Ee]nd[ \t]*[Ii]f")
(defconst blitzmax-mode-continuation-regexp "[ \t]*\\.\\.")
(defconst blitzmax-mode-label-regexp "^[ \t]*#[a-zA-Z0-9_]+$")
(defconst blitzmax-mode-select-regexp "^[ \t]*[Ss]elect")
(defconst blitzmax-mode-case-regexp "^[ \t]*\\([Cc]ase\\|[Dd]efault\\)")
(defconst blitzmax-mode-select-end-regexp "^[ \t]*[Ee]nd[ \t]*[Ss]elect")
(defconst blitzmax-mode-for-regexp "^[ \t]*[Ff]or[ \t]+[[:alnum:]]+")
(defconst blitzmax-mode-next-regexp "^[ \t]*[Nn]ext")
(defconst blitzmax-mode-while-regexp "^[ \t]*[Ww]hile")
(defconst blitzmax-mode-wend-regexp "^[ \t]*[Ww]end")
(defconst blitzmax-mode-repeat-regexp "^[ \t]*[Rr]epeat")
(defconst blitzmax-mode-until-regexp "^[ \t]*\\([Ff]orever\\|[Uu]ntil\\)")
(defconst blitzmax-mode-try-regexp "^[ \t]*[Tt]ry")
(defconst blitzmax-mode-catch-regexp "^[ \t]*[Cc]atch")
(defconst blitzmax-mode-try-end-regexp "^[ \t]*[Ee]nd[ \t]*[Tt]ry")
(defconst blitzmax-mode-blank-regexp "^[ \t]*$")
(defconst blitzmax-mode-comment-regexp "^[ \t]*\\s<.*$")
;; --------------------------------------------------
;; -- Keywords
(defconst blitzmax-mode-all-keywords
'("CreateStaticAudioSample" "TStreamWriteException" "TStreamReadException"
"bglFixedFontBitmaps" "CountGraphicsModes" "CreateSocketStream"
"CreateStaticPixmap" "GraphicsModeExists" "LittleEndianStream"
"TAudioSampleLoader" "bglSetMouseVisible" "bglSetSwapInterval"
"CreateAudioSample" "CreateGNetMessage" "GNetMessageObject" "GNetPeerTCPSocket"
"GNetPeerUDPSocket" "GNetTotalBytesOut" "SetGraphicsDriver" "CreateBankStream"
"CreateGNetObject" "CreateStaticBank" "GNetObjectRemote" "GNetTotalBytesIn"
"SetChannelVolume" "SocketRemotePort" "StandardIOStream" "TStreamException"
"bglAdjustTexSize" "bglCreateContext" "bglDeleteContext" "bglTexFromPixmap"
"HandleFromObject" "HandleToObject"
"BigEndianStream" "CloseGNetObject" "CreateRamStream" "CreateTCPSocket"
"CreateUDPSocket" "D3D7Max2DDriver" "GNetObjectLocal" "GNetObjectState"
"GetGraphicsMode" "LoadAudioSample" "ResetCollisions" "SendGNetMessage"
"SetChannelDepth" "SocketConnected" "SocketLocalPort" "SocketReadAvail"
"bglDisplayModes" "AutoImageFlags" "ChannelPlaying" "CreateGNetHost"
"GraphicsHeight" "ImagesCollide2" "MidHandleImage" "PixmapPixelPtr"
"SetChannelRate" "SetImageHandle" "SocketRemoteIP" "TStreamFactory"
"TStreamWrapper" "bglSwapBuffers" "AutoMidHandle" "CasedFileName"
"CloseGNetHost" "CloseGNetPeer" "ConnectSocket" "ConvertPixmap" "DrawImageRect"
"GLMax2DDriver" "GetGNetString" "GetGNetTarget" "GraphicsWidth" "ImagesCollide"
"JoyButtonCaps" "ListFromArray" "LoadAnimImage" "LoadByteArray" "LoadImageFont"
"LoadPixmapPNG" "ResumeChannel" "SaveByteArray" "SavePixmapPNG" "SetChannelPan"
"SetGNetString" "SetGNetTarget" "SocketLocalIP" "TPixmapLoader" "AllocChannel"
"BankCapacity" "CollideImage" "CreatePixmap" "GNetMessages" "GetGNetFloat"
"GetImageFont" "GetLineWidth" "GetMaskColor" "ListAddFirst" "ListContains"
"ListFindLink" "PauseChannel" "PixmapFormat" "PixmapHeight" "PixmapWindow"
"ResizePixmap" "RuntimeError" "SetGNetFloat" "SetImageFont"
"SetLineWidth" "SetMaskColor" "SetTransform" "SocketAccept" "SocketListen"
"TAudioSample" "CloseSocket" "CloseStream" "CollideRect" "CreateImage"
"CreateTimer" "CurrentDate" "CurrentTime" "EndFunction" "EndGraphics"
"FlushStream" "GNetConnect" "GNetObjects" "GetClsColor" "GetGraphics"
"GetRotation" "GetViewport" "ImageHeight" "JoyAxisCaps" "ListAddLast"
"ListIsEmpty" "ListToArray" "PixmapPitch" "PixmapWidth" "RequestFile"
"RestoreData" "ReverseList" "SetClsColor" "SetFileMode" "SetRotation"
"SetViewport" "StopChannel" "SuperStrict" "TBankStream" "UnlockImage"
"WriteDouble" "WriteStderr" "WriteStdout" "WriteStream" "WriteString"
"XFlipPixmap" "bglDrawText" "BindSocket" "CopyPixmap" "CopyStream" "CreateBank"
"CreateFile" "CreateList" "CurrentDir" "DeleteFile" "DrawPixmap" "ExtractDir"
"ExtractExt" "GNetAccept" "GNetListen" "GetGNetInt" "GrabPixmap" "ImageWidth"
"ListRemove" "LoadPixmap" "LoadString" "MaskPixmap" "MemAlloced" "ModuleInfo"
"OpenStream" "PeekDouble" "PokeDouble" "PollSystem" "ReadDouble" "ReadStream"
"ReadString" "RemoveLink" "RenameFile" "RequestDir" "ResizeBank" "SaveString"
"SeekStream" "SetGNetInt" "StreamSize" "StripSlash" "TextHeight" "WaitSystem"
"WriteFloat" "WritePixel" "WriteShort" "uncompress" "ChangeDir" "ClearList"
"CloseFile" "CopyBytes" "CountList" "CreateDir" "DebugStop" "DeleteDir"
"DrawImage" "EndExtern" "EndMethod" "EndSelect" "FlushKeys" "Framework"
"GCSetMode" "GCSuspend" "GCResume" "GCCollect" "GCMemAlloced" "GCEnter" "GCLeave"
"GNetPeers" "GetHandle" "GetOrigin" "GrabImage" "HideMouse" "IncbinLen"
"IncbinPtr" "LaunchDir" "LoadImage" "LoadSound" "LockImage" "MemExtend"
"MilliSecs" "MouseDown" "MoveMouse" "PeekFloat" "PeekShort" "PlaySound"
"PokeFloat" "PokeShort" "ReadFloat" "ReadPixel" "ReadShort" "ReadStdin"
"RndDouble" "SetHandle" "SetOrigin" "ShowMouse" "StreamPos" "SwapLists"
"TListEnum" "TextWidth" "TileImage" "WaitMouse" "WaitTimer" "WriteBank"
"WriteByte" "WriteFile" "WriteLine" "WriteLong" "compress2" "Abstract"
"AppTitle" "BankSize" "CloseDir" "Continue" "CopyBank" "CueSound" "DebugLog"
"DottedIP" "DrawLine" "DrawOval" "DrawPoly" "DrawRect" "DrawText" "FileMode"
"FileSize" "FileTime" "FileType" "FlushMem" "Function" "GCMalloc"
"GNetSync" "GetAlpha" "GetBlend" "GetColor" "GetScale" "Graphics" "HostName"
"JoyCount" "JoyPitch" "JoyWheel" "LoadBank" "MemAlloc" "MemClear" "MemUsage"
"MouseHit" "NextFile" "OpenFile" "PeekByte" "PeekLong" "PokeByte" "PokeLong"
"ReadBank" "ReadByte" "ReadData" "ReadFile" "ReadLine" "ReadLong" "RealPath"
"RndFloat" "SaveBank" "SetAlpha" "SetBlend" "SetColor" "SetScale" "SortList"
"StripAll" "StripDir" "StripExt" "TCStream" "TChannel" "WaitChar" "WriteInt"
"compress" "AppArgs" "AppFile" "BankBuf" "Confirm" "DefData" "Default" "EndType"
"Extends" "Forever" "GetChar" "HostIps" "Include" "JoyDown" "JoyName" "JoyRoll"
"KeyDown" "LoadDir" "LongBin" "LongHex" "MemCopy" "MemFree" "MemMove" "PeekInt"
"PokeInt" "Private" "Proceed" "ReadDir" "Readint" "Release" "Replace" "Restore"
"RndSeed" "SeedRnd" "TPixmap" "TStream" "WaitKey" "AppDir" "Assert" "Before"
"Delete" "EachIn" "ElseIf" "EndRem" "EndTry" "Extern" "Global"
"HostIp" "Import" "Incbin" "Insert" "JoyHat" "JoyYaw" "KeyHit" "Method" "Module"
"MouseX" "MouseY" "MouseZ" "Notify" "Public" "Repeat" "Return" "Select"
"SizeOf" "Strict" "TSound" "Varptr" "ATan2" "After" "Catch" "Const"
"Delay" "EndIf" "Field" "Final" "First" "Floor" "Gosub"
"Input" "Instr" "Local" "Log10" "Lower" "OnEnd" "Print" "Right" "Super"
"TBank" "TLink" "TList" "Throw" "Until" "Upper" "While" "ACos" "ASin" "ATan"
"Case" "Ceil" "Cosh" "Data" "Each" "Else" "Exit" "Flip" "Goto" "JoyR" "JoyU"
"JoyV" "JoyX" "JoyY" "JoyZ" "LSet" "Last" "Left" "Next" "Plot"
"RSet" "Rand" "Read" "Self" "Sinh" "Step" "Tanh" "Then" "Trim" "Type"
"Wend" "Abs" "And" "Asc" "Bin" "Chr" "Cls" "Cos" "Dim" "End" "Eof" "Exp" "For"
"Hex" "Len" "Log" "Max" "Mid" "Min" "Mod" "New" "Not" "Ptr" "Rem" "Rnd"
"Sar" "Sgn" "Shl" "Shr" "Sin" "Sqr" "Str" "TIO" "Tan" "Try" "Var" "Xor" "YFl"
"If" "Or" "Pi" "To"))
(defconst blitzmax-mode-type-keywords
'("Int" "String" "Float" "Object" "Short" "Byte" "Long" "Double"))
(defconst blitzmax-mode-constant-keywords
'("True" "False" "Null"))
;; --------------------------------------------------
;; -- Automatic capitalization
(defun blitzmax-mode--abbrev-keyword-lookup ()
"Fetch a keyword lookup for all keywords, types and constants.
Returns a list of abbrev pairs. The first member of the pair is
a lowercase word, the second is the correctly-capitalized word."
(let ((all-keywords (append blitzmax-mode-all-keywords
blitzmax-mode-type-keywords
blitzmax-mode-constant-keywords)))
(mapcar (lambda (word)
(list (downcase word) word))
all-keywords)))
(defun blitzmax-mode--create-abbrev-table ()
"Create a new abbreviation table for automatic capitalization.
Returns `t` if the table was created, `nil` if it already exists."
(unless blitzmax-mode-abbrev-table
(define-abbrev-table
'blitzmax-mode-abbrev-table
(blitzmax-mode--abbrev-keyword-lookup))
t))
(blitzmax-mode--create-abbrev-table)
(defun blitzmax-mode--in-code-context-p ()
"Check if point is in code.
Returns `t` if in code, `nil` if in a comment or string."
(if (fboundp 'buffer-syntactic-context)
(null (buffer-syntactic-context))
(let* ((beg (save-excursion
(beginning-of-line)
(point)))
(list
(parse-partial-sexp beg (point))))
(and (null (nth 3 list)) ;; Is inside string.
(null (nth 4 list)))))) ;; Is inside comment.
(defun blitzmax-mode--capitalize-keywords ()
"Automatically capitalize keywords if in a code context."
(setq local-abbrev-table
(when (blitzmax-mode--in-code-context-p)
blitzmax-mode-abbrev-table)))
;; --------------------------------------------------
;; -- Font-lock functions
(defun blitzmax-mode--fontify-buffer ()
"Enable BlitzMax syntax highlighting for the current buffer."
(let ((blitzmax-mode-keywords-regexp (regexp-opt blitzmax-mode-all-keywords 'symbols))
(blitzmax-mode-types-regexp (regexp-opt blitzmax-mode-type-keywords 'symbols))
(blitzmax-mode-constants-regexp (regexp-opt blitzmax-mode-constant-keywords 'symbols)))
;; Build highlight table.
(make-local-variable 'blitzmax-mode-font-lock-keywords)
(setq blitzmax-mode-font-lock-keywords
`((,blitzmax-mode-types-regexp . font-lock-type-face)
(,blitzmax-mode-keywords-regexp . font-lock-keyword-face)
(,blitzmax-mode-constants-regexp . font-lock-constant-face)))
;; Highlight keywords.
(setq font-lock-defaults
'(blitzmax-mode-font-lock-keywords nil t))))
;; Propertize Rem/End Rem comments. BlitzMax ignores the case, so REM and rEm
;; are treated exactly the same (i.e. the start of a comment).
(defconst blitzmax-mode--syntax-propertize-function
(syntax-propertize-rules
("\\_<\\([Rr]\\)[Ee][Mm]\\_>" (1 "<")) ;; Start of comment.
("\\_<[Ee][Nn][Dd][ ]?[Rr][Ee]\\([Mm]\\)\\_>" (1 ">")))) ;; End of comment.
;; --------------------------------------------------
;; -- Indentation functions
(defun blitzmax-mode--one-line-if-p ()
"Check if the current IF statement is complete on a single line."
(looking-at blitzmax-mode-if-oneline-regexp))
(defun blitzmax-mode--abstract-defun-p ()
"Check if the current function or method is abstract."
(looking-at blitzmax-mode-abstract-defun-regexp))
(defun blitzmax-mode--externed-function-p ()
"Check if the current function is part of an `Extern` block."
(save-excursion
(blitzmax-mode--find-matching-extern)
(looking-at blitzmax-mode-extern-start-regexp)))
(defun blitzmax-mode--previous-line-of-code ()
"Move to the previous line of code, skipping over any comments or whitespace."
(unless (bobp)
(forward-line -1))
(while (and (not (bobp))
(or (looking-at blitzmax-mode-blank-regexp)
(looking-at blitzmax-mode-comment-regexp)))
(forward-line -1)))
(defun blitzmax-mode--next-line-of-code ()
"Move to the next line of code, skipping over any comments or whitespace."
(unless (eobp)
(forward-line 1))
(while (and (not (eobp))
(or (looking-at blitzmax-mode-blank-regexp)
(looking-at blitzmax-mode-comment-regexp)))
(forward-line 1)))
(defun blitzmax-mode--find-original-statement ()
"Move to original statement if current line is part of a continuation."
(let ((here (point)))
(blitzmax-mode--previous-line-of-code)
(while (and (not (bobp))
(looking-at blitzmax-mode-continuation-regexp))
(setq here (point))
(blitzmax-mode--previous-line-of-code))
(goto-char here)))
(defun blitzmax-mode--find-matching-statement (open-regexp close-regexp)
"Find the start of a pair that begins with OPEN-REGEXP and ends with CLOSE-REGEXP."
(let ((level 0))
(while (and (>= level 0) (not (bobp)))
(blitzmax-mode--previous-line-of-code)
(blitzmax-mode--find-original-statement)
(cond ((looking-at close-regexp)
(setq level (+ level 1)))
((looking-at open-regexp)
(setq level (- level 1)))))))
(defun blitzmax-mode--find-matching-extern ()
"Find the start of an If/End If statement."
(blitzmax-mode--find-matching-statement blitzmax-mode-extern-start-regexp blitzmax-mode-extern-end-regexp))
(defun blitzmax-mode--find-matching-if ()
"Find the start of an If/End If statement."
(blitzmax-mode--find-matching-statement blitzmax-mode-if-regexp blitzmax-mode-endif-regexp)
(when (blitzmax-mode--one-line-if-p)
(blitzmax-mode--find-matching-if)))
(defun blitzmax-mode--find-matching-select ()
"Find the start of a Select/End Select statement."
(blitzmax-mode--find-matching-statement blitzmax-mode-select-regexp blitzmax-mode-select-end-regexp))
(defun blitzmax-mode--find-matching-for ()
"Find the start of a For/Next statement."
(blitzmax-mode--find-matching-statement blitzmax-mode-for-regexp blitzmax-mode-next-regexp))
(defun blitzmax-mode--find-matching-while ()
"Find the start of a While/Wend statement."
(blitzmax-mode--find-matching-statement blitzmax-mode-while-regexp blitzmax-mode-wend-regexp))
(defun blitzmax-mode--find-matching-repeat ()
"Find the start of a Repeat/Until statement."
(blitzmax-mode--find-matching-statement blitzmax-mode-repeat-regexp blitzmax-mode-until-regexp))
(defun blitzmax-mode--find-matching-defun ()
"Find the start of a Method/End Method OR Function/End Function statement."
(blitzmax-mode--find-matching-statement blitzmax-mode-defun-start-regexp blitzmax-mode-defun-end-regexp))
(defun blitzmax-mode--find-matching-try ()
"Find the start of a Try/Catch statement."
(blitzmax-mode--find-matching-statement blitzmax-mode-try-regexp blitzmax-mode-try-end-regexp))
(defun blitzmax-mode--calculate-indent ()
"Calculate the indent level for the current line."
(save-excursion
(beginning-of-line)
;; Don't indent if at the start or end of a type.
(cond ((or (looking-at blitzmax-mode-type-start-regexp)
(looking-at blitzmax-mode-type-end-regexp))
0)
;; Don't indent if at the start of end of an Extern
((or (looking-at blitzmax-mode-extern-start-regexp)
(looking-at blitzmax-mode-extern-end-regexp))
0)
;; Don't indent if on a label (#whatever).
((or (looking-at blitzmax-mode-label-regexp))
0)
;; If in an Else/End If - indent to indentation of the matching If.
((or (looking-at blitzmax-mode-else-regexp)
(looking-at blitzmax-mode-endif-regexp))
(blitzmax-mode--find-matching-if)
(current-indentation))
;; Remove indent for end function / end method
((looking-at blitzmax-mode-defun-end-regexp)
(blitzmax-mode--find-matching-defun)
(current-indentation))
;; All the other matching pairs act alike.
;; For/Next
((looking-at blitzmax-mode-next-regexp)
(blitzmax-mode--find-matching-for)
(current-indentation))
;; while/wend
((looking-at blitzmax-mode-wend-regexp)
(blitzmax-mode--find-matching-while)
(current-indentation))
;; repeat/until
((looking-at blitzmax-mode-until-regexp)
(blitzmax-mode--find-matching-repeat)
(current-indentation))
;; select case/end select
((looking-at blitzmax-mode-select-end-regexp)
(blitzmax-mode--find-matching-select)
(current-indentation))
;; CASE within a SELECT block.
((looking-at blitzmax-mode-case-regexp)
(blitzmax-mode--find-matching-select)
(+ (current-indentation) blitzmax-mode-indent))
;; Try/End Try block.
((or (looking-at blitzmax-mode-catch-regexp)
(looking-at blitzmax-mode-try-end-regexp))
(blitzmax-mode--find-matching-try)
(current-indentation))
;; All other indentation depends on the previous lines.
(t
(blitzmax-mode--previous-line-of-code)
;; Skip over label lines, which always have 0 indent.
(while (looking-at blitzmax-mode-label-regexp)
(blitzmax-mode--previous-line-of-code))
(blitzmax-mode--find-original-statement)
(let ((indent (current-indentation)))
;; All the various +indent regexps.
(cond ((and (looking-at blitzmax-mode-defun-start-regexp)
(not (blitzmax-mode--abstract-defun-p))
(not (blitzmax-mode--externed-function-p)))
(+ indent blitzmax-mode-indent))
((looking-at blitzmax-mode-type-start-regexp)
(+ indent blitzmax-mode-indent))
;; Extern block.
((looking-at blitzmax-mode-extern-start-regexp)
(+ indent blitzmax-mode-indent))
;; "Else"/"ElseIf is always indented
((looking-at blitzmax-mode-else-regexp)
(+ indent blitzmax-mode-indent))
;; Check if the "If" is a multi-line one.
((and (looking-at blitzmax-mode-if-regexp)
(not (blitzmax-mode--one-line-if-p)))
(+ indent blitzmax-mode-indent))
;; Select...Case block.
((or (looking-at blitzmax-mode-select-regexp)
(looking-at blitzmax-mode-case-regexp))
(+ indent blitzmax-mode-indent))
;; For...Next, While...Wend and Repeat...Until loops.
((or (looking-at blitzmax-mode-for-regexp)
(looking-at blitzmax-mode-while-regexp)
(looking-at blitzmax-mode-repeat-regexp))
(+ indent blitzmax-mode-indent))
;; Try...End Try.
((or (looking-at blitzmax-mode-try-regexp)
(looking-at blitzmax-mode-catch-regexp))
(+ indent blitzmax-mode-indent))
;; If nothing has changed, copy indent from prev line.
(t
indent)))))))
(defun blitzmax-mode--indent-to-column (col)
"Indent current line to COL."
(let* ((bol (save-excursion
(beginning-of-line)
(point)))
(point-in-whitespace
(<= (point) (+ bol (current-indentation))))
(blank-line-p
(save-excursion
(beginning-of-line)
(looking-at blitzmax-mode-blank-regexp))))
(cond ((/= col (current-indentation))
(save-excursion
(beginning-of-line)
(back-to-indentation)
(delete-region bol (point))
(indent-to col))))
;; If point was in the whitespace, move back-to-indentation.
(cond (blank-line-p
(end-of-line))
(point-in-whitespace
(back-to-indentation)))))
(defun blitzmax-mode-indent-line ()
"Indent the current line."
(interactive)
(blitzmax-mode--indent-to-column (blitzmax-mode--calculate-indent)))
(defun blitzmax-mode-newline-and-indent ()
"Insert a newline, indent it, and complete any pairs if needed."
(interactive)
;; Automatically complete any pairs.
(when (and blitzmax-mode-complete-pairs-p
(eolp))
(blitzmax-mode-insert-matching-pair))
;; Insert a newline and indent any code.
(newline-and-indent))
;; --------------------------------------------------
;; -- Pair completion
(defun blitzmax-mode--construct-unclosed-p (open-regexp close-regexp)
"Search for instances of OPEN-REGEXP before CLOSE-REGEXP.
This is used for constructs that cannot be nested, such as types or methods. It
checks if there is an instance of OPEN-REGEXP before CLOSE-REGEXP. If there
is, it means the construct is unclosed and can have a pair inserted."
(save-excursion
;; TODO: This is pretty darn ugly.
(let ((openp nil)
(closedp nil))
(while (and (not openp) (not closedp) (not (eobp)))
(blitzmax-mode--next-line-of-code)
(setq openp (looking-at open-regexp))
(setq closedp (looking-at close-regexp)))
(or openp (eobp)))))
(defun blitzmax-mode--can-complete-pair (open-regexp close-regexp)
"Check if pair that can be completed.
Checks if point is at the start of the pair (declared via OPEN-REGEXP) and also
checks if that pair is unclosed (via CLOSE-REGEXP)."
(and (looking-at open-regexp)
(blitzmax-mode--construct-unclosed-p open-regexp close-regexp)))
(defun blitzmax-mode-insert-matching-pair ()
"Complete a pair if the current line is the start."
(interactive)
;; TODO: This should only run when at the end of the current line, not the start.
(save-excursion
(beginning-of-line)
;; TODO: This can be cleaned up considerably.
(cond ((blitzmax-mode--can-complete-pair blitzmax-mode-extern-start-regexp
blitzmax-mode-extern-end-regexp)
(end-of-line)
(insert "\nEnd Extern")
(indent-according-to-mode))
((blitzmax-mode--can-complete-pair blitzmax-mode-type-start-regexp
blitzmax-mode-type-end-regexp)
(end-of-line)
(insert "\nEnd Type"))
((and (blitzmax-mode--can-complete-pair blitzmax-mode-function-start-regexp
blitzmax-mode-function-end-regexp)
(not (blitzmax-mode--abstract-defun-p)))
(end-of-line)
(insert "\nEnd Function")
(indent-according-to-mode))
((and (blitzmax-mode--can-complete-pair blitzmax-mode-method-start-regexp
blitzmax-mode-method-end-regexp)
(not (blitzmax-mode--abstract-defun-p)))
(end-of-line)
(insert "\nEnd Method")
(indent-according-to-mode))
;; Nested structures that don't require any close checks.
((looking-at blitzmax-mode-for-regexp)
(end-of-line)
(insert "\nNext")
(indent-according-to-mode))
((looking-at blitzmax-mode-while-regexp)
(end-of-line)
(insert "\nWend")
(indent-according-to-mode))
((looking-at blitzmax-mode-try-regexp)
(end-of-line)
(insert "\nEnd Try")
(indent-according-to-mode))
((looking-at blitzmax-mode-select-regexp)
(end-of-line)
(insert "\nEnd Select")
(indent-according-to-mode)))))
;; --------------------------------------------------
;; -- Quickrun Support
(declare-function quickrun-add-command "quickrun")
(defvar quickrun-file-alist)
;;;###autoload
(defun blitzmax-mode-quickrun-integration ()
"Register BlitzMax with quickrun."
;; Will compile the current buffer in threaded + debug mode and then run it.
(quickrun-add-command
"blitzmax"
`((:command . ,blitzmax-mode-compiler-pathname)
(:cmdopt . "makeapp -h -d -o %e")
(:exec . ("%c %o %s"
"%e %a"))
(:tempfile . nil)
(:remove . ("%e")))
:mode 'blitxmax-mode
:default "blitzmax")
;; Add `.bmx` to list of quickrun file types.
(add-to-list 'quickrun-file-alist '("\\.bmx$" . "blitzmax")))
;; --------------------------------------------------
;; -- Main Mode
;;;###autoload
(define-derived-mode blitzmax-mode prog-mode "BlitzMax"
"Major mode for editing BlitzMax source files."
;; Bind <enter> to custom indentation function.
(define-key blitzmax-mode-map (kbd "C-j") #'blitzmax-mode-newline-and-indent)
(define-key blitzmax-mode-map (kbd "RET") #'blitzmax-mode-newline-and-indent)
;; Fontify buffer.
(blitzmax-mode--fontify-buffer)
;; Enable smart indentation.
(when blitzmax-mode-smart-indent-p
(make-local-variable 'indent-line-function)
(setq indent-line-function #'blitzmax-mode-indent-line))
;; Enable automatic capitalization.
(when blitzmax-mode-capitalize-keywords-p
(make-local-variable 'pre-abbrev-expand-hook)
(add-hook 'pre-abbrev-expand-hook #'blitzmax-mode--capitalize-keywords)
(abbrev-mode 1))
;; Add single line comments to syntax table.
(modify-syntax-entry ?\' "< b" blitzmax-mode-syntax-table)
(modify-syntax-entry ?\n "> b" blitzmax-mode-syntax-table)
;; Configure comment syntax for comment-line/comment-region.
(setq-local comment-start "' ")
(setq-local comment-end "")
(setq-local comment-start-skip "'[ \t]*")
(setq-local comment-column 0)
(setq-local comment-use-syntax t)
;; Modify syntax to not treat \ as an escape char. Treat ~ as one instead.
(modify-syntax-entry ?\\ "." blitzmax-mode-syntax-table)
(modify-syntax-entry ?~ "\\" blitzmax-mode-syntax-table)
;; Additional syntax support.
(setq syntax-propertize-function blitzmax-mode--syntax-propertize-function))
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.bmx\\'" . blitzmax-mode))
;;;###autoload
(add-to-list 'interpreter-mode-alist '("bmx" . blitzmax-mode))
(provide 'blitzmax-mode)
;;; blitzmax-mode.el ends here