From 8d30efb109e174633fcb958612b6b06f2249f046 Mon Sep 17 00:00:00 2001 From: 4hnme Date: Fri, 26 Jan 2024 23:51:20 +0600 Subject: [PATCH] Ocaml filetype handling improvements Manage indentation when creating a new line in Ocaml files --- rc/filetype/ocaml.kak | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/rc/filetype/ocaml.kak b/rc/filetype/ocaml.kak index 0c7986c4..06a1bf5d 100644 --- a/rc/filetype/ocaml.kak +++ b/rc/filetype/ocaml.kak @@ -15,6 +15,8 @@ hook global WinSetOption filetype=ocaml %{ require-module ocaml set-option window static_words %opt{ocaml_static_words} hook window InsertChar -group ocaml-insert '\*' ocaml-insert-closing-comment-bracket + hook window InsertChar \n -group ocaml-insert ocaml-insert-on-new-line + hook window ModeChange pop:insert:.* -group ocaml-trim-indent ocaml-trim-indent } hook -group ocaml-highlight global WinSetOption filetype=ocaml %{ @@ -99,6 +101,23 @@ define-command ocaml-alternative-file -docstring 'Switch between .ml and .mli fi } +# Remove trailing whitespaces +define-command -hidden ocaml-trim-indent %{ + evaluate-commands -no-hooks -draft -itersel %{ + try %{ execute-keys -draft x s \h+$ d } + } +} + +# Preserve indentation when creating new line +define-command -hidden ocaml-insert-on-new-line %{ + evaluate-commands -draft -itersel %{ + # copy white spaces at the beginnig of the previous line + try %{ execute-keys -draft k x s ^\h+ y jgh P x s \h+$ } + # increase indentation if the previous line ended with either '=' sign or do keyword + try %{ execute-keys -draft k x s (=|\bdo)$ j } + } +} + # The OCaml comment is `(* Some comment *)`. Like the C-family this can be a multiline comment. # # Recognize when the user is trying to commence a comment when they type `(*` and