-
Notifications
You must be signed in to change notification settings - Fork 47
/
hy-font-lock.el
625 lines (528 loc) · 14.4 KB
/
hy-font-lock.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
;;; hy-font-lock.el --- Font Locks -*- lexical-binding: t -*-
;; Copyright © 2013 Julien Danjou <[email protected]>
;; © 2017-2019 Eric Kaschalk <[email protected]>
;;
;; hy-mode 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, or (at your option)
;; any later version.
;;
;; hy-mode 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 hy-mode. If not, see <http://www.gnu.org/licenses/>.
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Font lock definitions and setup for `hy-mode'.
;; Font locks are organized and exposed at the end: `hy-font-lock-kwds'
;; Comint Font locks are exposed in: `inferior-hy-font-lock-kwds'
;; Also implements docstring detection: `hy-font-lock-syntactic-face-function'
;;; Code:
(require 'hy-base)
(defvar hy-font-lock-highlight-percent-args? t
"Whether to highlight '%i' symbols in Hy's clojure-like syntax for lambdas.")
;;; Names Lists
;;;; Hy Builtins
(defconst hy-font-lock--hy-builtins
'("*map"
"accumulate"
"assoc"
"butlast"
"calling-module-name"
"chain"
"coll?"
"combinations"
"comp"
"complement"
"compress"
"constantly"
"count"
"cut"
"cycle"
"dec"
"defmain"
"del"
"disassemble"
"distinct"
"doto"
"drop"
"drop-last"
"drop-while"
"empty?"
"even?"
"every?"
"filter"
"first"
"flatten"
"float?"
"fraction"
"gensym"
"get"
"group-by"
"identity"
"inc"
"instance?"
"integer"
"integer-char?"
"integer?"
"interleave"
"interpose"
"is"
"is-not"
"islice"
"iterable?"
"iterate"
"iterator?"
"juxt"
"keyword"
"keyword?"
"last"
"macroexpand"
"macroexpand-1"
"merge-with"
"multicombinations"
"name"
"neg?"
"none?"
"nth"
"numeric?"
"odd?"
"partition"
"permutations"
"pos?"
"product"
"quasiquote"
"quote"
"read"
"read-str"
"reduce"
"remove"
"repeat"
"repeatedly"
"rest"
"second"
"setv"
"some"
"string"
"string?"
"symbol?"
"take"
"take-nth"
"take-while"
"tee"
"unquote"
"unquote-splice"
"xor"
"zero?"
"zip"
"zip-longest"
"--macros--" "__macros__")
"Hy-only builtin names.")
;;;; Python Builtins
(defconst hy-font-lock--python-builtins
'("abs"
"all"
"any"
"ascii"
"bytes"
"bin"
"bool"
"bytearray"
"callable"
"chr"
"compile"
"complex"
"delattr"
"dict"
"dir"
"divmod"
"enumerate"
"eval"
"exec"
"float"
"format"
"frozenset"
"getattr"
"globals"
"hasattr"
"hash"
"help"
"hex"
"id"
"input"
"int"
"isinstance"
"issubclass"
"iter"
"len"
"list"
"locals"
"map"
"max"
"memoryview"
"min"
"next"
"object"
"oct"
"open"
"ord"
"pow"
"range"
"repr"
"reversed"
"round"
"set"
"setattr"
"slice"
"sorted"
"str"
"sum"
"super"
"tuple"
"type"
"vars"
"--package--" "__package__"
"--import--" "__import__"
"--all--" "__all__"
"--doc--" "__doc__"
"--name--" "__name__")
"Builtin names available in Python normally.")
;;;; Constants
(defconst hy-font-lock--constants
'("True"
"False"
"None"
"Ellipsis"
"NotImplemented"
"nil" ; Provided for those that alias None as nil, not a part of Hy
)
"Constant names in Hy.")
;;;; Exceptions
(defconst hy-font-lock--exceptions
'("ArithmeticError" "AssertionError" "AttributeError" "BaseException"
"DeprecationWarning" "EOFError" "EnvironmentError" "Exception"
"FloatingPointError" "FutureWarning" "GeneratorExit" "IOError" "ImportError"
"ImportWarning" "IndexError" "KeyError" "KeyboardInterrupt" "LookupError"
"MemoryError" "NameError" "NotImplementedError" "OSError" "OverflowError"
"PendingDeprecationWarning" "ReferenceError" "RuntimeError" "RuntimeWarning"
"StopIteration" "SyntaxError" "SyntaxWarning" "SystemError" "SystemExit"
"TypeError" "UnboundLocalError" "UnicodeDecodeError" "UnicodeEncodeError"
"UnicodeError" "UnicodeTranslateError" "UnicodeWarning" "UserWarning"
"VMSError" "ValueError" "Warning" "WindowsError" "ZeroDivisionError"
"BufferError" "BytesWarning" "IndentationError" "ResourceWarning"
"TabError")
"Exception and error names.")
;;;; Definitions
(defconst hy-font-lock--definitions
'(;; Functions
"defn" "defn/a"
;; Macros
"defmacro" "defmacro/g!" "defmacro!"
;; Tag Macros
"deftag"
;; Defining __main__
"defmain"
;; Multi-methods
"defmulti" "defmethod")
"Names in Hy that define functions, macros, etc.")
;;;; Operators
(defconst hy-font-lock--operators
'("!=" "%" "%=" "&" "&=" "*" "**" "**=" "*=" "+" "+=" "," "-"
"-=" "/" "//" "//=" "/=" "<" "<<" "<<=" "<=" "=" ">" ">=" ">>" ">>="
"^" "^=" "|" "|=" "~")
"Operators in Hy.")
;;;; Special Names
(defconst hy-font-lock--special-names
'(;; Looping
"for" "for/a"
"dfor" "lfor" "sfor" ; comprehensions
"loop" "recur" ; hy.contrib.loop
;; Threading
"->" "->>" "as->"
;; Flow control
"return"
"if" "if*" "if-not" "lif" "lif-not"
"else" "unless" "when"
"break" "continue" "while"
"cond"
"do"
;; Functional
"fn" "fn/a"
"await"
"yield" "yield-from"
"with" "with*" "with/a" "with/a*"
"with-gensyms"
;; Error Handling
"except" "try" "throw" "raise" "catch" "finally" "assert"
;; Python builtins (and shadowed calls to builtins)
"print"
"not" "and" "or"
"in" "not-in"
;; Namespaces
"global" "nonlocal"
;; Evaluation
"eval" "eval-and-compile" "eval-when-compile")
"Special names like compiler stuff to highlight as keywords.")
;;;; Anaphorics
(defconst hy-font-lock--anaphorics
'("ap-dotimes"
"ap-each"
"ap-each-while"
"ap-filter"
"ap-first"
"ap-if"
"ap-last"
"ap-map"
"ap-map-when"
"ap-reduce"
"ap-reject")
"Hy anaphoric contrib keywords.")
;;; Keywords
;;;; Based on Names Lists
(defconst hy-font-lock--kwds-builtins
(list
(rx-to-string
`(: symbol-start
(or ,@hy-font-lock--hy-builtins
,@hy-font-lock--python-builtins
,@hy-font-lock--operators
,@hy-font-lock--anaphorics)
symbol-end))
'(0 font-lock-builtin-face))
"Hy builtin keywords.")
(defconst hy-font-lock--kwds-constants
(list
(rx-to-string
`(: symbol-start
(or ,@hy-font-lock--constants)
symbol-end))
'(0 font-lock-constant-face))
"Hy constant keywords.")
(defconst hy-font-lock--kwds-definitions
(list
(rx-to-string
`(: "("
symbol-start
(group-n 1 (or ,@hy-font-lock--definitions))
(1+ space)
(group-n 2 (1+ word))))
'(1 font-lock-keyword-face)
'(2 font-lock-function-name-face nil t))
"Hy definition keywords.")
(defconst hy-font-lock--kwds-exceptions
(list
(rx-to-string
`(: symbol-start
(or ,@hy-font-lock--exceptions)
symbol-end))
'(0 font-lock-type-face))
"Hy exception keywords.")
(defconst hy-font-lock--kwds-special-names
(list
(rx-to-string
`(: symbol-start
(or ,@hy-font-lock--special-names)
symbol-end))
'(0 font-lock-keyword-face))
"Hy special names keywords.")
;;;; Static
(defconst hy-font-lock--kwds-class
(list
(rx (group-n 1 "defclass")
(1+ space)
(group-n 2 (1+ word)))
'(1 font-lock-keyword-face)
'(2 font-lock-type-face))
"Hy class keywords.")
(defconst hy-font-lock--kwds-decorators
(list
(rx
(or (: "#@"
(syntax open-parenthesis))
(: symbol-start
"with-decorator"
symbol-end
(1+ space)))
(1+ word))
'(0 font-lock-type-face))
"Hylight the symbol after `#@' or `with-decorator' macros keywords.")
(defconst hy-font-lock--kwds-imports
(list
(rx symbol-start
(or "import"
"require"
":as")
symbol-end)
'(0 font-lock-keyword-face))
"Hy import keywords.")
(defconst hy-font-lock--kwds-self
(list
(rx symbol-start
(group "self")
(or "." symbol-end))
'(1 font-lock-keyword-face))
"Hy self keyword.")
(defconst hy-font-lock--kwds-tag-macros
(list
(rx "#"
;; #* is unpacking, #@ decorator, #[ bracket str
(not (any "*"
"@"
"["
")"
space))
(0+ (syntax word)))
'(0 font-lock-function-name-face))
"Hylight tag macros, ie. `#tag-macro', so they stand out.")
;;;; Misc
(defconst hy-font-lock--kwds-anonymous-funcs
(list
(rx symbol-start
(group "%" (1+ digit))
(or "." symbol-end))
'(1 font-lock-variable-name-face))
"Hy '#%(print %1 %2)' styling anonymous variables.")
(defconst hy-font-lock--kwds-func-modifiers
(list
(rx symbol-start
"&"
(1+ word))
'(0 font-lock-type-face))
"Hy '&rest/&kwonly/...' styling.")
(defconst hy-font-lock--kwds-kwargs
(list
(rx symbol-start
":"
(1+ word))
'(0 font-lock-constant-face))
"Hy ':kwarg' styling.")
(defconst hy-font-lock--kwds-shebang
(list
(rx buffer-start
"#!"
(0+ not-newline)
eol)
'(0 font-lock-comment-face))
"Hy shebang line.")
(defconst hy-font-lock--kwds-unpacking
(list
(rx (or "#*"
"#**")
symbol-end)
'(0 font-lock-keyword-face))
"Hy #* arg and #** kwarg unpacking keywords.")
(defconst hy-font-lock--kwds-variables
(list
(rx symbol-start
"setv"
symbol-end
(1+ space)
(group (1+ word)))
'(1 font-lock-variable-name-face))
"Hylight variable names in setv/def, only first name.")
;;;; Advanced
(defconst hy-font-lock--tag-comment-prefix-rx
(rx "#_"
(* " ")
(group-n 1 (not (any " "))))
"The regex to match #_ tag comment prefixes.")
(defun hy-font-lock--search-comment-macro (limit)
"Search for a comment forward stopping at LIMIT."
(-when-let* ((_ (re-search-forward hy-font-lock--tag-comment-prefix-rx limit t))
(md (match-data))
(start (match-beginning 1))
(state (syntax-ppss start)))
(if (hy--in-string-or-comment? state)
(hy-font-lock--search-comment-macro limit)
(goto-char start)
(forward-sexp)
(setf (elt md 3) (point))
(set-match-data md)
t)))
(defconst hy-font-lock--kwds-tag-comment-prefix
(list #'hy-font-lock--search-comment-macro
'(1 font-lock-comment-face t))
"Support for higlighting #_(form) the form as a comment.")
;;; Syntactic Face Function
;;;; Utilities
(defun hy-font-lock--string-is-module-docstring? (syntax)
"Is string SYNTAX specifically a module docstring?"
(= 1 (hy--syntax->string-start syntax)))
(defun hy-font-lock--string-is-function-docstring? (syntax)
"Is string SYNTAX specifically a function docstring?"
(-when-let (inner-symbol (hy--syntax->inner-symbol syntax))
(when (and (not (s-equals? "defmethod" inner-symbol))
(s-matches? (rx "def" (not blank)) inner-symbol))
(let ((start-point (point)))
(save-excursion
(hy--goto-inner-sexp syntax)
(-when-let* ((start (ignore-errors (scan-sexps (point) 3)))
(end (ignore-errors (scan-sexps (point) 4))))
(<= start start-point end)))))))
;;;; Exposes
(defun hy-font-lock-syntactic-face-function (syntax)
"Return syntactic face function for synatax STATE."
(if (hy--in-string? syntax)
(if (or (hy-font-lock--string-is-module-docstring? syntax)
(hy-font-lock--string-is-function-docstring? syntax))
font-lock-doc-face
font-lock-string-face)
font-lock-comment-face))
;;; Comint Support
(defun hy-font-lock--kwd->comint-kwd (kwd)
"Converts a `font-lock-keywords' KWD for `comint-mode' input fontification.
This is a rather clever solution to fontifying repl input. I wrote a post
about this idea here: http://www.modernemacs.com/post/comint-highlighting/.
The `comint-snapshot-last-prompt' call within `comint-send' is what makes
this solution tick as future attempts to font-lock prior to the current
prompt will be frozen by comint.
It actually implements comint fontification for arbitrary major modes and have
applied with success to `ielm'."
(-let (((matcher . match-highlights) kwd))
`((lambda (limit)
;; Matcher can be a function or a regex
(when ,(if (symbolp matcher)
`(,matcher limit)
`(re-search-forward ,matcher limit t))
;; While the SUBEXP can be anything, this search always can use zero
(-let ((start (match-beginning 0))
((comint-last-start . comint-last-end) comint-last-prompt)
(state (syntax-ppss)))
(and (> start comint-last-start)
;; Make sure not in comment or string
;; have to manually do this in custom MATCHERs
(not (or (nth 3 state) (nth 4 state)))))))
,@match-highlights)))
;;; Font Lock Keywords
(defconst hy-font-lock-kwds
(list hy-font-lock--kwds-builtins
hy-font-lock--kwds-class
hy-font-lock--kwds-constants
hy-font-lock--kwds-definitions
hy-font-lock--kwds-decorators
hy-font-lock--kwds-exceptions
hy-font-lock--kwds-func-modifiers
hy-font-lock--kwds-imports
hy-font-lock--kwds-kwargs
hy-font-lock--kwds-self
hy-font-lock--kwds-shebang
hy-font-lock--kwds-special-names
hy-font-lock--kwds-tag-macros
hy-font-lock--kwds-unpacking
hy-font-lock--kwds-variables
;; Advanced kwds
hy-font-lock--kwds-tag-comment-prefix
;; Optional kwds
(when hy-font-lock-highlight-percent-args?
hy-font-lock--kwds-anonymous-funcs))
"All Hy font lock keywords.")
(defconst inferior-hy-font-lock-kwds
(-map #'hy-font-lock--kwd->comint-kwd hy-font-lock-kwds)
"Comint-compatible version of `hy-font-lock-kwds'.
See `hy-font-lock--kwd->comint-kwd' for details.")
;;; Provide:
(provide 'hy-font-lock)
;;; hy-font-lock.el ends here