Write Racket With Emacs
Jul 13, 2020
4 minute read

At the opening of my previous blog Using Emacs Editor For The First Time says I want to use Emacs to program in Racket language. Now it’s the time to share my Emacs configuration and some humble opinions.

racket-mode

In Emacs, almost every programming language has its own major mode. And the Racket language has no exception. racket-mode is the only and powerful Emacs major mode package.

(use-package racket-mode
  :straight t
  :hook (racket-mode . racket-xp-mode))

Add the above configuration codes into your Emacs configuration for installation and configuration. See default key-binding maps from racket-mode-map, racket-xp-mode-map and racket-xp-control-c-hash-keymap variables if you want to know or to configure your own key-binding.

The above configuration codes require straight.el and use-package packages. You can know how to install them in my previous blog.

After installing the racket-mode package, you can program in Racket smoothly.

Enhance Programming Experience (Optional)

Company-Mode

Use company-mode for text completion. The company-mode is able to cooperate with the completion-at-point-functions(capf) variable. And the racket-mode will configure this variable automatically while activating.

(use-package company
  :straight t
  :config
  (setq company-minimum-prefix-length 2)
  (setq company-idle-delay 0.1)
  (setq company-tooltip-align-annotations t)
  :hook
  ((racket-mode . company-mode)
   (racket-repl-mode . company-mode)))

Rainbow Delimiters

Use rainbow-delimiters to highlight delimiters such as parentheses, brackets or braces, in rainbow-colors according to their depth.

(use-package rainbow-delimiters
  :straight t
  :hook
  ((racket-mode . rainbow-delimiters-mode)
   (racket-repl-mode . rainbow-delimiters-mode)))

paredit-mode

Paredit is a minor mode for performing structured editing of S-expression data. Paredit helps keep parentheses balanced and adds many keys for moving S-expressions and moving around in S-expressions.

See The Animated Guide to Paredit for more details.

(use-package paredit
  :straight t
  :hook
  ((racket-mode . paredit-mode)
   (racket-repl-mode . paredit-mode)))

The racket-mode Guide

This the simple and common guide for the beginner of racket-mode user. See more details from www.racket-mode.com.

Open Emacs and type <C-x C-f> (find-file) to open a new file "fibonacci.rkt". Typing below code writes a function to calculate Fibonacci numbers.

#lang racket/base

(define (fibonacci n)
  (define (iter a b count)
    (cond [(< count n)
           (iter b
                 (+ a b)
                 (add1 count))]
          [else a]))
  (iter 0 1 0))

Run

Type <C-c C-c> (racket-run-module-at-point) to run above code with repl, it will pop up a repl window at the bottom of frame. And type <C-x o> (other-window) or <C-c C-z> (racket-repl) to switch the focused window to repl. Type (fibonacci 6) and <enter> to calculate Fibonacci numbers.

AnimatedExample_Run.gif

Or just type <f5> to run above code with repl, it will change the focused window automatically.

Launch Repl

Also can type <C-c C-z> (racket-repl) to launch repl without running code, it will change the focused window automatically. If already having an existed repl, it will reuse it.

You can switch between buffer and repl by using this command if repl is used to run code from this buffer.

AnimatedExample_Repl.gif

Describe Function

Move the cursor on the function name, and type <C-c C-.> (racket-xp-describe). Also, <C-c C-.> work in repl, it will execute racket-repl-describe command instead.

AnimatedExample_DescribeFunction.gif

Jump To The Function Source

Move the cursor on the function name, and type <M-.> (racket-xp-visit-definition) to jump to the function source. Type <M-,> (racket-unvisit) to return the previous location. Same as above, it also has racket-repl-visit-definition command for repl.

AnimatedExample_JumpSource.gif

Find Reference

Move the cursor on the function name, and type <C-c # n> (racket-xp-next-use) to find the next reference of function.

AnimatedExample_FindReference.gif

Testing

Type <C-c C-t> or <C-f5> to run the command racket-test, it will run the submodule test of the current file. If any rackunit test failure messages showing, we can use <C-x `> to jump to the location of each failing test.

(module+ test
  (require rackunit)
  (check-eq? (fibonacci 0) 0)
  (check-eq? (fibonacci 1) 1)
  (check-eq? (fibonacci 2) 1)
  (check-true #f)
  (check-eq? (fibonacci 3) 2)
  (check-eq? (fibonacci 4) 3)
  (check-eq? (fibonacci 5) 5))

AnimatedExample_Testing.gif

Debugging

This is an experimental feature of racket-mode. See the document of latest instruction for debugging.

Type <C-u C-M-x> to add a breakpoint on the function which cursor currently on. And run the code (or execute function) with repl to launch the debugger.

AnimatedExample_Debugging.gif

Profiling

Type <C-c C-o> to open the Racket-Profile window. It will run (call racket-run-module-at-point) and profile code of current buffer, then show the profiling result. If nothing to profile, it will take no effect. Move the cursor on the submodule test then run the profiling command. It will run and profile all the tests. After activating racket-profile-mode, the repl is profiled too. So we can execute a function call, then switch to racket-profile window, type <g> to refresh the profiling result.

AnimatedExample_Profiling.gif

References

The GIF images are recorded by CleanShot X.




comments powered by Disqus