helix/helix.scm

161 lines
5.0 KiB
Scheme
Raw Normal View History

2023-05-08 12:53:19 +08:00
(require-builtin helix/core/typable as helix.)
(require-builtin helix/core/static as helix.static.)
(require-builtin helix/core/keybindings as helix.keybindings.)
2023-06-25 13:24:52 +08:00
(provide set-theme-dracula
set-theme-dracula__doc__
set-theme-custom
set-theme-custom__doc__
theme-then-vsplit
theme-then-vsplit__doc__
custom-undo
custom-undo__doc__
lam
lam__doc__
delete-word-forward
insert-string-at-selection
highlight-to-matching-paren
highlight-to-matching-paren__doc__
delete-sexpr
delete-sexpr__doc__
run-expr
run-highlight
make-minor-mode!
git-status
reload-helix-scm
static-format)
2023-05-08 12:53:19 +08:00
;;@doc
;; Sets the theme to be the dracula theme
(define (set-theme-dracula cx)
2023-06-25 13:24:52 +08:00
(helix.theme cx (list "dracula") helix.PromptEvent::Validate))
(enqueue-callback! 'helix.static.format)
(enqueue-callback! 'set-theme-dracula)
2023-05-08 12:53:19 +08:00
;;@doc
;; Sets the theme to be the theme passed in
(define (set-theme-custom cx entered-theme)
2023-06-25 13:24:52 +08:00
(helix.theme cx (list entered-theme) helix.PromptEvent::Validate))
2023-05-08 12:53:19 +08:00
;;@doc
;; Switch theme to the entered theme, then split the current file into
;; a vsplit
(define (theme-then-vsplit cx entered-theme)
2023-06-25 13:24:52 +08:00
(set-theme-custom cx entered-theme)
(helix.vsplit cx '() helix.PromptEvent::Validate))
2023-05-08 12:53:19 +08:00
;;@doc
;; Perform an undo
(define (custom-undo cx)
2023-06-25 13:24:52 +08:00
(helix.static.undo cx))
2023-05-08 12:53:19 +08:00
;;@doc
;; Insert a lambda
(define (lam cx)
2023-06-25 13:24:52 +08:00
(helix.static.insert_char cx #\λ)
(helix.static.insert_mode cx))
2023-05-08 12:53:19 +08:00
;;@doc
;; Insert the string at the selection and go back into insert mode
(define (insert-string-at-selection cx str)
2023-06-25 13:24:52 +08:00
(helix.static.insert_string cx str)
(helix.static.insert_mode cx))
2023-05-08 12:53:19 +08:00
;;@doc
;; Delete the word forward
(define (delete-word-forward cx)
2023-06-25 13:24:52 +08:00
(helix.static.delete_word_forward cx))
2023-05-08 12:53:19 +08:00
;;@doc
;; Registers a minor mode with the registered modifer and key map
2023-06-25 13:24:52 +08:00
;;
2023-05-08 12:53:19 +08:00
;; Examples:
;; ```scheme
2023-06-25 13:24:52 +08:00
;; (make-minor-mode! "+"
2023-05-08 12:53:19 +08:00
;; (hash "P" ":lam"))
;; ```
(define (make-minor-mode! modifier bindings)
2023-06-25 13:24:52 +08:00
(~> (hash "normal" (hash modifier bindings))
(value->jsexpr-string)
(helix.keybindings.set-keybindings!)))
2023-05-08 12:53:19 +08:00
2023-06-25 13:24:52 +08:00
(define-syntax minor-mode!
(syntax-rules (=>)
[(minor-mode! modifier (key => function))
(make-minor-mode! modifier (minor-mode-cruncher (key => function)))]
[(minor-mode! modifier (key => (function ...)))
(make-minor-mode! modifier (minor-mode-cruncher (key => (function ...))))]
2023-05-08 12:53:19 +08:00
2023-06-25 13:24:52 +08:00
[(minor-mode! modifier (key => function) remaining ...)
(make-minor-mode! modifier (minor-mode-cruncher (key => function) remaining ...))]
2023-05-08 12:53:19 +08:00
2023-06-25 13:24:52 +08:00
[(minor-mode! modifier (key => (function ...)) remaining ...)
(make-minor-mode! modifier (minor-mode-cruncher (key => function) ... remaining ...))]))
2023-05-08 12:53:19 +08:00
2023-06-25 13:24:52 +08:00
(define-syntax minor-mode-cruncher
(syntax-rules (=>)
2023-05-08 12:53:19 +08:00
2023-06-25 13:24:52 +08:00
[(minor-mode-cruncher (key => (function ...)))
(hash key (map (lambda (x) (string-append ":" (symbol->string x))) (quote (function ...))))]
2023-05-08 12:53:19 +08:00
2023-06-25 13:24:52 +08:00
[(minor-mode-cruncher (key => function))
(hash key (string-append ":" (symbol->string (quote function))))]
2023-05-08 12:53:19 +08:00
2023-06-25 13:24:52 +08:00
[(minor-mode-cruncher (key => (function ...)) remaining ...)
(hash-insert (minor-mode-cruncher remaining ...)
key
(map (lambda (x) (string-append ":" (symbol->string x))) (quote (function ...))))]
2023-05-08 12:53:19 +08:00
2023-06-25 13:24:52 +08:00
[(minor-mode-cruncher (key => function) remaining ...)
(hash-insert (minor-mode-cruncher remaining ...)
key
(string-append ":" (symbol->string (quote function))))]))
2023-05-08 12:53:19 +08:00
;;@doc
;; Highlight to the matching paren
(define (highlight-to-matching-paren cx)
2023-06-25 13:24:52 +08:00
(helix.static.select_mode cx)
(helix.static.match_brackets cx))
2023-05-08 12:53:19 +08:00
(define (run-expr cx)
2023-06-25 13:24:52 +08:00
(define current-selection (helix.static.current_selection cx))
(when (or (equal? "(" current-selection) (equal? ")" current-selection))
(highlight-to-matching-paren cx)
(helix.static.run-in-engine! cx (helix.static.current-highlighted-text! cx))
(helix.static.normal_mode cx)))
2023-05-08 12:53:19 +08:00
(define (run-highlight cx)
2023-06-25 13:24:52 +08:00
(helix.static.run-in-engine! cx (helix.static.current-highlighted-text! cx)))
2023-05-08 12:53:19 +08:00
;;@doc
;; Delete the s-expression matching this bracket
;; If the current selection is not on a bracket, this is a no-op
(define (delete-sexpr cx)
2023-06-25 13:24:52 +08:00
(define current-selection (helix.static.current_selection cx))
(when (or (equal? "(" current-selection) (equal? ")" current-selection))
(highlight-to-matching-paren cx)
(helix.static.delete_selection cx)))
2023-05-08 12:53:19 +08:00
; (minor-mode! "+" ("l" => lam)
; ("q" => (set-theme-dracula lam)))
2023-06-25 13:24:52 +08:00
(minor-mode! "P"
("l" => lam)
("p" => highlight-to-matching-paren)
("d" => delete-sexpr)
("r" => run-expr))
2023-05-08 12:53:19 +08:00
(make-minor-mode! "+" (hash "l" ":lam"))
(define (git-status cx)
2023-06-25 13:24:52 +08:00
(helix.run-shell-command cx '("git" "status") helix.PromptEvent::Validate))
2023-05-08 12:53:19 +08:00
(minor-mode! "G" ("s" => git-status))
(define (reload-helix-scm cx)
2023-06-25 13:24:52 +08:00
(helix.static.run-in-engine! cx
(string-append "(require \"" (helix.static.get-helix.scm-path) "\")")))