In my auto-indenter and style checker (not yet a package that’s widely available): evidently, there are some preferences that control which constructs get tabified differently than expected. So I tweak those preferences before invoking |tabify-all|. (In my case, I’m trying to support two plausible indentation styles, depending on whether students have different defaults set, so I compute the indentation twice.)

|#lang racket ;; Read in a source file and tabify it according to the following three tabbing styles: ;; ;; 1. Untabbed (as writen directly in the file) ;; 2. Standard DrRacket tabbing excluding the big-bang default. ;; big-bang indents as: ;; (big-bang a ;; b) ;; 3. Standard DrRacket tabbing including the big-bang default. ;; big-bang indents as: ;; (big-bang a ;; b) ;; ;; (-> racket:text%? (values string? string? string?)) ;; ;; WARNING!!! This function likely has effects based on the framework library. It should NOT touch ;; your filesystem. However, this module should not be instantiated alongside ;; other DrRacket preferences. ;; (provide tabify-text) (require framework/preferences) (define (tabify-text t) (parameterize* ([preferences:low-level-put-preferences (λ _ (void))] [preferences:low-level-get-preference (λ _ #f)]) (define untabbed (send t get-text)) (define tabbed (let () (match-define (list table rx1 rx2 rx3 rx4) (preferences:get 'framework:tabify)) (hash-remove! table 'big-bang) (preferences:set 'framework:tabify (list table rx1 rx2 rx3 rx4)) (send t tabify-all) (send t get-text))) (define lambda-tabbed (let () (match-define (list table rx1 rx2 rx3 rx4) (preferences:get 'framework:tabify)) (hash-set! table 'big-bang 'lambda) (preferences:set 'framework:tabify (list table rx1 rx2 rx3 rx4)) (send t tabify-all) (send t get-text))) (values untabbed tabbed lambda-tabbed))) |

On 3/20/20 3:12 PM, ‘John Clements’ via Racket Users wrote:

That actually solves a bunch of problems for me… but strangely, not the initial 
one. If, for instance, I tabify

(+      3

using the tabify-all method, the line with the four gets an indentation of 2, 
not 8. This is definitely different from DrRacket’s behavior.

Any idea how to fix this?

(cc:ing racket-users without permission, hope that’s okay?)


On Mar 20, 2020, at 12:05 PM, John Clements <> wrote:

Ah! That’s probably a better solution, I’ll just do that. I searched for 
“indent”, should I try to add that as a search term for the “tabify-all” method?

Many thanks!

(Also, sending mail to the list using “” seems to be 
broken right now for me, sigh.)


On Mar 20, 2020, at 12:01 PM, Robby Findler <> wrote:

I'm not sure of the details but did you consider just inserting it
into a racket:text% and then calling the tabify-all method?


On Fri, Mar 20, 2020 at 1:26 PM John Clements <> wrote:
Hmm… followup problem. If I try to indent the string “#|\nabc\n|#” (that is, a 
block comment), the compute-racket-amount-to-indent method returns #f. Is this 
a bug, or just undocumented behavior?


On Mar 20, 2020, at 10:41 AM, John Clements <> wrote:

I’m writing code to help me grade exams, and one of the issues I’ve run into is 
that the code coming out of the LMS is all totally unindented. So, for 
instance, a student’s response might read:

;Closures help a function value remember what substitutions have already been 
applied to it.
'{vars {{x {lam {x} {+ 1 x}}}
{y {lam {y} {+ 3 y}}}}
{+ {x 2} {y 4}}}

Obviously, it would be a lot easier to grade that if it was indented.

Excellent! A chance to whip up a quick-and-dirty auto-indenter, using DrRacket’s 
indentation framework. Specifically, the `compute-racket-amount-to-indent` method of 

I’m pleased to report almost total success, aside from one strange off-by-one 
error that I’m looking for help with.  To see it, here’s the result of my 
auto-indenter on the prior block:

'{vars {{x {lam {x} {+ 1 x}}}
      {y {lam {y} {+ 3 y}}}}
    {+ {x 2} {y 4}}}

(if you’re viewing this in a proportional-width font, that’s going to look 
terrible, sorry.)

The issue is that the final plus line isn’t lined up with the curly-brace 
that’s two chars after the end of `vars`… instead, it’s one character to the 

A much simpler test case is simply running on “(+ 3\n4)”. Rather than getting 
the 4 lined up with the 3, I get it one character to the left.

And… uh oh. A more illuminating test case occurs when I run my code on “(+      
3\n4)”. That is, I put a bunch more spaces before the 3. After indentation, the 
4 is still only indented by two characters. So it looks like the 
`compute-racket-amount-to-indent` method is not looking for the token following 
the first one following the paren in the prior line (which would account for 
all the spaces), but just adding one to the last position of the first token 
following the paren. This makes me wonder whether DrRacket actually uses this 
method to indent.


Well, here’s the code to reproduce this. Note that there’s an obvious 
bug/assumption in that my code doesn’t account for any existing leading spaces 
on a line, but none of my examples have leading spaces following newlines.

Advice appreciated!


#lang racket

(require framework)

;; given a string of racket text, return the corresponding indented string
(define (string-indent text-to-indent)
(define t (new racket:text%))
(send t erase)
(send t insert text-to-indent 0)
(define num-paragraphs (add1 (send t last-paragraph)))
(define indents
  (for/list ([n (in-range num-paragraphs)])
    (define par-start (send t paragraph-start-position n))
    (list par-start
          (send t compute-racket-amount-to-indent
;; NB: OBVIOUSLY WRONG, assumes all lines start with zero leading spaces:
;; act in reverse order, to avoid messing up the meaning of char posns:
(for ([indent (in-list (reverse indents))])
  (send t set-position (first indent))
  (define space-string
    (list->string (for/list ([i (in-range (second indent))]) #\space)))
  (send t insert space-string))
(send t get-text))

;; for now, always return 'other
(define (head-sexp-type str)

(define text-to-indent
;Closures help a function value remember what substitutions have already been 
applied to it.
'{vars {{x {lam {x} {+ 1 x}}}
{y {lam {y} {+ 3 y}}}}
{+ {x 2} {y 4}}}

(displayln (string-indent text-to-indent))

(displayln (string-indent "
(+ 3

(module+ test
(require rackunit)

(check-equal? (string-indent "
(+ 3
(+ 3

(check-equal? (string-indent "
(+  3
(+  3

(check-equal? (string-indent "
(+        3
(+        3


You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
To view this discussion on the web visit

Reply via email to