-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-window.rkt
68 lines (60 loc) · 2.43 KB
/
main-window.rkt
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
#lang racket/gui
(provide main-window)
(define main-window
(new (class frame%
(super-new)
(define/public (on-dump-keys event)
;; Prevent the user from repositioning the cursor
(let ([key (send event get-key-code)])
(for/or ([dump (list 'left 'right 'up 'down
'end 'home #\rubout)])
(eq? key dump))))
(define/override (on-subwindow-char receiver event)
(or (send this on-system-menu-char event)
(send this on-dump-keys event)
(send this on-traverse-char event))))
[label "Spitter"]
[min-width 600]
[min-height 30]
[stretchable-width #f]
[stretchable-height #f]
[style (list 'fullscreen-button)]))
(define pane (new horizontal-pane% [parent main-window]))
(define (get-last-character t)
(send t get-character (- (send t get-end-position) 1)))
(define spitter
;; Main text entry object
(new (class text%
(super-new)
(define deleted-words 0)
(define/augment (after-insert s l)
(if (and (> deleted-words 0)
(eq? (get-last-character this) #\space))
(set! deleted-words (- deleted-words 1)) (void)))
(define/augment (on-delete s l)
(if (eq? (get-last-character this) #\space)
(set! deleted-words (+ deleted-words 1)) (void)))
(define/augment (can-delete? s l)
(< deleted-words 3))
(define/override (on-default-event event) (void)) ;Eliminate any mouse events
(define/public (spit)
;; Make sure the text ends with a newline:
(if (eq? (get-last-character this) #\newline)
(void)
(send this insert #\newline))
;; Make a double newline between paragraphs
(for ([i (send this find-string-all (string #\newline) 'backward)])
(send this insert #\newline i))
(call-with-output-file "output" #:exists 'append
(λ (out)
(send this save-port out 'text)))
(send this erase)))))
(define keymap (new keymap%))
(send keymap add-function "spit" (λ (in event) (send in spit)))
(send keymap map-function "c:s" "spit")
(send spitter set-keymap keymap)
(define editor-canvas
(new editor-canvas%
[parent pane]
[editor spitter]
[style (list 'no-border 'hide-hscroll 'hide-vscroll 'transparent)]))