forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjulia-mode.el
3253 lines (3154 loc) · 136 KB
/
julia-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
;;; julia-mode.el --- Major mode for editing Julia source code
;; Copyright (C) 2009-2014 Julia contributors
;; URL: https://github.com/JuliaLang/julia
;; Version: 0.3
;; Keywords: languages
;;; Usage:
;; Put the following code in your .emacs, site-load.el, or other relevant file
;; (add-to-list 'load-path "path-to-julia-mode")
;; (require 'julia-mode)
;;; Commentary:
;; This is the official Emacs mode for editing Julia programs.
;;; License:
;; Permission is hereby granted, free of charge, to any person obtaining
;; a copy of this software and associated documentation files (the
;; "Software"), to deal in the Software without restriction, including
;; without limitation the rights to use, copy, modify, merge, publish,
;; distribute, sublicense, and/or sell copies of the Software, and to
;; permit persons to whom the Software is furnished to do so, subject to
;; the following conditions:
;;
;; The above copyright notice and this permission notice shall be
;; included in all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
;; LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
;; OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
;; WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
;;; Code:
;; We can't use cl-lib whilst supporting Emacs 23 users who don't use
;; ELPA.
(with-no-warnings
(require 'cl)) ;; incf, decf, plusp
(defvar julia-mode-hook nil)
(defgroup julia ()
"Major mode for the julia programming language."
:group 'languages
:prefix "julia-")
(defcustom julia-indent-offset 4
"Number of spaces per indentation level."
:type 'integer
:group 'julia)
(defface julia-macro-face
'((t :inherit font-lock-preprocessor-face))
"Face for Julia macro invocations."
:group 'julia-mode)
(defface julia-quoted-symbol-face
'((t :inherit font-lock-preprocessor-face))
"Face for quoted Julia symbols, e.g. :foo."
:group 'julia-mode)
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.jl\\'" . julia-mode))
;; define ignore-errors macro if it isn't present
;; (necessary for emacs 22 compatibility)
(when (not (fboundp 'ignore-errors))
(defmacro ignore-errors (body) `(condition-case nil ,body (error nil))))
(defun julia--regexp-opt (strings &optional paren)
"Emacs 23 provides `regexp-opt', but it does not support PAREN taking the value 'symbols.
This function provides equivalent functionality, but makes no efforts to optimise the regexp."
(cond
((>= emacs-major-version 24)
(regexp-opt strings paren))
((not (eq paren 'symbols))
(regexp-opt strings paren))
((null strings)
"")
('t
(rx-to-string `(seq symbol-start (or ,@strings) symbol-end)))))
(defvar julia-mode-syntax-table
(let ((table (make-syntax-table)))
(modify-syntax-entry ?_ "_" table)
(modify-syntax-entry ?@ "_" table)
(modify-syntax-entry ?! "_" table)
(modify-syntax-entry ?# "< 14" table) ; # single-line and multiline start
(modify-syntax-entry ?= ". 23bn" table)
(modify-syntax-entry ?\n ">" table) ; \n single-line comment end
(modify-syntax-entry ?\{ "(} " table)
(modify-syntax-entry ?\} "){ " table)
(modify-syntax-entry ?\[ "(] " table)
(modify-syntax-entry ?\] ")[ " table)
(modify-syntax-entry ?\( "() " table)
(modify-syntax-entry ?\) ")( " table)
;; Here, we treat ' as punctuation (when it's used for transpose),
;; see our use of `julia-char-regex' for handling ' as a character
;; delimeter
(modify-syntax-entry ?' "." table)
(modify-syntax-entry ?\" "\"" table)
(modify-syntax-entry ?` "\"" table)
(modify-syntax-entry ?. "." table)
(modify-syntax-entry ?? "." table)
(modify-syntax-entry ?$ "." table)
(modify-syntax-entry ?& "." table)
(modify-syntax-entry ?* "." table)
(modify-syntax-entry ?/ "." table)
(modify-syntax-entry ?+ "." table)
(modify-syntax-entry ?- "." table)
(modify-syntax-entry ?< "." table)
(modify-syntax-entry ?> "." table)
(modify-syntax-entry ?% "." table)
table)
"Syntax table for `julia-mode'.")
(eval-when-compile
(defconst julia-char-regex
(rx (or (any "-" ";" "\\" "^" "!" "|" "?" "*" "<" "%" "," "=" ">" "+" "/" "&" "$" "~" ":")
(syntax open-parenthesis)
(syntax whitespace)
bol)
(group "'")
(group
(or (repeat 0 8 (not (any "'"))) (not (any "\\"))
"\\\\"))
(group "'"))))
(defconst julia-triple-quoted-string-regex
;; We deliberately put a group on the first and last delimiter, so
;; we can mark these as string delimiters for font-lock.
(rx (group "\"")
(group "\"\""
;; After the delimiter, we're a sequence of
;; non-backslashes or blackslashes paired with something.
(*? (or (not (any "\\"))
(seq "\\" anything)))
"\"\"")
(group "\"")))
(defconst julia-unquote-regex
"\\(\\s(\\|\\s-\\|-\\|[,%=<>\\+*/?&|!\\^~\\\\;:]\\|^\\)\\($[a-zA-Z0-9_]+\\)")
(defconst julia-forloop-in-regex
"for +.*[^
].* \\(in\\)\\(\\s-\\|$\\)+")
(defconst julia-function-regex
(rx line-start (* (or space "@inline" "@noinline")) symbol-start
"function"
(1+ space)
;; Don't highlight module names in function declarations:
(* (seq (1+ (or word (syntax symbol))) "."))
;; The function name itself
(group (1+ (or word (syntax symbol))))))
(defconst julia-function-assignment-regex
(rx line-start (* (or space "@inline" "@noinline")) symbol-start
(* (seq (1+ (or word (syntax symbol))) ".")) ; module name
(group (1+ (or word (syntax symbol))))
(* space)
(? "{" (* (not (any "}"))) "}")
(* space)
"(" (* (or
(seq "(" (* (not (any "(" ")"))) ")")
(not (any "(" ")"))))
")"
(* space)
"="
(not (any "="))))
(defconst julia-type-regex
(rx symbol-start (or "immutable" "type" "abstract") (1+ space) (group (1+ (or word (syntax symbol))))))
(defconst julia-type-annotation-regex
(rx "::" (0+ space) (group (1+ (or word (syntax symbol))))))
;;(defconst julia-type-parameter-regex
;; (rx symbol-start (1+ (or (or word (syntax symbol)) ?_)) "{" (group (1+ (or (or word (syntax symbol)) ?_))) "}"))
(defconst julia-subtype-regex
(rx "<:" (0+ space) (group (1+ (or word (syntax symbol)))) (0+ space) (or "\n" "{" "}" "end")))
(defconst julia-macro-regex
(rx symbol-start (group "@" (1+ (or word (syntax symbol))))))
(defconst julia-keyword-regex
(julia--regexp-opt
'("if" "else" "elseif" "while" "for" "begin" "end" "quote"
"try" "catch" "return" "local" "abstract" "function" "macro" "ccall"
"finally" "typealias" "break" "continue" "type" "global"
"module" "using" "import" "export" "const" "let" "bitstype" "do" "in"
"baremodule" "importall" "immutable")
'symbols))
(defconst julia-builtin-regex
(julia--regexp-opt
;;'("error" "throw")
'()
'symbols))
(defconst julia-builtin-types-regex
(julia--regexp-opt
'("Number" "Real" "BigInt" "Integer"
"UInt" "UInt8" "UInt16" "UInt32" "UInt64" "UInt128"
"Int" "Int8" "Int16" "Int32" "Int64" "Int128"
"BigFloat" "AbstractFloat" "Float16" "Float32" "Float64"
"Complex128" "Complex64"
"Bool"
"Cuchar" "Cshort" "Cushort" "Cint" "Cuint" "Clonglong" "Culonglong" "Cintmax_t" "Cuintmax_t"
"Cfloat" "Cdouble" "Cptrdiff_t" "Cssize_t" "Csize_t"
"Cchar" "Clong" "Culong" "Cwchar_t"
"Char" "ASCIIString" "UTF8String" "ByteString" "SubString"
"Array" "DArray" "AbstractArray" "AbstractVector" "AbstractMatrix" "AbstractSparseMatrix" "SubArray" "StridedArray" "StridedVector" "StridedMatrix" "VecOrMat" "StridedVecOrMat" "DenseArray" "SparseMatrixCSC" "BitArray"
"Range" "OrdinalRange" "StepRange" "UnitRange" "FloatRange"
"Tuple" "NTuple" "Vararg"
"DataType" "Symbol" "Function" "Vector" "Matrix" "Union" "Type" "Any" "Complex" "AbstractString" "Ptr" "Void" "Exception" "Task" "Signed" "Unsigned" "Associative" "Dict" "IO" "IOStream" "Rational" "Regex" "RegexMatch" "Set" "IntSet" "Expr" "WeakRef" "ObjectIdDict"
"AbstractRNG" "MersenneTwister"
)
'symbols))
(defconst julia-quoted-symbol-regex
;; :foo and :foo2 are valid, but :123 is not.
(rx (or whitespace "(" "[" "," "=")
(group ":" (or letter (syntax symbol)) (0+ (or word (syntax symbol))))))
(defconst julia-font-lock-keywords
(list
;; Ensure :: and <: aren't highlighted, so we don't confuse ::Foo with :foo.
;; (in Emacs, keywords don't overlap).
(cons (rx (or "::" "<:")) ''default)
;; Highlight quoted symbols before keywords, so :function is not
;; highlighted as a keyword.
(list julia-quoted-symbol-regex 1 ''julia-quoted-symbol-face)
(cons julia-builtin-types-regex 'font-lock-type-face)
(cons julia-keyword-regex 'font-lock-keyword-face)
(cons julia-macro-regex ''julia-macro-face)
(cons
(julia--regexp-opt
'("true" "false" "C_NULL" "Inf" "NaN" "Inf32" "NaN32" "nothing")
'symbols)
'font-lock-constant-face)
(list julia-unquote-regex 2 'font-lock-constant-face)
(list julia-forloop-in-regex 1 'font-lock-keyword-face)
(list julia-function-regex 1 'font-lock-function-name-face)
(list julia-function-assignment-regex 1 'font-lock-function-name-face)
(list julia-type-regex 1 'font-lock-type-face)
(list julia-type-annotation-regex 1 'font-lock-type-face)
;;(list julia-type-parameter-regex 1 'font-lock-type-face)
(list julia-subtype-regex 1 'font-lock-type-face)
(list julia-builtin-regex 1 'font-lock-builtin-face)
))
(defconst julia-block-start-keywords
(list "if" "while" "for" "begin" "try" "function" "type" "let" "macro"
"quote" "do" "immutable"))
(defconst julia-block-end-keywords
(list "end" "else" "elseif" "catch" "finally"))
(defun julia-stringify-triple-quote ()
"Put `syntax-table' property on triple-quoted string delimeters.
Based on `python-syntax-stringify'."
(let* ((string-start-pos (- (point) 3))
(string-end-pos (point))
(ppss (prog2
(backward-char 3)
(syntax-ppss)
(forward-char 3)))
(in-comment (nth 4 ppss))
(in-string (nth 8 ppss)))
(unless in-comment
(if in-string
;; We're in a string, so this must be the closing triple-quote.
;; Put | on the last " character.
(put-text-property (1- string-end-pos) string-end-pos
'syntax-table (string-to-syntax "|"))
;; We're not in a string, so this is the opening triple-quote.
;; Put | on the first " character.
(put-text-property string-start-pos (1+ string-start-pos)
'syntax-table (string-to-syntax "|"))))))
(unless (< emacs-major-version 24)
(defconst julia-syntax-propertize-function
(syntax-propertize-rules
("\"\"\""
(0 (ignore (julia-stringify-triple-quote))))
(julia-char-regex
(1 "\"") ; Treat ' as a string delimiter.
(2 ".") ; Don't highlight anything between.
(3 "\""))))) ; Treat the last " in """ as a string delimiter.
(defun julia-in-comment ()
"Return non-nil if point is inside a comment.
Handles both single-line and multi-line comments."
(nth 4 (syntax-ppss)))
(defun julia-in-string ()
"Return non-nil if point is inside a string.
Note this is Emacs' notion of what is highlighted as a string.
As a result, it is true inside \"foo\", `foo` and 'f'."
(nth 3 (syntax-ppss)))
(defun julia-in-brackets ()
"Return non-nil if point is inside square brackets."
(let ((start-pos (point))
(open-count 0))
;; Count all the [ and ] characters on the current line.
(save-excursion
(beginning-of-line)
(while (< (point) start-pos)
;; Don't count [ or ] inside strings, characters or comments.
(unless (or (julia-in-string) (julia-in-comment))
(when (looking-at (rx "["))
(incf open-count))
(when (looking-at (rx "]"))
(decf open-count)))
(forward-char 1)))
;; If we've opened more than we've closed, we're inside brackets.
(plusp open-count)))
(defun julia-at-keyword (kw-list)
"Return the word at point if it matches any keyword in KW-LIST.
KW-LIST is a list of strings. The word at point is not considered
a keyword if used as a field name, X.word, or quoted, :word."
(and (or (= (point) 1)
(and (not (equal (char-before (point)) ?.))
(not (equal (char-before (point)) ?:))))
(member (current-word t) kw-list)
(not (julia-in-comment))
;; 'end' is not a keyword when used for indexing, e.g. foo[end-2]
(or (not (equal (current-word t) "end"))
(not (julia-in-brackets)))))
;; if backward-sexp gives an error, move back 1 char to move over the '('
(defun julia-safe-backward-sexp ()
(if (condition-case nil (backward-sexp) (error t))
(ignore-errors (backward-char))))
(defun julia-last-open-block-pos (min)
"Return the position of the last open block, if one found.
Do not move back beyond position MIN."
(save-excursion
(let ((count 0))
(while (not (or (> count 0) (<= (point) min)))
(julia-safe-backward-sexp)
(setq count
(cond ((julia-at-keyword julia-block-start-keywords)
(+ count 1))
;; fixme: breaks on strings
((and (equal (current-word t) "end")
(not (julia-in-comment)) (not (julia-in-brackets)))
(- count 1))
(t count))))
(if (> count 0)
(point)
nil))))
(defun julia-last-open-block (min)
"Move back and return indentation level for last open block.
Do not move back beyond MIN."
;; Ensure MIN is not before the start of the buffer.
(setq min (max min (point-min)))
(let ((pos (julia-last-open-block-pos min)))
(and pos
(progn
(goto-char pos)
(+ julia-indent-offset (current-indentation))))))
(defsubst julia--safe-backward-char ()
"Move back one character, but don't error if we're at the
beginning of the buffer."
(unless (eq (point) (point-min))
(backward-char)))
(defvar julia-max-paren-lookback 400
"When indenting, don't look back more than this
many characters to see if there are unclosed parens.
This variable has a major effect on indent performance if set too
high.")
(defvar julia-max-block-lookback 5000
"When indenting, don't look back more than this
many characters to see if there are unclosed blocks.
This variable has a moderate effect on indent performance if set too
high.")
(defun julia-paren-indent ()
"Return the column of the text following the innermost
containing paren before point, so we can align succeeding code
with it. Returns nil if we're not within nested parens."
(save-excursion
;; Back up to previous line (beginning-of-line was already called)
(backward-char)
(let ((min-pos (max (- (point) julia-max-paren-lookback)
(point-min)))
(open-count 0))
(while (and (> (point) min-pos)
(not (plusp open-count)))
(when (looking-at (rx (any "[" "]" "(" ")")))
(unless (or (julia-in-string) (julia-in-comment))
(cond ((looking-at (rx (any "[" "(")))
(incf open-count))
((looking-at (rx (any "]" ")")))
(decf open-count)))))
(julia--safe-backward-char))
(if (plusp open-count)
(progn (forward-char 2)
(while (looking-at (rx blank))
(forward-char))
(current-column))
nil))))
(defun julia-indent-line ()
"Indent current line of julia code."
(interactive)
(let* ((point-offset (- (current-column) (current-indentation))))
(end-of-line)
(indent-line-to
(or
;; If we're inside an open paren, indent to line up arguments.
(save-excursion
(beginning-of-line)
(ignore-errors (julia-paren-indent)))
;; If the previous line ends in =, increase the indent.
(ignore-errors ; if previous line is (point-min)
(save-excursion
(if (and (not (equal (point-min) (line-beginning-position)))
(progn
(forward-line -1)
(end-of-line) (backward-char 1)
(and (equal (char-after (point)) ?=)
(not (julia-in-comment)))))
(+ julia-indent-offset (current-indentation))
nil)))
;; Indent according to how many nested blocks we are in.
(save-excursion
(beginning-of-line)
(forward-to-indentation 0)
(let ((endtok (julia-at-keyword julia-block-end-keywords))
(last-open-block (julia-last-open-block (- (point) julia-max-block-lookback))))
(max 0 (+ (or last-open-block 0)
(if endtok (- julia-indent-offset) 0)))))))
;; Point is now at the beginning of indentation, restore it
;; to its original position (relative to indentation).
(when (>= point-offset 0)
(move-to-column (+ (current-indentation) point-offset)))))
(defmacro julia--should-indent (from to)
"Assert that we indent text FROM producing text TO in `julia-mode'."
`(with-temp-buffer
(let ((julia-indent-offset 4))
(julia-mode)
(insert ,from)
(indent-region (point-min) (point-max))
(should (equal (buffer-substring-no-properties (point-min) (point-max))
,to)))))
;; Emacs 23.X doesn't include ert, so we ignore any errors that occur
;; when we define tests.
(ignore-errors
(require 'ert)
(ert-deftest julia--test-indent-if ()
"We should indent inside if bodies."
(julia--should-indent
"
if foo
bar
end"
"
if foo
bar
end"))
(ert-deftest julia--test-indent-else ()
"We should indent inside else bodies."
(julia--should-indent
"
if foo
bar
else
baz
end"
"
if foo
bar
else
baz
end"))
(ert-deftest julia--test-indent-toplevel ()
"We should not indent toplevel expressions. "
(julia--should-indent
"
foo()
bar()"
"
foo()
bar()"))
(ert-deftest julia--test-indent-nested-if ()
"We should indent for each level of indentation."
(julia--should-indent
"
if foo
if bar
bar
end
end"
"
if foo
if bar
bar
end
end"))
(ert-deftest julia--test-indent-function ()
"We should indent function bodies."
(julia--should-indent
"
function foo()
bar
end"
"
function foo()
bar
end"))
(ert-deftest julia--test-indent-begin ()
"We should indent after a begin keyword."
(julia--should-indent
"
@async begin
bar
end"
"
@async begin
bar
end"))
(ert-deftest julia--test-indent-paren ()
"We should indent to line up with the text after an open paren."
(julia--should-indent
"
foobar(bar,
baz)"
"
foobar(bar,
baz)"))
(ert-deftest julia--test-indent-paren-space ()
"We should indent to line up with the text after an open
paren, even if there are additional spaces."
(julia--should-indent
"
foobar( bar,
baz )"
"
foobar( bar,
baz )"))
(ert-deftest julia--test-indent-equals ()
"We should increase indent on a trailing =."
(julia--should-indent
"
foo() =
bar"
"
foo() =
bar"))
(ert-deftest julia--test-indent-ignores-blank-lines ()
"Blank lines should not affect indentation of non-blank lines."
(julia--should-indent
"
if foo
bar
end"
"
if foo
bar
end"))
(ert-deftest julia--test-indent-comment-equal ()
"`=` at the end of comment should not increase indent level."
(julia--should-indent
"
# a =
# b =
c"
"
# a =
# b =
c"))
(ert-deftest julia--test-indent-leading-paren ()
"`(` at the beginning of a line should not affect indentation."
(julia--should-indent
"
(1)"
"
(1)"))
(ert-deftest julia--test-top-level-following-paren-indent ()
"`At the top level, a previous line indented due to parens should not affect indentation."
(julia--should-indent
"y1 = f(x,
z)
y2 = g(x)"
"y1 = f(x,
z)
y2 = g(x)"))
(defun julia--run-tests ()
(interactive)
(ert-run-tests-interactively "julia--test")))
(defalias 'julia-mode-prog-mode
(if (fboundp 'prog-mode)
'prog-mode
'fundamental-mode))
;;; IMENU
(defvar julia-imenu-generic-expression
;; don't use syntax classes, screws egrep
'(("Function (_)" "[ \t]*function[ \t]+\\(_[^ \t\n]*\\)" 1)
("Function" "^[ \t]*function[ \t]+\\([^_][^\t\n]*\\)" 1)
("Const" "[ \t]*const \\([^ \t\n]*\\)" 1)
("Type" "^[ \t]*[a-zA-Z0-9_]*type[a-zA-Z0-9_]* \\([^ \t\n]*\\)" 1)
("Require" " *\\(\\brequire\\)(\\([^ \t\n)]*\\)" 2)
("Include" " *\\(\\binclude\\)(\\([^ \t\n)]*\\)" 2)
;; ("Classes" "^.*setClass(\\(.*\\)," 1)
;; ("Coercions" "^.*setAs(\\([^,]+,[^,]*\\)," 1) ; show from and to
;; ("Generics" "^.*setGeneric(\\([^,]*\\)," 1)
;; ("Methods" "^.*set\\(Group\\|Replace\\)?Method(\"\\(.+\\)\"," 2)
;; ;;[ ]*\\(signature=\\)?(\\(.*,?\\)*\\)," 1)
;; ;;
;; ;;("Other" "^\\(.+\\)\\s-*<-[ \t\n]*[^\\(function\\|read\\|.*data\.frame\\)]" 1)
;; ("Package" "^.*\\(library\\|require\\)(\\(.*\\)," 2)
;; ("Data" "^\\(.+\\)\\s-*<-[ \t\n]*\\(read\\|.*data\.frame\\).*(" 1)))
))
;;;###autoload
(define-derived-mode julia-mode julia-mode-prog-mode "Julia"
"Major mode for editing julia code."
(set-syntax-table julia-mode-syntax-table)
(set (make-local-variable 'comment-start) "# ")
(set (make-local-variable 'comment-start-skip) "#+\\s-*")
(set (make-local-variable 'font-lock-defaults) '(julia-font-lock-keywords))
(if (< emacs-major-version 24)
;; Emacs 23 doesn't have syntax-propertize-function
(set (make-local-variable 'font-lock-syntactic-keywords)
(list
`(,julia-char-regex
(1 "\"") ; Treat ' as a string delimiter.
(2 ".") ; Don't highlight anything between the open and close '.
(3 "\"")); Treat the close ' as a string delimiter.
`(,julia-triple-quoted-string-regex
(1 "\"") ; Treat the first " in """ as a string delimiter.
(2 ".") ; Don't highlight anything between.
(3 "\"")))) ; Treat the last " in """ as a string delimiter.
;; Emacs 24 and later has syntax-propertize-function, so use that instead.
(set (make-local-variable 'syntax-propertize-function)
julia-syntax-propertize-function))
(set (make-local-variable 'indent-line-function) 'julia-indent-line)
(setq indent-tabs-mode nil)
(setq imenu-generic-expression julia-imenu-generic-expression)
(imenu-add-to-menubar "Imenu"))
(defvar julia-latexsubs (make-hash-table :test 'equal))
(defun julia-latexsub ()
"Perform a LaTeX-like Unicode symbol substitution."
(interactive "*i")
(let ((orig-pt (point)))
(while (not (or (bobp) (= ?\\ (char-before))
(= ?\s (char-syntax (char-before)))))
(backward-char))
(if (and (not (bobp)) (= ?\\ (char-before)))
(progn
(backward-char)
(let ((sub (gethash (buffer-substring (point) orig-pt) julia-latexsubs)))
(if sub
(progn
(delete-region (point) orig-pt)
(insert sub))
(goto-char orig-pt))))
(goto-char orig-pt))))
(defalias 'latexsub 'julia-latexsub)
(defun julia-latexsub-or-indent (arg)
"Either indent according to mode or perform a LaTeX-like symbol substution"
(interactive "*i")
(if (latexsub)
(indent-for-tab-command arg)))
(define-key julia-mode-map (kbd "TAB") 'julia-latexsub-or-indent)
(defalias 'latexsub-or-indent 'julia-latexsub-or-indent)
; LaTeX-like symbol substitutions, equivalent to those in the Julia REPL,
; generated by:
;for (k,v) in sort!(collect(Base.REPLCompletions.latex_symbols), by=x->x[2])
; ks = escape_string(k)
; vs = escape_string(v)
; if ismatch(r"^\\U[0-9A-Fa-f]+$", vs)
; # codepoints outside the BMP can be problematic in older Emacsen
; cp = vs[3:end]
; println("(let ((c (decode-char 'ucs #x$cp)))\n",
; " (if c (puthash \"$ks\" (char-to-string c) julia-latexsubs)))")
; else
; println("(puthash \"$ks\" \"$vs\" julia-latexsubs)")
; end
;end
; (See Julia issue #8947 for why we don't use the Emacs tex input mode.)
(puthash "\\textexclamdown" "¡" julia-latexsubs)
(puthash "\\sterling" "£" julia-latexsubs)
(puthash "\\yen" "¥" julia-latexsubs)
(puthash "\\textbrokenbar" "¦" julia-latexsubs)
(puthash "\\S" "§" julia-latexsubs)
(puthash "\\textasciidieresis" "¨" julia-latexsubs)
(puthash "\\copyright" "©" julia-latexsubs)
(puthash "\\textordfeminine" "ª" julia-latexsubs)
(puthash "\\neg" "¬" julia-latexsubs)
(puthash "\\circledR" "®" julia-latexsubs)
(puthash "\\textasciimacron" "¯" julia-latexsubs)
(puthash "\\degree" "°" julia-latexsubs)
(puthash "\\pm" "±" julia-latexsubs)
(puthash "\\^2" "²" julia-latexsubs)
(puthash "\\^3" "³" julia-latexsubs)
(puthash "\\textasciiacute" "´" julia-latexsubs)
(puthash "\\P" "¶" julia-latexsubs)
(puthash "\\cdotp" "·" julia-latexsubs)
(puthash "\\^1" "¹" julia-latexsubs)
(puthash "\\textordmasculine" "º" julia-latexsubs)
(puthash "\\textonequarter" "¼" julia-latexsubs)
(puthash "\\textonehalf" "½" julia-latexsubs)
(puthash "\\textthreequarters" "¾" julia-latexsubs)
(puthash "\\textquestiondown" "¿" julia-latexsubs)
(puthash "\\AA" "Å" julia-latexsubs)
(puthash "\\AE" "Æ" julia-latexsubs)
(puthash "\\DH" "Ð" julia-latexsubs)
(puthash "\\times" "×" julia-latexsubs)
(puthash "\\O" "Ø" julia-latexsubs)
(puthash "\\TH" "Þ" julia-latexsubs)
(puthash "\\ss" "ß" julia-latexsubs)
(puthash "\\aa" "å" julia-latexsubs)
(puthash "\\ae" "æ" julia-latexsubs)
(puthash "\\eth" "ð" julia-latexsubs)
(puthash "\\div" "÷" julia-latexsubs)
(puthash "\\o" "ø" julia-latexsubs)
(puthash "\\th" "þ" julia-latexsubs)
(puthash "\\DJ" "Đ" julia-latexsubs)
(puthash "\\dj" "đ" julia-latexsubs)
(puthash "\\Elzxh" "ħ" julia-latexsubs)
(puthash "\\hbar" "ħ" julia-latexsubs)
(puthash "\\L" "Ł" julia-latexsubs)
(puthash "\\l" "ł" julia-latexsubs)
(puthash "\\NG" "Ŋ" julia-latexsubs)
(puthash "\\ng" "ŋ" julia-latexsubs)
(puthash "\\OE" "Œ" julia-latexsubs)
(puthash "\\oe" "œ" julia-latexsubs)
(puthash "\\texthvlig" "ƕ" julia-latexsubs)
(puthash "\\textnrleg" "ƞ" julia-latexsubs)
(puthash "\\Zbar" "Ƶ" julia-latexsubs)
(puthash "\\textdoublepipe" "ǂ" julia-latexsubs)
(puthash "\\Elztrna" "ɐ" julia-latexsubs)
(puthash "\\Elztrnsa" "ɒ" julia-latexsubs)
(puthash "\\Elzopeno" "ɔ" julia-latexsubs)
(puthash "\\Elzrtld" "ɖ" julia-latexsubs)
(puthash "\\Elzschwa" "ə" julia-latexsubs)
(puthash "\\varepsilon" "ɛ" julia-latexsubs)
(puthash "\\Elzpgamma" "ɣ" julia-latexsubs)
(puthash "\\Elzpbgam" "ɤ" julia-latexsubs)
(puthash "\\Elztrnh" "ɥ" julia-latexsubs)
(puthash "\\Elzbtdl" "ɬ" julia-latexsubs)
(puthash "\\Elzrtll" "ɭ" julia-latexsubs)
(puthash "\\Elztrnm" "ɯ" julia-latexsubs)
(puthash "\\Elztrnmlr" "ɰ" julia-latexsubs)
(puthash "\\Elzltlmr" "ɱ" julia-latexsubs)
(puthash "\\Elzltln" "ɲ" julia-latexsubs)
(puthash "\\Elzrtln" "ɳ" julia-latexsubs)
(puthash "\\Elzclomeg" "ɷ" julia-latexsubs)
(puthash "\\textphi" "ɸ" julia-latexsubs)
(puthash "\\Elztrnr" "ɹ" julia-latexsubs)
(puthash "\\Elztrnrl" "ɺ" julia-latexsubs)
(puthash "\\Elzrttrnr" "ɻ" julia-latexsubs)
(puthash "\\Elzrl" "ɼ" julia-latexsubs)
(puthash "\\Elzrtlr" "ɽ" julia-latexsubs)
(puthash "\\Elzfhr" "ɾ" julia-latexsubs)
(puthash "\\Elzrtls" "ʂ" julia-latexsubs)
(puthash "\\Elzesh" "ʃ" julia-latexsubs)
(puthash "\\Elztrnt" "ʇ" julia-latexsubs)
(puthash "\\Elzrtlt" "ʈ" julia-latexsubs)
(puthash "\\Elzpupsil" "ʊ" julia-latexsubs)
(puthash "\\Elzpscrv" "ʋ" julia-latexsubs)
(puthash "\\Elzinvv" "ʌ" julia-latexsubs)
(puthash "\\Elzinvw" "ʍ" julia-latexsubs)
(puthash "\\Elztrny" "ʎ" julia-latexsubs)
(puthash "\\Elzrtlz" "ʐ" julia-latexsubs)
(puthash "\\Elzyogh" "ʒ" julia-latexsubs)
(puthash "\\Elzglst" "ʔ" julia-latexsubs)
(puthash "\\Elzreglst" "ʕ" julia-latexsubs)
(puthash "\\Elzinglst" "ʖ" julia-latexsubs)
(puthash "\\textturnk" "ʞ" julia-latexsubs)
(puthash "\\Elzdyogh" "ʤ" julia-latexsubs)
(puthash "\\Elztesh" "ʧ" julia-latexsubs)
(puthash "\\^h" "ʰ" julia-latexsubs)
(puthash "\\^j" "ʲ" julia-latexsubs)
(puthash "\\^r" "ʳ" julia-latexsubs)
(puthash "\\^w" "ʷ" julia-latexsubs)
(puthash "\\^y" "ʸ" julia-latexsubs)
(puthash "\\rasp" "ʼ" julia-latexsubs)
(puthash "\\textasciicaron" "ˇ" julia-latexsubs)
(puthash "\\Elzverts" "ˈ" julia-latexsubs)
(puthash "\\Elzverti" "ˌ" julia-latexsubs)
(puthash "\\Elzlmrk" "ː" julia-latexsubs)
(puthash "\\Elzhlmrk" "ˑ" julia-latexsubs)
(puthash "\\Elzsbrhr" "˒" julia-latexsubs)
(puthash "\\Elzsblhr" "˓" julia-latexsubs)
(puthash "\\Elzrais" "˔" julia-latexsubs)
(puthash "\\Elzlow" "˕" julia-latexsubs)
(puthash "\\u" "˘" julia-latexsubs)
(puthash "\\texttildelow" "˜" julia-latexsubs)
(puthash "\\^l" "ˡ" julia-latexsubs)
(puthash "\\^s" "ˢ" julia-latexsubs)
(puthash "\\^x" "ˣ" julia-latexsubs)
(puthash "\\grave" "̀" julia-latexsubs)
(puthash "\\acute" "́" julia-latexsubs)
(puthash "\\hat" "̂" julia-latexsubs)
(puthash "\\tilde" "̃" julia-latexsubs)
(puthash "\\bar" "̄" julia-latexsubs)
(puthash "\\overbar" "̅" julia-latexsubs)
(puthash "\\breve" "̆" julia-latexsubs)
(puthash "\\dot" "̇" julia-latexsubs)
(puthash "\\ddot" "̈" julia-latexsubs)
(puthash "\\ovhook" "̉" julia-latexsubs)
(puthash "\\ocirc" "̊" julia-latexsubs)
(puthash "\\H" "̋" julia-latexsubs)
(puthash "\\check" "̌" julia-latexsubs)
(puthash "\\candra" "̐" julia-latexsubs)
(puthash "\\oturnedcomma" "̒" julia-latexsubs)
(puthash "\\ocommatopright" "̕" julia-latexsubs)
(puthash "\\droang" "̚" julia-latexsubs)
(puthash "\\Elzpalh" "̡" julia-latexsubs)
(puthash "\\Elzrh" "̢" julia-latexsubs)
(puthash "\\c" "̧" julia-latexsubs)
(puthash "\\k" "̨" julia-latexsubs)
(puthash "\\Elzsbbrg" "̪" julia-latexsubs)
(puthash "\\wideutilde" "̰" julia-latexsubs)
(puthash "\\underbar" "̱" julia-latexsubs)
(puthash "\\Elzxl" "̵" julia-latexsubs)
(puthash "\\Elzbar" "̶" julia-latexsubs)
(puthash "\\sout" "̶" julia-latexsubs)
(puthash "\\not" "̸" julia-latexsubs)
(puthash "\\Alpha" "Α" julia-latexsubs)
(puthash "\\Beta" "Β" julia-latexsubs)
(puthash "\\Gamma" "Γ" julia-latexsubs)
(puthash "\\Delta" "Δ" julia-latexsubs)
(puthash "\\Epsilon" "Ε" julia-latexsubs)
(puthash "\\Zeta" "Ζ" julia-latexsubs)
(puthash "\\Eta" "Η" julia-latexsubs)
(puthash "\\Theta" "Θ" julia-latexsubs)
(puthash "\\Iota" "Ι" julia-latexsubs)
(puthash "\\Kappa" "Κ" julia-latexsubs)
(puthash "\\Lambda" "Λ" julia-latexsubs)
(puthash "\\upMu" "Μ" julia-latexsubs)
(puthash "\\upNu" "Ν" julia-latexsubs)
(puthash "\\Xi" "Ξ" julia-latexsubs)
(puthash "\\upOmicron" "Ο" julia-latexsubs)
(puthash "\\Pi" "Π" julia-latexsubs)
(puthash "\\Rho" "Ρ" julia-latexsubs)
(puthash "\\Sigma" "Σ" julia-latexsubs)
(puthash "\\Tau" "Τ" julia-latexsubs)
(puthash "\\Upsilon" "Υ" julia-latexsubs)
(puthash "\\Phi" "Φ" julia-latexsubs)
(puthash "\\Chi" "Χ" julia-latexsubs)
(puthash "\\Psi" "Ψ" julia-latexsubs)
(puthash "\\Omega" "Ω" julia-latexsubs)
(puthash "\\alpha" "α" julia-latexsubs)
(puthash "\\beta" "β" julia-latexsubs)
(puthash "\\gamma" "γ" julia-latexsubs)
(puthash "\\delta" "δ" julia-latexsubs)
(puthash "\\upepsilon" "ε" julia-latexsubs)
(puthash "\\zeta" "ζ" julia-latexsubs)
(puthash "\\eta" "η" julia-latexsubs)
(puthash "\\theta" "θ" julia-latexsubs)
(puthash "\\iota" "ι" julia-latexsubs)
(puthash "\\kappa" "κ" julia-latexsubs)
(puthash "\\lambda" "λ" julia-latexsubs)
(puthash "\\mu" "μ" julia-latexsubs)
(puthash "\\nu" "ν" julia-latexsubs)
(puthash "\\xi" "ξ" julia-latexsubs)
(puthash "\\upomicron" "ο" julia-latexsubs)
(puthash "\\pi" "π" julia-latexsubs)
(puthash "\\rho" "ρ" julia-latexsubs)
(puthash "\\varsigma" "ς" julia-latexsubs)
(puthash "\\sigma" "σ" julia-latexsubs)
(puthash "\\tau" "τ" julia-latexsubs)
(puthash "\\upsilon" "υ" julia-latexsubs)
(puthash "\\varphi" "φ" julia-latexsubs)
(puthash "\\chi" "χ" julia-latexsubs)
(puthash "\\psi" "ψ" julia-latexsubs)
(puthash "\\omega" "ω" julia-latexsubs)
(puthash "\\upvarbeta" "ϐ" julia-latexsubs)
(puthash "\\vartheta" "ϑ" julia-latexsubs)
(puthash "\\phi" "ϕ" julia-latexsubs)
(puthash "\\varpi" "ϖ" julia-latexsubs)
(puthash "\\upoldKoppa" "Ϙ" julia-latexsubs)
(puthash "\\upoldkoppa" "ϙ" julia-latexsubs)
(puthash "\\Stigma" "Ϛ" julia-latexsubs)
(puthash "\\upstigma" "ϛ" julia-latexsubs)
(puthash "\\Digamma" "Ϝ" julia-latexsubs)
(puthash "\\digamma" "ϝ" julia-latexsubs)
(puthash "\\Koppa" "Ϟ" julia-latexsubs)
(puthash "\\upkoppa" "ϟ" julia-latexsubs)
(puthash "\\Sampi" "Ϡ" julia-latexsubs)
(puthash "\\upsampi" "ϡ" julia-latexsubs)
(puthash "\\varkappa" "ϰ" julia-latexsubs)
(puthash "\\varrho" "ϱ" julia-latexsubs)
(puthash "\\textTheta" "ϴ" julia-latexsubs)
(puthash "\\epsilon" "ϵ" julia-latexsubs)
(puthash "\\backepsilon" "϶" julia-latexsubs)
(puthash "\\^A" "\u1d2c" julia-latexsubs)
(puthash "\\^B" "\u1d2e" julia-latexsubs)
(puthash "\\^D" "\u1d30" julia-latexsubs)
(puthash "\\^E" "\u1d31" julia-latexsubs)
(puthash "\\^G" "\u1d33" julia-latexsubs)
(puthash "\\^H" "\u1d34" julia-latexsubs)
(puthash "\\^I" "\u1d35" julia-latexsubs)
(puthash "\\^J" "\u1d36" julia-latexsubs)
(puthash "\\^K" "\u1d37" julia-latexsubs)
(puthash "\\^L" "\u1d38" julia-latexsubs)
(puthash "\\^M" "\u1d39" julia-latexsubs)
(puthash "\\^N" "\u1d3a" julia-latexsubs)
(puthash "\\^O" "\u1d3c" julia-latexsubs)
(puthash "\\^P" "\u1d3e" julia-latexsubs)
(puthash "\\^R" "\u1d3f" julia-latexsubs)
(puthash "\\^T" "\u1d40" julia-latexsubs)
(puthash "\\^U" "\u1d41" julia-latexsubs)
(puthash "\\^W" "\u1d42" julia-latexsubs)
(puthash "\\^a" "\u1d43" julia-latexsubs)
(puthash "\\^alpha" "\u1d45" julia-latexsubs)
(puthash "\\^b" "\u1d47" julia-latexsubs)
(puthash "\\^d" "\u1d48" julia-latexsubs)
(puthash "\\^e" "\u1d49" julia-latexsubs)
(puthash "\\^epsilon" "\u1d4b" julia-latexsubs)
(puthash "\\^g" "\u1d4d" julia-latexsubs)
(puthash "\\^k" "\u1d4f" julia-latexsubs)
(puthash "\\^m" "\u1d50" julia-latexsubs)
(puthash "\\^o" "\u1d52" julia-latexsubs)
(puthash "\\^p" "\u1d56" julia-latexsubs)
(puthash "\\^t" "\u1d57" julia-latexsubs)
(puthash "\\^u" "\u1d58" julia-latexsubs)
(puthash "\\^v" "\u1d5b" julia-latexsubs)
(puthash "\\^beta" "\u1d5d" julia-latexsubs)
(puthash "\\^gamma" "\u1d5e" julia-latexsubs)
(puthash "\\^delta" "\u1d5f" julia-latexsubs)
(puthash "\\^phi" "\u1d60" julia-latexsubs)
(puthash "\\^chi" "\u1d61" julia-latexsubs)
(puthash "\\_i" "\u1d62" julia-latexsubs)
(puthash "\\_r" "\u1d63" julia-latexsubs)
(puthash "\\_u" "\u1d64" julia-latexsubs)
(puthash "\\_v" "\u1d65" julia-latexsubs)
(puthash "\\_beta" "\u1d66" julia-latexsubs)
(puthash "\\_gamma" "\u1d67" julia-latexsubs)
(puthash "\\_rho" "\u1d68" julia-latexsubs)
(puthash "\\_phi" "\u1d69" julia-latexsubs)
(puthash "\\_chi" "\u1d6a" julia-latexsubs)
(puthash "\\^c" "\u1d9c" julia-latexsubs)
(puthash "\\^f" "\u1da0" julia-latexsubs)
(puthash "\\^iota" "\u1da5" julia-latexsubs)
(puthash "\\^Phi" "\u1db2" julia-latexsubs)
(puthash "\\^z" "\u1dbb" julia-latexsubs)
(puthash "\\^theta" "\u1dbf" julia-latexsubs)
(puthash "\\enspace" " " julia-latexsubs)
(puthash "\\quad" " " julia-latexsubs)
(puthash "\\thickspace" " " julia-latexsubs)
(puthash "\\thinspace" " " julia-latexsubs)
(puthash "\\hspace" " " julia-latexsubs)
(puthash "\\endash" "–" julia-latexsubs)
(puthash "\\emdash" "—" julia-latexsubs)