racket-ansi/rmacs/mode/fundamental.rkt

164 lines
6.1 KiB
Racket
Raw Normal View History

2014-12-23 06:43:01 +00:00
#lang racket/base
(provide fundamental-mode)
2014-12-23 16:09:22 +00:00
(require racket/set)
(require racket/match)
(require "../api.rkt")
2014-12-23 06:43:01 +00:00
(define fundamental-mode (make-mode "fundamental"))
(define-command fundamental-mode (self-insert-command buf #:window win #:keyseq keyseq)
2014-12-23 16:09:22 +00:00
(match keyseq
[(list (key (? char? ch) modifiers)) #:when (set-empty? (set-remove modifiers 'shift))
(buffer-insert! buf (window-point win) (string->rope (string ch)))]
2014-12-28 20:51:06 +00:00
[(list (key (? char? ch0) modifiers)) #:when (equal? modifiers (set 'control))
(define ch (integer->char (- (char->integer (char-upcase ch0)) (char->integer #\A) -1)))
(buffer-insert! buf (window-point win) (string->rope (string ch)))]
2014-12-23 16:09:22 +00:00
[_ #f]))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (unbound-key-sequence buf #:command cmd #:keyseq keyseq)
2014-12-28 20:51:06 +00:00
(invoke (copy-command cmd #:selector 'self-insert-command)))
2014-12-23 06:43:01 +00:00
2014-12-28 20:51:06 +00:00
(define-command fundamental-mode (quoted-insert buf #:command cmd #:keyseq keyseq)
#:bind-key "C-q #:default"
(invoke (copy-command cmd
#:selector 'self-insert-command
#:keyseq (list (cadr keyseq)))))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (newline buf #:window win)
2014-12-23 06:43:01 +00:00
#:bind-key "C-m"
#:bind-key "C-j"
(buffer-insert! buf (window-point win) (string->rope "\n")))
2014-12-23 06:43:01 +00:00
(define (move-forward-n-lines win count)
(define buf (window-buffer win))
2014-12-23 06:43:01 +00:00
(for ((i count))
(buffer-move-mark-to-end-of-line! buf (window-point win))
(buffer-move-mark! buf (window-point win) 1)))
2014-12-23 06:43:01 +00:00
(define (move-backward-n-lines win count)
(define buf (window-buffer win))
2014-12-23 06:43:01 +00:00
(for ((i count))
(buffer-move-mark-to-start-of-line! buf (window-point win))
(buffer-move-mark! buf (window-point win) -1)))
2014-12-23 06:43:01 +00:00
(define (move-to-column win col)
(define buf (window-buffer win))
(define sol (buffer-start-of-line buf (window-point win)))
(buffer-mark! buf (window-point win) (buffer-closest-pos-for-column buf sol 0 col)))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (forward-char buf #:window win #:prefix-arg [count 1])
2014-12-23 06:43:01 +00:00
#:bind-key "C-f"
#:bind-key "<right>"
(buffer-move-mark! buf (window-point win) count))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (backward-char buf #:window win #:prefix-arg [count 1])
2014-12-23 06:43:01 +00:00
#:bind-key "C-b"
#:bind-key "<left>"
(buffer-move-mark! buf (window-point win) (- count)))
2014-12-23 06:43:01 +00:00
(define-buffer-local last-vertical-movement-preferred-column)
(define (vertical-movement-preferred-column win)
(define buf (window-buffer win))
(last-vertical-movement-preferred-column
buf
(or (and (editor-last-command? (buffer-editor buf)
'next-line
'prev-line)
(last-vertical-movement-preferred-column buf))
(buffer-column buf (window-point win)))))
(define-command fundamental-mode (next-line buf #:window win #:prefix-arg [count 1])
2014-12-23 06:43:01 +00:00
#:bind-key "C-n"
#:bind-key "<down>"
(define col (vertical-movement-preferred-column win))
(move-forward-n-lines win count)
(move-to-column win col))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (prev-line buf #:window win #:prefix-arg [count 1])
2014-12-23 06:43:01 +00:00
#:bind-key "C-p"
#:bind-key "<up>"
(define col (vertical-movement-preferred-column win))
(move-backward-n-lines win count)
(move-to-column win col))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (move-end-of-line buf #:window win #:prefix-arg [count 1])
2014-12-23 06:43:01 +00:00
#:bind-key "C-e"
#:bind-key "<end>"
(when (positive? count) (move-forward-n-lines win (- count 1)))
(buffer-move-mark-to-end-of-line! buf (window-point win)))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (move-beginning-of-line buf #:window win #:prefix-arg [count 1])
2014-12-23 06:43:01 +00:00
#:bind-key "C-a"
#:bind-key "<home>"
(when (positive? count) (move-forward-n-lines win (- count 1)))
(buffer-move-mark-to-start-of-line! buf (window-point win)))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (delete-backward-char buf #:window win #:prefix-arg [count 1])
2014-12-23 06:43:01 +00:00
#:bind-key "<backspace>"
#:bind-key "C-h" ;; differs from GNU emacs
(define pos (buffer-mark-pos buf (window-point win)))
(buffer-region-update! buf (- pos 1) pos (lambda (_deleted) (empty-rope))))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (delete-forward-char buf #:window win #:prefix-arg [count 1])
2014-12-23 06:43:01 +00:00
#:bind-key "<delete>"
#:bind-key "C-d"
(define pos (buffer-mark-pos buf (window-point win)))
(buffer-region-update! buf pos (+ pos 1) (lambda (_deleted) (empty-rope))))
2014-12-23 06:43:01 +00:00
2014-12-28 20:33:00 +00:00
(define (set-window-mark! win [pos (window-point win)])
(window-mark! win pos)
(message (window-editor win) "Mark set")
pos)
(define-command fundamental-mode (beginning-of-buffer buf #:window win #:prefix-arg [tenths 0])
2014-12-23 06:43:01 +00:00
#:bind-key "M-<"
#:bind-key "C-<home>"
#:bind-key "<begin>"
2014-12-28 20:33:00 +00:00
(if (eq? tenths '#:prefix) (set! tenths 0) (set-window-mark! win))
(window-move-to! win (* (buffer-size buf) (max 0 (min 10 tenths)) 1/10)))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (end-of-buffer buf #:window win #:prefix-arg [tenths 0])
2014-12-23 06:43:01 +00:00
#:bind-key "M->"
#:bind-key "C-<end>"
2014-12-28 20:33:00 +00:00
(if (eq? tenths '#:prefix) (set! tenths 0) (set-window-mark! win))
(window-move-to! win (* (buffer-size buf) (- 10 (max 0 (min 10 tenths))) 1/10)))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (exchange-point-and-mark buf #:window win)
2014-12-23 06:43:01 +00:00
#:bind-key "C-x C-x"
(define m (buffer-mark-pos* buf (window-mark win)))
2014-12-23 06:43:01 +00:00
(when m
2014-12-28 20:33:00 +00:00
(window-mark! win)
(window-move-to! win m)))
2014-12-23 06:43:01 +00:00
(define-command fundamental-mode (set-mark-command buf #:window win #:prefix-arg arg)
2014-12-23 06:43:01 +00:00
#:bind-key "C-@"
#:bind-key "C-space"
(if (eq? arg '#:prefix)
(let ((m (buffer-mark-pos* buf (window-mark win))))
(and m (window-move-to! win m)))
2014-12-28 20:33:00 +00:00
(set-window-mark! win)))
(define-command fundamental-mode (split-window-below buf #:window win #:editor ed)
#:bind-key "C-x 2"
2014-12-28 05:14:55 +00:00
(open-window ed buf #:after-window win #:activate? #f))
(define-command fundamental-mode (delete-other-windows buf #:window win #:editor ed)
#:bind-key "C-x 1"
(close-other-windows ed win))
(define-command fundamental-mode (delete-window buf #:window win #:editor ed)
#:bind-key "C-x 0"
(close-window ed win))
(define-command fundamental-mode (other-window buf #:window win #:editor ed)
#:bind-key "C-tab"
#:bind-key "C-x o"
(select-window ed (editor-next-window ed win)))
2014-12-28 17:21:58 +00:00
(define-command fundamental-mode (save-buffer buf)
#:bind-key "C-x C-s"
(save-buffer! buf))