From 45e77f60541351050d713455cce594c8f952cea0 Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Sat, 3 Aug 2019 16:26:59 -0400 Subject: Implement an initial `myrddin-mode-indent-line'. --- myrddin-mode.el | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/myrddin-mode.el b/myrddin-mode.el index c144995..e46bda5 100644 --- a/myrddin-mode.el +++ b/myrddin-mode.el @@ -98,6 +98,8 @@ "int64" "uint64" "int" "uint" "flt32" "flt64")) +(defvar myrddin-mode-block-keywords + '("elif" "else" "for" "if" "match" "struct" "trait" "while")) (defconst myrddin-mode-identifier-rx '(and (or alpha ?_) @@ -135,6 +137,41 @@ (,myrddin-mode-type-specification-regexp 1 font-lock-type-face) (,myrddin-mode-variable-declaration-regexp 1 font-lock-variable-name-face))) + +;;; +;;; Indentation. +;;; + +(defun myrddin-mode-indent-line () + (interactive) + (let* ((prev-level (save-excursion + (previous-line) + (back-to-indentation) + (/ (current-column) myrddin-indent-offset))) + (prev-line (save-excursion + (previous-line) + (back-to-indentation) + (buffer-substring-no-properties + (line-beginning-position) + (line-end-position))))) + (let ((indent + (save-excursion + (back-to-indentation) + (cond + ((looking-at + (rx (or "}" ";;" "elif" "else"))) + (max 0 (* myrddin-indent-offset (1- prev-level)))) + ((string-match-p + (rx (eval (cons 'or (cons "{" myrddin-mode-block-keywords)))) + prev-line) + (* myrddin-indent-offset (1+ prev-level))) + (t (* myrddin-indent-offset prev-level)))))) + + (when indent + (if (<= (current-column) (current-indentation)) + (indent-line-to indent) + (save-excursion (indent-line-to indent))))))) + ;;; ;;; Mode declaration. @@ -147,7 +184,7 @@ :syntax-table myrddin-mode-syntax-table (setq-local font-lock-defaults '(myrddin-mode-font-lock-keywords nil nil nil nil)) - + (setq-local indent-line-function 'myrddin-mode-indent-line) (setq comment-start "/*") (setq comment-end "*/")) -- cgit v1.3