(module rope mzscheme
  (require (planet "" ("dyoo" "rope.plt" 3 2))
           (only (lib "" "srfi") string-count string-fold)
           (lib "")
           (lib "")
           (lib "")
           (lib "")
           (lib ""))
  ;; The references to pos here is a number starting from 1.
  ;; Indexes start from zero.
  (define pos/c (and/c integer? (>/c 0)))
  (define index/c natural-number/c)
  (define (index->pos index) (+ index 1))
  (define (pos->index pos) (- pos 1))
  ;; line-index: rope index -> index
  ;; Returns the index of the beginning of the line
  ;; that contains the input index.
  (define (line-index text index)
    (let loop ([i (sub1 (min index (rope-length text)))])
        [(< i 0) 0]
         (local ((define ch/special (rope-ref text i)))
             [(and (char? ch/special)
                   (char=? ch/special #\newline))
              (add1 i)]
              (loop (sub1 i))]))])))
  ;; line-pos : rope pos -> pos
  ;; Returns the position at the beginning of the line containing pos.
  (define (line-pos text pos)
    (index->pos (line-index text (pos->index pos))))
  ;; line-end-index : rope index -> index
  ;; retusn the position at the end of the line containing index.
  (define (line-end-index text index)
    (let loop ([i index])
        [(= i (rope-length text)) i]
        [(and (char? (rope-ref text i))
              (char=? (rope-ref text i) #\newline))
         (loop (add1 i))])))
  ;; line-end-pos : rope pos -> pos
  (define (line-end-pos text pos)
    (index->pos (line-end-index text (pos->index pos))))
  ;; line-rope/index : rope index -> rope
  ;; returns the line of text that contains index.
  (define (line-rope/index text index)
    (subrope text
             (line-index text index)
             (line-end-index text index)))
  ;; line-rope/pos : rope pos -> rope
  (define (line-rope/pos text pos)
    (line-rope/index text (pos->index pos)))
  ;; line-number: rope number -> number
  ;; computes line number at pos, starting at line one.
  (define (line-number a-rope pos)
    (local ((define (accum-line-count string/special acc)
              (match string/special
                [(struct rope:string (s))
                 (+ acc
                    (string-count s (lambda (x) (char=? x #\newline))))]
                [(struct rope:special (s))
      (rope-fold/leaves accum-line-count
                        (subrope a-rope 0 (pos->index pos)))))
  ;; rope-count-whitespace: rope -> natural-number
  ;; Returns the number of whitespace characters in a rope.
  (define (rope-count-whitespace a-rope)
    (local ((define (f string-or-special current-count)
              (match string-or-special
                [(struct rope:string (s))
                 (+ current-count
                    (count-whitespace-in-string s))]
                [(struct rope:special (s))
            (define (count-whitespace-in-string a-str)
              (string-fold (lambda (ch acc)
                             (case ch
                               [(#\space #\tab #\newline #\return)
                                (add1 acc)]
      (rope-fold/leaves f 0 a-rope)))
  ;; rope-leading-whitespace: rope -> rope
  ;; Returns the leading whitespace at the head of a-rope.
  (define (rope-leading-whitespace a-rope)
    (let/ec return
       (lambda (string/special acc)
         (match string/special
           [(struct rope:string (s))
              [(regexp-match #rx"^[ \t\n]*$" s)
               (rope-append acc string/special)]
              [(regexp-match #rx"^[ \t\n]*" s)
               (lambda (result)
                  (rope-append acc (string->rope (first result)))))])]
           [(struct rope:special (s))
            (return acc)]))
       (string->rope "")
  ;; rope-string/erasing-specials: rope->string
  ;; Like rope->string, but removes specials from the output.
  (define (rope->string/erasing-specials a-rope)
     (rope-fold/leaves (lambda (string/special acc)
                         (match string/special
                           [(struct rope:string (s))
                            (rope-append acc string/special)]
                           [(struct rope:special (s))
  (define rope-space (string->rope " "))
  (define rope-empty (string->rope ""))
  (provide (all-from (planet "" ("dyoo" "rope.plt" 3 2))))
   [line-index (rope? index/c . -> . index/c)]
   [line-pos (rope? pos/c . -> . pos/c)]
   [line-end-index (rope? index/c . -> . index/c)]
   [line-end-pos (rope? pos/c . -> . pos/c)]
   [line-rope/index (rope? index/c . -> . rope?)]
   [line-rope/pos (rope? pos/c . -> . rope?)]
   [line-number (rope? pos/c . -> . pos/c)]
   [rope-count-whitespace (rope? . -> . natural-number/c)]
   [rope-leading-whitespace (rope? . -> . rope?)]
   [rope->string/erasing-specials (rope? . -> . string?)]
   [rope-space rope?]
   [rope-empty rope?]))