-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsimple.rkt
1747 lines (1533 loc) · 63.6 KB
/
simple.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang racket/base
(provide (all-defined-out)
(all-from-out "narrow.rkt"))
;;;
;;; SIMPLE
;;;
; This file contains various general editing commands that
; don't belong in a specific major mode.
;; The names of the commands were chosen to match the command names used in Emacs.
;; The index of Emacs commands are here:
;; https://www.gnu.org/software/emacs/manual/html_node/emacs/Command-Index.html#Command-Index
;; However it is often better to look at:
;; https://www.gnu.org/software/emacs/manual/html_node/elisp/index.html
;; The interactive commands in this file are all user commands.
;; The user can invoke them either via key bindings or via M-x.
;; See more on interactive commands in "commands.rkt".
(require (for-syntax syntax/parse racket/base)
racket/class racket/format racket/list racket/match racket/set racket/string
syntax/to-string
racket/gui/base
framework
"buffer.rkt"
"buffer-locals.rkt"
"chars.rkt"
"colors.rkt"
"commands.rkt"
"deletion.rkt"
"frame.rkt"
"killing.rkt"
"line.rkt"
"locals.rkt"
"mark.rkt"
"message.rkt"
"mode.rkt"
"narrow.rkt"
"parameters.rkt"
"point.rkt"
"recently-opened.rkt"
"region.rkt"
"representation.rkt"
"region.rkt"
"render.rkt"
"search.rkt"
"status-line.rkt"
"string-utils.rkt"
"text.rkt"
"window.rkt"
"words.rkt"
(prefix-in overlays: "overlays.rkt"))
;;;
;;; LINES
;;;
; beginning-of-line
; move point to beginning of line (logical line, not screen line)
(define-interactive (beginning-of-line [m (get-point)])
(goto-char (line-beginning-position m) m))
(define-interactive (end-of-line [m (get-point)])
(goto-char (line-end-position m) m))
(define-interactive (move-to-column n [m (get-point)])
; n = numprefix
(define-values (start end) (mark-line-span m))
(goto-char (clamp start (+ start n) end) m))
(define-interactive (forward-line [n 1])
; Move point to start of text line i+n, where i is the current line.
(deactivate-region-mark)
(cond
[(< n 0) (backward-line (- n))]
[(= n 0) (beginning-of-line)]
[else (define b (current-buffer))
(let loop ([n n] [i (point)])
(define-values (start end) (buffer-line-span b i))
(cond [(= n 0) (goto-char (or start (point-max)))]
[else (loop (- n 1) end)]))]))
(define-interactive (backward-line [n 1])
; Move point to start of text line i-n, where i is the current line.
(deactivate-region-mark)
(cond
[(< n 0) (forward-line (- n))]
[(= n 0) (beginning-of-line)]
[else (define b (current-buffer))
(let loop ([n n] [i (position (get-point))])
(define-values (start end) (buffer-line-span b i))
(cond [(= n 0) (goto-char (or start (point-min)))]
[else (loop (- n 1) (- start 1))]))]))
(define-interactive (next-line [n 1])
; Moves point one screen line down. Attempts to keep the column.
(deactivate-region-mark)
(define point (get-point))
(define (next-line1)
(define len (line-length (mark-line point)))
(define n (local screen-line-length))
(define whole (quotient len (- n 1))) ; number of full screen lines
(define col (mark-column point))
(define screen-col (remainder col n))
(cond
[(and (not (mark-on-last-line? point))
(>= col (* whole n))) ; we need to move to the next text line
(forward-line) ; moves to beginning of line
(move-to-column (min screen-col (- (line-length (mark-line point)) 1)))]
[(<= (+ col n) len) ; there is room to move an entire screen line (same text line)
(move-to-column (+ col n))]
[else ; there is not room to move an entire line, so go to end of this text line
(move-to-column (line-length (mark-line point)))]))
(cond
[(= n 1) (next-line1)]
[(> n 0) (for ([_ n]) (next-line1))]
[(< n 0) (previous-line (- n))]
[else (void)]))
(define (on-first-line?)
(define b (current-buffer))
(define m (get-point))
(cond
[(buffer-restricted? b) (on-same-line? m (buffer-restriction-start b))]
[else (mark-on-first-line? m)]))
(define-interactive (previous-line [k 1])
; Moves point one screen line up.
(deactivate-region-mark)
(define point (get-point))
(define prev-len (line-length (mark-line point)))
(define n (local screen-line-length))
(define (previous-line1)
(cond
[(on-first-line?) (void)] ; use bell?
[else (define len (line-length (mark-line point)))
(define col (mark-column point))
(define screen-col (remainder col n))
(define whole (quotient len (- n 1))) ; number of full screen lines
;(displayln (list 'col col 'screen-col screen-col 'whole whole 'n n 'prev-len prev-len))
(cond
[(>= col n) ; room to move entire screen line (same text line)
(move-to-column (- col n))]
[else
(beginning-of-line)
(backward-line)
(define new-len (line-length (mark-line point)))
(move-to-column (min (+ (* n (quotient new-len (- n 1))) screen-col)
(- new-len 1)))])]))
(cond
[(= k 1) (previous-line1)]
[(> k 0) (for ([_ k]) (previous-line1))]
[(< k 0) (next-line (- k))]
[else (void)]))
(define-interactive (swap-line-up) ; C-D-<up>
; Swap current line with the previous line. Keep position of point.
(unless (on-first-line?)
(define col (current-column))
(beginning-of-line)
(command-set-mark)
(end-of-line)
(kill-region)
(backward-delete-char)
(beginning-of-line)
(buffer-insert-latest-kill)
(open-line)
(move-to-column col)))
(define-interactive (swap-line-down) ; C-D-<down>
; Swap current line with the next line. Keep position of point.
(unless (on-last-line?)
(define col (current-column))
(beginning-of-line)
(command-set-mark)
(end-of-line)
(kill-region)
(when (on-first-line?)
(forward-char))
(backward-delete-char)
(forward-char)
(forward-line)
(when (eob?)
(break-line))
(buffer-insert-latest-kill)
(unless (on-last-line?)
(open-line))
(move-to-column col)))
(define (on-last-line?)
(mark-on-last-line? (get-point)))
;;;
;;; PAGES
;;;
(define-interactive (page-down)
(define w (current-window))
(define point (buffer-point (window-buffer w)))
(define start-mark (window-start-mark w))
(define end-mark (window-end-mark w))
(define start-row (mark-row start-mark))
(define end-row (mark-row end-mark))
(define delta (max 0 (- (number-of-lines-on-screen w)
(current-next-screen-context-lines))))
(mark-move-down! point delta)
(mark-move-down! start-mark delta)
(mark-move-down! end-mark delta)
(refresh-frame))
(define-interactive (page-up)
(define w (current-window))
(define point (buffer-point (window-buffer w)))
(define start-mark (window-start-mark w))
(define end-mark (window-end-mark w))
(define start-row (mark-row start-mark))
(define end-row (mark-row end-mark))
(define delta (max 0 (- (number-of-lines-on-screen w)
(current-next-screen-context-lines))))
(mark-move-up! point delta)
(mark-move-up! start-mark delta)
(mark-move-up! end-mark delta)
(refresh-frame))
;;;
;;; MARK AND POINT - REGION
;;;
(define-interactive (command-set-mark)
(buffer-set-mark-to-point (current-buffer)))
(define-interactive (exchange-point-and-mark)
(unless (get-mark) (new-mark (current-buffer) "*mark-pm*")) ; TODO
(buffer-exchange-point-and-mark! (current-buffer))
(mark-activate! (get-mark)))
(define-interactive (mark-word) ; Set mark after next word (doesn't move point)
(define m (get-mark))
(define swap exchange-point-and-mark)
(cond [(and m (mark-active? m))
(with-saved-point
(cond
[(mark<= (get-point) m) (swap) (forward-word) (swap)]
[else (swap) (backward-word) (swap)]))]
[else (command-set-mark) (mark-word)]))
(define (prepare-extend-region)
(unless (mark-active? (get-mark))
(set-mark (get-point))))
(define-syntax (save-region-activity stx)
(syntax-parse stx
[(_save-region-activity body ...)
(syntax/loc stx
(let* ([m (get-mark)] [active? (mark-active? m)])
(begin0 (begin body ...)
(set-mark-active?! m active?))))]))
(define-interactive (backward-char/extend-region)
(check-mark (get-point))
(prepare-extend-region)
(save-region-activity
(backward-char +1)))
(define-interactive (forward-char/extend-region)
(check-mark (get-point))
(prepare-extend-region)
(save-region-activity
(forward-char +1)))
(define-interactive (previous-line/extend-region [n 1])
(prepare-extend-region)
(save-region-activity
(previous-line n)))
(define-interactive (next-line/extend-region [n 1])
(prepare-extend-region)
(save-region-activity
(next-line n)))
(define-interactive (forward-word/extend-region)
(prepare-extend-region)
(save-region-activity
(forward-word)))
(define-interactive (backward-word/extend-region)
(prepare-extend-region)
(save-region-activity
(backward-word)))
(define-interactive (beginning-of-line/extend-region)
(prepare-extend-region)
(save-region-activity
(beginning-of-line)))
(define-interactive (end-of-line/extend-region)
(prepare-extend-region)
(save-region-activity
(end-of-line)))
;;;
;;; BUFFER
;;;
;;; Buffer Movement
(define-interactive (beginning-of-buffer)
(goto-char (point-min)))
(define-interactive (end-of-buffer)
(goto-char (point-max)))
(define-interactive (end-of-buffer/extend-region)
(prepare-extend-region)
(end-of-buffer))
(define-interactive (beginning-of-buffer/extend-region)
(prepare-extend-region)
(beginning-of-buffer))
;;; Buffer Input and Output
(define-interactive (save-buffer [b (current-buffer)])
(save-buffer! b)
(add-recently-opened-file (buffer-path b))
(refresh-frame))
(define-interactive (save-buffer-as [b (current-buffer)])
(save-buffer-as! b)
(add-recently-opened-file (buffer-path b))
(refresh-frame))
(define-interactive (save-some-buffers [b (current-buffer)])
(add-recently-opened-file (buffer-path b))
(save-buffer b)) ; todo : ask in minibuffer
(define-interactive (open-file-or-create [path (finder:get-file)])
(when path ; #f = none selected
; open buffer and display it in window
(define b (buffer-open-file-or-create path))
(window-switch-buffer! (current-window) b)
; (todo -switch-start-and-end-marks-here-) ; xxx
; Problem: The start and end marks are currently owned by the window.
; Should they be be in the buffer instead?
; Or should we delete them, change their buffer, and add them to the new buffer?
(current-buffer b)
; set mode
(cond [(file-path->mode-function path) => (λ (mode) (mode))]
[else (fundamental-mode)])
(define f (current-frame))
; update recent files
(add-recently-opened-file path)
; make sure a mode change is seen in the status line below
(send (frame-status-line f) set-label (status-line-hook))
(refresh-frame f)))
(define (open-file-in-current-window path)
(when (file-exists? path)
; open buffer and display it in window
(define b (buffer-open-file-or-create path))
(window-switch-buffer! (current-window) b)
(current-buffer b)
; set mode
(cond [(file-path->mode-function path) => (λ (mode) (mode))]
[else (fundamental-mode)])
(define f (current-frame))
; update recent files
(add-recently-opened-file path)
; make sure a mode change is seen in the status line below
(send (frame-status-line f) set-label (status-line-hook))
(refresh-frame f)))
;;; Buffer Navigation
(define-interactive (next-buffer) ; show next buffer in current window
(define w (current-window))
(define b (get-next-buffer))
(window-switch-buffer! w b)
(current-buffer b))
(define-interactive (previous-buffer) ; show next buffer in current window
(define w (current-window))
(define b (get-previous-buffer))
(window-switch-buffer! w b)
(current-buffer b))
(define (switch-to-buffer buffer-or-name)
"Switch to the given buffer in the current window."
(define b buffer-or-name)
(when (string? buffer-or-name)
(set! b (get-buffer b)))
(when (buffer? b)
(window-switch-buffer! (current-window) b)
(current-buffer b)))
;;;
;;; WINDOWS (27.2 Windows and Frames)
;;;
(define (focus-window [w (current-window)])
(send (window-canvas w) focus))
(define-interactive (other-window) ; switch current window and buffer
(define ws (frame-window-tree (current-frame)))
(define w (list-next ws (current-window) eq?))
(current-window w)
(unless (window? w) (error))
(current-buffer (window-buffer w))
(focus-window w))
(define-interactive (delete-window [w (current-window)])
(window-delete! w))
(define-interactive (delete-other-windows [w (current-window)])
(define ws (frame-window-tree (window-frame w)))
(for ([win (in-list ws)])
(unless (eq? w win)
(delete-window win)))
(refresh-frame))
(define (window-list [frame (current-frame)])
"Return list of windows in the frame.\n
If frame is #f or omitted the current frame is used."
(set! frame (or frame (current-frame)))
(frame->windows frame))
; See window.rkt
#;(define (get-buffer-window-list [buffer-or-name #f])
"Return list of all windows displaying the buffer."
(set! buffer-or-name (or buffer-or-name (current-buffer)))
(define this?
(cond [(buffer? buffer-or-name) (λ (b) (eq? b buffer-or-name))]
[else (λ (b) (equal? (buffer-name b) buffer-or-name))]))
(for/list ([w (window-list)]
#:when (this? (window-buffer w)))
(window-buffer w)))
(define (buffer-visible? [buffer-or-name #f])
"Is the buffer visible in the current frame?"
(not (empty? (get-buffer-window-list buffer-or-name))))
;;;
;;; FRAME
;;;
(define-interactive (maximize-frame [f (current-frame)]) ; maximize / demaximize frame
(when (frame? f)
(define f% (frame-frame% f))
(when (is-a? f% frame%)
(send f% maximize (not (send f% is-maximized?))))))
;;;
;;;
;;;
(define (position->index pos)
(if (mark? pos) (mark-position pos) pos))
(define (check-position who what)
(unless (or (number? what) (mark? what))
(error who "expected a position (index or mark), got ~a" what)))
(define-interactive (set-mark pos)
(check-position 'set-mark pos)
(define mb (current-buffer))
(define m (buffer-the-mark mb))
(define b (and (mark? pos) (mark-buffer pos)))
(cond
; the two marks belong to the same buffer
[(and b (eq? b mb))
; set position
(set-mark-position! m (mark-position pos))
; and link
(remove-mark-from-linked-line! m (mark-link m))
(define l (mark-link pos))
(set-mark-link! m l)
(add-mark-to-linked-line! m l)
(mark-activate! m)]
[else
(with-saved-point
(goto-char pos)
(mark-activate!
(buffer-set-mark-to-point)))]))
(define-interactive (text-scale-adjust m)
; (displayln `(text-scale-adjust ,m))
(font-size (min 40 (max 1 (+ (font-size) m)))))
; create-new-buffer : -> void
; create new buffer and switch to it
(define-interactive (create-new-buffer [title "Untitled"])
(define b (new-buffer (new-text) #f (generate-new-buffer-name title)))
(window-switch-buffer! (current-window) b)
(current-buffer b)
(refresh-frame (current-frame)))
; eval-buffer : -> void
; Read and evaluate each s-expression in the current buffer one at a time.
(define-interactive (eval-buffer)
(define b (current-buffer)) ; we get the buffer here
; (displayln (list 'eval-buffer 'before: (current-buffer)))
(define t (buffer-text b))
(define s (text->string t))
(define in (open-input-string s))
(define ns (buffer-locals b))
(define (read1 in)
(define stx (read-syntax 'read-from-buffer in))
(if (syntax? stx)
(namespace-syntax-introduce stx)
stx))
#;(; a few experiments that show current-buffer is not set, when ns is used.
(parameterize ([current-namespace ns])
(displayln (list 'eval-buffer 'later (current-buffer))))
(parameterize ([current-namespace ns])
(displayln (list 'eval-buffer 'later2 (eval-syntax #'(current-buffer) ns))))
(parameterize ([current-namespace ns] [current-buffer b])
(displayln (list 'eval-buffer 'later3 (eval-syntax #'(current-buffer) ns))))
(parameterize ([current-namespace ns] [current-buffer b])
(eval `(current-buffer ,b) ns)
(displayln (list 'eval-buffer 'later3 (eval-syntax #'(current-buffer) ns))))
(for ([stx (in-port read1 in)])
(displayln (eval-syntax stx ns))))
(parameterize ([current-namespace ns])
(localize ([current-buffer b])
; probably eof
(for ([stx (in-port read1 in)])
(with-handlers
(#;[exn:fail? (λ (e)
; position
(define pos (syntax-position stx))
(set! pos (and pos (- pos 1))) ; syntax positions count from 1
; expression
(define str (syntax->string stx))
(define datum (syntax->datum stx))
(when (list? datum) (set! str (string-append "\"" str "\"")))
; display it
(displayln "Error: Exception triggered during evaluation.")
(display "Exn: ") (displayln e)
(display "Pos: ") (displayln pos)
(display "Stx: ") (displayln stx)
(display "Expr: ") (displayln str))])
(eval `(current-buffer ,b) ns) ; sigh - the parameterize doesn't set current-buffer
(displayln (eval-syntax stx ns)))))))
(define-interactive (test-buffer-output)
(define b (new-buffer (new-text) #f (generate-new-buffer-name "*output*")))
(define p (make-output-buffer b))
(window-switch-buffer! (current-window) b)
(parameterize ([current-output-port p])
(localize ([current-buffer b])
(thread
(λ ()
(let loop ([n 0])
(displayln n)
(sleep 1.)
(loop (+ n 1))))))))
; (self-insert-command k) : -> void
; insert character k and move point
(define ((self-insert-command k))
(when (use-region?) (delete-region))
(define pa (current-prefix-argument))
(define i (or (and (integer? pa) (positive? pa) pa) 1))
(define b (current-buffer))
(define point (get-point))
(for ([_ (in-range i)])
; insert character
(buffer-insert-char! b point k)
; does the character break the line - if so maybe call auto-fill
(cond [(and (local auto-fill-mode?)
(set-member? (local auto-fill-chars) k)
(or (local auto-fill-function) normal-auto-fill-function))
=> (λ (fill) (fill))])))
(define-interactive (insert-newline)
(buffer-break-line! (current-buffer) (get-point)))
(define-interactive (break-line) ; called newline in Emacs
; Insert newline at before point.
; If the column of point is greater than fill-column, auto-fill-function is called.
; If the new line is blank, move to left-margin.
(cond [(and (local auto-fill-mode?)
(fill-needed?)
(or (local auto-fill-function)
normal-auto-fill-function))
=> (λ (fill) (fill))]
[else (insert-newline)])
(cond
[(and (blank-line?) (local left-margin)) ; move to left-margin ?
=> insert-spaces]))
(define (blank-line?)
; Is the line containing point blank?
(with-saved-point
(beginning-of-line)
(looking-at #px"^[[:blank:]]*$")))
(define-interactive (insert-line-after)
; insert new line after the current line,
; place point at beginning of new line
; [Sublime: cmd+enter]
(end-of-line)
(insert-newline))
(define-interactive (insert-line-before)
; insert new line before the current line,
; place point at beginning of new line
; [Sublime: cmd+enter]
(beginning-of-line)
(insert-newline)
(backward-char))
(define-interactive (delete-region [start #f] [end #f])
(region-delete start end))
; backward-delete-char
; Delete n characters backwards.
; If n=1 and region is active, delete region.
(define-interactive (backward-delete-char [n 1])
(define b (current-buffer))
(cond
[(and (= n 1) (use-region?)) (delete-region)]
[(not (buffer-restricted? b)) (buffer-delete-backward-char! b (get-point) n)]
[else (define limit (- (point) (point-min)))
(buffer-delete-backward-char! b (get-point) (min limit n))]))
; select all
(define-interactive (mark-whole-buffer [b (current-buffer)])
(localize ([current-buffer b])
(end-of-buffer)
(command-set-mark)
(beginning-of-buffer)))
(define-interactive (kill-line)
(buffer-kill-line)
(update-current-clipboard-at-latest-kill)
(refresh-frame))
;;;
;;; KILLING LINES
;;;
; buffer-kill-whole-line : [buffer] -> void
; kill whole line including its newline
(define (buffer-kill-whole-line [b (current-buffer)])
(localize ([current-buffer b])
(beginning-of-line b)
(buffer-kill-line b #t)
(forward-char)
(backward-delete-char)))
; mark-kill-line-to-beginning : mark -> void
; Kill text from the given mark to beginning of line.
; If mark is at the beginning of line, the newline is deleted.
; The mark is at the beginning of line, if text from the given mark to newline is all whitespace.
(define (mark-kill-line-to-beginning [m (get-point)])
; TODO : store deleted text in kill ring
(define p1 (mark-position m))
(define p2 (line-beginning-position m))
(define rest-of-line (subtext->string (buffer-text b) p2 p1))
; delete to beginning of line
(define b (mark-buffer m))
(let ([old (buffer-set-the-mark b m)])
(error 'mark-kill-line-to-beginning "todo")
#;(beginning-of-line b)
#;(region-delete b)
; maybe delete newline
#;(when (and (string-whitespace? rest-of-line)
(not (= (mark-position m) 0)))
(buffer-backward-delete-char! b))))
(define-interactive (kill-whole-line)
(buffer-kill-whole-line)
(update-current-clipboard-at-latest-kill)
(refresh-frame))
(define-interactive (kill-line-to-beginning)
(mark-kill-line-to-beginning)
(update-current-clipboard-at-latest-kill)
(refresh-frame))
(define-interactive (recenter-top-bottom)
(maybe-recenter-top-bottom #t))
(define-interactive (insert-latest-kill)
; If another application has put any text onto the system clipboard
; later than the latest kill, that text is inserted.
; Note: The timestamp is ignored in OS X.
(define time 0) ; current time
(define s (send the-clipboard get-clipboard-string time))
(when (and (use-region?) (positive? (region-size)))
(displayln 1)
(backward-delete-char))
(cond
[(or (equal? s "")
(equal? s (current-clipboard-at-latest-kill)))
(displayln (list 2 (point)))
; no changes to the system clipboard, so latest kill is used
(buffer-insert-latest-kill)]
[else
(displayln 3)
; system clipboard is newer
(buffer-insert-string! (current-buffer) (get-point) s)]))
(define-interactive (copy-region)
(update-current-clipboard-at-latest-kill)
(kill-ring-push-region))
(define-interactive (kill-word)
; kill to end of word
(deactivate-region-mark)
(forward-word/extend-region)
(kill-region))
(define-interactive (backward-kill-word)
; Kill to beginning of word
(deactivate-region-mark)
(backward-word/extend-region)
(kill-region))
;;;
;;; MODES
;;;
; The buffer-local variable major-mode holds a symbol representing the major mode.
; Example: the symbol 'fundamental-mode represents the fundamental mode.
(define-interactive (fundamental-mode [b (current-buffer)])
(set-major-mode! 'fundamental)
(set-mode-name! "Fundamental")
; add all interactive commands defined here to fundamental mode
(for ([(name cmd) (in-hash all-interactive-commands-ht)])
(define sym (string->symbol name))
(set-buffer-local! sym cmd b)))
(define-interactive (text-mode [b (current-buffer)])
(fundamental-mode b) ; add all commands from fundamental mode
(set-major-mode! 'text)
(set-mode-name! "Text"))
(define-interactive (test)
(define b (current-buffer))
(mark-whole-buffer b)
; (displayln (text->string (buffer-text b)))
; (buffer-insert-string-before-point! (get-point) "x")
(displayln (text->string (buffer-text b))))
;;;
;;; INSERTION
;;;
; https://www.gnu.org/software/emacs/manual/html_node/elisp/Insertion.html
(define-interactive (insert . strings-and-characters)
(define b (current-buffer))
; Insert strings and characters in the current buffer.
; Insert at point, moving the point forward (the insertation is done before point).
(define strings (for/list ([x strings-and-characters])
(cond [(string? x) x]
[(char? x) (string x)]
[else (error 'insert
"expected strings and characters as input, got ~a"
x)])))
(buffer-insert-string! b (get-point) (string-append* strings)))
(define-interactive (insert-char character [count 1])
; Insert the character count times before point.
(insert (make-string count character)))
; (define (insert-before-markers) ...)
; (define (insert-buffer-substring from-buffer-or-name &optional start end) ...)
; (define (insert-buffer-substring-no-properties from-buffer-or-name &optional start end)
(define (insert-property! sym val [val-end val])
(define b (current-buffer))
(buffer-insert-property! b sym val val-end))
;;;
;;; INDENTATION
;;;
;;; 32.17.1 Indentation Primitives
(define (current-indentation)
"Column of first non-blank character. "
"If line is all blank, then the position of the end of line is returned"
(with-saved-point
(beginning-of-line)
(if (blank-line?)
(line-end-position)
(forward-whitespace))))
(define (indent-to column [minimum #f])
"Indent from point to column."
"If minimum is present, insert at least minimum spaces even if column is exceeded."
"If point is past column do nothing (except when minimum is given)."
"Return column of position where indentation ends."
(define c (current-column))
(define amount (max (if (< c column) (- column c) 0)
(or minimum 0)))
(insert (make-string amount #\space))
(+ c amount))
;;; 24.1 Indentation Commands
(define-interactive (back-to-indentation)
"Move to first non-blank character on line."
(beginning-of-line)
(let loop ()
(unless (eol?)
(when (blank? (char-category (char-after-point)))
(forward-char)
(loop)))))
(define (change-indentation column)
(back-to-indentation)
(define cur (current-column))
(define n (abs (- cur column)))
(cond
[(< column cur) (delete-region column (point))]
[(= column cur) (void)]
[(> column cur) (insert (make-string n #\space))]))
(define-interactive (split-line)
"Split line at two at point. Keep point at first line, indent second line to align with point."
; TODO: Insert fill prefix on new line.
(define c (current-column))
(break-line)
(backward-char)
(with-saved-point
(forward-char)
(change-indentation c)))
(define-interactive (indent-region)
(define b (current-buffer))
; Indent line or region.
; Use `indent-line-function` to indent each line.
(define indent-line (local indent-for-tab))
(cond
[(use-region?)
(define beg (region-beginning))
(define end (region-end))
; indenting the lines in the region affects the position of end,
; so we need a mark that moves along.
(define end-m (new-mark b "*temp" (- end 1)))
; go to the beginning of the region and indent each line
(goto-char beg)
(let loop ()
(when (< (point) (mark-position end-m))
(indent-line)
(when (< (point) (mark-position end-m))
(beginning-of-line)
(forward-line)
(loop))))
; clean up
(delete-mark! end-m)
#;(back-to-indentation)]
[else
(indent-line)]))
(define (on-same-line? pos1 pos2 [b (current-buffer)])
(define p1 (position pos1))
(define p2 (position pos2))
(define t (buffer-text b))
(text-on-same-line? t p1 p2))
;;; Indentation helpers
(define-interactive insert-space
(self-insert-command #\space))
(define-interactive (insert-spaces [n 1])
(for ([i n]) (insert-space)))
(define-interactive (insert-tab-as-spaces)
(define (next-greater-in-list y xs default) ; find first element in xs, greater than y
(define gs (filter (λ (x) (not (<= x y))) (sort xs <)))
(cond [(null? gs) default] [else (first gs)]))
(define (next-greater y m) ; find first integer z greater than y such that m divides z
(if (zero? (remainder (+ y 1) m)) (+ y 1) (next-greater (+ y 1) m)))
(define w (local tab-width))
(define tabs (local tab-stop-list))
(unless (and (integer? w) (<= 0 w))
(message "insert-tab-as-spaces: tab-width is not set to a positive integer, using 8 as tab-width")
(set! w 8))
(unless (or (eq? tabs #f) (and (list? tabs) (andmap integer? tabs)))
(message "insert-tab-as-spaces: tab-stop-list not #f or a list of natural numbers")
(set! tabs #f))
(define col (mark-column (get-point)))
(define next-by-width (next-greater col w))
(define next-tab-stop (cond [tabs (next-greater-in-list col tabs next-by-width)]
[else next-by-width]))
(insert-spaces (- next-tab-stop col)))
;;;
;;; 7.7 Blank Lines
;;;
(define-interactive (open-line) ; C-o
; Insert newline after point.
(insert-newline)
(backward-char)
(when (and (local fill-prefix)
(= (point) (line-beginning-position)))
(insert (local fill-prefix))))
;;;
;;; 7.9 Cursor Position Information
;;;
(define-interactive (what-line)
; Shows line number relative to the accessible portion.
; Note: Rewrite when narrowing is introduced.
(message (~a "Line: " (+ (mark-row (get-point)) 1))))
(define-interactive (line-number-mode)
; Toggle line number mode
(define new (not (local line-number-mode?)))
(local! line-number-mode? new)
(message (cond [new "Line-Number mode enabled"]
[else "Line-Number mode disabled"])))
(define-interactive (column-number-mode)
; Toggle column number mode
(define new (not (local column-number-mode?)))
(local! column-number-mode? new)
(message (cond [new "Column-Number mode enabled"]
[else "Column-Number mode disabled"])))
(define-interactive (hl-line-mode)
; Toggle hl-line-mode?
(define new (not (local hl-line-mode?)))
(local! hl-line-mode? new)
(message (cond [new "Highlight line mode enabled"]
[else "Highlight line mode disabled"])))
; TODO M-= (define (count-words-region) ...)
(define (following-char) ; TODO: use char-after-point instead?
; Return character at point.
; If point at end position, return #f.
(define p (mark-position (get-point)))
(define t (buffer-text (current-buffer)))
(cond [(>= p (- (text-length t) 1)) #f]
[else (define s (subtext->string t p (+ p 1)))
(if (not (zero? (string-length s)))
(string-ref s 0)
#f)]))
(define-interactive (what-cursor-position) ; C-x =
(define pos (mark-position (get-point)))
(define len (max 1. (* 1. (buffer-length (current-buffer)))))
(define pct (inexact->exact (floor (* (/ (+ pos 1.) len) 100))))
(define c (following-char))
(cond
[c (define i (char->integer c))
(define octal (number->string i 8))
(define hex (number->string i 16))
(message (~a "Char: " c " "
"(" i ", #o" octal ", #x" hex ") "
"point=" (+ pos 1) " of " len " (" pct "%)"))]
[else
(message (~a "point=" (+ pos 1) " of " len " (EOB) "))]))
;;;
;;; 25.6 Filling Text
;;;
; When a fill prefix is set, then the fill fill commands will
; remove the prefix from each line before filling, and insert it after filling.
(define-interactive (move-to-left-margin)
(move-to-column (local left-margin)))
(define-interactive (set-fill-prefix)
; make from point to beginning of line
(define b (current-buffer))
; (with-saved-point (beginning-of-line) (set-mark))
(define pos (point))
(define left-margin-pos (with-saved-point (move-to-left-margin) (point)))
(cond
[(> pos left-margin-pos) (define s (buffer-substring b left-margin-pos pos))
(local! fill-prefix (if (equal? s "") #f s))]