forked from clojure-emacs/clojure-mode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clojure-mode.el
3342 lines (3008 loc) · 126 KB
/
clojure-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
;;; clojure-mode.el --- Major mode for Clojure code -*- lexical-binding: t; -*-
;; Copyright © 2007-2013 Jeffrey Chu, Lennart Staflin, Phil Hagelberg
;; Copyright © 2013-2024 Bozhidar Batsov, Artur Malabarba, Magnar Sveen
;;
;; Authors: Jeffrey Chu <jochu0@gmail.com>
;; Lennart Staflin <lenst@lysator.liu.se>
;; Phil Hagelberg <technomancy@gmail.com>
;; Bozhidar Batsov <bozhidar@batsov.dev>
;; Artur Malabarba <bruce.connor.am@gmail.com>
;; Magnar Sveen <magnars@gmail.com>
;; Maintainer: Bozhidar Batsov <bozhidar@batsov.dev>
;; URL: https://github.com/clojure-emacs/clojure-mode
;; Keywords: languages clojure clojurescript lisp
;; Version: 5.20.0-snapshot
;; Package-Requires: ((emacs "25.1"))
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Provides font-lock, indentation, navigation and basic refactoring for the
;; Clojure programming language (https://clojure.org).
;; Using clojure-mode with paredit or smartparens is highly recommended.
;; Here are some example configurations:
;; ;; require or autoload paredit-mode
;; (add-hook 'clojure-mode-hook #'paredit-mode)
;; ;; require or autoload smartparens
;; (add-hook 'clojure-mode-hook #'smartparens-strict-mode)
;; See inf-clojure (https://github.com/clojure-emacs/inf-clojure) for
;; basic interaction with Clojure subprocesses.
;; See CIDER (https://github.com/clojure-emacs/cider) for
;; better interaction with subprocesses via nREPL.
;;; License:
;; 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Code:
(defvar calculate-lisp-indent-last-sexp)
(defvar delete-pair-blink-delay)
(defvar font-lock-beg)
(defvar font-lock-end)
(defvar paredit-space-for-delimiter-predicates)
(defvar paredit-version)
(defvar paredit-mode)
(require 'cl-lib)
(require 'imenu)
(require 'newcomment)
(require 'thingatpt)
(require 'align)
(require 'subr-x)
(require 'lisp-mnt)
(declare-function lisp-fill-paragraph "lisp-mode" (&optional justify))
(defgroup clojure nil
"Major mode for editing Clojure code."
:prefix "clojure-"
:group 'languages
:link '(url-link :tag "GitHub" "https://github.com/clojure-emacs/clojure-mode")
:link '(emacs-commentary-link :tag "Commentary" "clojure-mode"))
(defconst clojure-mode-version
(eval-when-compile
(lm-version (or load-file-name buffer-file-name)))
"The current version of `clojure-mode'.")
(defface clojure-keyword-face
'((t (:inherit font-lock-constant-face)))
"Face used to font-lock Clojure keywords (:something)."
:package-version '(clojure-mode . "3.0.0"))
(defface clojure-character-face
'((t (:inherit font-lock-string-face)))
"Face used to font-lock Clojure character literals."
:package-version '(clojure-mode . "3.0.0"))
(defcustom clojure-indent-style 'always-align
"Indentation style to use for function forms and macro forms.
For forms that start with a keyword see `clojure-indent-keyword-style'.
There are two cases of interest configured by this variable.
- Case (A) is when at least one function argument is on the same
line as the function name.
- Case (B) is the opposite (no arguments are on the same line as
the function name). Note that the body of macros is not
affected by this variable, it is always indented by
`lisp-body-indent' (default 2) spaces.
Note that this variable configures the indentation of function
forms (and function-like macros), it does not affect macros that
already use special indentation rules.
The possible values for this variable are keywords indicating how
to indent function forms.
`always-align' - Follow the same rules as `lisp-mode'. All
args are vertically aligned with the first arg in case (A),
and vertically aligned with the function name in case (B).
For instance:
(reduce merge
some-coll)
(reduce
merge
some-coll)
`always-indent' - All args are indented like a macro body.
(reduce merge
some-coll)
(reduce
merge
some-coll)
`align-arguments' - Case (A) is indented like `lisp', and
case (B) is indented like a macro body.
(reduce merge
some-coll)
(reduce
merge
some-coll)"
:safe #'symbolp
:type '(choice (const :tag "Same as `lisp-mode'" always-align)
(const :tag "Indent like a macro body" always-indent)
(const :tag "Indent like a macro body unless first arg is on the same line"
align-arguments))
:package-version '(clojure-mode . "5.2.0"))
(defcustom clojure-indent-keyword-style 'always-align
"Indentation style to use for forms that start with a keyword.
For function/macro forms, see `clojure-indent-style'.
There are two cases of interest configured by this variable.
- Case (A) is when at least one argument following the keyword is
on the same line as the keyword.
- Case (B) is the opposite (no arguments are on the same line as
the keyword).
The possible values for this variable are keywords indicating how
to indent keyword invocation forms.
`always-align' - Follow the same rules as `lisp-mode'. All
args are vertically aligned with the first arg in case (A),
and vertically aligned with the function name in case (B).
For instance:
(:require [foo.bar]
[bar.baz])
(:require
[foo.bar]
[bar.baz])
`always-indent' - All args are indented like a macro body.
(:require [foo.bar]
[bar.baz])
(:x
location
0)
`align-arguments' - Case (A) is indented like `lisp', and
case (B) is indented like a macro body.
(:require [foo.bar]
[bar.baz])
(:x
location
0)"
:safe #'symbolp
:type '(choice (const :tag "Same as `lisp-mode'" always-align)
(const :tag "Indent like a macro body" always-indent)
(const :tag "Indent like a macro body unless first arg is on the same line"
align-arguments))
:package-version '(clojure-mode . "5.19.0"))
(defcustom clojure-use-backtracking-indent t
"When non-nil, enable context sensitive indentation."
:type 'boolean
:safe 'booleanp)
(defcustom clojure-max-backtracking 3
"Maximum amount to backtrack up a list to check for context."
:type 'integer
:safe 'integerp)
(defcustom clojure-docstring-fill-column fill-column
"Value of `fill-column' to use when filling a docstring."
:type 'integer
:safe 'integerp)
(defcustom clojure-docstring-fill-prefix-width 2
"Width of `fill-prefix' when filling a docstring.
The default value conforms with the de facto convention for
Clojure docstrings, aligning the second line with the opening
double quotes on the third column."
:type 'integer
:safe 'integerp)
(defcustom clojure-omit-space-between-tag-and-delimiters '(?\[ ?\{ ?\()
"Allowed opening delimiter characters after a reader literal tag.
For example, \[ is allowed in :db/id[:db.part/user]."
:type '(set (const :tag "[" ?\[)
(const :tag "{" ?\{)
(const :tag "(" ?\()
(const :tag "\"" ?\"))
:safe (lambda (value)
(and (listp value)
(cl-every 'characterp value))))
(defcustom clojure-build-tool-files
'("project.clj" ; Leiningen
"build.boot" ; Boot
"build.gradle" ; Gradle
"build.gradle.kts" ; Gradle
"deps.edn" ; Clojure CLI (a.k.a. tools.deps)
"shadow-cljs.edn" ; shadow-cljs
"bb.edn" ; babashka
"nbb.edn" ; nbb
"basilisp.edn" ; Basilisp (Python)
)
"A list of files, which identify a Clojure project's root.
Out-of-the box `clojure-mode' understands lein, boot, gradle,
shadow-cljs, tools.deps, babashka and nbb."
:type '(repeat string)
:package-version '(clojure-mode . "5.0.0")
:safe (lambda (value)
(and (listp value)
(cl-every 'stringp value))))
(defcustom clojure-directory-prefixes
'("\\`clj[scxd]?\\.")
"A list of directory prefixes used by `clojure-expected-ns'.
The prefixes are used to generate the correct namespace."
:type '(repeat string)
:package-version '(clojure-mode . "5.14.0")
:safe (lambda (value)
(and (listp value)
(cl-every 'stringp value))))
(defcustom clojure-project-root-function #'clojure-project-root-path
"Function to locate clojure project root directory."
:type 'function
:risky t
:package-version '(clojure-mode . "5.7.0"))
(defcustom clojure-refactor-map-prefix (kbd "C-c C-r")
"Clojure refactor keymap prefix."
:type 'string
:package-version '(clojure-mode . "5.6.0"))
(defvar clojure-refactor-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-t") #'clojure-thread)
(define-key map (kbd "t") #'clojure-thread)
(define-key map (kbd "C-u") #'clojure-unwind)
(define-key map (kbd "u") #'clojure-unwind)
(define-key map (kbd "C-f") #'clojure-thread-first-all)
(define-key map (kbd "f") #'clojure-thread-first-all)
(define-key map (kbd "C-l") #'clojure-thread-last-all)
(define-key map (kbd "l") #'clojure-thread-last-all)
(define-key map (kbd "C-p") #'clojure-cycle-privacy)
(define-key map (kbd "p") #'clojure-cycle-privacy)
(define-key map (kbd "C-(") #'clojure-convert-collection-to-list)
(define-key map (kbd "(") #'clojure-convert-collection-to-list)
(define-key map (kbd "C-'") #'clojure-convert-collection-to-quoted-list)
(define-key map (kbd "'") #'clojure-convert-collection-to-quoted-list)
(define-key map (kbd "C-{") #'clojure-convert-collection-to-map)
(define-key map (kbd "{") #'clojure-convert-collection-to-map)
(define-key map (kbd "C-[") #'clojure-convert-collection-to-vector)
(define-key map (kbd "[") #'clojure-convert-collection-to-vector)
(define-key map (kbd "C-#") #'clojure-convert-collection-to-set)
(define-key map (kbd "#") #'clojure-convert-collection-to-set)
(define-key map (kbd "C-i") #'clojure-cycle-if)
(define-key map (kbd "i") #'clojure-cycle-if)
(define-key map (kbd "C-w") #'clojure-cycle-when)
(define-key map (kbd "w") #'clojure-cycle-when)
(define-key map (kbd "C-o") #'clojure-cycle-not)
(define-key map (kbd "o") #'clojure-cycle-not)
(define-key map (kbd "n i") #'clojure-insert-ns-form)
(define-key map (kbd "n h") #'clojure-insert-ns-form-at-point)
(define-key map (kbd "n u") #'clojure-update-ns)
(define-key map (kbd "n s") #'clojure-sort-ns)
(define-key map (kbd "n r") #'clojure-rename-ns-alias)
(define-key map (kbd "s i") #'clojure-introduce-let)
(define-key map (kbd "s m") #'clojure-move-to-let)
(define-key map (kbd "s f") #'clojure-let-forward-slurp-sexp)
(define-key map (kbd "s b") #'clojure-let-backward-slurp-sexp)
(define-key map (kbd "C-a") #'clojure-add-arity)
(define-key map (kbd "a") #'clojure-add-arity)
(define-key map (kbd "-") #'clojure-toggle-ignore)
(define-key map (kbd "C--") #'clojure-toggle-ignore)
(define-key map (kbd "_") #'clojure-toggle-ignore-surrounding-form)
(define-key map (kbd "C-_") #'clojure-toggle-ignore-surrounding-form)
(define-key map (kbd "P") #'clojure-promote-fn-literal)
(define-key map (kbd "C-P") #'clojure-promote-fn-literal)
map)
"Keymap for Clojure refactoring commands.")
(fset 'clojure-refactor-map clojure-refactor-map)
(defvar clojure-mode-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map prog-mode-map)
(define-key map (kbd "C-:") #'clojure-toggle-keyword-string)
(define-key map (kbd "C-c SPC") #'clojure-align)
(define-key map clojure-refactor-map-prefix 'clojure-refactor-map)
(easy-menu-define clojure-mode-menu map "Clojure Mode Menu"
'("Clojure"
["Toggle between string & keyword" clojure-toggle-keyword-string]
["Align expression" clojure-align]
["Cycle privacy" clojure-cycle-privacy]
["Cycle if, if-not" clojure-cycle-if]
["Cycle when, when-not" clojure-cycle-when]
["Cycle not" clojure-cycle-not]
["Toggle #_ ignore form" clojure-toggle-ignore]
["Toggle #_ ignore of surrounding form" clojure-toggle-ignore-surrounding-form]
["Add function arity" clojure-add-arity]
["Promote #() fn literal" clojure-promote-fn-literal]
("ns forms"
["Insert ns form at the top" clojure-insert-ns-form]
["Insert ns form here" clojure-insert-ns-form-at-point]
["Update ns form" clojure-update-ns]
["Sort ns form" clojure-sort-ns]
["Rename ns alias" clojure-rename-ns-alias])
("Convert collection"
["Convert to list" clojure-convert-collection-to-list]
["Convert to quoted list" clojure-convert-collection-to-quoted-list]
["Convert to map" clojure-convert-collection-to-map]
["Convert to vector" clojure-convert-collection-to-vector]
["Convert to set" clojure-convert-collection-to-set])
("Refactor -> and ->>"
["Thread once more" clojure-thread]
["Fully thread a form with ->" clojure-thread-first-all]
["Fully thread a form with ->>" clojure-thread-last-all]
"--"
["Unwind once" clojure-unwind]
["Fully unwind a threading macro" clojure-unwind-all])
("Let expression"
["Introduce let" clojure-introduce-let]
["Move to let" clojure-move-to-let]
["Forward slurp form into let" clojure-let-forward-slurp-sexp]
["Backward slurp form into let" clojure-let-backward-slurp-sexp])
("Documentation"
["View a Clojure guide" clojure-view-guide]
["View a Clojure reference section" clojure-view-reference-section]
["View the Clojure cheatsheet" clojure-view-cheatsheet]
["View the Clojure style guide" clojure-view-style-guide])
"--"
["Report a clojure-mode bug" clojure-mode-report-bug]
["Clojure-mode version" clojure-mode-display-version]))
map)
"Keymap for Clojure mode.")
(defvar clojure-mode-syntax-table
(let ((table (make-syntax-table)))
;; Initialize ASCII charset as symbol syntax
;; Control characters from 0-31 default to the punctuation syntax class
(modify-syntax-entry '(32 . 127) "_" table)
;; Word syntax
(modify-syntax-entry '(?0 . ?9) "w" table)
(modify-syntax-entry '(?a . ?z) "w" table)
(modify-syntax-entry '(?A . ?Z) "w" table)
;; Whitespace
(modify-syntax-entry ?\s " " table)
(modify-syntax-entry ?\xa0 " " table) ; non-breaking space
(modify-syntax-entry ?\t " " table)
(modify-syntax-entry ?\f " " table)
(modify-syntax-entry ?\r " " table)
;; Setting commas as whitespace makes functions like `delete-trailing-whitespace' behave unexpectedly (#561)
(modify-syntax-entry ?, "." table)
;; Delimiters
(modify-syntax-entry ?\( "()" table)
(modify-syntax-entry ?\) ")(" table)
(modify-syntax-entry ?\[ "(]" table)
(modify-syntax-entry ?\] ")[" table)
(modify-syntax-entry ?\{ "(}" table)
(modify-syntax-entry ?\} "){" table)
;; Prefix chars
(modify-syntax-entry ?` "'" table)
(modify-syntax-entry ?~ "'" table)
(modify-syntax-entry ?^ "'" table)
(modify-syntax-entry ?@ "'" table)
(modify-syntax-entry ?? "_ p" table) ; ? is a prefix outside symbols
(modify-syntax-entry ?# "_ p" table) ; # is allowed inside keywords (#399)
(modify-syntax-entry ?' "_ p" table) ; ' is allowed anywhere but the start of symbols
;; Others
(modify-syntax-entry ?\; "<" table) ; comment start
(modify-syntax-entry ?\n ">" table) ; comment end
(modify-syntax-entry ?\" "\"" table) ; string
(modify-syntax-entry ?\\ "\\" table) ; escape
table)
"Syntax table for Clojure mode.")
(defconst clojure--prettify-symbols-alist
'(("fn" . ?λ)))
(defvar-local clojure-expected-ns-function nil
"The function used to determine the expected namespace of a file.
`clojure-mode' ships a basic function named `clojure-expected-ns'
that does basic heuristics to figure this out.
CIDER provides a more complex version which does classpath analysis.")
(defun clojure-mode-display-version ()
"Display the current `clojure-mode-version' in the minibuffer."
(interactive)
(message "clojure-mode (version %s)" clojure-mode-version))
(defconst clojure-mode-report-bug-url "https://github.com/clojure-emacs/clojure-mode/issues/new"
"The URL to report a `clojure-mode' issue.")
(defun clojure-mode-report-bug ()
"Report a bug in your default browser."
(interactive)
(browse-url clojure-mode-report-bug-url))
(defconst clojure-guides-base-url "https://clojure.org/guides/"
"The base URL for official Clojure guides.")
(defconst clojure-guides '(("Getting Started" . "getting_started")
("Install Clojure" . "install_clojure")
("Editors" . "editors")
("Structural Editing" . "structural_editing")
("REPL Programming" . "repl/introduction")
("Learn Clojure" . "learn/clojure")
("FAQ" . "faq")
("spec" . "spec")
("Reading Clojure Characters" . "weird_characters")
("Destructuring" . "destructuring")
("Threading Macros" . "threading_macros")
("Equality" . "equality")
("Comparators" . "comparators")
("Reader Conditionals" . "reader_conditionals")
("Higher Order Functions" . "higher_order_functions")
("Dev Startup Time" . "dev_startup_time")
("Deps and CLI" . "deps_and_cli")
("tools.build" . "tools_build")
("core.async Walkthrough" . "async_walkthrough")
("Go Block Best Practices" . "core_async_go")
("test.check" . "test_check_beginner"))
"A list of all official Clojure guides.")
(defun clojure-view-guide ()
"Open a Clojure guide in your default browser.
The command will prompt you to select one of the available guides."
(interactive)
(let ((guide (completing-read "Select a guide: " (mapcar #'car clojure-guides))))
(when guide
(let ((guide-url (concat clojure-guides-base-url (cdr (assoc guide clojure-guides)))))
(browse-url guide-url)))))
(defconst clojure-reference-base-url "https://clojure.org/reference/"
"The base URL for the official Clojure reference.")
(defconst clojure-reference-sections '(("The Reader" . "reader")
("The REPL and main" . "repl_and_main")
("Evaluation" . "evaluation")
("Special Forms" . "special_forms")
("Macros" . "macros")
("Other Functions" . "other_functions")
("Data Structures" . "data_structures")
("Datatypes" . "datatypes")
("Sequences" . "sequences")
("Transients" . "transients")
("Transducers" . "transducers")
("Multimethods and Hierarchies" . "multimethods")
("Protocols" . "protocols")
("Metadata" . "metadata")
("Namespaces" . "namespaces")
("Libs" . "libs")
("Vars and Environments" . "vars")
("Refs and Transactions" . "refs")
("Agents" . "agents")
("Atoms" . "atoms")
("Reducers" . "reducers")
("Java Interop" . "java_interop")
("Compilation and Class Generation" . "compilation")
("Other Libraries" . "other_libraries")
("Differences with Lisps" . "lisps")
("Deps and CLI" . "deps_and_cli")))
(defun clojure-view-reference-section ()
"Open a Clojure reference section in your default browser.
The command will prompt you to select one of the available sections."
(interactive)
(let ((section (completing-read "Select a reference section: " (mapcar #'car clojure-reference-sections))))
(when section
(let ((section-url (concat clojure-reference-base-url (cdr (assoc section clojure-reference-sections)))))
(browse-url section-url)))))
(defconst clojure-cheatsheet-url "https://clojure.org/api/cheatsheet"
"The URL of the official Clojure cheatsheet.")
(defun clojure-view-cheatsheet ()
"Open the Clojure cheatsheet in your default browser."
(interactive)
(browse-url clojure-cheatsheet-url))
(defconst clojure-style-guide-url "https://guide.clojure.style"
"The URL of the Clojure style guide.")
(defun clojure-view-style-guide ()
"Open the Clojure style guide in your default browser."
(interactive)
(browse-url clojure-style-guide-url))
(defun clojure-space-for-delimiter-p (endp delim)
"Prevent paredit from inserting useless spaces.
See `paredit-space-for-delimiter-predicates' for the meaning of
ENDP and DELIM."
(and (not endp)
;; don't insert after opening quotes, auto-gensym syntax, or reader tags
(not (looking-back
(if (member delim clojure-omit-space-between-tag-and-delimiters)
"\\_<\\(?:'+\\|#.*\\)"
"\\_<\\(?:'+\\|#\\)")
(line-beginning-position)))))
(defconst clojure--collection-tag-regexp "#\\(::[a-zA-Z0-9._-]*\\|:?\\([a-zA-Z0-9._-]+/\\)?[a-zA-Z0-9._-]+\\)"
"Collection reader macro tag regexp.
It is intended to check for allowed strings that can come before a
collection literal (e.g. '[]' or '{}'), as reader macro tags.
This includes #fully.qualified/my-ns[:kw val] and #::my-ns{:kw
val} as of Clojure 1.9.")
(make-obsolete-variable 'clojure--collection-tag-regexp nil "5.12.0")
(make-obsolete 'clojure-no-space-after-tag 'clojure-space-for-delimiter-p "5.12.0")
(declare-function paredit-open-curly "ext:paredit" t t)
(declare-function paredit-close-curly "ext:paredit" t t)
(declare-function paredit-convolute-sexp "ext:paredit")
(defvar clojure--let-regexp
"\(\\(when-let\\|if-let\\|let\\)\\(\\s-*\\|\\[\\)"
"Regexp matching let like expressions, i.e. \"let\", \"when-let\", \"if-let\".
The first match-group is the let expression.
The second match-group is the whitespace or the opening square
bracket if no whitespace between the let expression and the
bracket.")
(defun clojure--replace-let-bindings-and-indent (&rest _)
"Replace let bindings and indent."
(save-excursion
(backward-sexp)
(when (looking-back clojure--let-regexp nil)
(clojure--replace-sexps-with-bindings-and-indent))))
(defun clojure-paredit-setup (&optional keymap)
"Make \"paredit-mode\" play nice with `clojure-mode'.
If an optional KEYMAP is passed the changes are applied to it,
instead of to `clojure-mode-map'.
Also advice `paredit-convolute-sexp' when used on a let form as drop in
replacement for `cljr-expand-let`."
(when (>= paredit-version 21)
(let ((keymap (or keymap clojure-mode-map)))
(define-key keymap "{" #'paredit-open-curly)
(define-key keymap "}" #'paredit-close-curly))
(make-local-variable 'paredit-space-for-delimiter-predicates)
(add-to-list 'paredit-space-for-delimiter-predicates
#'clojure-space-for-delimiter-p)
(advice-add 'paredit-convolute-sexp :after #'clojure--replace-let-bindings-and-indent)))
(defun clojure-current-defun-name ()
"Return the name of the defun at point, or nil.
`add-log-current-defun-function' is set to this, for use by `which-func'."
(save-excursion
(let ((location (point)))
;; If we are now precisely at the beginning of a defun, make sure
;; beginning-of-defun finds that one rather than the previous one.
(or (eobp) (forward-char 1))
(beginning-of-defun-raw)
;; Make sure we are really inside the defun found, not after it.
(when (and (looking-at "\\s(")
(progn (end-of-defun)
(< location (point)))
(progn (forward-sexp -1)
(>= location (point))))
(if (looking-at "\\s(")
(forward-char 1))
;; Skip the defining construct name, e.g. "defn" or "def".
(forward-sexp 1)
;; The second element is usually a symbol being defined. If it
;; is not, use the first symbol in it.
(skip-chars-forward " \t\n'(")
;; Skip metadata
(while (looking-at "\\^")
(forward-sexp 1)
(skip-chars-forward " \t\n'("))
(buffer-substring-no-properties (point)
(progn (forward-sexp 1)
(point)))))))
(defun clojure-mode-variables ()
"Set up initial buffer-local variables for Clojure mode."
(add-to-list 'imenu-generic-expression '(nil clojure-match-next-def 0))
(setq-local indent-tabs-mode nil)
(setq-local paragraph-ignore-fill-prefix t)
(setq-local outline-regexp ";;;;* ")
(setq-local outline-level 'lisp-outline-level)
(setq-local comment-start ";")
(setq-local comment-start-skip ";+ *")
(setq-local comment-add 1) ; default to `;;' in comment-region
(setq-local comment-column 40)
(setq-local comment-use-syntax t)
(setq-local multibyte-syntax-as-symbol t)
(setq-local electric-pair-skip-whitespace 'chomp)
(setq-local electric-pair-open-newline-between-pairs nil)
(setq-local fill-paragraph-function #'clojure-fill-paragraph)
(setq-local adaptive-fill-function #'clojure-adaptive-fill-function)
(setq-local normal-auto-fill-function #'clojure-auto-fill-function)
(setq-local comment-start-skip
"\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
(setq-local indent-line-function #'clojure-indent-line)
(setq-local indent-region-function #'clojure-indent-region)
(setq-local lisp-indent-function #'clojure-indent-function)
(setq-local lisp-doc-string-elt-property 'clojure-doc-string-elt)
(setq-local clojure-expected-ns-function #'clojure-expected-ns)
(setq-local parse-sexp-ignore-comments t)
(setq-local prettify-symbols-alist clojure--prettify-symbols-alist)
(setq-local open-paren-in-column-0-is-defun-start nil)
(setq-local add-log-current-defun-function #'clojure-current-defun-name)
(setq-local beginning-of-defun-function #'clojure-beginning-of-defun-function))
(defsubst clojure-in-docstring-p ()
"Check whether point is in a docstring."
(let ((ppss (syntax-ppss)))
;; are we in a string?
(when (nth 3 ppss)
;; check font lock at the start of the string
(eq (get-text-property (nth 8 ppss) 'face)
'font-lock-doc-face))))
;;;###autoload
(define-derived-mode clojure-mode prog-mode "Clojure"
"Major mode for editing Clojure code.
\\{clojure-mode-map}"
(clojure-mode-variables)
(clojure-font-lock-setup)
(add-hook 'paredit-mode-hook #'clojure-paredit-setup)
;; `electric-layout-post-self-insert-function' prevents indentation in strings
;; and comments, force indentation of non-inlined docstrings:
(add-hook 'electric-indent-functions
(lambda (_char) (if (and (clojure-in-docstring-p)
;; make sure we're not dealing with an inline docstring
;; e.g. (def foo "inline docstring" bar)
(save-excursion
(beginning-of-line-text)
(eq (get-text-property (point) 'face)
'font-lock-doc-face)))
'do-indent))))
(defcustom clojure-verify-major-mode t
"If non-nil, warn when activating the wrong `major-mode'."
:type 'boolean
:safe #'booleanp
:package-version '(clojure-mode "5.3.0"))
(defun clojure--check-wrong-major-mode ()
"Check if the current `major-mode' matches the file extension.
If it doesn't, issue a warning if `clojure-verify-major-mode' is
non-nil."
(when (and clojure-verify-major-mode
(stringp (buffer-file-name)))
(let* ((case-fold-search t)
(problem (cond ((and (string-match "\\.clj\\'" (buffer-file-name))
(not (eq major-mode 'clojure-mode)))
'clojure-mode)
((and (string-match "\\.cljs\\'" (buffer-file-name))
(not (eq major-mode 'clojurescript-mode)))
'clojurescript-mode)
((and (string-match "\\.cljc\\'" (buffer-file-name))
(not (eq major-mode 'clojurec-mode)))
'clojurec-mode))))
(when problem
(message "[WARNING] %s activated `%s' instead of `%s' in this buffer.
This could cause problems.
\(See `clojure-verify-major-mode' to disable this message.)"
(if (eq major-mode real-this-command)
"You have"
"Something in your configuration")
major-mode
problem)))))
(add-hook 'clojure-mode-hook #'clojure--check-wrong-major-mode)
(defsubst clojure-docstring-fill-prefix ()
"The prefix string used by `clojure-fill-paragraph'.
It is simply `clojure-docstring-fill-prefix-width' number of spaces."
(make-string clojure-docstring-fill-prefix-width ? ))
(defun clojure-adaptive-fill-function ()
"Clojure adaptive fill function.
This only takes care of filling docstring correctly."
(when (clojure-in-docstring-p)
(clojure-docstring-fill-prefix)))
(defun clojure-fill-paragraph (&optional justify)
"Like `fill-paragraph', but can handle Clojure docstrings.
If JUSTIFY is non-nil, justify as well as fill the paragraph."
(if (clojure-in-docstring-p)
(let ((paragraph-start
(concat paragraph-start
"\\|\\s-*\\([(:\"[]\\|~@\\|`(\\|#'(\\)"))
(paragraph-separate
(concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
(fill-column (or clojure-docstring-fill-column fill-column))
(fill-prefix (clojure-docstring-fill-prefix)))
;; we are in a string and string start pos (8th element) is non-nil
(let* ((beg-doc (nth 8 (syntax-ppss)))
(end-doc (save-excursion
(goto-char beg-doc)
(or (ignore-errors (forward-sexp) (point))
(point-max)))))
(save-restriction
(narrow-to-region beg-doc end-doc)
(fill-paragraph justify))))
(let ((paragraph-start (concat paragraph-start
"\\|\\s-*\\([(:\"[]\\|`(\\|#'(\\)"))
(paragraph-separate
(concat paragraph-separate "\\|\\s-*\".*[,\\.[]$")))
(or (fill-comment-paragraph justify)
(fill-paragraph justify))
;; Always return `t'
t)))
(defun clojure-auto-fill-function ()
"Clojure auto-fill function."
;; Check if auto-filling is meaningful.
(let ((fc (current-fill-column)))
(when (and fc (> (current-column) fc))
(let ((fill-column (if (clojure-in-docstring-p)
clojure-docstring-fill-column
fill-column))
(fill-prefix (clojure-adaptive-fill-function)))
(do-auto-fill)))))
;;; #_ comments font-locking
;; Code heavily borrowed from Slime.
;; https://github.com/slime/slime/blob/master/contrib/slime-fontifying-fu.el#L186
(defvar clojure--comment-macro-regexp
(rx (seq (+ (seq "#_" (* " ")))) (group-n 1 (not (any " "))))
"Regexp matching the start of a comment sexp.
The beginning of match-group 1 should be before the sexp to be
marked as a comment. The end of sexp is found with
`clojure-forward-logical-sexp'.")
(defvar clojure--reader-and-comment-regexp
(rx (or (seq (+ (seq "#_" (* " ")))
(group-n 1 (not (any " "))))
(seq (group-n 1 "(comment" symbol-end))))
"Regexp matching both `#_' macro and a comment sexp." )
(defcustom clojure-comment-regexp clojure--comment-macro-regexp
"Comment mode.
The possible values for this variable are keywords indicating
what is considered a comment (affecting font locking).
- Reader macro `#_' only - the default
- Reader macro `#_' and `(comment)'"
:type '(choice (const :tag "Reader macro `#_' and `(comment)'" clojure--reader-and-comment-regexp)
(other :tag "Reader macro `#_' only" clojure--comment-macro-regexp))
:package-version '(clojure-mode . "5.7.0"))
(defun clojure--search-comment-macro-internal (limit)
"Search for a comment forward stopping at LIMIT."
(when (search-forward-regexp clojure-comment-regexp limit t)
(let* ((md (match-data))
(start (match-beginning 1))
(state (syntax-ppss start)))
;; inside string or comment?
(if (or (nth 3 state)
(nth 4 state))
(clojure--search-comment-macro-internal limit)
(goto-char start)
;; Count how many #_ we got and step by that many sexps
;; For (comment ...), step at least 1 sexp
(clojure-forward-logical-sexp
(max (count-matches (rx "#_") (elt md 0) (elt md 1))
1))
;; Data for (match-end 1).
(setf (elt md 3) (point))
(set-match-data md)
t))))
(defun clojure--search-comment-macro (limit)
"Find comment macros and set the match data.
Search from point up to LIMIT. The region that should be
considered a comment is between `(match-beginning 1)'
and `(match-end 1)'."
(let ((result 'retry))
(while (and (eq result 'retry) (<= (point) limit))
(condition-case nil
(setq result (clojure--search-comment-macro-internal limit))
(end-of-file (setq result nil))
(scan-error (setq result 'retry))))
result))
;;; General font-locking
(defun clojure-match-next-def ()
"Scans the buffer backwards for the next \"top-level\" definition.
Called by `imenu--generic-function'."
;; we have to take into account namespace-definition forms
;; e.g. s/defn
(when (re-search-backward "^[ \t]*(\\([a-z0-9.-]+/\\)?\\(def\\sw*\\)" nil t)
(save-excursion
(let (found?
(deftype (match-string 2))
(start (point)))
;; ignore user-error from down-list when called from inside a string or comment
;; TODO: a better workaround would be to wrap it in
;; unless (ppss-comment-or-string-start (syntax-ppss)) instead of ignore-errors,
;; but ppss-comment-or-string-start is only available since Emacs 27
(ignore-errors
(down-list))
(forward-sexp)
(while (not found?)
(ignore-errors
(forward-sexp))
(or (when (char-equal ?\[ (char-after (point)))
(backward-sexp))
(when (char-equal ?\) (char-after (point)))
(backward-sexp)))
(cl-destructuring-bind (def-beg . def-end) (bounds-of-thing-at-point 'sexp)
(when (char-equal ?^ (char-after def-beg))
;; move to the beginning of next sexp
(progn (forward-sexp) (backward-sexp)))
(when (or (not (char-equal ?^ (char-after def-beg)))
(and (char-equal ?^ (char-after (point))) (= def-beg (point))))
(setq found? t)
(when (string= deftype "defmethod")
(setq def-end (progn (goto-char def-end)
(forward-sexp)
(point))))
(set-match-data (list def-beg def-end)))))
(goto-char start)))))
(eval-and-compile
(defconst clojure--sym-forbidden-rest-chars "][\";@\\^`~\(\)\{\}\\,\s\t\n\r"
"A list of chars that a Clojure symbol cannot contain.
See definition of `macros': URL `https://git.io/vRGLD'.")
(defconst clojure--sym-forbidden-1st-chars (concat clojure--sym-forbidden-rest-chars "0-9:'")
"A list of chars that a Clojure symbol cannot start with.
See the for-loop: URL `https://git.io/vRGTj' lines: URL
`https://git.io/vRGIh', URL `https://git.io/vRGLE' and value
definition of `macros': URL `https://git.io/vRGLD'.")
(defconst clojure--sym-regexp
(concat "[^" clojure--sym-forbidden-1st-chars "][^" clojure--sym-forbidden-rest-chars "]*")
"A regexp matching a Clojure symbol or namespace alias.
Matches the rule `clojure--sym-forbidden-1st-chars' followed by
any number of matches of `clojure--sym-forbidden-rest-chars'.")
(defconst clojure--keyword-sym-forbidden-1st-chars
(concat clojure--sym-forbidden-rest-chars ":'")
"A list of chars that a Clojure keyword symbol cannot start with.")
(defconst clojure--keyword-sym-regexp
(concat "[^" clojure--keyword-sym-forbidden-1st-chars "]"
"[^" clojure--sym-forbidden-rest-chars "]*")
"A regexp matching a Clojure keyword name or keyword namespace.
Matches the rule `clojure--keyword-sym-forbidden-1st-chars' followed by
any number of matches of `clojure--sym-forbidden-rest-chars'."))
(defconst clojure-font-lock-keywords
(eval-when-compile
`(;; Any def form
(,(concat "(\\(?:" clojure--sym-regexp "/\\)?"
"\\("
(regexp-opt '("def"
"defonce"
"defn"
"defn-"
"defmacro"
"definline"
"defmulti"
"defmethod"
"defprotocol"
"definterface"
"defrecord"
"deftype"
"defstruct"
;; clojure.test
"deftest"
"deftest-"
;; clojure.logic
"defne"
"defnm"
"defnu"
"defnc"
"defna"
;; Third party
"deftask"
"defstate"
"defproject"))
"\\)\\>")
(1 font-lock-keyword-face))
;; Top-level variable definition
(,(concat "(\\(?:clojure.core/\\)?\\("
(regexp-opt '("def" "defonce"))
;; variable declarations
"\\)\\>"
;; Any whitespace
"[ \r\n\t]*"
;; Possibly type or metadata
"\\(?:#?^\\(?:{[^}]*}\\|\\sw+\\)[ \r\n\t]*\\)*"
"\\(\\sw+\\)?")
(2 font-lock-variable-name-face nil t))
;; Type definition
(,(concat "(\\(?:clojure.core/\\)?\\("
(regexp-opt '("defstruct" "deftype" "defprotocol"
"defrecord"))
;; type declarations
"\\)\\>"
;; Any whitespace
"[ \r\n\t]*"
;; Possibly type or metadata
"\\(?:#?^\\(?:{[^}]*}\\|\\sw+\\)[ \r\n\t]*\\)*"
"\\(\\sw+\\)?")
(2 font-lock-type-face nil t))
;; Function definition
(,(concat "(\\(?:clojure.core/\\)?\\("
(regexp-opt '("defn"
"defn-"
"defmulti"
"defmethod"
"deftest"
"deftest-"
"defmacro"
"definline"))
"\\)"
;; Function declarations
"\\>"
;; Any whitespace
"[ \r\n\t]*"
;; Possibly type or metadata
"\\(?:#?^\\(?:{[^}]*}\\|\\sw+\\)[ \r\n\t]*\\)*"
(concat "\\(" clojure--sym-regexp "\\)?"))
(2 font-lock-function-name-face nil t))
;; (fn name? args ...)
(,(concat "(\\(?:clojure.core/\\)?\\(fn\\)[ \t]+"
;; Possibly type
"\\(?:#?^\\sw+[ \t]*\\)?"
;; Possibly name
"\\(\\sw+\\)?" )
(2 font-lock-function-name-face nil t))
;; Special forms
(,(concat
"("
(regexp-opt
'("do" "if" "let*" "var" "fn" "fn*" "loop*"
"recur" "throw" "try" "catch" "finally"
"set!" "new" "."
"monitor-enter" "monitor-exit" "quote") t)
"\\>")
1 font-lock-keyword-face)
;; Built-in binding and flow of control forms
(,(concat
"(\\(?:clojure.core/\\)?"
(regexp-opt
'(
"->"
"->>"
".."
"amap"
"and"
"areduce"
"as->"
"assert"