forked from roadrunner1776/magik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magik-mode.el
2240 lines (2022 loc) · 92.1 KB
/
magik-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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; magik-mode.el --- Emacs major mode for Smallworld Magik files
;; Package-Version: 0.3.3
;; Package-Requires: ((emacs "24.4") (compat "28.1"))
;; URL: https://github.com/roadrunner1776/magik
;; Keywords: languages
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides a major mode for editing Smallworld Magik files.
;;; Code:
(eval-when-compile
(require 'font-lock)
(defvar msb-menu-cond)
(defvar ac-sources)
(defvar ac-prefix)
(defvar ac-modes)
(require 'magik-indent)
(require 'magik-electric)
(require 'magik-pragma))
(require 'compat)
(require 'imenu)
(require 'magik-template)
(defgroup magik nil
"Customise Magik Language group."
:group 'smallworld
:group 'languages)
(defcustom magik-transmit-debug-p nil
"*If t, \"#DEBUG\" patterns get stripped out of things being transmitted to magik."
:group 'magik
:type 'boolean)
(defcustom magik-under-as-char t
"*Non-nil means that the _ (underline) should be treated as word char."
:group 'magik
:type 'boolean)
(defcustom magik-mark-method-exchange t
"*If t, `magik-mark-method' (\\[magik-mark-method]) leaves point at end of marked method region.
nil leaves point at start of marked method region.
Standard Emacs marking commands like `mark-paragraph' and `mark-sexp'
\(on keys \\[mark-paragraph] and \\[mark-sexp] respectively)
normally leave point at the start of the region.
But to keep most users happy, this is defaulted to t as this is
what most Smallworld users expect.
Users can also swap the point and mark positions using \\[exchange-point-and-mark]."
:group 'magik
:type 'boolean)
(defcustom magik-work-buffer nil
"*Name of Magik buffer to be used for development work."
:group 'magik
:type 'string)
(defcustom magik-auto-abbrevs t
"*User option which enables magik abbreviation expansion automatically."
:group 'magik
:type 'boolean)
(defcustom magik-sw-menu-file-max-length 90
"*The maximum length of the displayed path in the SW -> Magik Files submenu."
:group 'magik
:type 'integer)
(define-derived-mode magik-base-mode prog-mode "Magik"
"Generic major mode for editing Magik files.
This is a generic major mode intended to be inherited by
concrete implementations."
:group 'magik
:interactive nil
(compat-call setq-local
magik-template-file-type (magik-template-file-type)
paragraph-start (concat "^$\\|" page-delimiter)
paragraph-separate paragraph-start
require-final-newline mode-require-final-newline
comment-start "#"
comment-end ""
comment-column 8
comment-start-skip "#+ *"
comment-multi-line nil
parse-sexp-ignore-comments nil
magik-transmit-debug-mode-line-string " #DEBUG"
imenu-generic-expression imenu-generic-expression
imenu-create-index-function 'magik-imenu-create-index-function
imenu-syntax-alist '((?_ . "w"))
ac-sources (append '(
magik-ac-class-method-source
magik-ac-dynamic-source
magik-ac-global-source
magik-ac-object-source
magik-ac-raise-condition-source
)
(and (boundp 'ac-sources)
ac-sources))
outline-regexp "\\(^\\(_abstract +\\|\\)\\(_private +\\|\\)\\(_iter +\\|\\)_method.*\\|.*\.\\(def_property\\|add_child\\)\\|.*\.define_\\(shared_variable\\|shared_constant\\|slot_access\\|slot_externally_\\(read\\|writ\\)able\\|property\\|interface\\|method_signature\\).*\\|^\\(\t*#+\>[^>]\\|def_\\(slotted\\|indexed\\)_exemplar\\|def_mixin\\|#% text_encoding\\|_global\\|read_\\(message\\|translator\\)_patch\\).*\\)")
(when magik-auto-abbrevs (abbrev-mode 1))
(imenu-add-menubar-index))
;;;###autoload
(define-derived-mode magik-mode magik-base-mode "Magik"
"Major mode for editing Magik code.
\\{magik-mode-map}"
:group 'magik
:abbrev-table nil
:syntax-table nil
(compat-call setq-local
font-lock-defaults '((magik-font-lock-keywords
magik-font-lock-keywords-1
magik-font-lock-keywords-2
magik-font-lock-keywords-3
magik-font-lock-keywords-4
magik-font-lock-keywords-5)
nil t
((?_ . "w"))
magik-goto-code
(font-lock-fontify-buffer-function . magik-font-lock-fontify-buffer)
(font-lock-fontify-region-function . magik-font-lock-fontify-region)
(font-lock-unfontify-buffer-function . magik-font-lock-unfontify-buffer))
indent-line-function 'magik-indent-line))
(defvar magik-menu nil
"Keymap for the Magik buffer menu bar.")
(easy-menu-define magik-menu magik-base-mode-map
"Menu for Magik Mode."
`(,"Magik"
[,"Transmit Method" magik-transmit-method :active (magik-utils-buffer-mode-list 'magik-session-mode)
:keys "<f7>, <f2> <f7>, <f2> m"]
[,"Transmit Region" magik-transmit-region :active (magik-utils-buffer-mode-list 'magik-session-mode)
:keys "<f8>, <f2> <f8>, <f2> r"]
[,"Transmit Buffer" magik-transmit-buffer (magik-utils-buffer-mode-list 'magik-session-mode)]
[,"Transmit Chunk" magik-transmit-$-chunk (magik-utils-buffer-mode-list 'magik-session-mode)]
[,"Transmit Thing" magik-transmit-thing (magik-utils-buffer-mode-list 'magik-session-mode)]
"---"
[,"Copy Region to Work Buffer" magik-copy-region-to-buffer t]
[,"Copy Method to Work Buffer" magik-copy-method-to-buffer t]
[,"Set Work Buffer Name" magik-set-work-buffer-name t]
"---"
[,"Electric Template" magik-explicit-electric-space t]
[,"Mark Method" magik-mark-method :active t :keys "C-M-h, <f9>"]
[,"Copy Method" magik-copy-method :active t :keys "<f4> c, <f6>"]
[,"Compare Method between Windows" magik-compare-methods t]
[,"Compare Method using Ediff" magik-ediff-methods t]
"---"
[,"Add Debug Statement" magik-add-debug-statement t]
[,"Trace Statement" magik-trace-curr-statement t]
[,"Symbol Complete" magik-symbol-complete (magik-utils-buffer-mode-list 'magik-session-mode)]
"---"
[,"Comment Region" magik-comment-region t]
[,"Uncomment Region" magik-uncomment-region t]
[,"Fill Comment" magik-fill-public-comment t]
"---"
[,"Check sw-method-docs for method" magik-single-sw-method-docs t]
[,"Check sw-method-docs for file" magik-file-sw-method-docs t]
[,"Check pragma for method/def_slotted_exemplar" magik-single-pragma t]
[,"Check pragma for file" magik-file-pragma t]
"---"
(,"Toggle.."
[,"Method Name Display" magik-method-name-mode
:active t
:style toggle
:selected magik-method-name-mode]
[,"Electric Magik Mode" magik-electric-mode
:active t
:style toggle
:selected magik-electric-mode]
[,"#DEBUG Statements" magik-toggle-transmit-debug-p
:active t
:style toggle
:selected magik-transmit-debug-p]
[,"Point at End of Marked Region" magik-mark-method-exchange-mode
:active t
:style toggle
:selected magik-mark-method-exchange]
,"Options.."
[,"Transmit Method = Move to End" (magik-transmit-method-eom-mode 'end)
:active t
:style radio
:selected (eq magik-transmit-method-eom-mode 'end)]
[,"Transmit Method = Do Not Move Point" (magik-transmit-method-eom-mode nil)
:active t
:style radio
:selected (eq magik-transmit-method-eom-mode nil)]
[,"Transmit Method = On Repeat, Move to End" (magik-transmit-method-eom-mode 'repeat)
:active t
:style radio
:selected (eq magik-transmit-method-eom-mode 'repeat)
])
[,"Customize" magik-customize t]))
(defvar magik-imenu-expression
`(
(nil
"^\\s-*\\(_abstract\\(\n\\|\\s-\\)+\\)?\\(_private\\(\n\\|\\s-\\)+\\)?\\(_iter\\(\n\\|\\s-\\)+\\)?_method\\s-+\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\.\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)\\)" magik-imenu-method-name 9)
(,"Public Methods"
"^\\s-*_method\\s-+\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\.\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" magik-imenu-method-name 1)
(,"Iterators"
"^\\s-*\\(_abstract\\(\n\\|\\s-\\)+\\)?\\(_private\\(\n\\|\\s-\\)+\\)?_iter\\(\n\\|\\s-\\)+_method\\s-+\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\.\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" magik-imenu-method-name 6)
(,"Private"
"^\\s-*_private\\(\n\\|\\s-\\)+\\(_iter\\(\n\\|\\s-\\)+\\)?_method\\s-+\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\.\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" magik-imenu-method-name 4)
(,"Abstract"
"^\\s-*_abstract\\(\n\\|\\s-\\)+\\(_private\\(\n\\|\\s-\\)+\\)?\\(_iter\\(\n\\|\\s-\\)+\\)?_method\\s-+\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\.\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" magik-imenu-method-name 6)
(,"show/write/print/trace"
"_method\\s-+\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\.\\(show\\|write\\|print\\|debug_print\\|trace\\)\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" magik-imenu-method-name 1)
(,"new/init"
"_method\\s-+\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\.\\(new\\|init\\)\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" magik-imenu-method-name 1)
(,"Procedures"
"\\b_\\sw+\\(\n\\|\\s-\\)+\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)\\s-*<<\\(\n\\|\\s-\\)*_proc\\s-*(" 2) ;unamed, use variable assignment
(,"Procedures"
"_proc\\s-*\\(@\\s-*\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)\\s-*(" magik-imenu-method-name 1) ;named using @
(,"Condition"
"^\\s-*condition.define_condition([ \t\n]*:\\s-*\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" 1)
(,"Properties"
"^\\s-*\\(.+\\)\\.def\\(\\|ine\\)_property([ \t\n]*:\\s-*\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" 3)
(,"Shared Variables"
"^\\s-*\\(.+\\)\\.define_shared_variable([ \t\n]*:\\s-*\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" 2)
(,"Shared Constants"
"^\\s-*\\(.+\\)\\.define_shared_constant([ \t\n]*:\\s-*\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" 2)
(,"Slot Access"
"^\\s-*\\(.+\\)\\.define_slot_\\(access\\|externally_readable\\|externally_writable\\)([ \t\n]*:\\s-*\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" 3)
(,"Pseduo Slots"
"^\\s-*\\(.+\\)\\.define_pseudo_slot([ \t\n]*:\\s-*\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" 2) ; define_slot_externally_* rarely used.
(,"Mixins"
"^\\s-*def_mixin([ \t\n]*:\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" 1)
(,"Operators"
"^\\s-*define_binary_operator_case([ \t\n]*:\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" 1)
(,"Arrays"
"^\\s-*_method\\s-+\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)\\s-*?\\[" magik-imenu-method-name 1)
(,"Exemplars"
"^\\s-*def_\\(slott\\|index\\)ed_exemplar([ \t\n]*:\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" 2) ;; def_enumeration not used.
(,"Globals"
"^\\s-*_global\\(\n\\|\\s-\\)+\\(_constant\\(\n\\|\\s-\\)+\\)?\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)" 4)
(,"Packages"
"^\\s-*_package[ \t\n]*\\(\\sw+\\)" 1))
"Imenu expression for Magik mode. See `magik-imenu-create-index-function'.")
;;; Font-lock configuration
;;
;; Do not use :inherit because we want Emacs 20 support.
;;
;; On Windows Emacsen 21 and earlier, there is a GDI Object based memory leak.
;; This has been tracked down to creating italic forms of the font.
;; However, bold italic forms do not appear affected,
;; so all color based italic fonts are also made bold too.
(defgroup magik-faces nil
"Fontification colours for Magik."
:group 'magik)
;; font-lock-variable-use-face was introduced in Emacs 29.1.
(unless (member `font-lock-variable-use-face (face-list))
(put 'font-lock-variable-use 'face-alias 'font-lock-variable-name-face))
(defface magik-argument-face
'((t (:inherit font-lock-variable-use-face)))
"Font-lock Face to use when displaying arguments for methods."
:group 'magik-faces)
(defface magik-boolean-face
'((t (:inherit font-lock-variable-name-face)))
"Font-lock Face to use when displaying boolean and kleenean references."
:group 'magik-faces)
(defface magik-character-face
'((t (:inherit font-lock-constant-face)))
"Font-lock Face to use when displaying characters."
:group 'magik-faces)
(defface magik-class-face
'((t (:inherit font-lock-type-face)))
"Font-lock Face to use when displaying exemplars."
:group 'magik-faces)
(defface magik-comment-face
'((t (:inherit font-lock-comment-face)))
"Font-lock Face to use when displaying comments."
:group 'magik-faces)
(defface magik-constant-face
'((t (:inherit font-lock-constant-face)))
"Font-lock Face to use when displaying constants."
:group 'magik-faces)
(defface magik-doc-face
'((t (:inherit font-lock-doc-face)))
"Font-lock Face to use when displaying documentation."
:group 'magik-faces)
(defface magik-keyword-operators-face
'((t (:inherit font-lock-keyword-face)))
"Font-lock Face to use when displaying Magik operator keywords."
:group 'magik-faces)
(defface magik-keyword-statements-face
'((t (:inherit font-lock-keyword-face)))
"Font-lock Face to use when displaying Magik statement keywords."
:group 'magik-faces)
(defface magik-keyword-loop-face
'((t (:inherit font-lock-keyword-face)))
"Font-lock Face to use when displaying Magik loop keywords."
:group 'magik-faces)
(defface magik-keyword-arguments-face
'((t (:inherit font-lock-keyword-face)))
"Font-lock Face to use when displaying Magik argument keywords."
:group 'magik-faces)
(defface magik-dynamic-face
'((t (:inherit font-lock-variable-name-face)))
"Font-lock Face to use when displaying dynamic variables."
:group 'magik-faces)
(defface magik-global-face
'((t (:inherit font-lock-variable-name-face)))
"Font-lock Face to use when displaying global variables."
:group 'magik-faces)
(defface magik-global-reference-face
'((t (:inherit font-lock-constant-face)))
"Font-lock Face to use when displaying global references."
:group 'magik-faces)
(defface magik-keyword-variable-face
'((t (:inherit font-lock-variable-name-face)))
"Font-lock Face to use when displaying Magik variable keywords."
:group 'magik-faces)
(defface magik-keyword-obsolete-face
'((t (:inherit font-lock-warning-face)))
"Font-lock Face to use when displaying obsolete Magik keywords."
:group 'magik-faces)
(defface magik-method-face
'((t (:inherit font-lock-function-name-face)))
"Font-lock Face to use when displaying method names and method and procedure keywords."
:group 'magik-faces)
(defface magik-label-face
'((t (:inherit font-lock-variable-name-face)))
"Font-lock Face to use when displaying labels for loops."
:group 'magik-faces)
;; font-lock-number-face was introduced in Emacs 29.1 as new face without any inheritance.
(unless (member `font-lock-number-face (face-list))
(put 'font-lock-number-face 'face-alias 'font-lock-constant-face))
(defface magik-number-face
'((t (:inherit font-lock-number-face)))
"Font-lock Face to use when displaying numbers."
:group 'magik-faces)
(defface magik-pragma-face
'((t (:inherit font-lock-builtin-face)))
"Font-lock Face to use when displaying pragma directives."
:group 'magik-faces)
(defface magik-procedure-face
'((t (:inherit font-lock-function-name-face)))
"Font-lock Face to use when displaying procedure calls."
:group 'magik-faces)
(defface magik-slot-face
'((t (:inherit font-lock-variable-name-face)))
"Font-lock Face to use when displaying slots."
:group 'magik-faces)
(defface magik-symbol-face
'((t (:inherit font-lock-constant-face)))
"Font-lock Face to use when displaying symbols."
:group 'magik-faces)
(defface magik-string-face
'((t (:inherit font-lock-string-face)))
"Font-lock Face to use when displaying strings."
:group 'magik-faces)
(defface magik-variable-face
'((t (:inherit font-lock-variable-name-face)))
"Font-lock Face to use when displaying variables."
:group 'magik-faces)
(defface magik-warning-face
'((t (:inherit font-lock-warning-face)))
"Font-lock Face to use when displaying warning statements."
:group 'magik-faces)
(defface magik-write-face
'((t (:inherit font-lock-warning-face)))
"Font-lock Face to use when displaying write() statements."
:group 'magik-faces)
(defconst magik-regexp
'(("method" .
"^[_abstract\s|_private\s|_iter\s]*?_method")
("method-with-arguments" .
"^[_abstract\s|_private\s|_iter\s]*?_method.*(\\([\0-\377[:nonascii:]]*?\\))")
("assignment-method" .
"^[_abstract\s|_private\s|_iter\s]*?_method.*<<\s?\\(.*\\)")
("endmethod" .
"^\\s-*_endmethod\\s-*\\(\n\\$\\s-*\\)?$")
("method-argument" .
"_gather\\|_scatter\\|_optional")
("pragma" .
"^_pragma(.*)")
("def_slotted_exemplar" .
"^[sw:]?def_slotted_exemplar(.*")
)
"List of regexp strings which can be used for searching for a magik-specific string in a buffer.")
(defvar magik-keyword-kleenean
'("false" "true" "maybe")
"List of keywords relating to kleenean values to highlight for font-lock.")
(defvar magik-keyword-constants
'("unset" "constant")
"List of keywords relating to constant values to highlight for font-lock.
The \"no_way\" constant is treated as a special case in this Magik mode
because it does not have an _ preceding like all the other Magik keywords.")
(defvar magik-keyword-operators
'("and" "andif" "div" "is" "isnt" "cf" "mod" "not" "or" "orif" "xor")
"List of keywords relating to operators to highlight for font-lock.")
(defvar magik-keyword-class
'("self" "super" "clone")
"List of keywords relating to exemplars to highlight for font-lock.")
(defvar magik-keyword-methods
'("abstract" "private" "method" "endmethod" "primitive")
"List of keywords relating to methods to highlight for font-lock.")
(defvar magik-keyword-procedures
'("proc" "endproc")
"List of keywords relating to procedures to highlight for font-lock.")
(defvar magik-keyword-statements
'("block" "endblock" "catch" "throw" "endcatch"
"if" "then" "elif" "else" "endif"
"lock" "endlock" "protect" "locking" "protection" "endprotect"
"try" "endtry" "when" "handling" "with" "using"
"pragma" "package" "default" "thisthread")
"List of keywords relating to statements to highlight for font-lock.")
(defvar magik-keyword-loop
'("iter" "continue" "for" "loop" "endloop" "loopbody" "over" "leave" "finally" "while")
"List of keywords relating to loops to highlight for font-lock.")
(defvar magik-keyword-arguments
'("gather" "scatter" "allresults" "optional" "return")
"List of keywords relating to arguments to highlight for font-lock.")
(defvar magik-keyword-variable
'("dynamic" "global" "import" "local" "class" "recursive")
"List of keywords relating to variables to highlight for font-lock.")
(defvar magik-keyword-obsolete
'("concat" "case" "endcase" "otherwise" "void")
"List of obsolete/unimplemented keywords to highlight for font-lock.")
(defvar magik-other-keywords '("def_indexed_exemplar" "def_slotted_exemplar")
"List of other Magik `keywords'.")
(defvar magik-warnings
'("TODO" "DEBUG" "FIXME" "sys!slot" "sys!perform" "sys!perform_iter")
"List of Magik Warnings.")
(defcustom magik-font-lock-keywords-1
(list
(cons (concat "\\<_" (regexp-opt magik-keyword-kleenean t) "\\>") ''magik-boolean-face)
(cons (concat "\\<no_way\\|_" (regexp-opt magik-keyword-constants t) "\\>") ''magik-constant-face)
(cons (concat "\\<_"
(regexp-opt (append magik-keyword-operators
magik-keyword-class
magik-keyword-methods
magik-keyword-procedures
magik-keyword-statements
magik-keyword-loop
magik-keyword-arguments
magik-keyword-variable)
t)
"\\>")
'font-lock-keyword-face)
(cons (concat "\\<\\(" (mapconcat 'identity magik-other-keywords "\\|") "\\)\\>")
'font-lock-keyword-face))
"Font lock setting for 1st level of Magik fontification.
Fontifies all Magik keywords in the same face except Magik
constants which use the `font-lock-constant-face' face."
:group 'magik
:type 'sexp)
(defcustom magik-font-lock-keywords-2
(list
'("\\b_method\\s-*\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)\.\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)"
(1 'magik-class-face)
(3 'magik-method-face))
'("\\<!\\sw+\\!\\>" . 'magik-dynamic-face)
'("\\<:\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?" . 'magik-symbol-face)
'("\\<\\(write\\|print\\|debug_print\\)\\s-*(" 1 'magik-write-face)
(list (concat "\\<\\("
(mapconcat 'identity magik-warnings "\\|")
"\\)")
0 ''magik-warning-face t)
'("^\\s-*##.*$" 0 'magik-doc-face t)
)
"Font lock setting for 2nd level of Magik fontification.
Fontifies certain Magik language features like symbols, dynamics but does
NOT fontify ANY Magik Keywords."
:group 'magik
:type 'sexp)
(defcustom magik-font-lock-keywords-3
(append magik-font-lock-keywords-1 magik-font-lock-keywords-2 nil)
"Combines 1st and 2nd level fontification levels.
See `magik-font-lock-keywords-1' and `magik-font-lock-keywords-2'."
:group 'magik
:type 'sexp)
(defcustom magik-font-lock-keywords-4
(append
magik-font-lock-keywords-3
(list
(cons (concat "\\<_" (regexp-opt magik-keyword-operators t) "\\>") ''magik-keyword-operators-face)
(cons (concat "\\<_" (regexp-opt magik-keyword-class t) "\\>") ''magik-class-face)
(cons (concat "\\<_" (regexp-opt magik-keyword-methods t) "\\>") ''magik-method-face)
(cons (concat "\\<_" (regexp-opt magik-keyword-procedures t) "\\>") ''magik-procedure-face)
(cons (concat "\\<_" (regexp-opt magik-keyword-statements t) "\\>") ''magik-keyword-statements-face)
(cons (concat "\\<_" (regexp-opt magik-keyword-loop t) "\\>") ''magik-keyword-loop-face)
(cons (concat "\\<_" (regexp-opt magik-keyword-arguments t) "\\>") ''magik-keyword-arguments-face)
(cons (concat "\\<_" (regexp-opt magik-keyword-variable t) "\\>") ''magik-keyword-variable-face)
(cons (concat "\\<_" (regexp-opt magik-keyword-obsolete t) "\\>") ''magik-keyword-obsolete-face)
'("^_pragma\\s-*\\(([^)]*)\\)" 1 'magik-pragma-face)
;; methods
'("\\(\\sw\\|\\s$\\)\\.\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)\\(\\s-*(\\)" 2 'magik-method-face)
;; procedures
'("\\<\\(\\sw+\\)\\(\\s-*(\\)" 1 'magik-procedure-face)
'("^\\(def_slotted_exemplar\\|def_indexed_exemplar\\)\\>" 0 'magik-class-face t)
'("^\\(\\sw+\\)\\.define_\\(shared_constant\\|shared_variable\\|slot_access\\)\\>" 1 'magik-class-face)
'("\\Sw\\(\\.\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)\\>" 1 'magik-slot-face)
'("\\<\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\?\\>" 0 'magik-boolean-face t)
'("_for\\s-+\\(\\sw+\\)" 1 'magik-variable-face) ;; _for loop variable
'("@\\s-*\\sw+" 0 'magik-global-reference-face t)
'(">>" 0 'magik-keyword-arguments-face t)
))
"Font lock setting for 4th level of Magik fontification.
As 1st level but also fontifies all Magik keywords according their
different classifications. ie. loop keywords are fontified in the same face."
:group 'magik
:type 'sexp)
(defcustom magik-font-lock-keywords-5
(append magik-font-lock-keywords-4 nil)
"Provides an easy user configurable level for personal/site fontification of styles. Based from `magik-font-lock-keywords-4'."
:group 'magik
:type 'sexp)
(defcustom magik-font-lock-keywords magik-font-lock-keywords-1
"Default fontification of Magik."
:group 'magik
:type 'sexp)
;;Could have coded the following to be generic but this is more maintainable.
;;Have added support for 3d arrays although there are none in Core at present.
;;Also used for returning regexps for normal methods and ()<< and ()^<< methods too
(defvar magik-goto-class-method-alist
'(("[]" . "\\[[^\],]+\\]\\s-*$")
("[]<<" . "\\[[^\],]+\\]\\s-*<<")
("[]^<<" . "\\[[^\],]+\\]\\s-*\\^<<")
("[,]" . "\\[[^\],]+,[^\]]+\\]\\s-*$")
("[,]<<" . "\\[[^\],]+,[^\]]+\\]\\s-*<<")
("[,]^<<" . "\\[[^\],]+,[^\]]+\\]\\s-*\\^<<")
("[,,]" . "\\[[^\],]+,[^\],]+,[^\]]+\\]\\s-*$")
("[,,]<<" . "\\[[^\],]+,[^\],]+,[^\]]+\\]\\s-*<<")
("[,,]^<<" . "\\[[^\],]+,[^\],]+,[^\]]+\\]\\s-*\\^<<")
("<<" . "<<")
("^<<" . "^<<")
("()" . "(")
("()<<" . "([^\)]*)\\s-*<<")
("()^<<" . "([^\)]*)\\s-*^<<")
("" . "\r?\n")
)
"Alist to help searching for method types.")
(defvar magik-transmit-method-eom-alist
(list (cons "Transmit Method = Do Not Move Point" nil)
(cons "Transmit Method = Move to End" 'end)
(cons "Transmit Method = On Repeat, Move to End" 'repeat))
"Alist of options for variable `magik-transmit-method-eom-mode'.")
(defcustom magik-transmit-method-eom-mode nil
"Variable storing setting of \\[magik-transmit-method-eom-mode]."
:group 'magik
:type (list 'choice
(list 'const ':tag "Transmit Method = Do Not Move Point" nil)
(list 'const ':tag "Transmit Method = Move to End" 'end)
(list 'const ':tag "Transmit Method = On Repeat, Move to End" 'repeat)))
(defcustom magik-method-name-mode nil
"Variable storing setting of \\[magik-method-name-mode].
This variable exists to allow a user to set the mode before the Smallworld
code is loaded."
;;Use of integers is a standard way of forcing minor modes on and off.
:group 'magik
:type '(choice (const :tag "On" 1)
(const :tag "Off" -1)))
(defvar-local magik-method-name ""
"Variable storing method name at which point it in.
Used by \\[magik-method-name-mode].")
(defvar-local magik-transmit-debug-mode-line-string nil
"Mode-line string to use when transmitting of #DEBUG statements is enabled.")
(defvar magik-method-name-set-text-function 'magik-method-name-set-text-properties
"Function to use for setting the Mode line to include Method name.
Function takes two arguments BUFFER and METHOD.")
;; consider enabling refresh using auto-complete's 10 minute refresh idle timer?
(defvar magik-ac-object-source-cache nil
"Cache of all Magik Objects for use in auto-complete-mode.
Once initialised this variable is not refreshed.")
(defvar magik-ac-object-source
'((init . magik-ac-object-source-init)
(candidates . magik-ac-object-source-cache)
(prefix . magik-object)
(requires . 3)
(symbol . "o"))
"Auto-complete mode source definition for listing all Magik Objects.
Use auto-complete mode \"o\" symbol convention to represent an object.")
(defvar magik-ac-class-method-source-cache nil
"Cache of all Magik methods on current class for use in auto-complete-mode.")
(defvar magik-ac-class-method-source
'((init . magik-ac-object-source-init)
(candidates . magik-ac-class-method-source)
(prefix . magik-method)
(symbol . "f"))
"Auto-complete mode source definition for listing methods on a given class.
Use auto-complete mode \"f\" symbol convention to represent a function, method.")
(defvar magik-ac-raise-condition-source-cache nil
"Cache of all Magik Conditions for use in auto-complete-mode.")
(defvar magik-ac-raise-condition-source
'((init . magik-ac-raise-condition-source-init)
(candidates . magik-ac-raise-condition-source-cache)
(prefix . magik-condition)
(symbol . "c"))
"Auto-complete mode source definition for listing known conditions.
Uses auto-complete \"c\" symbol convention to represent a condition!")
(defvar magik-ac-global-source-cache nil
"Cache of all Magik Globals for use in auto-complete-mode.
Once initialised this variable is not refreshed.")
(defvar magik-ac-dynamic-source
'((init . magik-ac-global-source-init)
(candidates . magik-ac-global-source-cache)
(prefix . magik-dynamic)
(symbol . "d"))
"Auto-complete mode source definition for listing Magik language dynamics.
Use auto-complete mode \"d\" symbol convention to represent.")
(defvar magik-ac-global-source
'((init . magik-ac-global-source-init)
(candidates . magik-ac-global-source-cache)
;(requires . 3)
(symbol . "g"))
"Auto-complete mode source definition for listing all Magik Globals.
Use auto-complete mode \"g\" symbol convention to represent a global.")
(defun magik-customize ()
"Open Customization buffer for Magik Mode."
(interactive)
(customize-group 'magik))
;;; Functions
(defun magik-expand-abbrev ()
(save-excursion
(let*
((toks (progn (insert ? ) ;; so that the token closes!
(prog1
(magik-tokenise-region-no-eol (line-beginning-position) (point))
(delete-char -1))))
(last-tok (car (last toks)))
(last-tok-pos (cdr last-tok)))
(backward-word 1)
(if (and (eq (point) last-tok-pos)
(/= (preceding-char) ?.))
(insert ?_))
(if (and (derived-mode-p 'magik-base-mode)
(looking-at "_else\\|_elif\\|_finally\\|_using\\|_with\\|_when\\|_protection\\|_end"))
(magik-indent-command)))))
;;Actually only used by the Magik-Patch minor mode but we need a hook here
;;because a function must be referred to in font-lock-defaults.
(defvar magik-goto-code-function 'point-min
"Function used to place point on the line immediately preceeding Magik code.")
(defun magik-goto-code ()
"Goto start of code."
(funcall magik-goto-code-function))
(defun magik-font-lock-fontify-buffer ()
(let ((verbose (if (numberp font-lock-verbose)
(> (buffer-size) font-lock-verbose)
font-lock-verbose))
(code-start (save-excursion (magik-goto-code))))
(with-temp-message
(when verbose
(format "Fontifying %s..." (buffer-name)))
;; Make sure we have the right `font-lock-keywords' etc.
(unless font-lock-mode
(font-lock-set-defaults))
;; Make sure we fontify etc. in the whole buffer.
(save-restriction
(widen)
(condition-case nil
(save-excursion
(save-match-data
(font-lock-fontify-region code-start (point-max) verbose)
(font-lock-after-fontify-buffer)
(setq font-lock-fontified t)))
;; We don't restore the old fontification, so it's best to unfontify.
(quit (font-lock-unfontify-buffer))))
;; Make sure we undo `font-lock-keywords' etc.
(unless font-lock-mode
(font-lock-unset-defaults)))))
(defun magik-font-lock-unfontify-buffer ()
"Make sure we unfontify etc. in the whole buffer."
(save-restriction
(widen)
(font-lock-unfontify-region (save-excursion (magik-goto-code)) (point-max))
(font-lock-after-unfontify-buffer)
(setq font-lock-fontified nil)))
(defun magik-font-lock-fontify-region (beg end loudly)
(let*
((modified (buffer-modified-p))
(buffer-undo-list t)
(inhibit-read-only t)
(inhibit-point-motion-hooks t)
(inhibit-modification-hooks t)
deactivate-mark buffer-file-name buffer-file-truename
(old-syntax-table (syntax-table))
(code-start (save-excursion (magik-goto-code))))
(unwind-protect
(save-restriction
(widen)
;; Use the fontification syntax table, if any.
(when font-lock-syntax-table
(set-syntax-table font-lock-syntax-table))
;; check to see if we should expand the beg/end area for
;; proper multiline matches
(when (and (boundp 'font-lock-multiline)
font-lock-multiline
(> beg code-start)
(get-text-property (1- beg) 'font-lock-multiline))
;; We are just after or in a multiline match.
(setq beg (or (previous-single-property-change
beg 'font-lock-multiline)
code-start))
(goto-char beg)
(setq beg (line-beginning-position)))
(when (and (boundp 'font-lock-multiline) font-lock-multiline)
(setq end (or (text-property-any end (point-max)
'font-lock-multiline nil)
(point-max))))
(goto-char end)
(setq end (line-beginning-position 2))
(if (and (>= end code-start) (< beg code-start))
(setq beg code-start))
(when (and (>= beg code-start)
(>= end code-start))
;; Now do the fontification.
(font-lock-unfontify-region beg end)
(unless font-lock-keywords-only
(font-lock-fontify-syntactically-region beg end loudly))
(font-lock-fontify-keywords-region beg end loudly)))
;; Clean up.
(set-syntax-table old-syntax-table))
(if (and (not modified) (buffer-modified-p))
(set-buffer-modified-p nil))))
(defun magik-toggle-transmit-debug-p (&optional arg)
"Toggle transmission of #DEBUG statements in Magik code.
Optional argument ARG .."
(interactive "P")
(setq magik-transmit-debug-p
(if (null arg)
(not magik-transmit-debug-p)
(> (prefix-numeric-value arg) 0)))
(message
(if magik-transmit-debug-p
"Magik DEBUG statements on"
"Magik DEBUG statements off")))
(defun magik-add-debug-statement ()
"Add a debug statement at the current line of magik."
(interactive)
(let
((var (magik-utils-find-tag-default))
(pos (point))
line
col
tb)
(save-excursion
(or var (error "No current variable to print"))
(magik-backward-method)
(push-mark nil t)
(while
(not (or (eobp)
(looking-at "^\\s-*\\(_abstract\\(\n\\|\\s-\\)+\\)?\\(_private\\(\n\\|\\s-\\)+\\)?\\(_iter\\(\n\\|\\s-\\)+\\)?_method\\s-+\\(\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\.\\sw*\\(\\s$\\S$*\\s$\\sw*\\)?\\)")))
(forward-line 1))
(setq tb (match-string-no-properties 6))
(setq line (count-lines (point) pos)))
(back-to-indentation)
(setq col (current-column))
(beginning-of-line)
(indent-to col)
(insert (format "#DEBUG show(\"%s:Line %d\",:%s,%s)\n" tb line var var))))
;; bound to CR in magik mode.
(defun magik-newline ()
"Insert a newline and indent. (To insert a newline and not indent, use \\[electric-newline-and-maybe-indent])."
(interactive "*")
(if (eq major-mode 'magik-session-mode)
(error "Your magik shell buffer has got into magik mode! To recover, type `M-x magik-session-mode'. Please report this bug."))
(if abbrev-mode (save-excursion (expand-abbrev)))
(if (save-excursion
(back-to-indentation)
(looking-at "[]})]\\|_else\\|_finally\\|_using\\|_with\\|_when\\|_protection\\|_end"))
(magik-indent-command))
(newline-and-indent))
(defun magik-indent-line ()
"Indent the current line as Magik code."
(let ((indent (magik-calc-indent))
(pos (- (point-max) (point)))
beg change)
(beginning-of-line)
(setq beg (point))
(skip-chars-forward " \t")
(setq change (- indent (current-column)))
(if (zerop change)
nil
(delete-region beg (point))
(indent-to indent))
;; if the initial point was within the inden, leave point at the indent,
;; otherwise back to where we where
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos)))))
;; bound to TAB in magik mode.
(defun magik-indent-command ()
"Indent the current line as Magik code, or hop between pragma fields."
(interactive "*")
(let ((magik-pragma-brackets (magik-pragma-line-p)))
(if (consp magik-pragma-brackets)
(magik-electric-pragma-tab magik-pragma-brackets)
(magik-indent-line))))
(defun magik-method-name-type (name)
"Return cons cell describing the method name parts (NAME . TYPE).
For normal methods:
NAME is the method name root
TYPE is '^<<' or '<<' or '()' or '()<<' or '()^<<' or empty string.
For array methods
NAME is nil
TYPE is '[]' or '[]<<' or '[]^<<' or '[,]' or '[,]<<' or '[,]^<<' etc."
(if (eq (elt name 0) ?\[)
(cons nil name)
(save-match-data
(if (string-match "\\(()^<<\\|()<<\\|^<<\\|()\\|<<\\)$" name)
(cons (substring name 0 (match-beginning 1)) (match-string 1 name))
(cons name "")))))
(defun magik-goto-class-method (method &optional class)
"Find the specified METHOD and CLASS in the current buffer.
If more than one definition is found in the buffer, you will be
given the opportunity to visit each definition.
Also the search string is added to isearch mode's regexp ring so that
you can use \\[isearch-forward-regexp] and use M-p to recall the search."
(interactive
(list
(read-string "Method Name: " (current-word))
(if current-prefix-arg
(read-string "Class Name: "
(file-name-sans-extension (file-name-nondirectory (buffer-file-name)))))))
(if class nil
(setq class (file-name-sans-extension (file-name-nondirectory (buffer-file-name)))))
(let* ((method-cons (magik-method-name-type method))
(method-root (car method-cons))
(method-type (cdr method-cons))
search-str)
(cond
((string-equal class "<global>")
;; look for _global definitions
(if (re-search-forward (setq search-str (concat (regexp-quote method) "\\s-*<<")) nil t)
(magik-goto-class-method-loop search-str method)
(error "Cannot find method '%s'" method)))
((string-equal class "<condition>")
;; look for condition.define_condition
(if (re-search-forward (setq search-str (concat "condition.define_condition(\\(\n\\|\\s-\\)*:" (regexp-quote method) "\\(\n\\|\\s-\\)*,")) nil t)
(magik-goto-class-method-loop search-str method)
(error "Cannot find method '%s'" method)))
((null method-root);(eq (elt method 0) ?\[)
;; look for array definitions: [] []<< []^<< [,] [,]<< [,]^<<
(or (setq search-str (cdr (assoc method-type magik-goto-class-method-alist)))
(error "Cannot find method '%s'" method))
(if (re-search-forward (setq search-str (concat class search-str)) nil t)
(magik-goto-class-method-loop search-str method)
(error "Cannot find method '%s'" method)))
((re-search-forward
;; look for an ordinary _method constructs.
(setq search-str
(concat
"^"
"\\(_abstract\\s-+\\)?"
"\\(_private\\s-+\\)?"
"\\(_iter\\s-+\\)?"
"_method\\s-+"
class
"\\."
(regexp-quote method-root) "\\s-*" (or (cdr (assoc method-type magik-goto-class-method-alist)) method-type)))
nil t)
(magik-goto-class-method-loop search-str (concat class "." method)))
;; look for other method constructors.
((re-search-forward
(setq search-str (concat "^" class
".\\(define_\\(shared_constant\\|shared_variable\\|slot_access\\|property\\)\\|def_property\\)"
"\\s-*([ \t\r\n]*:" (regexp-quote method-root)))
nil t)
(magik-goto-class-method-loop search-str (concat class "." method)))
(t
(error "Cannot find method, '%s', in class, '%s'" method class)))))
(defun magik-goto-class-method-loop (search-str arg)
"Loop over subsequent definitions.
Adds string to `regexp-search-ring'. After wuiting this loop
you can use \\[isearch-forward-regexp] and use M-p to recall this search."
;;I would like to use the isearch functionality but I cannot work out
;;how to control isearch programmatically.
(let ((continue-p t)
(pt (point))
(prompt (concat (format "Warning: Goto next definition of '%s'" arg) " "))
(start-prompt (concat (format "Warning: Goto first definition of '%s'" arg) " "))
next)
(save-excursion
(setq next (re-search-forward search-str nil t)))
(if (null next)
nil ;only single definition found exit here.
(beep)
(isearch-update-ring search-str t)
(while continue-p
(cond ((null next)
;;No additional definitions found
(if (not (y-or-n-p start-prompt))
(setq continue-p nil)
(beep)
(goto-char pt)
(setq continue-p t)))
((y-or-n-p prompt)
(goto-char next))
(t
(setq continue-p nil)))
(save-excursion
(setq next (re-search-forward search-str nil t))))
(where-is 'isearch-forward-regexp))))