Skip to content

Commit ce57719

Browse files
Move lisp-mode advice to its own file, improve comments/doc strings
1 parent b702c19 commit ce57719

File tree

3 files changed

+60
-30
lines changed

3 files changed

+60
-30
lines changed

racket-indent.el

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,6 @@
3232
;; `racket-indent-line'. That just directly calls
3333
;; `racket-indent-function'.
3434

35-
;; Having said all that, we still have the matter of `paredit-mode'.
36-
;; It directly calls `lisp-indent-line' instead of
37-
;; `indent-line-function'. And, it directly calls `indent-sexp'
38-
;; instead of `prog-indent-sexp'. Therefore it gets `lisp-mode'
39-
;; indent, not ours. To address this, advise those two functions to do
40-
;; the right thing when one of our major modes is active.
41-
(defun racket--lisp-indent-line-advice (orig &rest args)
42-
"If `racket--mode-edits-racket-p' use the variable `indent-line-function'."
43-
(apply (if (racket--mode-edits-racket-p) indent-line-function orig)
44-
args))
45-
(defun racket--indent-sexp-advice (orig &rest args)
46-
"If `racket--mode-edits-racket-p' use `racket-indent-sexp-contents'."
47-
(apply (if (racket--mode-edits-racket-p) #'racket-indent-sexp-contents orig)
48-
args))
49-
(advice-add 'lisp-indent-line :around #'racket--lisp-indent-line-advice)
50-
(advice-add 'indent-sexp :around #'racket--indent-sexp-advice)
51-
52-
;; lisp-mode's `indent-sexp' differs from `prog-indent-sexp'. The
53-
;; former does NOT indent the current line, just subsequent lines (if
54-
;; any). In other words it does not indent the sexp as a whole, just
55-
;; its contents. That behavior is desirable in some cases; see e.g.
56-
;; issue #293. So supply our equivalent of `indent-sexp' here, too:
57-
(defun racket-indent-sexp-contents ()
58-
"Indent each line of the sexp starting just after point."
59-
(interactive)
60-
(let ((end-of-expression (save-excursion (forward-sexp 1) (point)))
61-
(beg-of-2nd-line (save-excursion (forward-line 1) (point))))
62-
(when (< beg-of-2nd-line end-of-expression)
63-
(indent-region beg-of-2nd-line end-of-expression))))
64-
6535
(defun racket-indent-line (&optional _whole-exp)
6636
"Indent current line as Racket code.
6737

racket-lisp-mode.el

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
;;; racket-lisp-mode.el -*- lexical-binding: t; -*-
2+
3+
;; Copyright (c) 2013-2024 by Greg Hendershott.
4+
;; Portions Copyright (C) 1985-1986, 1999-2013 Free Software Foundation, Inc.
5+
6+
;; Author: Greg Hendershott
7+
;; URL: https://github.com/greghendershott/racket-mode
8+
9+
;; SPDX-License-Identifier: GPL-3.0-or-later
10+
11+
;; 1. Some packages like paredit and lispy directly call `lisp-mode'
12+
;; functions `lisp-indent-line' and `indent-sexp'. (As opposed to
13+
;; calling functions like `indent-line-to' and `prog-indent-sexp'
14+
;; that a mode can specialize via `indent-line-function' and
15+
;; `indent-region-function'.)
16+
;;
17+
;; 2. Although that's OK for modes like `scheme-mode' that are derived
18+
;; from `lisp-mode', `racket-mode' is not.
19+
;;
20+
;; Therefore if users want to use such packages hardwired to call
21+
;; those two `lisp-mode' function, AFAICT we have no choice but to
22+
;; advise those two functions. :(
23+
;;
24+
;; Furthermore lisp-mode's `indent-sexp' differs from
25+
;; `prog-indent-sexp' as explained below in the doc string for
26+
;; `racket-indent-sexp-contents'.
27+
28+
(require 'lisp-mode)
29+
(require 'racket-util)
30+
31+
(defun racket--lisp-indent-line-advice (orig &rest args)
32+
"If `racket--mode-edits-racket-p' use the variable `indent-line-function'."
33+
(apply (if (racket--mode-edits-racket-p) indent-line-function orig)
34+
args))
35+
(advice-add 'lisp-indent-line :around #'racket--lisp-indent-line-advice)
36+
37+
(defun racket--indent-sexp-advice (orig &rest args)
38+
"If `racket--mode-edits-racket-p' use `racket-indent-sexp-contents'."
39+
(apply (if (racket--mode-edits-racket-p) #'racket-indent-sexp-contents orig)
40+
args))
41+
(advice-add 'indent-sexp :around #'racket--indent-sexp-advice)
42+
43+
(defun racket-indent-sexp-contents ()
44+
"Indent each line of the sexp starting just after point.
45+
46+
Unlike `prog-indent-sexp', which indents the entire sexp, this
47+
does /not/ indent the first line, at point, just subsequent lines
48+
if any. In other words it does not indent the sexp as a whole,
49+
just its contents. In this regard it is similar to the
50+
`lisp-mode'-specific function `indent-sexp'."
51+
(interactive)
52+
(let ((beg-of-2nd-line (save-excursion (forward-line 1) (point)))
53+
(end-of-expression (save-excursion (forward-sexp 1) (point))))
54+
(when (< beg-of-2nd-line end-of-expression)
55+
(indent-region beg-of-2nd-line end-of-expression))))
56+
57+
(provide 'racket-lisp-mode)
58+
59+
;; racket-lisp-mode.el ends here

racket-mode.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
(require 'racket-repl)
3737
(require 'racket-repl-buffer-name)
3838
(require 'racket-collection)
39+
(require 'racket-lisp-mode)
3940
(require 'racket-bug-report)
4041
(require 'racket-util)
4142
(require 'easymenu)

0 commit comments

Comments
 (0)