-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
racket-mode.texi
4238 lines (3101 loc) · 127 KB
/
racket-mode.texi
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
\input texinfo @c -*- texinfo -*-
@c %**start of header
@setfilename racket-mode.info
@settitle Racket Mode
@documentencoding UTF-8
@documentlanguage en
@syncodeindex pg cp
@c %**end of header
@copying
Copyright (C) 2013-2022 by Greg Hendershott.
SPDX-License-Identifier: GPL-3.0-or-later
@end copying
@dircategory Emacs
@direntry
* Racket Mode: (racket-mode). Edit and REPL major modes for Racket lang.
@end direntry
@finalout
@titlepage
@title Racket Mode
@author Greg Hendershott (@email{racket-mode-author@@greghendershott.com})
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage
@contents
@ifnottex
@node Top
@top Racket Mode
@end ifnottex
@menu
* Introduction::
* Install, Update, and Uninstall: Install Update and Uninstall.
* Configure::
* Architecture::
* Reference::
* Commands::
* Variables::
* Configuration functions::
* Faces::
@detailmenu
--- The Detailed Node Listing ---
Install, Update, and Uninstall
* Use Emacs 28.1 or newer with NonGNU ELPA: Use Emacs 281 or newer with NonGNU ELPA.
* Configure Emacs to use MELPA::
* Install::
* Minimal Racket::
* Uninstall::
* Update::
Update
* Upgrading all packages::
* Updating just Racket Mode::
Configure
* Which major mode to use::
* Key bindings::
* Font-lock (syntax highlighting)::
* Completion at point::
* Completion in minibuffer::
* Xref (definitions and references)::
* Indent::
* paredit::
* smartparens::
* Appearance of parentheses::
* Edit buffers and REPL buffers::
* eldoc::
* Start faster::
* Unicode input method::
* Ligatures::
Commands
* Edit::
* Hash Langs::
* Explore::
* Run::
* Test::
* Eval::
* Collections::
* Macro expand::
* Other::
Edit
* racket-mode::
* racket-insert-lambda::
* racket-fold-all-tests::
* racket-unfold-all-tests::
* racket-tidy-requires::
* racket-trim-requires::
* racket-base-requires::
* racket-add-require-for-identifier::
* racket-indent-line::
* racket-smart-open-bracket-mode::
* racket-insert-closing::
* racket-cycle-paren-shapes::
* racket-backward-up-list::
* racket-unicode-input-method-enable::
* racket-align::
* racket-unalign::
* racket-complete-at-point::
Hash Langs
* racket-hash-lang-mode::
* racket-hash-lang-backward::
* racket-hash-lang-forward::
* racket-hash-lang-up::
* racket-hash-lang-down::
* racket-hash-lang-C-M-q-dwim::
Explore
* racket-xp-mode::
* racket-xp-describe::
* racket-xp-documentation::
* racket-xp-next-definition::
* racket-xp-previous-definition::
* racket-xp-next-use::
* racket-xp-previous-use::
* racket-xp-next-error::
* racket-xp-previous-error::
* racket-xp-tail-up::
* racket-xp-tail-down::
* racket-xp-tail-next-sibling::
* racket-xp-tail-previous-sibling::
* racket-documentation-search::
* racket-describe-search::
Run
* racket-repl-mode::
* racket-run::
* racket-run-and-switch-to-repl::
* racket-run-module-at-point::
* racket-repl::
* racket-repl-describe::
* racket-repl-documentation::
* racket-racket::
* racket-profile::
* racket-profile-mode::
* racket-logger::
* racket-logger-mode::
* racket-debug-mode::
* racket-repl-clear::
* racket-repl-clear-leaving-last-prompt::
Test
* racket-test::
* racket-raco-test::
Eval
* racket-send-region::
* racket-send-definition::
* racket-send-last-sexp::
Collections
* racket-open-require-path::
Macro expand
* racket-stepper-mode::
* racket-expand-file::
* racket-expand-region::
* racket-expand-definition::
* racket-expand-last-sexp::
Other
* racket-debug-toggle-breakpoint::
* racket-mode-start-faster::
* racket-mode-start-slower::
Variables
* General variables::
* Hash lang variables::
* REPL variables::
* Other variables::
* Experimental debugger variables::
* Showing information::
* Running racket and raco commands in a shell or terminal::
General variables
* racket-program::
* racket-command-timeout::
* racket-memory-limit::
* racket-error-context::
* racket-user-command-line-arguments::
* racket-browse-url-function::
* racket-xp-after-change-refresh-delay::
* racket-xp-highlight-unused-regexp::
* racket-xp-add-binding-faces::
* racket-documentation-search-location::
* racket-expand-hiding::
Hash lang variables
* racket-hash-lang-token-face-alist::
* racket-hash-lang-pairs::
* racket-hash-lang-module-language-hook::
REPL variables
* racket-repl-buffer-name-function::
* racket-submodules-to-run::
* racket-repl-history-directory::
* racket-history-filter-regexp::
* racket-images-inline::
* racket-imagemagick-props::
* racket-images-keep-last::
* racket-images-system-viewer::
* racket-pretty-print::
* racket-repl-command-file::
Other variables
* racket-indent-curly-as-sequence::
* racket-indent-sequence-depth::
* racket-pretty-lambda::
* racket-smart-open-bracket-enable::
* racket-logger-config::
* racket-before-run-hook::
* racket-after-run-hook::
* racket-sexp-comment-fade::
Experimental debugger variables
* racket-debuggable-files::
Showing information
* racket-show-functions::
Running racket and raco commands in a shell or terminal
* racket-shell-or-terminal-function::
Configuration functions
* Showing information: Showing information (1).
* Associating edit buffers with REPL buffers::
* Browsing file URLs with anchors::
* Configuring back ends::
* Running racket and raco commands in a shell or terminal: Running racket and raco commands in a shell or terminal (1).
Showing information
* racket-show-pseudo-tooltip::
* racket-show-echo-area::
* racket-show-header-line::
* racket-show-pos-tip::
Associating edit buffers with REPL buffers
* racket-repl-buffer-name-shared::
* racket-repl-buffer-name-unique::
* racket-repl-buffer-name-project::
* racket-project-root::
Browsing file URLs with anchors
* racket-browse-url-using-temporary-file::
Configuring back ends
* racket-add-back-end::
Running racket and raco commands in a shell or terminal
* racket-shell::
* racket-term::
* racket-ansi-term::
* racket-vterm::
Faces
* All::
All
* racket-keyword-argument-face::
* racket-reader-quoted-symbol-face::
* racket-reader-syntax-quoted-symbol-face::
* racket-here-string-face::
* racket-xp-def-face::
* racket-xp-use-face::
* racket-xp-unused-face::
* racket-xp-tail-target-face::
* racket-xp-tail-position-face::
* racket-xp-binding-lang-face::
* racket-xp-binding-lang-use-face::
* racket-xp-binding-import-face::
* racket-xp-binding-import-use-face::
* racket-xp-binding-local-face::
* racket-xp-binding-local-use-face::
* racket-logger-config-face::
* racket-logger-topic-face::
* racket-logger-fatal-face::
* racket-logger-error-face::
* racket-logger-warning-face::
* racket-logger-info-face::
* racket-logger-debug-face::
* racket-doc-link-face::
* racket-ext-link-face::
* racket-doc-output-face::
* racket-doc-litchar-face::
* racket-repl-message::
* racket-repl-prompt::
* racket-repl-value::
* racket-repl-error-message::
* racket-repl-error-location::
* racket-repl-stdout::
* racket-repl-stderr::
@end detailmenu
@end menu
@node Introduction
@chapter Introduction
The @uref{https://www.racket-mode.com/, Racket Mode} package consists of a variety of Emacs major and minor modes, including:
@itemize
@item
@code{racket-mode}: A major mode to edit @code{.rkt} files. Generally assumes @code{#lang racket}.
@item
@ref{racket-hash-lang-mode}: An alternative to @code{racket-mode} using behavior specified by a @code{#lang} for colors, indent, expression navigation, etc. @emph{Experimental}.
@item
@ref{racket-xp-mode}: A minor mode to enhance either edit mode. Explain and explore code, similar to background check-syntax in Dr Racket.
@item
@code{racket-repl-mode}: A major mode to run programs and use a REPL@.
@item
Various other modes to support specific features:
@itemize
@item
@ref{racket-logger-mode}
@item
@ref{racket-profile-mode}
@item
@ref{racket-debug-mode}
@end itemize
@end itemize
For code, issues, and pull requests, see the @uref{https://github.com/greghendershott/racket-mode, Git repo}.
To sponsor this work, see @uref{https://github.com/users/greghendershott/sponsorship, GitHub Sponsors} or @uref{https://www.paypal.me/greghendershott, PayPal}.
@node Install Update and Uninstall
@chapter Install, Update, and Uninstall
The most common way to use Racket Mode is to install from a package archive like MELPA or NonGNU ELPA@.
Some people also use a system like @uref{https://github.com/radian-software/straight.el, straight.el}.
Note that Racket Mode is only available on MELPA (@emph{not} ``MELPA Stable''), and is available as a ``rolling release'' from NonGNU ELPA@.
@menu
* Use Emacs 28.1 or newer with NonGNU ELPA: Use Emacs 281 or newer with NonGNU ELPA.
* Configure Emacs to use MELPA::
* Install::
* Minimal Racket::
* Uninstall::
* Update::
@end menu
@node Use Emacs 281 or newer with NonGNU ELPA
@section Use Emacs 28.1 or newer with NonGNU ELPA
Emacs 28.1 or newer comes configured to use @uref{https://elpa.nongnu.org, NonGNU ELPA}, in which case you can skip ahead to @ref{Install}.
With older versions of Emacs, you can use MELPA@.
@node Configure Emacs to use MELPA
@section Configure Emacs to use MELPA
Following is a quick guide that may work for you. (For definitive instructions and the latest trouble-shooting tips, please see @uref{https://melpa.org/#/getting-started}.)
@itemize
@item
Add the following to your @samp{~/.emacs} or @samp{~/.emacs.d/init.el}:
@end itemize
@lisp
(require 'package)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/")
t)
(package-initialize)
@end lisp
@itemize
@item
Restart Emacs.
@end itemize
@quotation
NOTE: If you ever get an error message about ``contacting a host'' or ``downloading an archive'', the problem is not unique to Racket Mode. Please see @uref{https://melpa.org/#/getting-started}.
@end quotation
@node Install
@section Install
When Emacs is configured to use NonGNU ELPA or MELPA:
@enumerate
@item
Type @kbd{M-x} @code{package-initialize} @kbd{RET} .
@item
Type @kbd{M-x} @code{package-refresh-contents} @kbd{RET} .
@item
Type @kbd{M-x} @code{package-install} @kbd{RET} @code{racket-mode} @kbd{RET} .
@end enumerate
@quotation
NOTE: If you get an error message about ``contacting a host'' or ``downloading an archive'', the problem is not unique to Racket Mode. Please see @uref{https://melpa.org/#/getting-started}.
@end quotation
@node Minimal Racket
@section Minimal Racket
If you have installed the minimal Racket distribution (for example by using the @uref{https://github.com/Homebrew/homebrew-core/blob/master/Formula/minimal-racket.rb, homebrew formula}) Racket Mode needs some additional Racket packages. A simple way to get all these packages is to install the @code{drracket} Racket package. In a command shell:
@example
raco pkg install --auto drracket
@end example
A more-targeted approach is instead to install these specific packages and their dependencies:
@example
raco pkg install --auto data-lib errortrace-lib macro-debugger-text-lib rackunit-lib racket-index scribble-lib drracket-tool-text-lib
@end example
If you do @emph{not} want to use @code{racket-xp-mode}, then you can omit @code{drracket-tool-text-lib}.
On a headless server, you might want to omit @code{gui-lib}. Unfortunately, @code{racket-doc} depends on @code{gui-lib}. On the one hand, if you uninstall @code{racket-doc} and @code{gui-lib}, you will no longer be able to access documentation when using a Racket Mode back end running there. On the other hand, if you leave @code{gui-lib} installed, you should be careful to run the Racket Mode back end using @code{xvfb-run racket}.
@node Uninstall
@section Uninstall
To uninstall Racket Mode, simply type @kbd{M-x} @code{package-delete} @kbd{RET} @code{racket-mode} @kbd{RET} .
You should probably also exit and restart Emacs.
@node Update
@section Update
@menu
* Upgrading all packages::
* Updating just Racket Mode::
@end menu
@node Upgrading all packages
@subsection Upgrading all packages
The ``easy path'' provided by Emacs is to update @emph{all} packages to their latest versions. Although you might not want to do this --- see next section --- here is how to do so:
@enumerate
@item
Use @kbd{M-x} @code{package-initialize}.
@item
Use @kbd{M-x} @code{package-refresh-contents}.
@item
Use @kbd{M-x} @code{list-packages}. It should display a message like ``42 packages can be upgraded; type ‘U’ to mark them for upgrading.''.
@item
Press @kbd{U} as suggested to mark them all.
@item
Press @kbd{x} to execute.
@end enumerate
After such a mass update, it might be wise to exit and restart Emacs.
@quotation
NOTE: If you get an error message about ``contacting a host'' or ``downloading an archive'', the problem is not unique to Racket Mode. Please see @uref{https://melpa.org/#/getting-started}.
@end quotation
@node Updating just Racket Mode
@subsection Updating just Racket Mode
Updating all packages sometimes is more than you want. For example, maybe you will discover that some packages have changed in ways that require you to take time to learn about, change customizations, and so on.
To update just Racket Mode:
@enumerate
@item
@ref{Uninstall}.
@item
Optional but most reliable: Exit and restart Emacs.
@item
@ref{Install} again. This will install the latest version.
@end enumerate
@node Configure
@chapter Configure
Although Racket Mode can be customized with many @ref{Variables}, there is only one that you might @emph{need} to set: @ref{racket-program}. This is the name or pathname of the Racket executable. It defaults to @code{Racket.exe} on Windows else @code{racket}.
On Windows or Linux, this default will probably work for you.
On macOS, downloading Racket doesn't add its @code{bin} directory to your @code{PATH}. Even after you add it, GUI Emacs doesn't automatically use your path (unless you use the handy @uref{https://melpa.org/#/exec-path-from-shell, exec-path-from-shell} package). Therefore you might want to set @code{racket-program} to a complete pathname.
You can @code{setq} this directly in your Emacs init file (@samp{~/.emacs} or @samp{~/.emacs.d/init.el}), or, use @kbd{M-x} @code{customize}, as you prefer.
@menu
* Which major mode to use::
* Key bindings::
* Font-lock (syntax highlighting)::
* Completion at point::
* Completion in minibuffer::
* Xref (definitions and references)::
* Indent::
* paredit::
* smartparens::
* Appearance of parentheses::
* Edit buffers and REPL buffers::
* eldoc::
* Start faster::
* Unicode input method::
* Ligatures::
@end menu
@node Which major mode to use
@section Which major mode to use
Racket is a programming language.
Racket is also a ``language-oriented programming language''. Most Racket source files contain a `#lang` line. The lang may be an s-expression lang like @code{racket}, or an at-expression lang like @code{scribble/manual}, or something completely different like @code{datalog} or @code{rhombus}.
The Racket Mode package offers a choice of two major modes to use in buffers for viewing and editing source code. Each has pros and cons.
Whereas @code{racket-mode} is in the tradition of Emacs @code{lisp-mode} and @code{scheme-mode} and assumes s-expression langs, @code{racket-hash-lang-mode} takes the approach of DrRacket to work for all langs.
@itemize
@item
@code{racket-mode} is the original, ``classic'' mode for @code{#lang racket} and related s-expression languages. It is implemented entirely in Emacs and does @emph{not} need Racket Mode's back end racket process running. Font-lock (coloring) uses rules for a fixed set of identifiers from @code{racket} lang and popular modules like @code{racket/match}. Indentation uses rules for a fixed set of forms, and may be customized.
@item
@code{racket-hash-lang-mode} uses font-lock (colors) and indentation determined by the lang; to get this information it @emph{does} need the Racket Mode's back end racket process running. Although basic editing should feel fast, you might notice some delay when indenting. You might see colors appear after a small delay (but it will not block editing). Speaking of colors, they will be ``plainer'' than @code{racket-mode} -- just colors for tokens like numbers, comments, strings, and keywords. This looks similar to DrRacket. However you can enhance this in various ways; see the discussion of @ref{racket-hash-lang-module-language-hook}.
@end itemize
You can use different major modes for different kinds of files:
@itemize
@item
For editing @code{.rkt} files and s-expression langs, which mode to use is personal preference.
@item
For @code{.scrbl} and at-expression langs like @code{scribble/manual}, @code{racket-hash-lang-mode} is probably better than @code{racket-mode}. (Note there is also an unrelated @code{scribble-mode} package.)
@item
For non-s-expression langs like @code{datalog} or @code{rhombus} (@code{.rhm}), @code{racket-hash-lang-mode} is definitely better than @code{racket-mode}. (Note there is also an unrelated @code{rhombus-mode} package.)
@end itemize
You can use @code{auto-mode-alist} to tell Emacs which major mode to use initially for certain file extensions. Also, in a buffer you can use @code{M-x racket-mode} and @code{M-x racket-hash-lang-mode} to switch between them.
@node Key bindings
@section Key bindings
To customize things like key bindings, you can use @code{racket-mode-hook} in your Emacs init file to modify @code{racket-mode-map}. For example, although @kbd{C-c C-c} is bound by default to the @code{racket-run} command, let's say you wanted @kbd{F5} to be an additional binding:
@lisp
(add-hook 'racket-mode-hook
(lambda ()
(define-key racket-mode-map (kbd "<f5>") 'racket-run)))
@end lisp
Likewise for @code{racket-repl-mode-hook} and @code{racket-repl-mode-map}.
@node Font-lock (syntax highlighting)
@section Font-lock (syntax highlighting)
@quotation
Note: The alternative major mode @ref{racket-hash-lang-mode} disables all of the following behavior and uses colors determined by the #lang.
@end quotation
Font-lock (as Emacs calls syntax highlighting) can be controlled using the variable @code{font-lock-maximum-decoration}, which defaults to @code{t} (maximum). You can set it to a number, where @code{0} is the lowest level. You can even supply an association list to specify different values for different major modes.
Historically you might choose a lower level for speed. These days you might do so because you prefer a simpler appearance.
Racket Mode supports four, increasing levels of font-lock:
@itemize
@item
@code{0}: Just strings, comments, and @code{#lang}.
@item
@code{1}: @code{#:keyword} and self-evaluating literals like numbers, quoted symbols (including symbols with spaces delimited by @code{|} characters), and @code{#rx} and @code{#px} regular expressions.
@item
@code{2}: Identifiers in @code{define}-like and @code{let}-like forms.
@item
@code{3}: Identifiers provided by @code{racket}, @code{typed/racket}, @code{racket/syntax}, and @code{syntax/parse}. (This level effectively treats Racket as a language, instead of a language for making languages.).
@end itemize
@node Completion at point
@section Completion at point
In Emacs, a major mode may supply a ``completion-at-point function''. This function is used by manual completion commands like @code{complete-symbol} (bound by default to @kbd{C-M-i} ), as well as by auto-completion packages like @code{company-mode}.
@itemize
@item
@code{racket-mode} supplies @code{racket-complete-at-point}, which simply supplies the same symbols that it knows how to font-lock. This does @emph{not} require the Racket Mode back end to be running. But of course the completion candidates do not correspond to your program's definitions or those it imports. This is a static, ``better than nothing'' fallback.
@item
@code{racket-xp-mode} --- an optional minor mode that enhances @code{racket-mode} --- supplies @code{racket-xp-complete-at-point}, which uses a static analysis to find local and imported binding names. Although this requires the Racket Mode back end to be running --- and will automatically start it --- it does @emph{not} require the edit buffer to be @code{racket-run}. This also supplies meta data usable by the @code{company-capf} backend.
@item
@code{racket-repl-mode} supplies @code{racket-repl-complete-at-point}, which uses the result of @code{namespace-mapped-symbols} on the program currently running in the REPL@.
@end itemize
These completion functions are set by default. (However, @code{racket-xp-mode} is not enabled by default. To do so: @ref{racket-xp-mode}.)
If you want @kbd{TAB} to do completion as well as indent, add the following to your Emacs init file:
@lisp
(setq tab-always-indent 'complete)
@end lisp
This changes the behavior of Emacs' standard @code{indent-for-tab-command}, to which @kbd{TAB} is bound by default in @code{racket-mode} and @code{racket-repl-mode}.
@node Completion in minibuffer
@section Completion in minibuffer
Sometimes Racket Mode asks for input in the minibuffer. To do so it uses the standard Emacs function @code{completing-read}, so as to be compatible with all Emacs packages that enhance @code{completing-read}, such as helm, ivy, ido-completing-read+, vertico, and so on.
(Earlier versions of Racket Mode sometimes used @code{ido-completing-read}. If you have upgraded Racket Mode and miss that, simply install the ido-completing-read+ package.)
@node Xref (definitions and references)
@section Xref (definitions and references)
Several modes support the Emacs commands
@itemize
@item
@kbd{M-.} @code{xref-find-definitions}
@item
@kbd{M-?} @code{xref-find-references}
@item
@kbd{M-,} @code{xref-pop-marker-stack}
@end itemize
To do so, each mode adds a local hook for @code{xref-backend-functions}:
@itemize
@item
@ref{racket-mode}: @code{#'racket-mode-xref-backend-function}
@item
@ref{racket-xp-mode}: @code{#'racket-xp-xref-backend-function}
@item
@ref{racket-repl-mode}: @code{#'racket-repl-xref-backend-function}
@end itemize
If you prefer, you can remove the local hook --- e.g. for @code{racket-mode}: @code{(remove-hook 'xref-backend-functions #'racket-mode-xref-function t)}.
You can @code{M-x customize-group} and enter @code{xref} to adjust some other settings. For example, the customization variable @code{xref-prompt-for-identifier} controls which commands prompt you and when. You might prefer to set it to @code{nil}.
If you use @code{paredit}, by default it binds @kbd{M-?} to @code{paredit-convolute-sexp}. You can change that binding in @code{paredit-mode-map} allowing the global binding for @kbd{M-?} to be used, or, pick some other key for @code{xref-find-references} in the global map.
Finally, what to expect:
@itemize
@item
Racket does not have a global or project-wide database of definitions and references.
@item
Various modules can export identifiers with the same symbolic value -- for example a different ``define'' is provided by @code{racket/base}, @code{typed/racket/base}, and other modules.
@item
A module can import something, then rename, contract, and re-export it.
@end itemize
As a result, to find a definition, it is necessary to know exactly @emph{which} identifier is meant --- either by expanding the module (as is done by @code{racket-xp-mode}) or by actually running it (@code{racket-repl-mode}). Once known, we can usually find the definition site, even through a chain of renaming and/or contract-wrapping exports. In addition, when point is on a module within @code{require} form, we can usually find the source file. (In plain @code{racket-mode} edit buffers not enhanced by @code{racket-xp-mode}, the only thing that @code{xref-find-definitions} does is visit relative requires, e.g. @code{foo.rkt} in @code{(require "foo.rkt")}.)
As for finding references, the default xref implementation is used, which greps for strings among a project's files. Although @code{racket-xp-mode} can sometimes do better, using @code{drracket/check-syntax} for definitions and references @emph{within} the current buffer, beyond those it also falls back to the default implementation.
In any case, using the Emacs xref API allows for consistent command names, shortcut keys, and even a special buffer to navigate among references and visit each source location.
@node Indent
@section Indent
@quotation
Note: The alternative major mode @ref{racket-hash-lang-mode} disables all of the following behavior and uses indentation determined by the #lang.
@end quotation
Indentation can be customized in a way similar to lisp-mode and scheme-mode: @ref{racket-indent-line}.
(Indentation preserves your line breaks. If you want to use an auto-reformatter --- an expressive pretty printer that chooses line breaks while computing an optimal layout --- the Racket package @uref{https://docs.racket-lang.org/fmt/, fmt} is supported by the Emacs package @uref{https://github.com/lassik/emacs-format-all-the-code, emacs-format-all-the-code}.)
@node paredit
@section paredit
@quotation
Note: If you use @ref{racket-hash-lang-mode}, see @ref{racket-hash-lang-module-language-hook} for how to enable/disable paredit based on the specific #lang.
@end quotation
If you use @uref{https://melpa.org/#/paredit, paredit}, you might want to add keybindings to @code{paredit-mode-map}:
@itemize
@item
Bind the curly brace keys to @code{paredit-open-curly} and @code{paredit-close-curly}.
@item
Bind whatever keys you prefer for @code{paredit-wrap-square} and @code{paredit-wrap-curly}.
@end itemize
For example, with @uref{https://melpa.org/#/use-package, @code{use-package}}:
@lisp
(use-package paredit
:ensure t
:config
(dolist (m '(emacs-lisp-mode-hook
racket-mode-hook
racket-repl-mode-hook))
(add-hook m #'paredit-mode))
(bind-keys :map paredit-mode-map
("@{" . paredit-open-curly)
("@}" . paredit-close-curly))
(unless terminal-frame
(bind-keys :map paredit-mode-map
("M-[" . paredit-wrap-square)
("M-@{" . paredit-wrap-curly))))
@end lisp
Starting c. November 2022, paredit binds the @kbd{RET} key to its own command. Unfortunately this is @emph{not} compatible with interactive modes --- including but not limited to @code{racket-repl-mode} --- which expect @kbd{RET} to be bound to a command to submit your input to the REPL@. In other words, if you type an expression and hit @kbd{RET} , nothing will happen and the REPL will seem frozen. You @code{M-x racket-repl-submit} to proceed.
If you want to use paredit with interactive modes, their advice is to remove the binding from @code{paredit-mode-map} (note that this will also disable it for all buffers, including editing buffers). One way you can do this for all related keys:
@lisp
(dolist (k '("RET" "C-m" "C-j"))
(define-key paredit-mode-map (kbd k) nil))
@end lisp
@node smartparens
@section smartparens
@quotation
Note: If you use @ref{racket-hash-lang-mode}, see @ref{racket-hash-lang-module-language-hook} for how to enable/disable smartparens based on the specific #lang.
@end quotation
If instead of paredit you prefer @uref{https://melpa.org/#/smartparens, smartparens}, you can use the default configuration it provides for Lisp modes generally and for Racket Mode specifically:
@lisp
(require 'smartparens-config)
@end lisp
@node Appearance of parentheses
@section Appearance of parentheses
If you prefer parentheses to appear ``dimmed'', see @uref{https://melpa.org/#/paren-face, paren-face}.
If you prefer the opposite, see @uref{https://melpa.org/#/rainbow-delimiters, rainbow-delimiters}.
@node Edit buffers and REPL buffers
@section Edit buffers and REPL buffers
By default, all @code{racket-mode} edit buffers share one @code{racket-repl-mode} buffer, named @code{*Racket REPL*}. For example, if you run foo.rkt, the REPL prompt changes to @code{foo.rkt>}, and the REPL is inside the file module namespace. If you then run bar.rkt, the REPL prompt changes to @code{bar.rkt>}, and you are in that namespace.
If you prefer, you can use more than one REPL buffer, by customizing the variable @ref{racket-repl-buffer-name-function}:
@itemize
@item
Share a REPL buffer among files belonging to the same project; each REPL buffer is named @code{*Racket REPL <project-name>*}.
@item
A unique REPL buffer for each edit buffer, similar to Dr Racket; each REPL buffer is named @code{*Racket REPL <file.rkt>*}.
@item
You can also define your own, custom function.
@end itemize
You can customize where the REPL buffer is displayed by adding an item to the Emacs variable @code{display-buffer-alist}. A good regular expression to use for this would be @code{\\`\\*Racket REPL}. For example, if you wanted to make the REPL buffer appear in a new frame:
@lisp
(add-to-list 'display-buffer-alist
'("\\`\\*Racket REPL"
(display-buffer-reuse-window
display-buffer-pop-up-frame)
(reusable-frames . 0)
(inhibit-same-window . t)))
@end lisp
@node eldoc
@section eldoc
By default Racket Mode sets @code{eldoc-documentation-function} to @code{nil} --- no @code{eldoc-mode} support. You may set it to @code{racket-eldoc-function} in a @code{racket-mode-hook} and @code{racket-repl-mode-hook} if you really want to use @code{eldoc-mode} with Racket. But it is not a very satisfying experience because Racket is not a very ``eldoc-friendly'' language. Although Racket Mode attempts to discover argument lists, contracts, or types this doesn't work in many common cases:
@itemize
@item
Many Racket primitives are defined in @code{#%kernel} or @code{#%runtime}. There's no easy way to determine their argument lists. Most do not @code{provide} a contract.
@item
Many of the interesting Racket forms are syntax (macros) not functions. There's no easy way to determine their ``argument lists''.
@item
When a form has documentation, Racket Mode can show the \``bluebox\'' -- but often that does not fit in a single line as you would normally expect with eldoc.
@end itemize
A more satisfying experience is to use @ref{racket-xp-describe} or @ref{racket-xp-documentation}.
@node Start faster
@section Start faster
You can use @ref{racket-mode-start-faster} to make the Racket REPL start faster.
@node Unicode input method
@section Unicode input method
An optional Emacs input method, @code{racket-unicode}, lets you easily type various Unicode symbols that might be useful when writing Racket code.
To automatically enable the @code{racket-unicode} input method in @code{racket-mode} and @code{racket-repl-mode} buffers, put the following code in your Emacs init file:
@lisp
(add-hook 'racket-mode-hook #'racket-unicode-input-method-enable)
(add-hook 'racket-repl-mode-hook #'racket-unicode-input-method-enable)
@end lisp
@xref{racket-unicode-input-method-enable}.
@xref{racket-insert-lambda}.
@node Ligatures
@section Ligatures
Prior to Emacs 28.0.50, things like @code{auto-composition-mode} or @code{ligature-mode} that use @code{composition-function-table} to display ligatures can cause Emacs to freeze. This can happen when an Emacs @emph{overlay} displays a string containing such a ligature. Although the problem is not limited to Racket Mode, it affects the overlays created by @code{racket-show-pseudo-tooltip}, as used by @code{racket-xp-mode}. The only known work-around is to change the value of @code{racket-show-functions} to something ``boring'' such as @code{(racket-show-echo-area)}.
@node Architecture
@chapter Architecture
Racket Mode consists of a single Emacs front end, and one or more processes running a back end written in Racket.@footnote{Racket Mode's Racket code is delivered as part of the Emacs package --- @emph{not} as a Racket package. Delivering both Emacs and Racket code in one Emacs package simplifies installation and updates. The main drawback is that the Racket code is not automatically compiled, as would normally be done by @code{raco pkg install}. To address this: @xref{racket-mode-start-faster}.}
A back end is responsible for commands that cannot be implemented in Emacs Lisp, as well as supplying zero or more REPLs.
Although you can start and stop a back end with @code{racket-start-back-end} and @code{racket-stop-back-end}, a back end is normally started automatically when the front end needs to issue some command. This includes commands that do @emph{not} involve @code{racket-run} or a REPL@. For example @code{racket-xp-mode} issues commands to check your code and annotate the buffer, even if you do not run it. In other words, a back end supplies zero or more REPLs --- a back end is not the same thing as a REPL@.
To learn more about how @emph{many} REPLs are used: @xref{racket-repl-buffer-name-function}.
In the common case there is only one back end, on the same local host as Emacs, and it is used for @code{.rkt} files in any directory.
@image{scenario-0,,, Emacs front end and one local back end. Command I/O via pipe (local) or ssh (remote). Each back end provides zero or more REPLs.,.svg}
However you can configure using any number of back ends on any number of local or remote host paths.
As one example, you can have multiple back ends on the local host. One back end is used for a project under a specific subdirectory, and the other back end for all others. (Perhaps one project needs Racket built from source, and everything else uses an installed, older version of Racket. By using different back ends, not only will @code{racket-run} use the desired version of Racket for a file, so will commands for documentation or visiting definitions.)
@image{scenario-1,,, Emacs front end and two local back ends --- one for a project path. Command I/O via pipe (local) or ssh (remote). Each back end provides zero or more REPLs.,.svg}
(Note: If you use various versions of Racket via @uref{https://direnv.net/, @code{direnv}} combined with the @uref{https://github.com/purcell/envrc, @code{envrc} Emacs package}, you still need a distinct back end for each project. To arrange this, add or modify a @code{.dir-locals.el} file next to each @code{.envrc} file; @xref{racket-add-back-end}.)
Furthermore, you could work with a project located on a remote host, whose files you edit using TRAMP@. You also want the back end to run there. For a remote host, Racket Mode copies its back end source files to the remote when necessary, and runs the back end using ssh.
@image{scenario-2,,, Emacs front end and a back end on a remote host. Command I/O via pipe (local) or ssh (remote). Each back end provides zero or more REPLs.,.svg}
Of course the remote can also use different back ends for different paths.
@image{scenario-3,,, Emacs front end and two back ends on a remote host. Command I/O via pipe (local) or ssh (remote). Each back end provides zero or more REPLs.,.svg}
And of course you can have multiple remotes.
@image{scenario-4,,, Emacs front end and two back ends each on two remote hosts. Command I/O via pipe (local) or ssh (remote). Each back end provides zero or more REPLs.,.svg}
If you need any of these ``fancy'' configurations: @xref{racket-add-back-end}.
However by default a configuration is automatically created for one back end on the local host. For that very common case, you don't need to configure anything.
@node Reference
@chapter Reference
The following sections are generated from the doc strings for each command, variable, or face. (As a result, some of the formatting might not be quite as nice or correct as in the previous sections.)
You can also view these by using the normal Emacs help mechanism:
@itemize
@item
@kbd{C-h f} and enter the name of a command.
@item
@kbd{C-h v} and enter the name of a variable.
@end itemize
@node Commands
@chapter Commands
@menu
* Edit::
* Hash Langs::
* Explore::
* Run::
* Test::
* Eval::
* Collections::
* Macro expand::
* Other::
@end menu
@node Edit
@section Edit
@menu
* racket-mode::
* racket-insert-lambda::
* racket-fold-all-tests::
* racket-unfold-all-tests::
* racket-tidy-requires::
* racket-trim-requires::
* racket-base-requires::
* racket-add-require-for-identifier::
* racket-indent-line::
* racket-smart-open-bracket-mode::
* racket-insert-closing::
* racket-cycle-paren-shapes::
* racket-backward-up-list::
* racket-unicode-input-method-enable::
* racket-align::
* racket-unalign::
* racket-complete-at-point::
@end menu
@node racket-mode
@subsection racket-mode
@kbd{M-x} @code{racket-mode}
Major mode for editing Racket source files.
@multitable {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}
@item Key
@tab Binding
@item @kbd{@}}
@tab @ref{racket-insert-closing}
@item @kbd{]}
@tab @ref{racket-insert-closing}
@item @kbd{)}
@tab @ref{racket-insert-closing}
@item @kbd{TAB}
@tab @code{indent-for-tab-command}
@item @kbd{C-x C-e}
@tab @ref{racket-send-last-sexp}
@item @kbd{C-M-y}
@tab @ref{racket-insert-lambda}
@item @kbd{C-M-u}
@tab @ref{racket-backward-up-list}
@item @kbd{C-M-x}
@tab @ref{racket-send-definition}
@item @kbd{C-c C-u}
@tab @ref{racket-unfold-all-tests}
@item @kbd{C-c C-f}
@tab @ref{racket-fold-all-tests}
@item @kbd{C-c C-.}
@tab @ref{racket-describe-search}
@item @kbd{C-c C-s}
@tab @ref{racket-describe-search}
@item @kbd{C-c C-d}
@tab @ref{racket-documentation-search}
@item @kbd{C-c C-p}
@tab @ref{racket-cycle-paren-shapes}
@item @kbd{C-c C-x C-f}
@tab @ref{racket-open-require-path}
@item @kbd{C-c C-e r}
@tab @ref{racket-expand-region}
@item @kbd{C-c C-e e}
@tab @ref{racket-expand-last-sexp}
@item @kbd{C-c C-e x}
@tab @ref{racket-expand-definition}
@item @kbd{C-c C-e f}
@tab @ref{racket-expand-file}
@item @kbd{C-c C-r}
@tab @ref{racket-send-region}
@item @kbd{C-c C-o}
@tab @ref{racket-profile}
@item @kbd{C-c C-l}
@tab @ref{racket-logger}